import { afterEach, describe, expect, it, vi } from 'vitest'; import { GhClient } from '../src/gh-client.js'; import { executeGithubBind, executeGithubInstall, executeGithubWhoami } from '../agents/github/skills/github-account/handlers.js'; import installTool from '../agents/github/skills/github-account/tools/install/index.js'; afterEach(() => vi.restoreAllMocks()); describe('GitHub account localization', () => { it('uses the trusted tool language without translating installation metadata', async () => { const client = { appSlug: 'team-app', host: 'github.example.test', installations: [{ account: { login: '中文团队', type: 'Organization' } }] }; const result = await installTool.execute({}, { $client: client, locale: 'en' } as never); expect(result).toContain('Install the GitHub App'); expect(result).toContain('https://github.example.test/apps/team-app/installations/new'); expect(result).toContain('中文团队'); expect(await executeGithubInstall({} as never, 'zh-CN')).toBe('❌ Endpoint 未配置 GitHub App'); }); it('captures the binding language before the asynchronous authorization result', async () => { let finish!: (result: { status: 'authorized'; access_token: string; token_type: string; scope: string }) => void; vi.spyOn(GhClient, 'deviceFlowRequestCode').mockResolvedValue({ device_code: 'synthetic-device', user_code: 'TEST-CODE', verification_uri: 'https://github.example.test/device', interval: 5, expires_in: 600 }); vi.spyOn(GhClient, 'deviceFlowPollToken').mockImplementation(() => new Promise(resolve => { finish = resolve; })); vi.spyOn(GhClient.prototype, 'verifyAuth').mockResolvedValue({ ok: true, user: 'fixture-user', message: '' }); vi.spyOn(console, 'debug').mockImplementation(() => undefined); const insert = vi.fn(async () => undefined); const model = { select: () => ({ where: async () => [] }), insert }; const client = { clientId: 'synthetic-client', host: 'github.example.test', database: { models: new Map([['github_oauth_users', model]]) } }; const message = { clientAdapter: 'github', sender: { id: 'sender-1' }, locale: 'en' as 'en' | 'zh-CN', $reply: vi.fn(async () => undefined) }; const prompt = await executeGithubBind({}, client as never, message as never, 'en'); expect(prompt).toContain('Open this link in a browser'); expect(prompt).toContain('TEST-CODE'); message.locale = 'zh-CN'; finish({ status: 'authorized', access_token: 'synthetic-access-token', token_type: 'bearer', scope: '' }); await vi.waitFor(() => expect(message.$reply).toHaveBeenCalledWith('✅ GitHub account linked!\n👤 fixture-user')); expect(insert).toHaveBeenCalledTimes(1); }); it('shows the binding time in the repository date format (#1250)', async () => { const previous = process.env.TZ; process.env.TZ = 'Asia/Shanghai'; try { vi.spyOn(GhClient.prototype, 'verifyAuth').mockResolvedValue({ ok: true, user: 'fixture-user', message: '' }); const row = { github_login: 'fixture-user', access_token: 'synthetic', created_at: '2026-01-05T17:00:00.000Z' }; const client = { host: 'github.example.test', database: { models: new Map([['github_oauth_users', { select: () => ({ where: async () => [row] }) }]]) } }; const message = { clientAdapter: 'github', sender: { id: 'u1' }, locale: 'en' }; expect(await executeGithubWhoami({}, client as never, message as never, 'en')).toContain('2026-01-06 01:00:00'); } finally { if (previous === undefined) delete process.env.TZ; else process.env.TZ = previous; } }); it('localizes account guidance without inventing a command invocation', async () => { const client = { database: { models: new Map([['github_oauth_users', { select: () => ({ where: async () => [] }) }]]) } }; const message = { clientAdapter: 'github', sender: { id: 'u1' }, locale: 'en' }; expect(await executeGithubWhoami({}, client as never, message as never, 'en')).toBe('📭 No GitHub account is linked.\n🔗 Start account linking first.'); }); });