From af784b9a8c0b82913bbf55080005c829b3daf752 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 14 Feb 2026 20:09:21 +0000 Subject: [PATCH] refactor(test): share cli program e2e mocks --- src/cli/program.nodes-basic.e2e.test.ts | 52 +--------------- src/cli/program.smoke.e2e.test.ts | 79 +++++-------------------- src/cli/program.test-mocks.ts | 67 +++++++++++++++++++++ 3 files changed, 85 insertions(+), 113 deletions(-) create mode 100644 src/cli/program.test-mocks.ts diff --git a/src/cli/program.nodes-basic.e2e.test.ts b/src/cli/program.nodes-basic.e2e.test.ts index 12518b5aa22..7e778132c28 100644 --- a/src/cli/program.nodes-basic.e2e.test.ts +++ b/src/cli/program.nodes-basic.e2e.test.ts @@ -1,55 +1,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; +import { callGateway, installBaseProgramMocks, runTui, runtime } from "./program.test-mocks.js"; -const messageCommand = vi.fn(); -const statusCommand = vi.fn(); -const configureCommand = vi.fn(); -const configureCommandWithSections = vi.fn(); -const setupCommand = vi.fn(); -const onboardCommand = vi.fn(); -const callGateway = vi.fn(); -const runChannelLogin = vi.fn(); -const runChannelLogout = vi.fn(); -const runTui = vi.fn(); - -const runtime = { - log: vi.fn(), - error: vi.fn(), - exit: vi.fn(() => { - throw new Error("exit"); - }), -}; - -vi.mock("../commands/message.js", () => ({ messageCommand })); -vi.mock("../commands/status.js", () => ({ statusCommand })); -vi.mock("../commands/configure.js", () => ({ - CONFIGURE_WIZARD_SECTIONS: [ - "workspace", - "model", - "web", - "gateway", - "daemon", - "channels", - "skills", - "health", - ], - configureCommand, - configureCommandWithSections, -})); -vi.mock("../commands/setup.js", () => ({ setupCommand })); -vi.mock("../commands/onboard.js", () => ({ onboardCommand })); -vi.mock("../runtime.js", () => ({ defaultRuntime: runtime })); -vi.mock("./channel-auth.js", () => ({ runChannelLogin, runChannelLogout })); -vi.mock("../tui/tui.js", () => ({ runTui })); -vi.mock("../gateway/call.js", () => ({ - callGateway, - randomIdempotencyKey: () => "idem-test", - buildGatewayConnectionDetails: () => ({ - url: "ws://127.0.0.1:1234", - urlSource: "test", - message: "Gateway target: ws://127.0.0.1:1234", - }), -})); -vi.mock("./deps.js", () => ({ createDefaultDeps: () => ({}) })); +installBaseProgramMocks(); const { buildProgram } = await import("./program.js"); diff --git a/src/cli/program.smoke.e2e.test.ts b/src/cli/program.smoke.e2e.test.ts index 97e71d631fb..f7dd45a767a 100644 --- a/src/cli/program.smoke.e2e.test.ts +++ b/src/cli/program.smoke.e2e.test.ts @@ -1,68 +1,21 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; - -const messageCommand = vi.fn(); -const statusCommand = vi.fn(); -const configureCommand = vi.fn(); -const configureCommandWithSections = vi.fn(); -const setupCommand = vi.fn(); -const onboardCommand = vi.fn(); -const callGateway = vi.fn(); -const runChannelLogin = vi.fn(); -const runChannelLogout = vi.fn(); -const runTui = vi.fn(); -const loadAndMaybeMigrateDoctorConfig = vi.fn(); -const ensureConfigReady = vi.fn(); -const ensurePluginRegistryLoaded = vi.fn(); - -const runtime = { - log: vi.fn(), - error: vi.fn(), - exit: vi.fn(() => { - throw new Error("exit"); - }), -}; - -vi.mock("./plugin-registry.js", () => ({ - ensurePluginRegistryLoaded: () => undefined, -})); - -vi.mock("../commands/message.js", () => ({ messageCommand })); -vi.mock("../commands/status.js", () => ({ statusCommand })); -vi.mock("../commands/configure.js", () => ({ - CONFIGURE_WIZARD_SECTIONS: [ - "workspace", - "model", - "web", - "gateway", - "daemon", - "channels", - "skills", - "health", - ], +import { configureCommand, - configureCommandWithSections, -})); -vi.mock("../commands/setup.js", () => ({ setupCommand })); -vi.mock("../commands/onboard.js", () => ({ onboardCommand })); -vi.mock("../commands/doctor-config-flow.js", () => ({ - loadAndMaybeMigrateDoctorConfig, -})); -vi.mock("../runtime.js", () => ({ defaultRuntime: runtime })); -vi.mock("./channel-auth.js", () => ({ runChannelLogin, runChannelLogout })); -vi.mock("../tui/tui.js", () => ({ runTui })); -vi.mock("./plugin-registry.js", () => ({ ensurePluginRegistryLoaded })); -vi.mock("./program/config-guard.js", () => ({ ensureConfigReady })); -vi.mock("../gateway/call.js", () => ({ - callGateway, - randomIdempotencyKey: () => "idem-test", - buildGatewayConnectionDetails: () => ({ - url: "ws://127.0.0.1:1234", - urlSource: "test", - message: "Gateway target: ws://127.0.0.1:1234", - }), -})); -vi.mock("./deps.js", () => ({ createDefaultDeps: () => ({}) })); -vi.mock("./preaction.js", () => ({ registerPreActionHooks: () => {} })); + ensureConfigReady, + installBaseProgramMocks, + installSmokeProgramMocks, + messageCommand, + onboardCommand, + runChannelLogin, + runChannelLogout, + runTui, + runtime, + setupCommand, + statusCommand, +} from "./program.test-mocks.js"; + +installBaseProgramMocks(); +installSmokeProgramMocks(); const { buildProgram } = await import("./program.js"); diff --git a/src/cli/program.test-mocks.ts b/src/cli/program.test-mocks.ts new file mode 100644 index 00000000000..051b04b1b92 --- /dev/null +++ b/src/cli/program.test-mocks.ts @@ -0,0 +1,67 @@ +import { vi } from "vitest"; + +export const messageCommand = vi.fn(); +export const statusCommand = vi.fn(); +export const configureCommand = vi.fn(); +export const configureCommandWithSections = vi.fn(); +export const setupCommand = vi.fn(); +export const onboardCommand = vi.fn(); +export const callGateway = vi.fn(); +export const runChannelLogin = vi.fn(); +export const runChannelLogout = vi.fn(); +export const runTui = vi.fn(); + +export const loadAndMaybeMigrateDoctorConfig = vi.fn(); +export const ensureConfigReady = vi.fn(); +export const ensurePluginRegistryLoaded = vi.fn(); + +export const runtime = { + log: vi.fn(), + error: vi.fn(), + exit: vi.fn(() => { + throw new Error("exit"); + }), +}; + +export function installBaseProgramMocks() { + vi.mock("../commands/message.js", () => ({ messageCommand })); + vi.mock("../commands/status.js", () => ({ statusCommand })); + vi.mock("../commands/configure.js", () => ({ + CONFIGURE_WIZARD_SECTIONS: [ + "workspace", + "model", + "web", + "gateway", + "daemon", + "channels", + "skills", + "health", + ], + configureCommand, + configureCommandWithSections, + })); + vi.mock("../commands/setup.js", () => ({ setupCommand })); + vi.mock("../commands/onboard.js", () => ({ onboardCommand })); + vi.mock("../runtime.js", () => ({ defaultRuntime: runtime })); + vi.mock("./channel-auth.js", () => ({ runChannelLogin, runChannelLogout })); + vi.mock("../tui/tui.js", () => ({ runTui })); + vi.mock("../gateway/call.js", () => ({ + callGateway, + randomIdempotencyKey: () => "idem-test", + buildGatewayConnectionDetails: () => ({ + url: "ws://127.0.0.1:1234", + urlSource: "test", + message: "Gateway target: ws://127.0.0.1:1234", + }), + })); + vi.mock("./deps.js", () => ({ createDefaultDeps: () => ({}) })); +} + +export function installSmokeProgramMocks() { + vi.mock("./plugin-registry.js", () => ({ ensurePluginRegistryLoaded })); + vi.mock("../commands/doctor-config-flow.js", () => ({ + loadAndMaybeMigrateDoctorConfig, + })); + vi.mock("./program/config-guard.js", () => ({ ensureConfigReady })); + vi.mock("./preaction.js", () => ({ registerPreActionHooks: () => {} })); +}