feat(FN-3053): consolidate mock helpers across CLI, dashboard, and engine p

The merge consolidates mock helpers across packages by creating a new shared `mockCoreEngine.ts` in the CLI package and strengthening the core/engine helpers, then migrating both CLI command tests and the dashboard's `AgentsView` tests to use the canonical helpers. It also publishes SSE architecture

Fusion-Task-Id: FN-3053
This commit is contained in:
Fusion
2026-05-01 05:02:16 -07:00
committed by gsxdsm
parent 5e51542a1d
commit eaf9d6adf9
10 changed files with 226 additions and 47 deletions

View File

@@ -0,0 +1,33 @@
import { describe, expect, it, vi } from "vitest";
import {
createCliCoreMock,
createCliEngineMock,
resetCliCoreEngineMockState,
} from "./mockCoreEngine";
describe("cli test mock helpers", () => {
it("creates stable fallback functions for missing callable exports", async () => {
const module = await createCliCoreMock(async () => ({ known: vi.fn() }));
const first = module.missingThing as ReturnType<typeof vi.fn>;
const second = module.missingThing as ReturnType<typeof vi.fn>;
expect(first).toBe(second);
first("x");
expect(first).toHaveBeenCalledWith("x");
resetCliCoreEngineMockState();
expect(first).not.toHaveBeenCalled();
});
it("keeps real non-function exports while allowing callable overrides", async () => {
const module = await createCliEngineMock(
async () => ({ VERSION: "1.0.0", factory: () => "real" }),
{},
{ factory: vi.fn().mockReturnValue("mocked") },
);
expect(module.VERSION).toBe("1.0.0");
expect((module.factory as () => string)()).toBe("mocked");
});
});

View File

@@ -0,0 +1,59 @@
/**
* Canonical @fusion/core and @fusion/engine mock helpers for CLI command tests.
*
* When a new commonly-mocked export is added, update defaults here instead of
* copying large inline export lists into command suites.
*/
import { vi, type Mock } from "vitest";
type AnyModule = Record<string, unknown>;
type AnyMock = Mock;
const fallbackFns = new Map<string, AnyMock>();
function getFallback(name: string): AnyMock {
if (!fallbackFns.has(name)) {
fallbackFns.set(name, vi.fn());
}
return fallbackFns.get(name)!;
}
function withFallbackFunctions(actual: AnyModule, mocked: AnyModule): AnyModule {
return new Proxy(mocked, {
get(target, prop, receiver) {
if (typeof prop !== "string") return Reflect.get(target, prop, receiver);
if (Reflect.has(target, prop)) return Reflect.get(target, prop, receiver);
if (["then", "catch", "finally"].includes(prop)) return undefined;
const actualValue = actual[prop];
if (typeof actualValue === "function" || actualValue === undefined) {
const fn = getFallback(prop);
target[prop] = fn;
return fn;
}
return actualValue;
},
});
}
export async function createCliCoreMock(
importActual: () => Promise<AnyModule>,
defaults: AnyModule = {},
overrides: AnyModule = {},
): Promise<AnyModule> {
const actual = await importActual();
return withFallbackFunctions(actual, { ...actual, ...defaults, ...overrides });
}
export async function createCliEngineMock(
importActual: () => Promise<AnyModule>,
defaults: AnyModule = {},
overrides: AnyModule = {},
): Promise<AnyModule> {
const actual = await importActual();
return withFallbackFunctions(actual, { ...actual, ...defaults, ...overrides });
}
export function resetCliCoreEngineMockState(): void {
for (const fn of fallbackFns.values()) fn.mockReset();
}