138 lines
6.7 KiB
JavaScript
138 lines
6.7 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import crypto from "node:crypto";
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import test from "node:test";
|
|
import ts from "typescript";
|
|
|
|
const root = path.resolve(import.meta.dirname, "../../..");
|
|
function transpile(file, replacements = {}) {
|
|
let source = fs.readFileSync(path.join(root, file), "utf8");
|
|
for (const [from, to] of Object.entries(replacements)) source = source.replaceAll(`from "${from}"`, `from "${to}"`);
|
|
const result = ts.transpileModule(source, {
|
|
compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 },
|
|
fileName: file,
|
|
reportDiagnostics: true,
|
|
});
|
|
assert.deepEqual(result.diagnostics, []);
|
|
return `data:text/javascript;base64,${Buffer.from(result.outputText).toString("base64")}`;
|
|
}
|
|
|
|
const atomicUrl = transpile("web/protocol/atomic-checkpoint.ts");
|
|
const usage = await import(transpile("web/protocol/provider-usage.ts"));
|
|
const limits = await import(transpile("web/protocol/token-limit.ts", { "./provider-usage": transpile("web/protocol/provider-usage.ts") }));
|
|
const stream = await import(transpile("web/protocol/streaming-consumer.ts", { "./atomic-checkpoint": atomicUrl }));
|
|
const checkpoint = await import(atomicUrl);
|
|
|
|
function bytes(...values) { return Uint8Array.from(values); }
|
|
function streamOf(chunks, error) {
|
|
let index = 0;
|
|
return new ReadableStream({
|
|
pull(controller) {
|
|
if (index < chunks.length) controller.enqueue(chunks[index++]);
|
|
else if (error) controller.error(error);
|
|
else controller.close();
|
|
},
|
|
});
|
|
}
|
|
function digest(value) { return crypto.createHash("sha256").update(value).digest("hex"); }
|
|
|
|
test("stream consumer preserves UTF-8 code points split across chunks", async () => {
|
|
const checkpoints = [];
|
|
const result = await stream.consumeUtf8Stream(streamOf([bytes(0xe2), bytes(0x82), bytes(0xac, 0x20, 0x41)]), {
|
|
onCheckpoint: (value) => checkpoints.push(value),
|
|
});
|
|
assert.equal(result.text, "€ A");
|
|
assert.equal(result.byteCount, 5);
|
|
assert.equal(result.chunkCount, 3);
|
|
assert.equal(result.sha256, digest(Buffer.from([0xe2, 0x82, 0xac, 0x20, 0x41])));
|
|
assert.ok(checkpoints.every((value) => !Object.hasOwn(value, "text")));
|
|
assert.equal(checkpoints.at(-1).state, "COMPLETED");
|
|
});
|
|
|
|
test("stream consumer rejects a truncated UTF-8 sequence and records only failed metadata", async () => {
|
|
const checkpoints = [];
|
|
await assert.rejects(
|
|
stream.consumeUtf8Stream(streamOf([bytes(0xf0, 0x9f, 0x92)]), { onCheckpoint: (value) => checkpoints.push(value) }),
|
|
(error) => error.code === "STREAM_UTF8_INVALID" && error.byteCount === 3,
|
|
);
|
|
assert.equal(checkpoints.at(-1).state, "FAILED");
|
|
assert.deepEqual(Object.keys(checkpoints.at(-1)).sort(), ["byteCount", "chunkCount", "sha256", "state"]);
|
|
});
|
|
|
|
test("stream consumer classifies a transport/body decoding disconnect as incomplete", async () => {
|
|
await assert.rejects(
|
|
stream.consumeUtf8Stream(streamOf([bytes(0x61)], new Error("Transport error: network error: error decoding response body"))),
|
|
(error) => error.code === "STREAM_DISCONNECTED" && /stream disconnected before completion.*error decoding response body/u.test(error.message),
|
|
);
|
|
});
|
|
|
|
test("stream consumer bounds a provider error body before surfacing it", async () => {
|
|
const detail = "x".repeat(10_000);
|
|
await assert.rejects(
|
|
stream.consumeUtf8Stream(streamOf([], new Error(detail))),
|
|
(error) => error.code === "STREAM_DISCONNECTED" && error.message.length < 600 && error.cause?.message.length < 600,
|
|
);
|
|
});
|
|
|
|
test("stream consumer can require an explicit completion marker", async () => {
|
|
await assert.rejects(
|
|
stream.consumeUtf8Stream(streamOf([bytes(0x61)]), { requireCompletion: true }),
|
|
(error) => error.code === "STREAM_DISCONNECTED" && /before completion/u.test(error.message),
|
|
);
|
|
const result = await stream.consumeUtf8Stream(streamOf([bytes(0x61)]), { requireCompletion: true, isComplete: ({ byteCount }) => byteCount === 1 });
|
|
assert.equal(result.text, "a");
|
|
});
|
|
|
|
test("stream consumer cancels a pending read without publishing completion", async () => {
|
|
const controller = new AbortController();
|
|
controller.abort();
|
|
await assert.rejects(stream.consumeUtf8Stream(streamOf([bytes(0x61)]), { signal: controller.signal }), (error) => error.code === "STREAM_CANCELLED");
|
|
});
|
|
|
|
test("stream consumer closes async iterables after a transport failure", async () => {
|
|
let returned = false;
|
|
const source = {
|
|
[Symbol.asyncIterator]() {
|
|
return {
|
|
async next() {
|
|
throw new Error("connection reset");
|
|
},
|
|
async return() {
|
|
returned = true;
|
|
return { done: true, value: undefined };
|
|
},
|
|
};
|
|
},
|
|
};
|
|
await assert.rejects(stream.consumeUtf8Stream(source), (error) => error.code === "STREAM_DISCONNECTED");
|
|
assert.equal(returned, true);
|
|
});
|
|
|
|
test("atomic checkpoint publishes through temp then rename and rejects model text", async () => {
|
|
const store = checkpoint.createMemoryCheckpointStore();
|
|
const value = await checkpoint.commitAtomicCheckpoint(store, "response.checkpoint", {
|
|
state: "STREAMING", byteCount: 4, chunkCount: 2, sha256: "a".repeat(64),
|
|
});
|
|
assert.deepEqual(store.get("response.checkpoint"), value);
|
|
assert.throws(() => checkpoint.createAtomicCheckpoint({ ...value, text: "partial model output" }), /CHECKPOINT_INVALID/u);
|
|
});
|
|
|
|
test("provider usage normalizes nested aliases, derives totals, and detects arithmetic conflicts", () => {
|
|
assert.deepEqual(usage.parseProviderUsage({ response: { usage: { prompt_tokens: 7, completion_tokens: 5 } } }), {
|
|
status: "AVAILABLE", inputTokens: 7, outputTokens: 5, totalTokens: 12,
|
|
missing: [], inconsistent: [], derivedTotal: true,
|
|
sources: { inputTokens: ["$.response.usage.prompt_tokens"], outputTokens: ["$.response.usage.completion_tokens"], totalTokens: [] },
|
|
});
|
|
assert.equal(usage.parseProviderUsage({ usage: { input_tokens: 2, output_tokens: 3, total_tokens: 99 } }).status, "INCONSISTENT");
|
|
assert.equal(usage.parseProviderUsage({ result: { usage: { input_tokens: 2 } } }).status, "PARTIAL");
|
|
assert.equal(usage.parseProviderUsage({}).status, "MISSING");
|
|
});
|
|
|
|
test("token limit classifier distinguishes context, output and max-output truncation", () => {
|
|
assert.equal(limits.classifyTokenLimit({ error: { code: "context_length_exceeded" } }).category, "CONTEXT_LIMIT");
|
|
assert.equal(limits.classifyTokenLimit({ error: { code: "output_limit_exceeded" } }).category, "OUTPUT_LIMIT");
|
|
assert.equal(limits.classifyTokenLimit({ choices: [{ finish_reason: "length" }], usage: { completion_tokens: 16 } }, { maxOutputTokens: 16 }).category, "MAX_OUTPUT_TOKENS");
|
|
assert.equal(limits.classifyTokenLimit({ message: "ordinary provider failure" }).category, "NONE");
|
|
});
|