Files
fusion/packages/cli/src/__tests__/update-cache.test.ts
Fusion (runfusion.ai) 2a8e3e5f0c fix(FN-4812): stabilize hoisted update-cache test fixture
Fusion-Task-Id: FN-4812
Fusion-Task-Lineage: 9877a784-5320-410c-82fa-3f6a6d9b5358
2026-05-16 19:16:48 -07:00

60 lines
1.8 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
import { readFileSync } from "node:fs";
const CLI_PACKAGE_VERSION = (
JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf-8")) as { version: string }
).version;
const { cacheDir, mockResolveGlobalDir } = vi.hoisted(() => {
const dir = `/tmp/fusion-update-cache-test-${process.pid}-${Date.now()}`;
return {
cacheDir: dir,
mockResolveGlobalDir: vi.fn().mockReturnValue(dir),
};
});
vi.mock("@fusion/core", () => ({
resolveGlobalDir: mockResolveGlobalDir,
GlobalSettingsStore: vi.fn(),
}));
const { getCachedUpdateStatus } = await import("../update-cache.js");
function writeUpdateCache(payload: { updateAvailable: boolean; latestVersion: string; currentVersion: string }): void {
mkdirSync(cacheDir, { recursive: true });
writeFileSync(`${cacheDir}/update-check.json`, JSON.stringify(payload), "utf-8");
}
beforeEach(() => {
rmSync(cacheDir, { recursive: true, force: true });
mockResolveGlobalDir.mockReset();
mockResolveGlobalDir.mockReturnValue(cacheDir);
});
describe("getCachedUpdateStatus", () => {
it("returns the cached update when it matches the installed CLI version", () => {
writeUpdateCache({
updateAvailable: true,
currentVersion: CLI_PACKAGE_VERSION,
latestVersion: "9.9.9",
});
expect(getCachedUpdateStatus(CLI_PACKAGE_VERSION)).toEqual({
updateAvailable: true,
currentVersion: CLI_PACKAGE_VERSION,
latestVersion: "9.9.9",
});
});
it("ignores stale cached updates from a different installed CLI version", () => {
writeUpdateCache({
updateAvailable: true,
currentVersion: "0.0.1",
latestVersion: "9.9.9",
});
expect(getCachedUpdateStatus(CLI_PACKAGE_VERSION)).toBeNull();
});
});