import { expect, test } from "bun:test"; import { existsSync, readFileSync, readdirSync } from "node:fs"; import { join } from "node:path"; const repoRoot = join(import.meta.dir, "..", "..", ".."); const publishScript = join(repoRoot, "scripts", "publish-packages.ts"); /** * `@wrnexus/dev-server` ships two entries — `index` (the dev server bootstrap) * and `serve-entry` (the request path that compiles `.wrn` files). They share * `pipeline.ts`, which holds MUTABLE module state: `compileCacheDir`, set once * at startup by the bootstrap, and `browserArtifactPaths`, populated during * compilation and read when serving `/__wrnexus/client/*`. * * Bundling each entry independently gives each its own copy of that state. The * bootstrap then sets a cache dir the compiler never sees, and the compiler * records artifact paths the server never sees — so every client module 404s * and `.wrn` compilation writes nothing. It works from source (one module * instance) and fails only once published, which is why it reached a release. */ test("the publish build shares chunks so module state is not duplicated per entry", () => { const source = readFileSync(publishScript, "utf8"); expect(source).not.toMatch(/splitting:\s*false/); expect(source).toMatch(/splitting:\s*true/); }); test("a built dev-server dist declares its mutable pipeline state exactly once", () => { const dist = join(repoRoot, "packages", "dev-server", "dist"); if (!existsSync(dist)) { // The dist is a build artifact, absent on a clean checkout. The // configuration assertion above is the guard that always runs. return; } const bundles = readdirSync(dist).filter((file) => file.endsWith(".js")); expect(bundles.length).toBeGreaterThan(0); for (const declaration of [ "compileCacheDir", "browserArtifactPaths", "serveWrnBrowserArtifact", ]) { const owners = bundles.filter((file) => readFileSync(join(dist, file), "utf8").includes(declaration), ); expect({ declaration, owners }).toEqual({ declaration, owners: owners.slice(0, 1) }); } });