Skip to content
Open
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
56 changes: 56 additions & 0 deletions src/cli/commands/status/__tests__/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,3 +805,59 @@ describe('handleProjectStatus — invocation URL enrichment', () => {
expect(agentEntry!.invocationUrl).toBeUndefined();
});
});

describe('registerStatus --type / --state validation exit code', () => {
const renderMock = vi.fn();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the fact that we have to mock multiple modules to verify the validation logic feels like a code smell. I understand this fixes the bug, but I'm wondering if its worth refactoring the implementation to not only fix the bug, but also restructure it to make regressions more difficult to introduce.

Ex. extract the validation logic into a pure function and test that directly.

vi.doMock('ink', () => ({
Box: ({ children }: { children?: unknown }) => children,
Text: ({ children }: { children?: unknown }) => children,
render: (...args: unknown[]) => renderMock(...args),
}));
vi.doMock('../../../tui/guards', () => ({
requireProject: vi.fn(),
}));

beforeEach(() => {
renderMock.mockReset();
});

it('exits with non-zero code when --type is invalid', async () => {
const { Command } = await import('@commander-js/extra-typings');
const { registerStatus } = await import('../command.js');

const program = new Command();
program.exitOverride();
registerStatus(program as unknown as Parameters<typeof registerStatus>[0]);

const exitSpy = vi.spyOn(process, 'exit').mockImplementation(((code?: number) => {
throw new Error(`__exit:${code ?? 0}`);
}) as never);

try {
await expect(program.parseAsync(['node', 'agentcore', 'status', '--type', 'bogus'])).rejects.toThrow(/__exit:1/);
expect(exitSpy).toHaveBeenCalledWith(1);
} finally {
exitSpy.mockRestore();
}
});

it('exits with non-zero code when --state is invalid', async () => {
const { Command } = await import('@commander-js/extra-typings');
const { registerStatus } = await import('../command.js');

const program = new Command();
program.exitOverride();
registerStatus(program as unknown as Parameters<typeof registerStatus>[0]);

const exitSpy = vi.spyOn(process, 'exit').mockImplementation(((code?: number) => {
throw new Error(`__exit:${code ?? 0}`);
}) as never);

try {
await expect(program.parseAsync(['node', 'agentcore', 'status', '--state', 'bogus'])).rejects.toThrow(/__exit:1/);
expect(exitSpy).toHaveBeenCalledWith(1);
} finally {
exitSpy.mockRestore();
}
});
});
4 changes: 2 additions & 2 deletions src/cli/commands/status/command.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
Invalid resource type &apos;{cliOptions.type}&apos;. Valid types: {VALID_RESOURCE_TYPES.join(', ')}
</Text>
);
return;
process.exit(1);
}

// Validate --state
Expand All @@ -86,7 +86,7 @@
Invalid state &apos;{cliOptions.state}&apos;. Valid states: {VALID_STATES.join(', ')}
</Text>
);
return;
process.exit(1);
}

try {
Expand Down Expand Up @@ -299,7 +299,7 @@
});
};

function ResourceEntry({ entry, showRuntime }: { entry: ResourceStatusEntry; showRuntime?: boolean }) {

Check warning on line 302 in src/cli/commands/status/command.tsx

View workflow job for this annotation

GitHub Actions / lint

Fast refresh only works when a file only exports components. Move your component(s) to a separate file. If all exports are HOCs, add them to the `extraHOCs` option
return (
<Text>
{' '}
Expand Down
Loading