// Rôle : vérifie les en-têtes de cache du serveur Node sur des fichiers de production représentatifs.
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import assert from "node:assert/strict";
import { test } from "node:test";
import { createStaticServer } from "../server.mjs";
async function writeFixture(root, path, content) {
const file = join(root, path);
await mkdir(dirname(file), { recursive: true });
await writeFile(file, content);
return file;
}
test("production server separates stable files from versioned assets", async (t) => {
const root = await mkdtemp(join(tmpdir(), "sokko-server-cache-"));
await writeFixture(root, "index.html", "Sokko G");
await writeFixture(root, "data/site.json", JSON.stringify({ title: "Sokko G" }));
await writeFixture(root, "assets/index-Ab12_cd34.js", "console.log('versioned');");
await writeFixture(root, "assets/runtime.js", "console.log('stable');");
const imageFile = await writeFixture(root, "static/img/hero.png", "image-v1");
const server = createStaticServer({ root, publicRoot: root });
await new Promise((resolve, reject) => {
server.once("error", reject);
server.listen(0, "127.0.0.1", resolve);
});
t.after(async () => {
await new Promise((resolve, reject) => server.close((error) => error ? reject(error) : resolve()));
await rm(root, { recursive: true, force: true });
});
const address = server.address();
const request = (path) => fetch(`http://127.0.0.1:${address.port}${path}`);
const html = await request("/");
assert.equal(html.status, 200);
assert.equal(html.headers.get("cache-control"), "no-store");
const spaFallback = await request("/toolbox/example");
assert.equal(spaFallback.status, 200);
assert.equal(spaFallback.headers.get("cache-control"), "no-store");
const json = await request("/data/site.json");
assert.equal(json.status, 200);
assert.equal(json.headers.get("cache-control"), "no-cache");
const bundle = await request("/assets/index-Ab12_cd34.js");
assert.equal(bundle.status, 200);
assert.equal(bundle.headers.get("cache-control"), "public, max-age=31536000, immutable");
const stableScript = await request("/assets/runtime.js");
assert.equal(stableScript.status, 200);
assert.equal(stableScript.headers.get("cache-control"), "no-cache");
const firstImage = await request("/static/img/hero.png");
assert.equal(firstImage.headers.get("cache-control"), "no-cache");
assert.equal(await firstImage.text(), "image-v1");
await writeFile(imageFile, "image-v2");
const replacedImage = await request("/static/img/hero.png");
assert.equal(await replacedImage.text(), "image-v2");
const missingBundle = await request("/assets/index-OldHash9.js");
assert.equal(missingBundle.status, 404);
assert.equal(missingBundle.headers.get("cache-control"), "no-store");
});
test("stable files can be revalidated with an ETag while no-store and immutable responses stay unconditional", async (t) => {
const root = await mkdtemp(join(tmpdir(), "sokko-server-etag-"));
await writeFixture(root, "index.html", "Sokko G");
await writeFixture(root, "assets/index-Ab12_cd34.js", "console.log('versioned');");
const imageFile = await writeFixture(root, "static/img/card.webp", "image-v1");
const server = createStaticServer({ root, publicRoot: root });
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
t.after(async () => {
await new Promise((resolve) => server.close(resolve));
await rm(root, { recursive: true, force: true });
});
const url = (path) => `http://127.0.0.1:${server.address().port}${path}`;
const first = await fetch(url("/static/img/card.webp"));
const etag = first.headers.get("etag");
assert.match(etag, /^W\/"/);
assert.equal(await first.text(), "image-v1");
const unchanged = await fetch(url("/static/img/card.webp"), { headers: { "If-None-Match": `"other", ${etag}` } });
assert.equal(unchanged.status, 304);
assert.equal(unchanged.headers.get("cache-control"), "no-cache");
assert.equal(await unchanged.text(), "");
await writeFile(imageFile, "image-version-2");
const replaced = await fetch(url("/static/img/card.webp"), { headers: { "If-None-Match": etag } });
assert.equal(replaced.status, 200);
assert.equal(await replaced.text(), "image-version-2");
assert.equal((await fetch(url("/"))).headers.get("etag"), null);
assert.equal((await fetch(url("/assets/index-Ab12_cd34.js"))).headers.get("etag"), null);
});