import { fireEvent, screen, waitFor } from '@testing-library/react'; import { afterEach, describe, expect, it, vi } from 'vitest'; import { jsonResponse, renderUi } from '../test/utils'; import { BotStatusLine } from './TelegramCard'; import { TelegramLinkCard } from './TelegramLinkCard'; const urlOf = (input: RequestInfo | URL) => (typeof input === 'string' ? input : input instanceof URL ? input.href : input.url); /** A fake backend: `/api/channels/me` reports linked after `linkAfter` reads. */ function backend({ linkedFromStart = false, linkAfter = Infinity, botUsername = 'PatchwiseBot' as string | null } = {}) { const calls: { method: string; url: string }[] = []; let meReads = 0; let unlinked = false; vi.spyOn(globalThis, 'fetch').mockImplementation(async (input, init) => { const u = urlOf(input); const method = (init?.method ?? 'GET').toUpperCase(); calls.push({ method, url: u }); if (u.endsWith('/api/channels/me')) { meReads += 1; const linked = !unlinked && (linkedFromStart || meReads > linkAfter); return jsonResponse({ telegram: { linked, username: linked ? 'saiuttam' : null, linked_at: linked ? '2026-11-05T10:00:00Z' : null } }); } if (u.endsWith('/api/channels/telegram/link-code')) { return jsonResponse({ code: 'K7Q2-9F', expires_at: new Date(Date.now() + 10 * 60_000).toISOString(), bot_username: botUsername }); } if (u.endsWith('/api/channels/telegram/link') && method === 'DELETE') { unlinked = true; return new Response(null, { status: 204 }); } if (u.endsWith('/api/channels/telegram/status')) { return jsonResponse({ token_set: true, bot_username: 'PatchwiseBot', polling: true, last_update_at: '2026-11-05T10:00:00Z', linked_users: 3 }); } return jsonResponse({}); }); return calls; } describe('Astra in Telegram (Account page)', () => { afterEach(() => { vi.restoreAllMocks(); vi.useRealTimers(); }); it('not linked: shows the code with the bot link, then the linked state after the poll', async () => { backend({ linkAfter: 2 }); renderUi(, { auth: true }); fireEvent.click(await screen.findByTestId('tg-link')); expect(await screen.findByTestId('tg-code')).toHaveTextContent('K7Q2-9F'); expect(screen.getByRole('link', { name: 't.me/PatchwiseBot' })).toHaveAttribute('href', 'https://t.me/PatchwiseBot'); expect(screen.getByRole('group', { name: 'Your one-time link code' })).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'Copy the code' })).toBeInTheDocument(); await waitFor(() => expect(screen.getByTestId('tg-linked')).toHaveTextContent('Linked as @saiuttam'), { timeout: 8000 }); expect(screen.queryByTestId('tg-code')).not.toBeInTheDocument(); }, 12000); it('says when Telegram is not set up', async () => { backend({ botUsername: null }); renderUi(, { auth: true }); fireEvent.click(await screen.findByTestId('tg-link')); expect(await screen.findByTestId('tg-not-setup')).toHaveTextContent("Telegram isn't set up yet"); }); it('linked: unlink asks first, then calls the API and goes back to the link button', async () => { const calls = backend({ linkedFromStart: true }); vi.spyOn(window, 'confirm').mockReturnValue(true); renderUi(, { auth: true }); fireEvent.click(await screen.findByTestId('tg-unlink')); await waitFor(() => expect(calls.some((c) => c.method === 'DELETE' && c.url.endsWith('/api/channels/telegram/link'))).toBe(true)); expect(window.confirm).toHaveBeenCalled(); expect(await screen.findByTestId('tg-link')).toBeInTheDocument(); }); it('lists what you can say to the bot', async () => { backend(); renderUi(, { auth: true }); expect(await screen.findByText('/run Procure-to-Pay quick')).toBeInTheDocument(); expect(screen.getByText('/unlink')).toBeInTheDocument(); }); }); describe('the bot status line (Settings > Notifications)', () => { afterEach(() => vi.restoreAllMocks()); it('shows the bot, whether it listens, and how many people linked', async () => { backend(); renderUi(, { auth: true }); const line = await screen.findByTestId('tg-bot-status'); expect(line).toHaveTextContent('@PatchwiseBot'); expect(line).toHaveTextContent('Listening for messages'); expect(line).toHaveTextContent('3 people linked.'); }); });