import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import test from 'node:test'; const root = path.resolve(import.meta.dirname, '../..'); const overview = fs.readFileSync(path.join(root, 'src/web/components/ClientOverviewPage.jsx'), 'utf8'); const instructions = fs.readFileSync(path.join(root, 'src/web/instructions.js'), 'utf8'); const prometheus = fs.readFileSync(path.join(root, 'src/web/prometheus.js'), 'utf8'); const server = fs.readFileSync(path.join(root, 'src/server/index.js'), 'utf8'); const styles = fs.readFileSync(path.join(root, 'src/web/styles.css'), 'utf8'); const dashboard = JSON.parse(fs.readFileSync(path.join(root, 'monitoring/grafana/harbor-gateway.json'), 'utf8')); test('Gateway info drawer contains copyable Prometheus and Grafana instructions', () => { assert.match(instructions, /\.\.\.\(isGateway \? \[\{/); assert.match(instructions, /id: 'prometheus'/); assert.match(instructions, /title: 'Prometheus и Grafana'/); assert.match(instructions, /prometheusScrapeConfig\(controlHost\)/); assert.match(instructions, /label: 'prometheus\.yml'/); assert.match(instructions, /label: 'Grafana dashboard'/); assert.match(prometheus, /monitoring\/grafana\/harbor-gateway\.json/); assert.match(prometheus, /scrape_interval: 30s[\s\S]*metrics_path: \/metrics/); assert.match(overview, /const controlHost = window\.location\.host/); assert.match(overview, /client-copy-label">Скопировать/); assert.doesNotMatch(overview, /prometheus-toggle|Prometheus<\/span><\/button>/); assert.match(styles, /\.client-instruction-copy-button \{[\s\S]*width: 104px;[\s\S]*min-width: 104px/); assert.match(styles, /@media \(prefers-reduced-motion: reduce\)[\s\S]*\.client-copy-feedback/); }); test('metrics route reads the current snapshot before static fallback', () => { const metricsRoute = server.indexOf("requestUrl.pathname === '/metrics'"); const staticFallback = server.indexOf(': serveStatic(req, res)'); assert.ok(metricsRoute >= 0 && metricsRoute < staticFallback); assert.match(server, /requestUrl\.pathname === '\/metrics'[\s\S]*deviceInventory\.metricsSnapshot\(\)/); assert.doesNotMatch(server.slice(metricsRoute, staticFallback), /deviceInventory\.refresh\(/); }); test('Grafana dashboard is organized around period ranking and one stable device selection', () => { const expressions = dashboard.panels.flatMap((panel) => panel.targets || []).map(({ expr }) => expr).filter(Boolean); const titles = dashboard.panels.map(({ title }) => title); const deviceVariable = dashboard.templating.list[0]; const freshness = dashboard.panels.find(({ id }) => id === 2); const topDevices = dashboard.panels.find(({ id }) => id === 5); const domains = dashboard.panels.find(({ id }) => id === 9); assert.equal(dashboard.title, 'Harbor Gateway: трафик'); assert.equal(dashboard.refresh, '30s'); assert.deepEqual(titles.filter((title) => ['Обзор', 'Устройство', 'Сервисы и домены', 'Техническая детализация'].includes(title)), [ 'Обзор', 'Устройство', 'Сервисы и домены', 'Техническая детализация', ]); assert.ok(titles.includes('Скорость сейчас, средняя за 5 минут')); assert.ok(titles.includes('Топ-10 устройств за выбранный период')); assert.ok(titles.includes('${device_id:text}: накоплено')); assert.ok(titles.includes('${device_id:text}: сервисы за период')); assert.ok(titles.includes('${device_id:text}: домены за период')); assert.ok(expressions.some((expression) => expression.includes('harbor_traffic_bytes_total'))); assert.ok(expressions.some((expression) => expression.includes('harbor_device_traffic_bytes_total'))); assert.ok(expressions.some((expression) => expression.includes('harbor_device_domain_traffic_bytes_total{device_id="$device_id"}[$__range]'))); assert.ok(expressions.some((expression) => expression.includes('harbor_domain_traffic_last_observed_timestamp_seconds'))); assert.match(topDevices.targets[0].expr, /^topk\(10,[\s\S]*increase\(harbor_device_traffic_bytes_total\[\$__range\]\)[\s\S]*> 0[\s\S]*group_left \(name, ip\)/); assert.equal(topDevices.options.sortBy[0].desc, true); assert.match(topDevices.fieldConfig.overrides[1].properties[0].value[0].url, /var-device_id=\$\{__data\.fields/); assert.deepEqual(freshness.fieldConfig.defaults.thresholds.steps.map(({ value }) => value), [null, 60, 120]); assert.equal(freshness.fieldConfig.defaults.noValue, 'Нет данных'); assert.equal(deviceVariable.name, 'device_id'); assert.equal(deviceVariable.multi, false); assert.equal(deviceVariable.includeAll, false); assert.match(deviceVariable.query.query, /query_result\(label_join\(/); assert.match(deviceVariable.regex, //); assert.match(deviceVariable.regex, //); assert.deepEqual(domains.transformations[0].options.indexByName, { service: 0, domain: 1, Value: 2 }); assert.match(domains.targets[0].expr, /^topk\(15,[\s\S]*> 0\)$/); });