import assert from "node:assert/strict"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import test from "node:test"; import { assertCloudWebDistFresh, buildCloudWebDist, cloudWebAppSourceFiles, cloudWebDistAliasFiles, cloudWebDistRuntimeFiles, inspectCloudWebDistFreshness } from "./dist-contract.ts"; test("Cloud Web dist build copies runtime assets and route aliases", async () => { const rootDir = makeCloudWebFixture(); try { const distDir = await buildCloudWebDist(rootDir); assert.equal(distDir, path.join(rootDir, "dist")); for (const relativePath of cloudWebDistRuntimeFiles.filter((file) => file !== "app.js")) { assert.equal(read(path.join(distDir, relativePath)), read(path.join(rootDir, relativePath)), `runtime asset ${relativePath}`); } assert.match(read(path.join(distDir, "app.js")), /console\.log\("fixture"\)/u); for (const relativePath of cloudWebDistAliasFiles) { assert.equal(read(path.join(distDir, relativePath)), read(path.join(rootDir, "index.html")), `alias asset ${relativePath}`); } const freshness = await assertCloudWebDistFresh(rootDir); assert.equal(freshness.status, "pass"); assert.deepEqual(freshness.mismatches, []); } finally { fs.rmSync(rootDir, { recursive: true, force: true }); } }); test("Cloud Web dist freshness reports stale runtime assets", async () => { const rootDir = makeCloudWebFixture(); try { await buildCloudWebDist(rootDir); fs.writeFileSync(path.join(rootDir, "app-helpers.ts"), "console.log('changed');\n"); const freshness = await inspectCloudWebDistFreshness(rootDir); assert.equal(freshness.status, "blocked"); assert.deepEqual(freshness.mismatches, ["app.js"]); assert.equal(freshness.files.find((file) => file.path === "app.js")?.status, "mismatch"); } finally { fs.rmSync(rootDir, { recursive: true, force: true }); } }); function makeCloudWebFixture() { const rootDir = fs.mkdtempSync(path.join(os.tmpdir(), "hwlab-cloud-web-dist-")); for (const relativePath of cloudWebDistRuntimeFiles.filter((file) => file !== "app.js")) { const filePath = path.join(rootDir, relativePath); fs.mkdirSync(path.dirname(filePath), { recursive: true }); fs.writeFileSync(filePath, `${relativePath}\n`); } for (const relativePath of cloudWebAppSourceFiles) { const filePath = path.join(rootDir, relativePath); fs.mkdirSync(path.dirname(filePath), { recursive: true }); fs.writeFileSync(filePath, relativePath === "app.ts" ? "console.log(\"fixture\");\n" : ""); } fs.writeFileSync(path.join(rootDir, "index.html"), "fixture\n"); return rootDir; } function read(filePath) { return fs.readFileSync(filePath, "utf8"); }