import { afterEach, expect, test } from "bun:test";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { runUpdate } from "../src/update.ts";
const roots: string[] = [];
afterEach(() => {
for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true });
});
function project(name: string): string {
const root = mkdtempSync(join(tmpdir(), `wrnexus-update-exit-${name}-`));
roots.push(root);
mkdirSync(join(root, "app"), { recursive: true });
writeFileSync(
join(root, "package.json"),
JSON.stringify({
name: `update-exit-${name}`,
dependencies: { "@wrnexus/core": "^0.8.0" },
wrnexus: { version: "0.8.0" },
}),
);
return root;
}
/**
* `runUpdate` reports failure via `process.exitCode` (never a real
* `process.exit()` call) on this path -- see the existing `--delegated` and
* verification-failure branches in `src/update.ts`. `--delegated` skips the
* "fetch a newer published CLI" handoff (there is no published 0.9.0 yet),
* matching how a real newer CLI re-invokes itself. `--dry-run` keeps the test
* offline too: the dry-run path returns before `bun install`/verification
* ever run, so no network access is needed to observe the exit code this
* task adds.
*/
test("a project with a legacy bare body exits non-zero", async () => {
const root = project("needs-review");
writeFileSync(
join(root, "app", "Hello.wrn"),
`page Hello {
ssr {
api ssrUsers GET /api/users/ssr {
return userNames(users)
}
}
view { x }
}
`,
);
const before = process.exitCode;
process.exitCode = 0;
try {
await runUpdate(root, ["--dry-run", "--version=0.9.0", "--delegated"]);
expect(process.exitCode).toBeTruthy();
} finally {
process.exitCode = before ?? 0;
}
});
test("a fully-migratable project exits zero", async () => {
const root = project("clean");
writeFileSync(
join(root, "app", "Hello.wrn"),
`page Hello {
view { x }
}
`,
);
const before = process.exitCode;
process.exitCode = 0;
try {
await runUpdate(root, ["--dry-run", "--version=0.9.0", "--delegated"]);
expect(process.exitCode ?? 0).toBe(0);
} finally {
process.exitCode = before ?? 0;
}
});