import fs from "fs/promises";
import os from "os";
import path from "path";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import {
clearAppShellContentCache,
getAppShellContent,
setAppShellCacheHeaders,
} from "../../src/server/RenderHtml";
describe("RenderHtml", () => {
const originalGitCommit = process.env.GIT_COMMIT;
let tempDir: string | null = null;
beforeEach(() => {
vi.stubEnv("NUM_WORKERS", "1");
vi.stubEnv("TURNSTILE_SITE_KEY", "test-key");
vi.stubEnv("DOMAIN", "localhost");
});
afterEach(async () => {
vi.unstubAllEnvs();
process.env.GIT_COMMIT = originalGitCommit;
clearAppShellContentCache();
if (tempDir) {
await fs.rm(tempDir, { recursive: true, force: true });
tempDir = null;
}
});
test("reuses cached app shell content", async () => {
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "render-html-"));
const htmlPath = path.join(tempDir, "index.html");
await fs.writeFile(
htmlPath,
"",
"utf8",
);
process.env.GIT_COMMIT = "first";
const first = await getAppShellContent(htmlPath);
process.env.GIT_COMMIT = "second";
const second = await getAppShellContent(htmlPath);
expect(first).toContain('"first"');
expect(second).toBe(first);
expect(second).not.toContain('"second"');
});
test("sets shared-cache headers for the app shell", () => {
const headers = new Map();
const response = {
setHeader(name: string, value: string) {
headers.set(name, value);
},
} as any;
setAppShellCacheHeaders(response);
expect(headers.get("Cache-Control")).toBe(
"public, max-age=0, s-maxage=300, stale-while-revalidate=86400",
);
expect(headers.get("Content-Type")).toBe("text/html");
});
});