test: restore runtime-aware cli mocks

This commit is contained in:
Peter Steinberger
2026-03-22 18:28:42 -07:00
parent c43bfcbbec
commit 75835fc664
16 changed files with 110 additions and 81 deletions

View File

@@ -24,21 +24,24 @@ vi.mock("../../daemon/service.js", () => ({
resolveGatewayService: () => serviceMock,
}));
vi.mock("../../runtime.js", () => ({
defaultRuntime: {
log: (message: string) => runtimeLogs.push(message),
error: (message: string) => runtimeErrors.push(message),
writeStdout: (value: string) => {
runtimeLogs.push(value.endsWith("\n") ? value.slice(0, -1) : value);
vi.mock("../../runtime.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("../../runtime.js")>();
return {
...actual,
defaultRuntime: {
...actual.defaultRuntime,
log: (message: string) => runtimeLogs.push(message),
error: (message: string) => runtimeErrors.push(message),
writeStdout: (value: string) =>
runtimeLogs.push(value.endsWith("\n") ? value.slice(0, -1) : value),
writeJson: (value: unknown, space = 2) =>
runtimeLogs.push(JSON.stringify(value, null, space > 0 ? space : undefined)),
exit: (code: number) => {
throw new Error(`__exit__:${code}`);
},
},
writeJson: (value: unknown, space = 2) => {
runtimeLogs.push(JSON.stringify(value, null, space > 0 ? space : undefined));
},
exit: (code: number) => {
throw new Error(`__exit__:${code}`);
},
},
}));
};
});
const { runDaemonInstall } = await import("./install.js");
const { clearConfigCache } = await import("../../config/config.js");

View File

@@ -8,8 +8,6 @@ export const runtimeLogs: string[] = [];
type LifecycleRuntimeHarness = OutputRuntimeEnv & {
error: MockFn<OutputRuntimeEnv["error"]>;
exit: MockFn<OutputRuntimeEnv["exit"]>;
writeStdout: MockFn<(value: string) => void>;
writeJson: MockFn<(value: unknown, space?: number) => void>;
};
type LifecycleServiceHarness = GatewayService & {
@@ -26,16 +24,16 @@ export const defaultRuntime: LifecycleRuntimeHarness = {
log: (...args: unknown[]) => {
runtimeLogs.push(args.map((arg) => String(arg)).join(" "));
},
writeStdout: (value: string) => {
runtimeLogs.push(value.endsWith("\n") ? value.slice(0, -1) : value);
},
writeJson: (value: unknown, space = 2) => {
runtimeLogs.push(JSON.stringify(value, null, space > 0 ? space : undefined));
},
error: vi.fn(),
exit: vi.fn((code: number) => {
throw new Error(`__exit__:${code}`);
}),
writeStdout: vi.fn((value: string) => {
runtimeLogs.push(value.endsWith("\n") ? value.slice(0, -1) : value);
}),
writeJson: vi.fn((value: unknown, space = 2) => {
runtimeLogs.push(JSON.stringify(value, null, space > 0 ? space : undefined));
}),
};
export const service: LifecycleServiceHarness = {