Files
Sybil-2/server/tests/bounded-response.test.ts
T

27 lines
1.1 KiB
TypeScript
Raw Normal View History

2026-11-01 14:17:40 -08:00
import assert from "node:assert/strict";
import test from "node:test";
import { readBoundedResponseBytes } from "../src/llm/bounded-response.js";
const oneMiB = 1024 * 1024;
test("reads response bytes within the declared size limit", async () => {
const bytes = await readBoundedResponseBytes(new Response("small"), oneMiB);
assert.equal(new TextDecoder().decode(bytes), "small");
});
test("rejects oversized responses from Content-Length before buffering", async () => {
const response = new Response("x", { headers: { "content-length": String(oneMiB + 1) } });
await assert.rejects(readBoundedResponseBytes(response, oneMiB), /exceeds the 1 MiB download limit/);
});
test("rejects streamed responses when Content-Length is absent", async () => {
const response = new Response(new ReadableStream({
start(controller) {
controller.enqueue(new Uint8Array(oneMiB));
controller.enqueue(new Uint8Array(1));
controller.close();
},
}));
await assert.rejects(readBoundedResponseBytes(response, oneMiB), /exceeds the 1 MiB download limit/);
});