Files
fusion/packages/cli/src/__tests__/update-cache.test.ts
gsxdsm 550e9edd46 FN-6367: fix constructible CLI test mocks
Update CLI test mocks so Vitest-spied constructors remain new-able under current mock semantics.

- Add constructible mock wrappers for TaskStore and related CLI test constructor mocks.
- Replace arrow-function constructor mock implementations with function-based implementations.
- Bring task retry fixture data in line with the graph resume retry counter shape.

Files changed:
 .../cli/src/__tests__/experiment-finalize.test.ts   | 19 +++++++++++++++++--
 .../__tests__/extension-experiment-finalize.test.ts | 19 +++++++++++++++++--
 packages/cli/src/__tests__/plugin-dev.test.ts       | 19 +++++++++++++++++--
 packages/cli/src/__tests__/project-resolver.test.ts | 17 ++++++++++++++++-
 packages/cli/src/__tests__/task-plan.test.ts        | 17 ++++++++++++++++-
 packages/cli/src/__tests__/task-steer.test.ts       | 17 ++++++++++++++++-
 packages/cli/src/__tests__/update-cache.test.ts     | 17 ++++++++++++++++-
 packages/cli/src/commands/__tests__/agent.test.ts   | 17 ++++++++++++++++-
 packages/cli/src/commands/__tests__/backup.test.ts  | 17 ++++++++++++++++-
 packages/cli/src/commands/__tests__/db.test.ts      | 17 ++++++++++++++++-
 packages/cli/src/commands/__tests__/desktop.test.ts |  4 +++-
 packages/cli/src/commands/__tests__/init.test.ts    | 17 ++++++++++++++++-
 .../src/commands/__tests__/memory-backup.test.ts    | 17 ++++++++++++++++-
 packages/cli/src/commands/__tests__/message.test.ts | 17 ++++++++++++++++-
 packages/cli/src/commands/__tests__/node.test.ts    | 17 ++++++++++++++++-
 packages/cli/src/commands/__tests__/plugin.test.ts  | 19 +++++++++++++++++--
 packages/cli/src/commands/__tests__/project.test.ts | 21 ++++++++++++++++++---
 packages/cli/src/commands/__tests__/serve.test.ts   | 17 ++++++++++++++++-
 .../src/commands/__tests__/settings-export.test.ts  | 17 ++++++++++++++++-
 .../src/commands/__tests__/settings-import.test.ts  | 17 ++++++++++++++++-
 .../cli/src/commands/__tests__/settings.test.ts     | 17 ++++++++++++++++-
 packages/cli/src/commands/__tests__/task.test.ts    |  2 ++
 22 files changed, 331 insertions(+), 27 deletions(-)

Fusion-Task-Id: FN-6367
Fusion-Task-Lineage: d900dfe0-e286-4175-a5ba-7c319e2527b0
2026-06-13 09:14:02 -07:00

75 lines
2.6 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
import { readFileSync } from "node:fs";
function makeConstructibleMock<T extends (...args: any[]) => unknown>(impl?: T) {
const mock = vi.fn(function () {});
const originalMockImplementation = mock.mockImplementation.bind(mock);
const originalMockImplementationOnce = mock.mockImplementationOnce.bind(mock);
const wrap = (nextImpl: T) => function (this: unknown, ...args: Parameters<T>) {
return nextImpl(...args);
};
mock.mockImplementation = ((nextImpl: T) => originalMockImplementation(wrap(nextImpl))) as typeof mock.mockImplementation;
mock.mockImplementationOnce = ((nextImpl: T) => originalMockImplementationOnce(wrap(nextImpl))) as typeof mock.mockImplementationOnce;
if (impl) {
mock.mockImplementation(impl);
}
return mock;
}
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: makeConstructibleMock(),
}));
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();
});
});