27 lines
1.1 KiB
TypeScript
27 lines
1.1 KiB
TypeScript
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/);
|
|
});
|