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
47 changes: 47 additions & 0 deletions src/commanderAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,50 @@ describe('commanderAdapter arg passing', () => {
expect(mockExecuteCommand).not.toHaveBeenCalled();
});
});

describe('commanderAdapter boolean alias support', () => {
const cmd: CliCommand = {
site: 'reddit',
name: 'save',
description: 'Save a post',
browser: false,
args: [
{ name: 'post-id', positional: true, required: true, help: 'Post ID' },
{ name: 'undo', type: 'boolean', default: false, help: 'Unsave instead of save' },
],
func: vi.fn(),
};

beforeEach(() => {
mockExecuteCommand.mockReset();
mockExecuteCommand.mockResolvedValue([]);
mockRenderOutput.mockReset();
delete process.env.OPENCLI_VERBOSE;
process.exitCode = undefined;
});

it('coerces default false for boolean args to a real boolean', async () => {
const program = new Command();
const siteCmd = program.command('reddit');
registerCommandToProgram(siteCmd, cmd);

await program.parseAsync(['node', 'opencli', 'reddit', 'save', 't3_abc123']);

expect(mockExecuteCommand).toHaveBeenCalled();
const kwargs = mockExecuteCommand.mock.calls[0][1];
expect(kwargs['post-id']).toBe('t3_abc123');
expect(kwargs.undo).toBe(false);
});

it('coerces explicit false for boolean args to a real boolean', async () => {
const program = new Command();
const siteCmd = program.command('reddit');
registerCommandToProgram(siteCmd, cmd);

await program.parseAsync(['node', 'opencli', 'reddit', 'save', 't3_abc123', '--undo', 'false']);

expect(mockExecuteCommand).toHaveBeenCalled();
const kwargs = mockExecuteCommand.mock.calls[0][1];
expect(kwargs.undo).toBe(false);
});
});
2 changes: 1 addition & 1 deletion src/commanderAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
import { checkDaemonStatus } from './browser/discover.js';

export function normalizeArgValue(argType: string | undefined, value: unknown, name: string): unknown {
if (argType !== 'bool') return value;
if (argType !== 'bool' && argType !== 'boolean') return value;
if (typeof value === 'boolean') return value;
if (value == null || value === '') return false;

Expand Down