import { describe, expect, mock, test } from "bun:test";
mock.module("cloudflare:workers", () => ({ env: {}, waitUntil: () => undefined }));
const { countedResponse } = await import("./usage");
describe("countedResponse", () => {
test("reports the streamed byte count once the body is consumed", async () => {
const counts: number[] = [];
const response = countedResponse(new Response("hello world"), (bytes) => counts.push(bytes));
expect(await response.text()).toBe("hello world");
expect(counts).toEqual([11]);
});
test("an empty body is counted at once", () => {
const counts: number[] = [];
countedResponse(new Response(null, { status: 304 }), (bytes) => counts.push(bytes));
expect(counts).toEqual([0]);
});
test("a cancelled download still counts what was sent", async () => {
const counts: number[] = [];
const response = countedResponse(new Response("abcdef"), (bytes) => counts.push(bytes));
await response.body?.cancel();
expect(counts).toEqual([0]);
});
});