Skip to content
Merged
21 changes: 15 additions & 6 deletions src/__tests__/mcp-cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
// Mock variable names must be unique across .test.ts files (shared TS scope).
const mockBuildSessionMcp = vi.fn((args: Record<string, unknown>) => args);
const mockStartTUIMcp = vi.fn(() => ({
unmount: vi.fn(),
store: { session: {} },
}));
const mockReadApiKeyFromEnvMcp = vi.fn(() => undefined as string | undefined);
// Hoisted, not a plain const: analytics.ts now imports wizard-session.ts
// statically, so the mock factory runs before a const would initialize.
const { mockBuildSessionMcp, mockStartTUIMcp, mockReadApiKeyFromEnvMcp } =
vi.hoisted(() => ({
mockBuildSessionMcp: vi.fn((args: Record<string, unknown>) => args),
mockStartTUIMcp: vi.fn(() => ({
unmount: vi.fn(),
store: { session: {} },
})),
mockReadApiKeyFromEnvMcp: vi.fn(() => undefined as string | undefined),
}));

vi.mock('@lib/wizard-session', () => ({
buildSession: mockBuildSessionMcp,
// analytics.ts imports this for sessionProperties(); unused by this
// suite's assertions, stubbed only so the mocked module still satisfies
// the real module's exports.
reportableDiscoveredFeatures: () => undefined,
}));
vi.mock('@ui/tui/start-tui', () => ({
startTUI: mockStartTUIMcp,
Expand Down Expand Up @@ -60,13 +69,13 @@
});

test('starts the TUI with the McpAdd program id', async () => {
mcpAddCommand.handler!(makeArgv());

Check warning on line 72 in src/__tests__/mcp-cli.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Forbidden non-null assertion
await flush();
expect(mockStartTUIMcp).toHaveBeenCalledWith(expect.any(String), 'mcp-add');
});

test('passes --local through as localMcp', async () => {
mcpAddCommand.handler!(makeArgv({ local: true }));

Check warning on line 78 in src/__tests__/mcp-cli.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Forbidden non-null assertion
await flush();
expect(mockBuildSessionMcp).toHaveBeenCalledWith(
expect.objectContaining({ localMcp: true }),
Expand All @@ -74,7 +83,7 @@
});

test('passes --api-key through to buildSession', async () => {
mcpAddCommand.handler!(makeArgv({ apiKey: 'phx_from_flag' }));

Check warning on line 86 in src/__tests__/mcp-cli.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Forbidden non-null assertion
await flush();
expect(mockBuildSessionMcp).toHaveBeenCalledWith(
expect.objectContaining({ apiKey: 'phx_from_flag' }),
Expand All @@ -83,7 +92,7 @@

test('falls back to readApiKeyFromEnv when --api-key is omitted', async () => {
mockReadApiKeyFromEnvMcp.mockReturnValueOnce('phx_from_env');
mcpAddCommand.handler!(makeArgv());

Check warning on line 95 in src/__tests__/mcp-cli.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Forbidden non-null assertion
await flush();
expect(mockBuildSessionMcp).toHaveBeenCalledWith(
expect.objectContaining({ apiKey: 'phx_from_env' }),
Expand All @@ -91,7 +100,7 @@
});

test('parses --features into a trimmed array', async () => {
mcpAddCommand.handler!(makeArgv({ features: 'flags, errors , logs' }));

Check warning on line 103 in src/__tests__/mcp-cli.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Forbidden non-null assertion
await flush();
expect(mockBuildSessionMcp).toHaveBeenCalledWith(
expect.objectContaining({ mcpFeatures: ['flags', 'errors', 'logs'] }),
Expand Down Expand Up @@ -122,7 +131,7 @@
});

test('starts the TUI with the McpRemove program id', async () => {
mcpRemoveCommand.handler!(makeArgv());

Check warning on line 134 in src/__tests__/mcp-cli.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Forbidden non-null assertion
await flush();
expect(mockStartTUIMcp).toHaveBeenCalledWith(
expect.any(String),
Expand All @@ -131,7 +140,7 @@
});

test('passes --local through as localMcp', async () => {
mcpRemoveCommand.handler!(makeArgv({ local: true }));

Check warning on line 143 in src/__tests__/mcp-cli.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Forbidden non-null assertion
await flush();
expect(mockBuildSessionMcp).toHaveBeenCalledWith(
expect.objectContaining({ localMcp: true }),
Expand Down
Loading
Loading