| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- import { expect, test, type Page } from '@playwright/test';
- const chat = {
- chat_id: '-1001234567890',
- title: '产品运营测试群',
- username: 'product_ops_test',
- type: 'supergroup',
- accessible: true,
- member_count: 286,
- bot_status: 'administrator',
- bot_privileges: ['can_change_info', 'can_delete_messages', 'can_restrict_members'],
- };
- const recentMember = {
- user: {
- id: '10001',
- username: 'alice',
- first_name: '小明',
- last_name: '同学',
- is_bot: false,
- is_deleted: false,
- },
- status: 'recent',
- privileges: [],
- last_seen_at: '2026-07-24T08:00:00Z',
- };
- const bot = {
- bot_id: 'primary',
- label: 'Test Bot',
- enabled: true,
- bot_token_configured: true,
- sudo_users_id: ['10001'],
- log_group_id: '0',
- gban_log_group_id: '0',
- message_dump_chat: '-1001234567890',
- identity: { id: '999', username: 'test_bot', name: 'Test Bot' },
- ready_to_connect: true,
- runtime: { state: 'running' },
- };
- function envelope(data: unknown) {
- return JSON.stringify({ data });
- }
- async function selectChatWorkspace(page: Page, name: string) {
- if ((page.viewportSize()?.width || 0) <= 575) {
- await page.evaluate(() => window.scrollTo(0, 0));
- await page.locator('.workspace-tab-select').click();
- await page.getByText(name, { exact: true }).last().click();
- return;
- }
- await page.getByRole('tab', { name }).click();
- }
- test.beforeEach(async ({ page }) => {
- await page.route('**/api/admin/v1/**', async (route) => {
- const url = new URL(route.request().url());
- const path = url.pathname;
- let data: unknown = {};
- if (path.endsWith('/auth/login') || path.endsWith('/auth/me')) {
- data = { username: 'admin', must_change_password: false, csrf_token: 'csrf-test' };
- } else if (path.endsWith('/bots')) {
- data = { items: [bot] };
- } else if (path.endsWith('/settings/telegram')) {
- data = {
- api_id: 12345,
- api_hash_configured: true,
- api_ready: true,
- bots: [bot],
- };
- } else if (path.endsWith('/dashboard')) {
- data = {
- counts: { chats: 3, giveaways: 8, point_accounts: 126, point_transactions: 804, audit_events: 19 },
- recent_giveaways: [
- {
- giveaway_id: 'a1b2c3',
- chat_id: chat.chat_id,
- title: '七月活跃抽奖',
- status: 'running',
- prizes: [{ name: '一等奖', count: 1, points_reward: 50 }],
- minimum_points: 20,
- entry_cost: 5,
- participation_reward: 2,
- ends_at: '2026-07-25T08:00:00Z',
- created_at: '2026-07-24T08:00:00Z',
- },
- ],
- top_accounts: [{ chat_id: chat.chat_id, user_id: '10001', username: 'alice', display_name: '爱丽丝 示例', balance: 180, lifetime_earned: 220, lifetime_spent: 40 }],
- recent_transactions: [{ transaction_id: 'tx1', chat_id: chat.chat_id, user_id: '10001', username: 'alice', display_name: '爱丽丝 示例', delta: 10, balance_after: 180, source: 'checkin', created_at: '2026-07-24T08:00:00Z' }],
- };
- } else if (path.endsWith('/chats')) {
- data = { items: [chat], total: 1, page: 1, page_size: 20 };
- } else if (path === `/api/admin/v1/chats/${chat.chat_id}`) {
- data = {
- ...chat,
- description: '用于后台验收的测试群',
- permissions: {},
- automation: {
- chat_id: chat.chat_id,
- auto_replies: [],
- blacklist_words: ['广告'],
- risk_rules: [
- {
- rule_id: 'ads',
- name: '广告拦截',
- enabled: true,
- keywords: ['广告'],
- match_images: true,
- match_links: true,
- actions: ['delete', 'warn'],
- duration_seconds: 3600,
- },
- ],
- identity_monitor: { enabled: true, notify_in_chat: false },
- welcome: { enabled: false, text: '', media: null },
- captcha_enabled: false,
- chatbot_enabled: false,
- antiflood_enabled: true,
- },
- };
- } else if (path.endsWith('/admins')) {
- data = { items: [] };
- } else if (path.endsWith('/members/recent')) {
- data = { items: [recentMember] };
- } else if (path.endsWith('/members/identity-changes')) {
- data = {
- items: [{
- change_id: 'change-1',
- chat_id: chat.chat_id,
- user_id: '10001',
- before: { username: 'alice', display_name: '小明 同学' },
- after: { username: 'alice_new', display_name: '小明 新昵称' },
- changed_fields: ['username', 'display_name'],
- observed_at: '2026-07-24T08:30:00Z',
- }],
- total: 1,
- page: 1,
- page_size: 20,
- };
- } else if (path.endsWith('/members/search')) {
- data = { items: [{ ...recentMember, status: 'member' }] };
- } else if (path.endsWith('/giveaways')) {
- data = { items: [], total: 0, page: 1, page_size: 20 };
- } else if (path.endsWith('/points/accounts') || path.endsWith('/points/transactions')) {
- data = { items: [], total: 0, page: 1, page_size: 20 };
- } else if (path.endsWith('/audit-logs')) {
- data = { items: [], total: 0, page: 1, page_size: 20 };
- } else if (path.endsWith('/settings')) {
- data = {
- web_address: 'http://127.0.0.1:8088/admin',
- bind_host: '0.0.0.0',
- upload_limit_mb: 20,
- supervisor_mode: true,
- bot: { connected: false, id: '', username: '', name: '' },
- };
- }
- await route.fulfill({ status: 200, contentType: 'application/json', body: envelope(data) });
- });
- });
- test('登录与主要管理视图在不同视口无页面级横向滚动', async ({ page }, testInfo) => {
- await page.goto('/admin/login');
- await page.getByLabel('密码').fill('qwe0.123456');
- await page.getByRole('button', { name: /登\s*录/ }).click();
- await expect(page.getByRole('heading', { name: '概览' })).toBeVisible();
- const overflow = await page.evaluate(() => document.documentElement.scrollWidth - window.innerWidth);
- expect(overflow).toBeLessThanOrEqual(1);
- await page.screenshot({ path: testInfo.outputPath('dashboard.png'), fullPage: true });
- await page.goto('/admin/chats');
- await expect(page.getByText('产品运营测试群')).toBeVisible();
- expect(await page.evaluate(() => document.documentElement.scrollWidth - window.innerWidth)).toBeLessThanOrEqual(1);
- await page.goto(`/admin/chats/${chat.chat_id}`);
- await selectChatWorkspace(page, '自动化与风控');
- await expect(page.getByRole('heading', { name: '内容风控' })).toBeVisible();
- await expect(page.getByText('广告拦截')).toBeVisible();
- await page.getByRole('button', { name: '编辑 广告拦截' }).click();
- await expect(page.getByText('编辑风控规则')).toBeVisible();
- await expect(page.getByLabel('封禁时长')).toHaveValue('60');
- expect(await page.evaluate(() => document.documentElement.scrollWidth - window.innerWidth)).toBeLessThanOrEqual(1);
- await page.getByRole('button', { name: '关闭' }).click();
- await selectChatWorkspace(page, '成员与管理员');
- await expect(page.getByText('小明 同学', { exact: true }).first()).toBeVisible();
- await expect(page.getByText('小明 新昵称', { exact: true }).first()).toBeVisible();
- expect(await page.evaluate(() => document.documentElement.scrollWidth - window.innerWidth)).toBeLessThanOrEqual(1);
- await page.goto('/admin/giveaways');
- await expect(page.getByRole('heading', { name: '抽奖管理' })).toBeVisible();
- expect(await page.evaluate(() => document.documentElement.scrollWidth - window.innerWidth)).toBeLessThanOrEqual(1);
- for (const [path, heading] of [
- ['/admin/points', '积分管理'],
- ['/admin/audit', '操作日志'],
- ['/admin/settings', '系统设置'],
- ] as const) {
- await page.goto(path);
- await expect(page.getByRole('heading', { name: heading })).toBeVisible();
- expect(await page.evaluate(() => document.documentElement.scrollWidth - window.innerWidth)).toBeLessThanOrEqual(1);
- }
- });
|