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
39 changes: 39 additions & 0 deletions src/commands/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,45 @@ describe('runInit — happy path (interactive)', () => {
expect(agent.skills).toContain('testsprite-verify');
expect(agent.skills).toContain('testsprite-onboard');
});

it('debug mode reports a display-only whoami lookup failure without corrupting JSON stdout', async () => {
const { captured, deps } = makeCapture();
let callCount = 0;
const fetchMock = vi.fn(async () => {
callCount += 1;
if (callCount === 1) {
return new Response(JSON.stringify(ME), {
status: 200,
headers: { 'content-type': 'application/json' },
});
}
return new Response(
JSON.stringify({
error: {
code: 'AUTH_INVALID',
message: 'Invalid API key',
nextAction: 'Provide a valid key.',
requestId: 'r-whoami',
},
}),
{ status: 401, headers: { 'content-type': 'application/json' } },
);
}) as unknown as InitDeps['fetchImpl'];

await runInit(
makeBaseOpts({ apiKey: 'sk-json-test', debug: true, noAgent: true, output: 'json' }),
{
...deps,
fetchImpl: fetchMock,
credentialsPath,
isTTY: false,
},
);

const parsed = JSON.parse(captured.stdout.join('\n')) as Record<string, unknown>;
expect(parsed.status).toBe('initialized');
expect(captured.stderr.some(line => line.includes('setup identity lookup failed'))).toBe(true);
});
});

// ---------------------------------------------------------------------------
Expand Down
6 changes: 5 additions & 1 deletion src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,13 @@ export async function runInit(opts: InitOptions, deps: InitDeps = {}): Promise<v
let me: MeResponse;
try {
me = await runWhoami(opts, whoamiDeps);
} catch {
} catch (err) {
// Whoami is display-only. If it fails after a successful configure,
// continue with a minimal placeholder so the summary still prints.
if (opts.debug) {
const reason = err instanceof Error ? err.message : String(err);
stderrFn(`[debug] setup identity lookup failed after configure: ${reason}`);
}
me = { userId: '', keyId: '', scopes: [], env: 'production' };
}

Expand Down
Loading