Files
WRNexusJS/packages/cli/test/update-notifier.test.ts
T
Clintchiz 7a36c1905f
Quality / quality (ubuntu-latest) (push) Failing after 9m54s
Quality / quality (windows-latest) (push) Canceled after 0s
fix(cli): handle version flags before workspace discovery
2026-08-24 15:03:38 +05:30

60 lines
2.1 KiB
TypeScript

import { afterEach, expect, test } from "bun:test";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import {
compareVersions,
currentCliVersion,
notifyIfUpdateAvailable,
} from "../src/update-notifier.ts";
const originalCache = process.env.WRNEXUS_UPDATE_CACHE;
afterEach(() => {
if (originalCache === undefined) delete process.env.WRNEXUS_UPDATE_CACHE;
else process.env.WRNEXUS_UPDATE_CACHE = originalCache;
});
test("compareVersions compares stable semantic versions", () => {
expect(compareVersions("0.2.14", "0.2.13")).toBe(1);
expect(compareVersions("0.2.13", "0.2.13")).toBe(0);
expect(compareVersions("0.2.12", "0.2.13")).toBe(-1);
});
test("cached update checks suggest the exact upgrade command", () => {
const root = mkdtempSync(join(tmpdir(), "wrnexus-notify-"));
const cache = join(root, "update.json");
process.env.WRNEXUS_UPDATE_CACHE = cache;
writeFileSync(cache, JSON.stringify({ checkedAt: Date.now(), latest: "0.2.14" }));
const messages: string[] = [];
const original = console.log;
console.log = (...parts) => messages.push(parts.join(" "));
try {
notifyIfUpdateAvailable("0.2.13", "dev");
expect(messages.join("\n")).toContain("WRNexusJS 0.2.13 → 0.2.14");
expect(messages.join("\n")).toContain("wrnexus update --latest");
} finally {
console.log = original;
rmSync(root, { recursive: true, force: true });
}
});
test("--version prints the package version outside a WrNexus workspace", () => {
const root = mkdtempSync(join(tmpdir(), "wrnexus-version-"));
try {
const result = Bun.spawnSync({
cmd: [process.execPath, resolve(import.meta.dir, "../src/index.ts"), "--version"],
cwd: root,
env: { ...process.env, WRNEXUS_NO_UPDATE_CHECK: "1" },
stdout: "pipe",
stderr: "pipe",
});
expect(result.exitCode).toBe(0);
expect(result.stdout.toString().trim()).toBe(currentCliVersion());
expect(result.stderr.toString()).toBe("");
} finally {
rmSync(root, { recursive: true, force: true });
}
});