~james/core

core/web/src/lib/ssr/render.test.ts -rw-r--r-- 1.4 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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("<strong>a</strong>");
		expect(budget.chars).toBe(4);
		expect(await renderOnServer(budget, 6, markup("**b**"))).toBeNull();
		expect(await renderOnServer(budget, 4, markup("**c**"))).toContain("<strong>c</strong>");
	});

	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);
	});
});