Files
harbor-net/test/web/instructions-feature-contract.test.js
dokril 79ffff194f
Build and Deploy Gateway / build-and-push (push) Successful in 19s
Build and Deploy Gateway / deploy (push) Successful in 6s
Update VPN client and gateway behavior
2026-08-11 08:50:44 +03:00

53 lines
4.2 KiB
JavaScript

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 page = fs.readFileSync(path.join(root, 'src/web/components/ClientOverviewPage.tsx'), 'utf8');
const feature = fs.readFileSync(path.join(root, 'src/web/features/instructions/InstructionsFeature.tsx'), 'utf8');
const boundary = fs.readFileSync(path.join(root, 'src/web/features/instructions/index.ts'), 'utf8');
const blocks = fs.readFileSync(path.join(root, 'src/web/features/instructions/instructionBlocks.ts'), 'utf8');
const prometheus = fs.readFileSync(path.join(root, 'src/web/features/instructions/prometheus.ts'), 'utf8');
test('instructions feature is the sole owner behind one public boundary', () => {
assert.equal(fs.existsSync(path.join(root, 'src/web/instructions.js')), false);
assert.equal(fs.existsSync(path.join(root, 'src/web/prometheus.js')), false);
assert.match(boundary, /InstructionsPanel,[\s\S]*InstructionsToggle,[\s\S]*useInstructionsFeature/);
assert.doesNotMatch(boundary, /instructionBlocks|prometheusScrapeConfig|grafanaDashboardJson/);
assert.equal((page.match(/useInstructionsFeature\(/g) || []).length, 1);
assert.equal((page.match(/<InstructionsToggle/g) || []).length, 1);
assert.equal((page.match(/<InstructionsPanel/g) || []).length, 1);
assert.match(page, /from '..\/features\/instructions\/index\.js'/);
assert.doesNotMatch(page, /InstructionStep|InstructionBlock|instructionBlocks|openInstructionId|instructionsPanelRef|instructionsToggleRef|instructionsCloseRef|setInstructionsOpen|client-instruction-block/);
assert.doesNotMatch(feature, /from ['"][^'"]*\/api(?:\/|\.js)|SubscriptionFeature|RoutingFeature|DevicesFeature|DiagnosticsFeature/);
});
test('unconditional controller and conditional panel preserve lifecycle and reset boundaries', () => {
assert.match(page, /const instructionsFeature = useInstructionsFeature\([\s\S]*const diagnosticsAvailable/);
assert.match(page, /\{hasSubscription && <InstructionsPanel/);
assert.match(page, /if \(!hasSubscription\) \{[\s\S]*instructionsFeature\.close\(\)[\s\S]*devicesFeature\.close\(\)[\s\S]*diagnosticsFeature\.close\(\)/);
assert.doesNotMatch(page, /if \(!instructionsAvailable\)|instructionsAvailable/);
assert.match(feature, /const \[openInstructionId, setOpenInstructionId\] = useState\(''\)/);
assert.match(feature, /function InstructionBlock[\s\S]*const \[copyFeedback, setCopyFeedback\] = useState/);
assert.match(feature, /copyTimers = useRef\(new Map[\s\S]*copyTimers\.current\.get\(action\.id\)[\s\S]*copyTimers\.current\.set\(action\.id/);
assert.match(feature, /copyAttempts = useRef\(new Map[\s\S]*copyAttempts\.current\.set\(action\.id, attempt\)[\s\S]*await copyText\(action\.text\)[\s\S]*copyAttempts\.current\.get\(action\.id\) !== attempt/);
assert.match(feature, /setCopyFeedback\(\(current\) => \(\{ \.\.\.current, \[action\.id\]: feedback \}\)\)/);
assert.match(feature, /requestAnimationFrame\(\(\) => closeRef\.current\?\.focus\(\)\)[\s\S]*panelRef\.current\?\.contains[\s\S]*toggleRef\.current\?\.focus\(\)/);
assert.match(feature, /addEventListener\('pointerdown', closeOutside\)/);
assert.match(feature, /flushSync[\s\S]*prefers-reduced-motion: reduce[\s\S]*document\.startViewTransition\(update\)/);
});
test('private content helpers preserve guide, copy and monitoring contracts', () => {
assert.match(page, /const gatewayAddress = isGateway \? window\.location\.hostname : '127\.0\.0\.1'/);
assert.match(page, /const controlHost = window\.location\.host \|\| `\$\{gatewayAddress\}:3456`/);
assert.match(page, /port: state\?\.clientRuntime\?\.proxyPort \|\| \(isGateway \? 8080 : 8082\)/);
assert.match(blocks, /\.\.\.\(isGateway \? \[\{/);
assert.match(blocks, /id: 'router'[\s\S]*id: 'prometheus'/);
assert.match(blocks, /prometheusScrapeConfig\(controlHost\)[\s\S]*label: 'Grafana dashboard'/);
assert.match(prometheus, /\.\.\/\.\.\/\.\.\/\.\.\/monitoring\/grafana\/harbor-gateway\.json/);
assert.match(prometheus, /scrape_interval: 30s[\s\S]*scrape_timeout: 3s[\s\S]*metrics_path: \/metrics/);
assert.match(feature, /target="_blank" rel="noreferrer"/);
assert.match(feature, /<CopyButton[\s\S]*label="Скопировать"/);
});