|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import type { BrowserCliCommand } from '../registry.js'; |
| 3 | + |
| 4 | +const executeCommandMock = vi.hoisted(() => vi.fn()); |
| 5 | + |
| 6 | +vi.mock('../execution.js', () => ({ |
| 7 | + executeCommand: executeCommandMock, |
| 8 | +})); |
| 9 | + |
| 10 | +import { collectAuthStatus } from './auth.js'; |
| 11 | +import { AuthRequiredError } from '../errors.js'; |
| 12 | +import { cli, getRegistry, Strategy } from '../registry.js'; |
| 13 | + |
| 14 | +function registerWhoami(site: string, opts: { |
| 15 | + quick?: boolean; |
| 16 | + quickLoggedIn?: boolean; |
| 17 | + identity?: Record<string, unknown>; |
| 18 | +} = {}): void { |
| 19 | + cli({ |
| 20 | + site, |
| 21 | + name: 'whoami', |
| 22 | + access: 'read', |
| 23 | + description: `${site} whoami`, |
| 24 | + strategy: Strategy.COOKIE, |
| 25 | + browser: true, |
| 26 | + domain: `${site}.example.com`, |
| 27 | + navigateBefore: false, |
| 28 | + args: [], |
| 29 | + columns: ['logged_in', 'site', 'username'], |
| 30 | + authStatus: opts.quick |
| 31 | + ? { quickCheck: async () => ({ logged_in: opts.quickLoggedIn ?? false }) } |
| 32 | + : undefined, |
| 33 | + func: async () => opts.identity ?? { logged_in: true, site, username: site }, |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +beforeEach(() => { |
| 38 | + getRegistry().clear(); |
| 39 | + executeCommandMock.mockReset(); |
| 40 | + executeCommandMock.mockImplementation(async (cmd: BrowserCliCommand, kwargs: Record<string, unknown>) => { |
| 41 | + if (!cmd.func) return {}; |
| 42 | + return cmd.func({} as never, kwargs); |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +describe('auth status collection', () => { |
| 47 | + it('uses quickCheck by default and does not run full whoami', async () => { |
| 48 | + registerWhoami('alpha', { quick: true, quickLoggedIn: true, identity: { username: 'full-alpha' } }); |
| 49 | + |
| 50 | + const rows = await collectAuthStatus({ sites: 'alpha' }); |
| 51 | + |
| 52 | + expect(rows).toEqual([ |
| 53 | + { site: 'alpha', status: 'logged_in', logged_in: true, identity: '', checked: 'quick', error: '' }, |
| 54 | + ]); |
| 55 | + expect(executeCommandMock).toHaveBeenCalledTimes(1); |
| 56 | + expect(executeCommandMock.mock.calls[0]?.[0]).toMatchObject({ |
| 57 | + site: 'alpha', |
| 58 | + name: 'whoami', |
| 59 | + navigateBefore: false, |
| 60 | + siteSession: 'ephemeral', |
| 61 | + defaultWindowMode: 'background', |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('marks sites without quickCheck as unknown unless --full is used', async () => { |
| 66 | + registerWhoami('beta'); |
| 67 | + |
| 68 | + const rows = await collectAuthStatus({ sites: 'beta' }); |
| 69 | + |
| 70 | + expect(rows).toEqual([ |
| 71 | + { |
| 72 | + site: 'beta', |
| 73 | + status: 'unknown', |
| 74 | + logged_in: '', |
| 75 | + identity: '', |
| 76 | + checked: 'skipped', |
| 77 | + error: 'quickCheck not implemented; use --full to run whoami', |
| 78 | + }, |
| 79 | + ]); |
| 80 | + expect(executeCommandMock).not.toHaveBeenCalled(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('runs full whoami with --full and returns a safe identity summary', async () => { |
| 84 | + registerWhoami('gamma', { |
| 85 | + identity: { |
| 86 | + logged_in: true, |
| 87 | + site: 'gamma', |
| 88 | + email: 'hidden@example.com', |
| 89 | + username: 'public-handle', |
| 90 | + }, |
| 91 | + }); |
| 92 | + |
| 93 | + const rows = await collectAuthStatus({ sites: 'gamma', full: true }); |
| 94 | + |
| 95 | + expect(rows).toEqual([ |
| 96 | + { site: 'gamma', status: 'logged_in', logged_in: true, identity: 'public-handle', checked: 'full', error: '' }, |
| 97 | + ]); |
| 98 | + }); |
| 99 | + |
| 100 | + it('converts AuthRequiredError into not_logged_in rows', async () => { |
| 101 | + registerWhoami('delta', { quick: true }); |
| 102 | + executeCommandMock.mockRejectedValueOnce(new AuthRequiredError('delta.example.com')); |
| 103 | + |
| 104 | + const rows = await collectAuthStatus({ sites: 'delta' }); |
| 105 | + |
| 106 | + expect(rows).toEqual([ |
| 107 | + { site: 'delta', status: 'not_logged_in', logged_in: false, identity: '', checked: 'quick', error: '' }, |
| 108 | + ]); |
| 109 | + }); |
| 110 | +}); |
0 commit comments