Refactor VPN proxy client implementation
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import test from 'node:test';
|
||||
|
||||
import { parseConnectivityResult } from '../../.test-dist/src/web/features/diagnostics/connectivityResult.js';
|
||||
|
||||
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/diagnostics/DiagnosticsFeature.tsx'), 'utf8');
|
||||
const panel = fs.readFileSync(path.join(root, 'src/web/features/diagnostics/ConnectivityDiagnosticsPanel.tsx'), 'utf8');
|
||||
const model = fs.readFileSync(path.join(root, 'src/web/features/diagnostics/connectivityResult.ts'), 'utf8');
|
||||
const boundary = fs.readFileSync(path.join(root, 'src/web/features/diagnostics/index.ts'), 'utf8');
|
||||
|
||||
const ip = { source: 'cloudflare', address: '198.51.100.10', extra: true };
|
||||
const site = { id: 'google', status: 'available', httpStatus: 204, latencyMs: 120 };
|
||||
const pathResult = {
|
||||
available: true,
|
||||
internetAvailable: true,
|
||||
ipv4: { addresses: ['198.51.100.10'], sources: [ip] },
|
||||
ipv6: null,
|
||||
ipv6Source: null,
|
||||
sites: [site],
|
||||
};
|
||||
const valid = {
|
||||
checkedAt: '2026-08-08T12:00:00.000Z',
|
||||
direct: pathResult,
|
||||
vpn: { ...pathResult, server: { id: 'server-1', label: 'Server 1' } },
|
||||
extra: { retained: true },
|
||||
};
|
||||
|
||||
test('diagnostics feature is the sole owner while the conditional panel keeps reset semantics', () => {
|
||||
assert.match(boundary, /ConnectivityDiagnosticsPanel[\s\S]*DiagnosticsToggle,[\s\S]*useDiagnosticsFeature/);
|
||||
assert.equal(fs.existsSync(path.join(root, 'src/web/components/ConnectivityDiagnosticsPanel.jsx')), false);
|
||||
assert.equal((page.match(/useDiagnosticsFeature\(\)/g) || []).length, 1);
|
||||
assert.equal((page.match(/<DiagnosticsToggle/g) || []).length, 1);
|
||||
assert.equal((page.match(/<ConnectivityDiagnosticsPanel/g) || []).length, 1);
|
||||
assert.match(page, /const diagnosticsAvailable = isGateway \|\| \(hasSubscription && subscriptionContentReady\)/);
|
||||
assert.match(page, /\{diagnosticsAvailable && <ConnectivityDiagnosticsPanel[\s\S]*feature=\{diagnosticsFeature\}/);
|
||||
assert.match(page, /if \(!diagnosticsAvailable\) diagnosticsFeature\.close\(\)/);
|
||||
assert.doesNotMatch(page, /diagnosticsOpen|setDiagnosticsOpen|diagnosticsPanelRef|diagnosticsToggleRef|diagnosticsCloseRef|client-diagnostics-toggle/);
|
||||
assert.doesNotMatch(feature, /setResult|customServices|localStorage|runConnectivityDiagnostics|activeTarget|removingServiceId/);
|
||||
assert.match(feature, /closeRef\.current\?\.focus\(\)[\s\S]*panelRef\.current\?\.contains[\s\S]*toggleRef\.current\?\.focus\(\)/);
|
||||
assert.doesNotMatch([feature, panel].join('\n'), /from ['"][^'"]*\/api\/|ConnectionPanel|SubscriptionFeature|RoutingFeature|DevicesFeature/);
|
||||
});
|
||||
|
||||
test('unknown target and legacy-full results pass one identity-preserving parser before use', () => {
|
||||
assert.equal(parseConnectivityResult(valid), valid);
|
||||
const legacyFull = {
|
||||
...valid,
|
||||
direct: {
|
||||
...valid.direct,
|
||||
ipv4: { ...valid.direct.ipv4, sources: [ip, { source: 'ipify', address: null }] },
|
||||
sites: [site, { id: 'youtube', status: 'responded', httpStatus: 403, latencyMs: null }],
|
||||
},
|
||||
};
|
||||
assert.equal(parseConnectivityResult(legacyFull), legacyFull);
|
||||
for (const invalid of [
|
||||
null,
|
||||
{},
|
||||
{ ...valid, direct: undefined },
|
||||
{ ...valid, direct: { ...valid.direct, available: 'yes' } },
|
||||
{ ...valid, direct: { ...valid.direct, ipv4: { addresses: [], sources: [{}] } } },
|
||||
{ ...valid, direct: { ...valid.direct, ipv6: 6 } },
|
||||
{ ...valid, direct: { ...valid.direct, sites: [{ ...site, id: '' }] } },
|
||||
{ ...valid, direct: { ...valid.direct, sites: [{ ...site, status: 'blocked' }] } },
|
||||
{ ...valid, direct: { ...valid.direct, sites: [{ ...site, latencyMs: -1 }] } },
|
||||
{ ...valid, vpn: { ...valid.vpn, server: undefined } },
|
||||
{ ...valid, vpn: { ...valid.vpn, server: { id: 1, label: 'Server' } } },
|
||||
]) assert.throws(() => parseConnectivityResult(invalid), TypeError);
|
||||
assert.match(panel, /parseConnectivityResult\(await runConnectivityDiagnostics\(customServices, target\)\)/);
|
||||
assert.match(panel, /const legacyFullResult = partial\.direct\.ipv4\.sources\.length > 1 \|\| partial\.direct\.sites\.length > 1/);
|
||||
assert.match(panel, /next = legacyFullResult \? partial : mergeResult\(next, partial\)/);
|
||||
assert.doesNotMatch(model, /\sas\s(?:ConnectivityResult|Record<string, unknown>)/);
|
||||
});
|
||||
|
||||
test('serial probes, storage and editor behavior stay panel-owned', () => {
|
||||
assert.match(panel, /const targets = \[[\s\S]*CONNECTIVITY_IP_SOURCES\.map[\s\S]*sites\.map/);
|
||||
assert.match(panel, /for \(const target of targets\) \{[\s\S]*setActiveTarget\(target\)[\s\S]*await runConnectivityDiagnostics\(customServices, target\)[\s\S]*if \(legacyFullResult\) break/);
|
||||
assert.match(panel, /CUSTOM_SERVICES_KEY = 'harbor-diagnostic-services'/);
|
||||
assert.match(panel, /HIDDEN_SERVICES_KEY = 'harbor-hidden-diagnostic-services'/);
|
||||
assert.match(panel, /slice\(0, MAX_CUSTOM_DIAGNOSTIC_SERVICES\)/);
|
||||
assert.match(panel, /parsed\.protocol !== 'https:'/);
|
||||
assert.match(panel, /document\.startViewTransition\(update\)/);
|
||||
assert.match(panel, /retryable: Boolean\(Reflect\.get\(value, 'retryable'\)\)/);
|
||||
assert.match(panel, /requestDetails\(error\)[\s\S]*requestError\.retryable/);
|
||||
});
|
||||
Reference in New Issue
Block a user