Files
healer-man/server/bundled-content.test.mjs
2026-09-11 14:26:15 -04:00

49 lines
2.5 KiB
JavaScript

import assert from "node:assert/strict";
import { createServer, request } from "node:http";
import { mkdtemp, mkdir, writeFile, rm } from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import test from "node:test";
import { gzipSync, gunzipSync } from "node:zlib";
import { createBundledContentHandler } from "./bundled-content.mjs";
test("first-launch logical asset URLs serve shared gzip objects with lossless identity fallback", async () => {
const root = await mkdtemp(path.join(os.tmpdir(), "healer-bundled-server-"));
const original = Buffer.from("glTF original model bytes ".repeat(1000));
const compressed = gzipSync(original);
const objectUrl = `/content/bundled/${"a".repeat(64)}.glb.gzip`;
await mkdir(path.join(root, "content/bundled"), { recursive: true });
await writeFile(path.join(root, objectUrl.slice(1)), compressed);
await writeFile(path.join(root, "content/bootstrap-manifest.json"), JSON.stringify({ files: {
"/assets/game/dungeons/test/visual.glb": { bytes: original.length, download: { url: objectUrl, encoding: "gzip", bytes: compressed.length } },
} }));
const serve = createBundledContentHandler(root);
const server = createServer((req, res) => {
if (!serve(req, res, new URL(req.url, "http://localhost").pathname)) { res.writeHead(404); res.end(); }
});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
const port = server.address().port;
const get = (encoding, method = "GET") => new Promise((resolve, reject) => {
request({ host: "127.0.0.1", port, path: "/assets/game/dungeons/test/visual.glb?runtime=1", method, headers: { "Accept-Encoding": encoding } }, (response) => {
const chunks = [];
response.on("data", (chunk) => chunks.push(chunk));
response.on("end", () => resolve({ headers: response.headers, body: Buffer.concat(chunks) }));
}).on("error", reject).end();
});
try {
const gzip = await get("gzip");
assert.equal(gzip.headers["content-encoding"], "gzip");
assert.deepEqual(gzip.body, compressed);
assert.deepEqual(gunzipSync(gzip.body), original);
const identity = await get("gzip;q=0, *;q=1");
assert.equal(identity.headers["content-encoding"], undefined);
assert.deepEqual(identity.body, original);
const head = await get("gzip", "HEAD");
assert.equal(head.body.length, 0);
assert.equal(Number(head.headers["content-length"]), compressed.length);
} finally {
await new Promise((resolve) => server.close(resolve));
await rm(root, { recursive: true, force: true });
}
});