fix(ui): привязать flow-аналитику к контексту запроса

This commit is contained in:
Denozordec committed 2026-09-24 01:31:43 +07:00
1 parent 94dd453b33
commit 24313340d5
5 files changed
+163 -82

No files matched your search

+77
View File
@@ -0,0 +1,77 @@
import { test, expect, type Page } from '@playwright/test'
async function setup(page: Page) {
const control = { hold: '', fail: false, failList: false, release: () => {} }
const gate = new Promise<void>(resolve => { control.release = resolve })
const card = { id: '1', name: 'Экспортёр тест', subtitle: '', site: 'TEST', country: 'RU', status: 'online', rxNow: 2, txNow: 3, sessions: 1, rxSeries: [1, 2], txSeries: [2, 3], bytes: 200 }
const data = { bpsNow: 1000000, bytes: 200, packets: 2, conversations: 1, uniqueSrc: 1, uniqueDst: 1, topProto: 'TCP', rxSeries: [1], txSeries: [2], applications: [], protocols: [], sources: [], destinations: [], interfaces: [], conversationsList: [], ifaces: [], live: false }
await page.route('**/health', r => r.fulfill({ json: { ok: true } }))
await page.route('**/api/**', async route => {
const url = new URL(route.request().url()), path = url.pathname, range = url.searchParams.get('range')
if (path === '/api/traffic/flow/analytics') {
if (control.hold === range) await gate
if (control.fail) return route.fulfill({ status: 500, json: { error: 'Срез временно недоступен' } })
return route.fulfill({ json: { ...data, topCategory: 'Категория ' + range } })
}
if (path === '/api/traffic/flow/live') return route.fulfill({ contentType: 'text/event-stream', body: 'event: sample\ndata: ' + JSON.stringify({ ...data, live: true }) + '\n\n' })
if (path === '/api/traffic/flow/exporters' && control.failList) return route.fulfill({ status: 403, json: { error: 'Нет доступа к экспортёрам' } })
const json = path === '/api/auth/config' ? { required: false }
: path === '/api/traffic/flow/exporters' ? { exporters: [card] }
: path === '/api/traffic/flow/clients' ? { clients: [] }
: path === '/api/traffic/flows' ? { exportersOnline: 1, bytesPerMin: 1, uniqueSrc: 1, uniqueDst: 1, topProto: 'TCP', talkers: [], listenerBound: true }
: path === '/api/traffic/servers' ? { servers: [] }
: path.endsWith('/interfaces') || path.endsWith('/bound-interfaces') ? { interfaces: [] }
: path === '/api/traffic/users' ? { users: [] }
: path === '/api/servers' ? [] : {}
return route.fulfill({ json })
})
await page.goto('/traffic')
await page.getByRole('button', { name: 'Потоки', exact: true }).click()
await expect(page.getByLabel('Скорость потоков')).toBeVisible()
return control
}
test('Flow: поздний ответ периода не заменяет текущую аналитику', async ({ page }) => {
const control = await setup(page)
control.hold = '24h'
await page.getByRole('button', { name: '24ч', exact: true }).click()
await expect(page.getByText('Загрузка аналитики…', { exact: true })).toBeVisible()
await expect(page.getByLabel('Скорость потоков')).toHaveCount(0)
await page.getByRole('button', { name: '15м', exact: true }).click()
await expect(page.getByText('Категория 15m', { exact: true }).filter({ visible: true })).toBeVisible()
const response = page.waitForResponse(r => r.url().includes('/flow/analytics?') && r.url().includes('range=24h'))
control.release()
await response
await expect(page.getByText('Категория 15m', { exact: true }).filter({ visible: true })).toBeVisible()
await expect(page.getByText('Категория 24h', { exact: true })).toHaveCount(0)
})
test('Flow: ошибка аналитики и повтор сохраняют период', async ({ page }) => {
const control = await setup(page)
control.fail = true
await page.getByRole('button', { name: '15м', exact: true }).click()
await expect(page.getByRole('alert').filter({ hasText: 'Аналитика недоступна' })).toContainText('Срез временно недоступен')
await expect(page.getByLabel('Скорость потоков')).toHaveCount(0)
control.fail = false
await page.getByRole('button', { name: 'Повторить аналитику', exact: true }).click()
await expect(page.getByText('Категория 15m', { exact: true }).filter({ visible: true })).toBeVisible()
})
test('Flow: ошибка списка не отображается как отсутствие экспортёров', async ({ page }) => {
const control = await setup(page)
control.failList = true
await page.getByRole('button', { name: 'Обновить', exact: true }).click()
await expect(page.getByRole('alert').filter({ hasText: 'Потоки недоступны' })).toContainText('Нет доступа')
await expect(page.getByLabel('Сводка трафика')).toHaveCount(0)
await expect(page.getByText('Нет экспортёров IPFIX.', { exact: false })).toHaveCount(0)
control.failList = false
await page.getByRole('button', { name: 'Повторить загрузку потоков', exact: true }).click()
await expect(page.getByLabel('Скорость потоков')).toBeVisible()
})
test('Flow: завершённый SSE не помечает последние данные как live', async ({ page }) => {
await setup(page)
await page.getByRole('button', { name: '5м', exact: true }).click()
await expect(page.getByText('Последние данные · live недоступен', { exact: true }).filter({ visible: true })).toHaveCount(2)
await expect(page.getByText('live', { exact: true })).toHaveCount(0)
})