refactor: reuse shared cli runtime test mocks

This commit is contained in:
Peter Steinberger
2026-03-23 01:42:00 +00:00
parent 2e6f2b0f07
commit 54213b587f
16 changed files with 176 additions and 283 deletions

View File

@@ -1,5 +1,6 @@
import { Command } from "commander";
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { createCliRuntimeCapture } from "../test-runtime-capture.js";
const agentCliCommandMock = vi.fn();
const agentsAddCommandMock = vi.fn();
@@ -12,17 +13,7 @@ const agentsUnbindCommandMock = vi.fn();
const setVerboseMock = vi.fn();
const createDefaultDepsMock = vi.fn(() => ({ deps: true }));
const runtime = {
log: vi.fn(),
error: vi.fn(),
writeStdout: vi.fn((value: string) => {
runtime.log(value.endsWith("\n") ? value.slice(0, -1) : value);
}),
writeJson: vi.fn((value: unknown, space = 2) => {
runtime.log(JSON.stringify(value, null, space));
}),
exit: vi.fn(),
};
const { defaultRuntime: runtime, resetRuntimeCapture } = createCliRuntimeCapture();
vi.mock("../../commands/agent-via-gateway.js", () => ({
agentCliCommand: agentCliCommandMock,
@@ -80,6 +71,8 @@ describe("registerAgentCommands", () => {
beforeEach(() => {
vi.clearAllMocks();
resetRuntimeCapture();
runtime.exit.mockImplementation(() => {});
agentCliCommandMock.mockResolvedValue(undefined);
agentsAddCommandMock.mockResolvedValue(undefined);
agentsBindingsCommandMock.mockResolvedValue(undefined);

View File

@@ -1,20 +1,11 @@
import { Command } from "commander";
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { createCliRuntimeCapture } from "../test-runtime-capture.js";
const backupCreateCommand = vi.fn();
const backupVerifyCommand = vi.fn();
const runtime = {
log: vi.fn(),
error: vi.fn(),
writeStdout: vi.fn((value: string) => {
runtime.log(value.endsWith("\n") ? value.slice(0, -1) : value);
}),
writeJson: vi.fn((value: unknown, space = 2) => {
runtime.log(JSON.stringify(value, null, space));
}),
exit: vi.fn(),
};
const { defaultRuntime: runtime, resetRuntimeCapture } = createCliRuntimeCapture();
vi.mock("../../commands/backup.js", () => ({
backupCreateCommand,
@@ -56,6 +47,7 @@ describe("registerBackupCommand", () => {
beforeEach(() => {
vi.clearAllMocks();
resetRuntimeCapture();
backupCreateCommand.mockResolvedValue(undefined);
backupVerifyCommand.mockResolvedValue(undefined);
});

View File

@@ -1,5 +1,6 @@
import { Command } from "commander";
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { createCliRuntimeCapture } from "../test-runtime-capture.js";
const statusCommand = vi.fn();
const healthCommand = vi.fn();
@@ -7,17 +8,7 @@ const sessionsCommand = vi.fn();
const sessionsCleanupCommand = vi.fn();
const setVerbose = vi.fn();
const runtime = {
log: vi.fn(),
error: vi.fn(),
writeStdout: vi.fn((value: string) => {
runtime.log(value.endsWith("\n") ? value.slice(0, -1) : value);
}),
writeJson: vi.fn((value: unknown, space = 2) => {
runtime.log(JSON.stringify(value, null, space));
}),
exit: vi.fn(),
};
const { defaultRuntime: runtime, resetRuntimeCapture } = createCliRuntimeCapture();
vi.mock("../../commands/status.js", () => ({
statusCommand,
@@ -58,6 +49,8 @@ describe("registerStatusHealthSessionsCommands", () => {
beforeEach(() => {
vi.clearAllMocks();
resetRuntimeCapture();
runtime.exit.mockImplementation(() => {});
statusCommand.mockResolvedValue(undefined);
healthCommand.mockResolvedValue(undefined);
sessionsCommand.mockResolvedValue(undefined);