Files
OpenFrontIO/tests/server/RenderHtml.test.ts
T
2026-03-22 21:00:11 +01:00

61 lines
1.7 KiB
TypeScript

import fs from "fs/promises";
import os from "os";
import path from "path";
import { afterEach, describe, expect, test } from "vitest";
import {
clearAppShellContentCache,
getAppShellContent,
setAppShellCacheHeaders,
} from "../../src/server/RenderHtml";
describe("RenderHtml", () => {
const originalGitCommit = process.env.GIT_COMMIT;
let tempDir: string | null = null;
afterEach(async () => {
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,
"<script>window.GIT_COMMIT = <%- gitCommit %>;</script>",
"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<string, string>();
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");
});
});