File size: 4,531 Bytes
af1bee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea59f21
af1bee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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(<TelegramLinkCard />, { 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(<TelegramLinkCard />, { 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(<TelegramLinkCard />, { 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(<TelegramLinkCard />, { 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(<BotStatusLine />, { 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.');
  });
});