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:
@@ -1,27 +1,56 @@
|
||||
/**
|
||||
* Canonical @fusion/core and @fusion/engine mock helpers for dashboard server tests.
|
||||
*
|
||||
* Add new mocked exports here first before duplicating large per-suite mock objects.
|
||||
* If a route test starts failing with "No \"X\" export is defined", update this
|
||||
* helper first instead of adding another full inline export map in the test file.
|
||||
*/
|
||||
import { vi } from "vitest";
|
||||
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, moduleValue: AnyModule): AnyModule {
|
||||
return new Proxy(moduleValue, {
|
||||
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 createCoreMock(
|
||||
importActual: () => Promise<AnyModule>,
|
||||
overrides: AnyModule = {},
|
||||
): Promise<AnyModule> {
|
||||
const actual = await importActual();
|
||||
return {
|
||||
...actual,
|
||||
...overrides,
|
||||
};
|
||||
return withFallbackFunctions(actual, { ...actual, ...overrides });
|
||||
}
|
||||
|
||||
export function createEngineMock(overrides: AnyModule = {}): AnyModule {
|
||||
return {
|
||||
const actual: AnyModule = {};
|
||||
return withFallbackFunctions(actual, {
|
||||
createFnAgent: vi.fn(),
|
||||
promptWithFallback: vi.fn(),
|
||||
...overrides,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export function resetDashboardServerMockState(): void {
|
||||
for (const fn of fallbackFns.values()) fn.mockReset();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user