Settings.test.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { render, screen } from '@testing-library/react';
  2. import { beforeEach, expect, test, vi } from 'vitest';
  3. import SettingsPage from '@/pages/Settings';
  4. import { apiRequest } from '@/services/api';
  5. vi.mock('@/services/api', () => ({
  6. apiRequest: vi.fn(),
  7. jsonOptions: vi.fn((method, body) => ({ method, body: JSON.stringify(body) })),
  8. setCsrfToken: vi.fn(),
  9. setSelectedBotId: vi.fn(),
  10. }));
  11. beforeEach(() => {
  12. vi.mocked(apiRequest).mockImplementation(async (path) => {
  13. if (path === '/settings') {
  14. return {
  15. web_address: 'http://127.0.0.1:8088/admin',
  16. bind_host: '127.0.0.1',
  17. upload_limit_mb: 20,
  18. supervisor_mode: true,
  19. bot: { connected: false, id: '', username: '', name: '' },
  20. };
  21. }
  22. if (path === '/settings/telegram') {
  23. return {
  24. api_id: 12345,
  25. api_hash_configured: true,
  26. api_ready: true,
  27. bots: [
  28. {
  29. bot_id: 'one',
  30. label: '主 Bot',
  31. enabled: true,
  32. bot_token_configured: true,
  33. sudo_users_id: [],
  34. log_group_id: '0',
  35. gban_log_group_id: '0',
  36. message_dump_chat: '-100123',
  37. identity: { id: '1', username: 'primary_bot', name: 'Primary' },
  38. ready_to_connect: true,
  39. runtime: { state: 'running' },
  40. },
  41. {
  42. bot_id: 'two',
  43. label: '备用 Bot',
  44. enabled: false,
  45. bot_token_configured: true,
  46. sudo_users_id: [],
  47. log_group_id: '0',
  48. gban_log_group_id: '0',
  49. message_dump_chat: '0',
  50. identity: null,
  51. ready_to_connect: true,
  52. runtime: { state: 'stopped' },
  53. },
  54. ],
  55. };
  56. }
  57. throw new Error(`Unexpected request: ${path}`);
  58. });
  59. });
  60. test('展示多个机器人、脱敏凭据状态和运行状态', async () => {
  61. render(<SettingsPage />);
  62. expect(await screen.findByText('@primary_bot')).toBeInTheDocument();
  63. expect(screen.getByText('备用 Bot')).toBeInTheDocument();
  64. expect(screen.getByText('运行中')).toBeInTheDocument();
  65. expect(screen.getByText('已停用')).toBeInTheDocument();
  66. expect(screen.getAllByText('已配置')).toHaveLength(2);
  67. const credentialsLink = screen.getByRole('link', { name: 'my.telegram.org/apps' });
  68. expect(credentialsLink).toHaveAttribute('href', 'https://my.telegram.org/apps');
  69. expect(credentialsLink.parentElement).toHaveTextContent('不是机器人编号。');
  70. expect(screen.getByText('32 位十六进制字符串,不是机器人令牌。')).toBeInTheDocument();
  71. expect(screen.queryByText(/AA[A-Za-z0-9_-]{20}/)).not.toBeInTheDocument();
  72. });