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
9 changes: 4 additions & 5 deletions src/provider/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,25 +118,24 @@ function callbacks(interaction: AuthInteraction): OAuthLoginCallbacks {
};
}

function openAuthorizationUrl(url: string, onError: () => void) {
function openAuthorizationUrl(url: string) {
const command: [string, string[]] =
process.platform === 'darwin'
? ['open', [url]]
: process.platform === 'win32'
? ['rundll32', ['url.dll,FileProtocolHandler', url]]
: ['xdg-open', [url]];
const child = spawn(command[0], command[1], { detached: true, stdio: 'ignore' });
child.once('error', onError);
child.once('error', () => {});
child.unref();
}

function terminalInteraction(ctx: ExtensionCommandContext): AuthInteraction {
return {
notify(event) {
if (event.type === 'auth_url') {
openAuthorizationUrl(event.url, () => {
ctx.ui.notify(`Open this URL to continue login: ${event.url}`, 'warning');
});
openAuthorizationUrl(event.url);
ctx.ui.notify(`Open this URL to continue login: ${event.url}`, 'warning');
ctx.ui.notify(event.instructions ?? 'Complete the Grok CLI login in your browser.', 'info');
return;
}
Expand Down
10 changes: 8 additions & 2 deletions tests/provider/accounts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,11 @@ describe('vault account management', () => {
await expect(accounts.activate(ctx, account.id)).rejects.toThrow('environment token');
});

it('reports the authorization URL when the browser opener fails', async () => {
it('reports the authorization URL as a warning in terminal and launches browser opener', async () => {
const child = Object.assign(new EventEmitter(), { unref: vi.fn() });
spawnProcess.mockReturnValue(child);
login.mockImplementationOnce(async (callbacks) => {
callbacks.onAuth({ url: 'https://accounts.x.ai/authorize?state=test' });
child.emit('error', new Error('xdg-open missing'));
throw new Error('stop test login');
});
const commands = new Map<
Expand Down Expand Up @@ -413,10 +412,17 @@ describe('vault account management', () => {

await commands.get('grok-cli-accounts')?.handler('', commandContext);

expect(spawnProcess).toHaveBeenCalled();
expect(child.unref).toHaveBeenCalled();
expect(() => child.emit('error', new Error('spawn failed'))).not.toThrow();
expect(commandContext.ui.notify).toHaveBeenCalledWith(
expect.stringContaining('https://accounts.x.ai/authorize?state=test'),
'warning',
);
expect(commandContext.ui.notify).toHaveBeenCalledWith(
'Complete the Grok CLI login in your browser.',
'info',
);
});

it('does not save quota after the captured account logs out', async () => {
Expand Down
Loading