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
43 changes: 42 additions & 1 deletion src/commands/doctor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import { mkdtempSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { Command } from 'commander';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { CLIError } from '../lib/errors.js';
import { ApiError, CLIError } from '../lib/errors.js';
import { writeProfile } from '../lib/credentials.js';
import type { DoctorDeps, DoctorReport } from './doctor.js';
import { createDoctorCommand, runDoctor } from './doctor.js';
Expand Down Expand Up @@ -56,6 +57,14 @@ function healthyDeps(credentialsPath: string, extra: Partial<DoctorDeps> = {}):
};
}

function makeDoctorProgram(deps: DoctorDeps = {}): Command {
const program = new Command();
program.exitOverride();
program.option('--output <mode>', 'output', 'text');
program.addCommand(createDoctorCommand(deps));
return program;
}

let credentialsPath: string;

beforeEach(() => {
Expand Down Expand Up @@ -226,4 +235,36 @@ describe('createDoctorCommand wiring', () => {
it('--help describes the diagnostic', () => {
expect(createDoctorCommand().helpInformation()).toContain('Diagnose');
});

it('rejects invalid --output with the shared VALIDATION_ERROR', async () => {
const rejection = await makeDoctorProgram()
.parseAsync(['node', 'ts', '--output', 'yaml', 'doctor'])
.catch((error: unknown) => error);
expect(rejection).toBeInstanceOf(ApiError);
expect(rejection).toMatchObject({
code: 'VALIDATION_ERROR',
exitCode: 5,
nextAction: 'Flag `--output` is invalid: must be one of: json, text.',
});
});

it('accepts valid --output modes through command wiring', async () => {
writeProfile('default', { apiKey: 'sk-abc' }, { path: credentialsPath });
for (const mode of ['text', 'json'] as const) {
const { capture, deps } = makeCapture();
await makeDoctorProgram({ ...healthyDeps(credentialsPath), ...deps }).parseAsync([
'node',
'ts',
'--output',
mode,
'doctor',
]);
const raw = capture.stdout.join('');
if (mode === 'json') {
expect((JSON.parse(raw) as DoctorReport).failures).toBe(0);
} else {
expect(raw).toContain('All checks passed.');
}
}
});
});
4 changes: 2 additions & 2 deletions src/commands/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
import { loadConfig } from '../lib/config.js';
import { ApiError, CLIError, localValidationError } from '../lib/errors.js';
import type { FetchImpl } from '../lib/http.js';
import { GLOBAL_OPTS_HINT, Output, type OutputMode } from '../lib/output.js';
import { GLOBAL_OPTS_HINT, Output, resolveOutputMode, type OutputMode } from '../lib/output.js';
import { isVerifySkillInstalled } from '../lib/skill-nudge.js';
import { VERSION } from '../version.js';
import { MIN_SUPPORTED_NODE_MAJOR, shouldRejectNodeVersion } from '../version-guard.js';
Expand Down Expand Up @@ -257,7 +257,7 @@ function resolveCommonOptions(command: Command): CommonOptions {
};
return {
profile: globals.profile ?? 'default',
output: globals.output ?? 'text',
output: resolveOutputMode(globals.output),
endpointUrl: globals.endpointUrl,
debug: globals.debug ?? false,
verbose: globals.verbose ?? false,
Expand Down
Loading