import { expect, label, screenshot, test, visit, VULNERABILITY } from './fixtures.mjs'; const listPath = '/api/vulnerabilities'; const hostKey = 'host:api.example.test'; const taskKey = 'task:example-task'; const finding = index => index === 20 ? VULNERABILITY : { ...VULNERABILITY, id: `fixture-record-${index}`, title: `示例漏洞 ${index + 1}` }; function mockGroupedRecords(mockApi, extraGroups = {}) { const host = { key: hostKey, label: 'api.example.test', count: 21, severities: { critical: 21 }, pending: 21 }; const domain = { ...host, key: 'domain:example.test', label: 'example.test', children: [host] }; const task = { ...host, key: taskKey, label: 'example-task', count: 1, severities: { critical: 1 }, pending: 1 }; const queue = { ...task, key: 'queue:example-queue', label: '示例任务队列', children: [task] }; mockApi.respond('GET', listPath, (_, url) => { const params = url.searchParams; const mode = params.get('group_by'); const selection = params.get('selection') || ''; const limit = Number(params.get('limit')); const search = params.get(mode === 'host' ? 'host_q' : 'task_q'); const selectedHost = selection === hostKey; const total = mode === 'host' ? (selectedHost ? 21 : 24) : 1; const extra = extraGroups[mode] || []; const page = Number(params.get('page')); const start = (page - 1) * limit; let navigation; if (mode === 'host') navigation = [domain, ...extra]; if (mode === 'task') navigation = search === '不存在' ? [] : [queue, ...extra]; const extraCount = selection ? 0 : extra.reduce((sum, group) => sum + group.count, 0); const count = navigation?.length === 0 ? 0 : total + extraCount; return { json: { navigation, selection: count ? selection : '', page, total: count, total_pages: Math.max(1, Math.ceil(count / limit)), vulnerabilities: Array.from({ length: Math.min(limit, Math.max(0, count - start)) }, (_, i) => finding(start + i)) } }; }); } test('domain grouping retains severity, pages records, and opens details with the group selected', async ({ page, mockApi }, testInfo) => { mockGroupedRecords(mockApi); const main = await visit(page, 'vulnerabilities?severity=critical'); await main.getByText('按域名', { exact: true }).click(); const tree = main.getByRole('complementary', { name: '按域名' }).getByRole('tree'); await tree.getByText('api.example.test', { exact: true }).click(); await expect(main.locator('.vulnerability-group-overview')).toContainText('待处理 21'); await expect(main.getByRole('button', { name: '删除筛选结果' })).toHaveCount(0); await screenshot(page, testInfo, 'vulnerability-domain-view'); await main.getByRole('button', { name: 'Page 2', exact: true }).click(); await expect(main.getByText('显示 21–21 / 共 21 条漏洞', { exact: true })).toBeVisible(); await expect.poll(() => mockApi.calls('GET', listPath).some(call => { const { group_by, pagination, selection, severity, page } = Object.fromEntries(new URLSearchParams(call.query)); return group_by === 'host' && pagination === 'records' && selection === hostKey && severity === 'critical' && page === '2'; })).toBe(true); await main.getByText(VULNERABILITY.title, { exact: true }).click(); const drawer = page.getByRole('dialog'); await expect(drawer).toContainText(VULNERABILITY.description); await drawer.getByRole('button', { name: label('common.close', '关闭'), exact: true }).click(); await expect(tree.getByRole('treeitem', { name: /api\.example\.test/ })).toHaveAttribute('aria-selected', 'true'); await expect(main.getByText(VULNERABILITY.title, { exact: true })).toBeVisible(); await page.reload(); await expect(tree.getByRole('treeitem', { name: /api\.example\.test/ })).toHaveAttribute('aria-selected', 'true'); await expect(main.getByText(VULNERABILITY.title, { exact: true })).toBeVisible(); expect(mockApi.writes()).toEqual([]); }); for (const { mode, heading, parentKey, parentLabel, childLabel, firstLabel, total } of [ { mode: 'host', heading: '按域名', parentKey: 'domain:overflow.test', parentLabel: 'overflow.test', childLabel: i => `api-${i}.overflow.test`, firstLabel: 'api.example.test', total: 104 }, { mode: 'task', heading: '按任务', parentKey: 'queue:overflow', parentLabel: '更多任务', childLabel: i => `queue-task-1234567890-example-${i}`, firstLabel: '任务 example-task', total: 81 } ]) { test(`expanded ${mode} groups scroll within the height of the results`, async ({ page, mockApi }, testInfo) => { const children = Array.from({ length: 80 }, (_, i) => ({ key: `${mode}:${childLabel(i)}`, label: childLabel(i), count: 1 })); mockGroupedRecords(mockApi, { [mode]: [{ key: parentKey, label: parentLabel, count: children.length, children }] }); const main = await visit(page, `vulnerabilities?group_by=${mode}`); const sidebar = main.getByRole('complementary', { name: heading }); const tree = sidebar.getByRole('tree'); const scroll = sidebar.locator('.vulnerability-group-scroll'); const lastLabel = children.at(-1).label; const lastNode = tree.getByText(mode === 'task' ? `任务 ${lastLabel}` : lastLabel, { exact: true }); const heightDifference = () => main.locator('.vulnerability-group-layout').evaluate(layout => { const [sidebar, results] = layout.children; return Math.abs(sidebar.getBoundingClientRect().height - results.getBoundingClientRect().height); }); await expect(main.getByText(`显示 1–20 / 共 ${total} 条漏洞`, { exact: true })).toBeVisible(); await expect(tree.getByText(firstLabel, { exact: true })).toBeInViewport(); await expect(tree.getByRole('button', { name: /the tree item/ })).toHaveCount(0); await expect.poll(heightDifference).toBeLessThan(1); await expect(lastNode).not.toBeInViewport(); await lastNode.scrollIntoViewIfNeeded(); await expect(lastNode).toBeInViewport(); const rootLabel = tree.getByText(mode === 'task' ? '全部任务' : '全部域名', { exact: true }); const parent = tree.getByText(parentLabel, { exact: true }); const [rootBox, parentBox, childBox] = await Promise.all([ rootLabel.boundingBox(), parent.boundingBox(), lastNode.boundingBox() ]); expect(parentBox.x).toBe(rootBox.x); expect(childBox.x).toBeGreaterThan(parentBox.x); const parentCount = parent.locator('..').locator('.semi-tag'); const childCount = lastNode.locator('..').locator('.semi-tag'); const [parentCountBox, childCountBox] = await Promise.all([parentCount.boundingBox(), childCount.boundingBox()]); expect(parentCountBox.x + parentCountBox.width).toBe(childCountBox.x + childCountBox.width); if (mode === 'task') { await expect(lastNode).toHaveAttribute('title', `任务 ${lastLabel}`); expect(await lastNode.evaluate(node => node.scrollWidth > node.clientWidth)).toBe(true); } expect(await scroll.evaluate(node => node.scrollTop)).toBeGreaterThan(0); await screenshot(page, testInfo, `vulnerability-${mode}-scroll`); const lastPage = Math.ceil(total / 20); await main.getByRole('button', { name: `Page ${lastPage}`, exact: true }).click(); await expect(main.getByText(`显示 ${(lastPage - 1) * 20 + 1}–${total} / 共 ${total} 条漏洞`, { exact: true })).toBeVisible(); await expect.poll(heightDifference).toBeLessThan(1); await expect(lastNode).toHaveCount(1); }); } test('task grouping searches and resets server filters, and the mobile view fits', async ({ page, mockApi }, testInfo) => { mockGroupedRecords(mockApi); await page.setViewportSize({ width: 390, height: 844 }); const main = await visit(page, 'vulnerabilities?group_by=task'); const tree = main.getByRole('complementary', { name: '按任务' }).getByRole('tree'); await tree.getByText('任务 example-task', { exact: true }).click(); await expect(main.locator('.vulnerability-group-overview')).toContainText('任务 example-task'); await screenshot(page, testInfo, 'vulnerability-task-mobile'); expect(await main.evaluate(node => node.scrollWidth)).toBeLessThanOrEqual(391); await main.getByRole('textbox', { name: '搜索任务、队列或标签…' }).fill('不存在'); await expect(main.getByText('暂无匹配的分组', { exact: true })).toBeVisible(); expect(Object.fromEntries(new URLSearchParams(mockApi.calls('GET', listPath).at(-1).query))).toMatchObject({ group_by: 'task', task_q: '不存在', page: '1' }); await main.getByRole('button', { name: '重置全部' }).click(); await expect(tree.getByText('任务 example-task', { exact: true })).toBeVisible(); await main.getByText('列表', { exact: true }).click(); await expect(tree).toHaveCount(0); await expect(main.getByText('示例漏洞 1', { exact: true })).toBeVisible(); expect(new URLSearchParams(mockApi.calls('GET', listPath).at(-1).query).has('group_by')).toBe(false); expect(mockApi.writes()).toEqual([]); });