87 lines
3.1 KiB
TypeScript
87 lines
3.1 KiB
TypeScript
import { readFile } from "node:fs/promises";
|
|
import path from "node:path";
|
|
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { loadRuntimeConfig } from "../../../src/bootstrap/load-runtime-config.ts";
|
|
import { loadReleaseManifest } from "../../../src/bootstrap/load-release-manifest.ts";
|
|
|
|
type Surfaces = Record<string, { cacheControl?: string; path?: string; pathPattern?: string }>;
|
|
|
|
async function json<T>(relative: string): Promise<T> {
|
|
return JSON.parse(await readFile(path.resolve(relative), "utf8")) as T;
|
|
}
|
|
|
|
const policy = (await json<{ surfaces: Surfaces }>("config/hosting/cache-policy.json")).surfaces;
|
|
const fixtureHeaders = (
|
|
await json<{ responses: Record<string, Record<string, string>> }>(
|
|
"config/hosting/response-headers.fixture.json",
|
|
)
|
|
).responses;
|
|
|
|
function directives(value: string | undefined): Set<string> {
|
|
return new Set(
|
|
(value ?? "")
|
|
.split(",")
|
|
.map((entry) => entry.trim().toLowerCase())
|
|
.filter(Boolean),
|
|
);
|
|
}
|
|
|
|
describe("cache-policy contract for rolling releases", () => {
|
|
it("serves hashed assets as immutable, year-long public cache entries", () => {
|
|
expect(policy["hashedAsset"]?.cacheControl).toBe("public, max-age=31536000, immutable");
|
|
expect(policy["hashedAsset"]?.pathPattern).toBe("/assets/*");
|
|
expect(fixtureHeaders["hashedAsset"]?.["cache-control"]).toBe(
|
|
"public, max-age=31536000, immutable",
|
|
);
|
|
});
|
|
|
|
it("revalidates HTML and never marks it immutable", () => {
|
|
for (const value of [policy["index"]?.cacheControl, fixtureHeaders["index"]?.["cache-control"]]) {
|
|
const parsed = directives(value);
|
|
expect(parsed.has("immutable")).toBe(false);
|
|
expect(parsed.has("no-cache") || parsed.has("no-store") || parsed.has("max-age=0")).toBe(true);
|
|
}
|
|
});
|
|
|
|
it.each(["runtimeConfig", "releaseManifest"])(
|
|
"never lets %s be reused across release identity changes",
|
|
(surface) => {
|
|
expect(directives(policy[surface]?.cacheControl).has("no-store")).toBe(true);
|
|
expect(directives(fixtureHeaders[surface]?.["cache-control"]).has("no-store")).toBe(true);
|
|
expect(directives(policy[surface]?.cacheControl).has("immutable")).toBe(false);
|
|
},
|
|
);
|
|
|
|
it("requests runtime config and the release manifest with cache: no-store", async () => {
|
|
const seen: RequestInit[] = [];
|
|
const fetcher = vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => {
|
|
seen.push(init ?? {});
|
|
return new Response("{}", { headers: { "content-type": "application/json" } });
|
|
});
|
|
const buildConfig = Object.freeze({
|
|
buildId: "build-a",
|
|
commitSha: "abc",
|
|
routerBasePath: "/",
|
|
runtimeConfigUrl: "/config.json",
|
|
});
|
|
await loadRuntimeConfig({ buildConfig, fetcher }).catch(() => undefined);
|
|
await loadReleaseManifest(
|
|
{
|
|
build: buildConfig,
|
|
config: {
|
|
RELEASE_MANIFEST_URL: "/release-manifest.json",
|
|
CONFIG_SCHEMA_VERSION: "2.0",
|
|
} as never,
|
|
configSchema: "V2",
|
|
validationDurationMs: 0,
|
|
},
|
|
{ fetcher },
|
|
).catch(() => undefined);
|
|
|
|
expect(seen).toHaveLength(2);
|
|
expect(seen.every((init) => init.cache === "no-store")).toBe(true);
|
|
});
|
|
});
|