Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions apps/cli/src/commands/approve.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';

const {
createCliContextMock,
requireOnboardingMock,
createPipelineMock,
rehydrateContextMock,
startExecutionMock,
getThreadByIssueMock,
getLatestPlanMock,
updatePlanStatusMock,
} = vi.hoisted(() => ({
createCliContextMock: vi.fn(),
requireOnboardingMock: vi.fn(),
createPipelineMock: vi.fn(),
rehydrateContextMock: vi.fn(),
startExecutionMock: vi.fn(),
getThreadByIssueMock: vi.fn(),
getLatestPlanMock: vi.fn(),
updatePlanStatusMock: vi.fn(),
}));

vi.mock('../context', () => ({
createCliContext: createCliContextMock,
}));

vi.mock('./guard', () => ({
requireOnboarding: requireOnboardingMock,
}));

vi.mock('@shipcode/pipeline', () => ({
createPipeline: createPipelineMock,
}));

vi.mock('@shipcode/shared', () => ({
PIPELINE_PHASE: {
awaitingApproval: 'awaiting_approval',
},
}));

import { approveCommand } from './approve';

describe('approveCommand', () => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
vi.spyOn(process, 'exit').mockImplementation((code?: string | number | null) => {
throw new Error(`process.exit:${code ?? ''}`);
});

const structuredPlan = {
objective: 'Increase coverage',
files: [],
steps: [],
};

beforeEach(() => {
vi.clearAllMocks();
requireOnboardingMock.mockReturnValue(true);
createPipelineMock.mockReturnValue({
rehydrateContext: rehydrateContextMock,
startExecution: startExecutionMock,
});
createCliContextMock.mockReturnValue({
project: {
id: 'project-1',
path: '/repo',
},
pipelineDeps: { deps: true },
threads: {
getByProjectAndGithubIssue: getThreadByIssueMock,
},
plans: {
getLatest: getLatestPlanMock,
updateStatus: updatePlanStatusMock,
},
});
getThreadByIssueMock.mockReturnValue({
id: 'thread-1',
title: 'Ship coverage',
status: 'awaiting_approval',
});
getLatestPlanMock.mockReturnValue({
id: 'plan-1',
structured: structuredPlan,
});
startExecutionMock.mockResolvedValue(undefined);
});

it('exits on invalid issue numbers before looking up a thread', async () => {
await expect(approveCommand('abc')).rejects.toThrow('process.exit:1');

expect(errorSpy).toHaveBeenCalledWith('Invalid issue number:', 'abc');
expect(getThreadByIssueMock).not.toHaveBeenCalled();
});

it('exits when no thread exists for the issue', async () => {
getThreadByIssueMock.mockReturnValueOnce(null);

await expect(approveCommand('42')).rejects.toThrow('process.exit:1');

expect(errorSpy).toHaveBeenCalledWith('No thread found for issue #42 in this project.');
});

it('exits when the thread is not awaiting approval', async () => {
getThreadByIssueMock.mockReturnValueOnce({
id: 'thread-1',
title: 'Ship coverage',
status: 'executing',
});

await expect(approveCommand('42')).rejects.toThrow('process.exit:1');

expect(errorSpy).toHaveBeenCalledWith(
'Thread is in "executing" state, not "awaiting_approval". Cannot approve.',
);
});

it('exits when the latest plan is missing or unstructured', async () => {
getLatestPlanMock.mockReturnValueOnce({ id: 'plan-1', structured: null });

await expect(approveCommand('42')).rejects.toThrow('process.exit:1');

expect(errorSpy).toHaveBeenCalledWith('No plan found for this thread.');
});

it('marks the plan approved and resumes execution from the structured plan', async () => {
await approveCommand('42');

expect(createPipelineMock).toHaveBeenCalledWith({ deps: true });
expect(rehydrateContextMock).toHaveBeenCalledWith('thread-1', '/repo', 'Ship coverage');
expect(updatePlanStatusMock).toHaveBeenCalledWith('plan-1', 'approved');
expect(startExecutionMock).toHaveBeenCalledWith('thread-1', structuredPlan);
expect(logSpy).toHaveBeenCalledWith('Plan objective: Increase coverage');
expect(logSpy).toHaveBeenCalledWith('\nExecution started.');
});
});
64 changes: 45 additions & 19 deletions apps/cli/src/commands/logs.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';

const {
createCliContextMock,
requireOnboardingMock,
getThreadForIssueOrExitMock,
listByThreadMock,
} = vi.hoisted(() => ({
createCliContextMock: vi.fn(),
requireOnboardingMock: vi.fn(),
getThreadForIssueOrExitMock: vi.fn(),
listByThreadMock: vi.fn(),
}));
const { createCliContextMock, requireOnboardingMock, getThreadByIssueMock, listByThreadMock } =
vi.hoisted(() => ({
createCliContextMock: vi.fn(),
requireOnboardingMock: vi.fn(),
getThreadByIssueMock: vi.fn(),
listByThreadMock: vi.fn(),
}));

vi.mock('../context', () => ({
createCliContext: createCliContextMock,
Expand All @@ -20,26 +16,31 @@ vi.mock('./guard', () => ({
requireOnboarding: requireOnboardingMock,
}));

vi.mock('./issue-helpers', () => ({
parseIssueNumber: (value: string) => Number(value),
getThreadForIssueOrExit: getThreadForIssueOrExitMock,
}));

import { logsCommand } from './logs';

describe('logsCommand', () => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const writeSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
vi.spyOn(process, 'exit').mockImplementation((code?: string | number | null) => {
throw new Error(`process.exit:${code ?? ''}`);
});

beforeEach(() => {
vi.clearAllMocks();
requireOnboardingMock.mockReturnValue(true);
createCliContextMock.mockReturnValue({
project: {
id: 'project-1',
},
threads: {
getByProjectAndGithubIssue: getThreadByIssueMock,
},
terminalEvents: {
listByThread: listByThreadMock,
},
});
getThreadForIssueOrExitMock.mockReturnValue({
getThreadByIssueMock.mockReturnValue({
id: 'thread-1',
title: 'Add terminal log output',
status: 'running',
Expand All @@ -53,14 +54,29 @@ describe('logsCommand', () => {
await logsCommand('42');

expect(createCliContextMock).not.toHaveBeenCalled();
expect(getThreadForIssueOrExitMock).not.toHaveBeenCalled();
expect(getThreadByIssueMock).not.toHaveBeenCalled();
});

it('exits on invalid issue numbers before looking up a thread', async () => {
await expect(logsCommand('abc')).rejects.toThrow('process.exit:1');

expect(errorSpy).toHaveBeenCalledWith('Invalid issue number:', 'abc');
expect(getThreadByIssueMock).not.toHaveBeenCalled();
});

it('exits when no thread exists for the issue', async () => {
getThreadByIssueMock.mockReturnValueOnce(null);

await expect(logsCommand('42')).rejects.toThrow('process.exit:1');

expect(errorSpy).toHaveBeenCalledWith('No thread found for issue #42 in this project.');
});

it('prints a friendly empty state when a thread has no terminal events', async () => {
await logsCommand('42');

expect(createCliContextMock).toHaveBeenCalledWith(process.cwd());
expect(getThreadForIssueOrExitMock).toHaveBeenCalledWith(expect.any(Object), 42);
expect(getThreadByIssueMock).toHaveBeenCalledWith('project-1', 42);
expect(logSpy).toHaveBeenCalledWith('Logs for issue #42: Add terminal log output');
expect(logSpy).toHaveBeenCalledWith('Status: running\n');
expect(logSpy).toHaveBeenCalledWith('No terminal events recorded.');
Expand Down Expand Up @@ -116,6 +132,14 @@ describe('logsCommand', () => {
createdAt: '2026-05-08T12:00:12.000Z',
event: { kind: 'turn_start' },
},
{
createdAt: '2026-05-08T12:00:13.000Z',
event: { kind: 'turn_end' },
},
{
createdAt: '2026-05-08T12:00:14.000Z',
event: { kind: 'action', summary: 'hidden action' },
},
]);

await logsCommand('42');
Expand All @@ -131,5 +155,7 @@ describe('logsCommand', () => {
expect(logSpy).toHaveBeenCalledWith('[12:00:09] Clarification answered (2 questions)');
expect(logSpy).toHaveBeenCalledWith('[12:00:10] Done — cost: $0.1235');
expect(logSpy).toHaveBeenCalledWith('[12:00:11] Done');
expect(logSpy).not.toHaveBeenCalledWith(expect.stringContaining('hidden action'));
expect(writeSpy).not.toHaveBeenCalledWith(expect.stringContaining('hidden action'));
});
});
6 changes: 6 additions & 0 deletions apps/cli/src/commands/pipeline-commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
getReviewByPlanIdMock,
updatePlanStatusMock,
getLatestCheckpointMock,
getLatestVerificationMock,
} = vi.hoisted(() => ({
createCliContextMock: vi.fn(),
requireOnboardingMock: vi.fn(),
Expand All @@ -36,6 +37,7 @@ const {
getReviewByPlanIdMock: vi.fn(),
updatePlanStatusMock: vi.fn(),
getLatestCheckpointMock: vi.fn(),
getLatestVerificationMock: vi.fn(),
}));

vi.mock('../context', () => ({
Expand Down Expand Up @@ -114,6 +116,9 @@ describe('pipeline CLI commands', () => {
checkpoints: {
getLatest: getLatestCheckpointMock,
},
verifications: {
getLatest: getLatestVerificationMock,
},
});
getThreadForIssueOrExitMock.mockReturnValue({
id: 'thread-1',
Expand Down Expand Up @@ -144,6 +149,7 @@ describe('pipeline CLI commands', () => {
phase: 'executing',
reason: 'verification failed',
});
getLatestVerificationMock.mockReturnValue(null);
});

it('does not create a context when onboarding is incomplete', async () => {
Expand Down
Loading
Loading