import { afterAll, expect, test } from "bun:test" import { createHash } from "node:crypto" import { chmodSync, existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, statSync, symlinkSync, writeFileSync, } from "node:fs" import { get as httpGet } from "node:http" import { createServer as netServer, connect as netConnect } from "node:net" import { homedir, tmpdir } from "node:os" import { basename, dirname, join } from "node:path" import { LayerfsTools } from "./index.mjs" import { DEFAULT_ROOT, IMPORT_MAX_FILES, LEGACY_ROOT, SimBackend, checkSourceDir, createBackend, defaultRoot, scanImportTree, } from "./layerfs.mjs" const sha = (s) => createHash("sha256").update(s).digest("hex") // Hygiene: every temp dir a test makes is registered and removed, and // the process env the suite mutates is restored whole at the end — // a test run leaves nothing behind but its results. const envSnapshot = { ...process.env } const tmpDirs = [] function mkTmp(prefix, base = tmpdir()) { const d = mkdtempSync(join(base, prefix)) tmpDirs.push(d) return d } // The cleanup's time grows with the machine's disk load, not with the // suite: on a loaded box removing every dir took over bun's default 5s // hook bar (#362), so the hook carries its own. afterAll(() => { for (const d of tmpDirs) rmSync(d, { recursive: true, force: true }) for (const k of Object.keys(process.env)) if (!(k in envSnapshot)) delete process.env[k] Object.assign(process.env, envSnapshot) }, 120_000) // Suite hygiene (0.10.0 review, item 3): no test may touch the real // HOME — the first test calls LayerfsTools with no env at all, so the // suite pins a scratch home before any test runs; simTools still gives // each test its own home. process.env.MAGPIE_LAYERFS_HOME = mkTmp("layerfs-suite-home-") function fixture() { const src = mkTmp("layerfs-src-") mkdirSync(join(src, "docs"), { recursive: true }) writeFileSync(join(src, "README.md"), "hello layerfs\n") writeFileSync(join(src, "docs", "a.txt"), "A".repeat(1000)) writeFileSync(join(src, "docs", "b.txt"), "A".repeat(1000)) // identical to a.txt on purpose writeFileSync(join(src, "code.js"), "console.log(1)\n") return src } async function simTools(opts = { exec: true }) { const home = mkTmp("layerfs-home-") process.env.MAGPIE_LAYERFS_HOME = home process.env.MAGPIE_LAYERFS_BACKEND = "sim" delete process.env.MAGPIE_LAYERFS_EXEC delete process.env.MAGPIE_LAYERFS_EXEC2 delete process.env.MAGPIE_LAYERFS_INSTALL_DIR delete process.env.MAGPIE_LAYERFS_DATA_DIR delete process.env.MAGPIE_LAYERFS_PAGE_DIR delete process.env.MAGPIE_LAYERFS_PAGE_PORT // Each instance gets its own service port: the host starts one per // LayerfsTools(), and two instances sharing a port would leave the // second silently serviceless. const port = await freePort() const src = fixture() // ctx.directory is magpie's CONFIG directory in the real host — the // harness models it as its own temp dir, never the source const { tool } = await LayerfsTools({ directory: mkTmp("magpie-config-") }, { ...opts, pagePort: port }) // provision's source is a required argument since 0.7.0: the wrapper // passes the fixture explicitly, the way a caller now must const call = (name, args) => tool[name].execute( name === "provision" && args.source === undefined ? { ...args, source: src } : args, { agent: "test", abort: AbortSignal.timeout(5000) }) return { call, home, src, tool, port, url: `http://127.0.0.1:${port}/` } } // A port nothing holds right now: bind 0, read it back, let go. The // window between release and the service's bind is small, and a test // that polls the service fails loudly if it ever loses the race. const freePort = () => new Promise((resolve, reject) => { const s = netServer() s.once("error", reject) s.listen(0, "127.0.0.1", () => { const p = s.address().port; s.close(() => resolve(p)) }) }) // Poll the service until its embedded trajectory holds the expected // state: the service starts off the tool calls' path, so a test that // needs it waits for the state, not for a fixed duration. async function serviceData(url, want, ms = 8000) { const deadline = Date.now() + ms for (;;) { try { const res = await fetch(url) if (res.ok) { const html = await res.text() const m = html.match(/y" await call("provision", { project: "website" }) await call("checkpoint", { project: "website", message: evil }) const { data, html } = await serviceData(url, (x) => (x.projects.find((p) => p.name === "website")?.branches.find((b) => b.name === "main")?.commits ?? []) .some((c) => c.message === evil)) // the breakout is escaped in the raw HTML, and parses back whole expect(html).not.toContain("