import { describe, expect, it } from "vitest";
import { renderOnServer, type RenderTask } from "#lib/ssr/render.ts";
const ctx = { repo: "ada.test/infra", ref: "main" };
const markup = (source: string) => (): RenderTask => ({ kind: "markup", source, ctx });
describe("renderOnServer", () => {
it("renders within the page budget and spends it", async () => {
const budget = { chars: 10, html: 1_000_000 };
expect(await renderOnServer(budget, 6, markup("**a**"))).toContain("a");
expect(budget.chars).toBe(4);
expect(await renderOnServer(budget, 6, markup("**b**"))).toBeNull();
expect(await renderOnServer(budget, 4, markup("**c**"))).toContain("c");
});
it("leaves a render whose html doesn't fit what's left of the page to the browser", async () => {
const budget = { chars: 1_000_000, html: 30 };
expect(await renderOnServer(budget, 5, markup("**a**"))).toBeNull();
expect(budget.html).toBe(30);
budget.html = 1_000;
const html = await renderOnServer(budget, 5, markup("**a**"));
expect(budget.html).toBe(1_000 - (html?.length ?? 0));
expect(html?.length).toBeGreaterThan(30);
});
it("leaves one document past the per-document cap to the browser", async () => {
const budget = { chars: Number.MAX_SAFE_INTEGER, html: 1_000_000 };
expect(await renderOnServer(budget, 4 * 1024 * 1024, markup("x"))).toBeNull();
expect(budget.chars).toBe(Number.MAX_SAFE_INTEGER);
});
});