Skip to content

Commit 36e5b14

Browse files
authored
fix(init): debug log whoami fallback (#223)
Co-authored-by: Lexiie <28455136+Lexiie@users.noreply.github.com>
1 parent 3c3a2e0 commit 36e5b14

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

src/commands/init.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,45 @@ describe('runInit — happy path (interactive)', () => {
237237
expect(agent.skills).toContain('testsprite-verify');
238238
expect(agent.skills).toContain('testsprite-onboard');
239239
});
240+
241+
it('debug mode reports a display-only whoami lookup failure without corrupting JSON stdout', async () => {
242+
const { captured, deps } = makeCapture();
243+
let callCount = 0;
244+
const fetchMock = vi.fn(async () => {
245+
callCount += 1;
246+
if (callCount === 1) {
247+
return new Response(JSON.stringify(ME), {
248+
status: 200,
249+
headers: { 'content-type': 'application/json' },
250+
});
251+
}
252+
return new Response(
253+
JSON.stringify({
254+
error: {
255+
code: 'AUTH_INVALID',
256+
message: 'Invalid API key',
257+
nextAction: 'Provide a valid key.',
258+
requestId: 'r-whoami',
259+
},
260+
}),
261+
{ status: 401, headers: { 'content-type': 'application/json' } },
262+
);
263+
}) as unknown as InitDeps['fetchImpl'];
264+
265+
await runInit(
266+
makeBaseOpts({ apiKey: 'sk-json-test', debug: true, noAgent: true, output: 'json' }),
267+
{
268+
...deps,
269+
fetchImpl: fetchMock,
270+
credentialsPath,
271+
isTTY: false,
272+
},
273+
);
274+
275+
const parsed = JSON.parse(captured.stdout.join('\n')) as Record<string, unknown>;
276+
expect(parsed.status).toBe('initialized');
277+
expect(captured.stderr.some(line => line.includes('setup identity lookup failed'))).toBe(true);
278+
});
240279
});
241280

242281
// ---------------------------------------------------------------------------

src/commands/init.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,13 @@ export async function runInit(opts: InitOptions, deps: InitDeps = {}): Promise<v
291291
let me: MeResponse;
292292
try {
293293
me = await runWhoami(opts, whoamiDeps);
294-
} catch {
294+
} catch (err) {
295295
// Whoami is display-only. If it fails after a successful configure,
296296
// continue with a minimal placeholder so the summary still prints.
297+
if (opts.debug) {
298+
const reason = err instanceof Error ? err.message : String(err);
299+
stderrFn(`[debug] setup identity lookup failed after configure: ${reason}`);
300+
}
297301
me = { userId: '', keyId: '', scopes: [], env: 'production' };
298302
}
299303

0 commit comments

Comments
 (0)