From 7cc4e042bd8cf688727745369964510f420ac04a Mon Sep 17 00:00:00 2001 From: YanJiu <1+yanjiu@noreply.git.yanjiu.ltd> Date: Fri, 20 Nov 2026 18:59:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E5=B8=83=E7=BD=91=E7=BB=9C=E8=B4=A8?= =?UTF-8?q?=E9=87=8F=E6=8E=A2=E6=B5=8B=201.1.2=EF=BC=9A=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=B8=8E=E8=8A=82=E7=82=B9=E9=80=89=E5=9D=80?= =?UTF-8?q?=E6=BA=90=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 署名:言灸 --- client/test/chart_selection_test.dart | 118 +++++++++++++++++++++ client/test/latency_test.dart | 102 +++++++++++++++++++ client/test/node_edit_test.dart | 101 ++++++++++++++++++ client/test/selection_ledger_test.dart | 135 +++++++++++++++++++++++++ 4 files changed, 456 insertions(+) create mode 100644 client/test/chart_selection_test.dart create mode 100644 client/test/latency_test.dart create mode 100644 client/test/node_edit_test.dart create mode 100644 client/test/selection_ledger_test.dart diff --git a/client/test/chart_selection_test.dart b/client/test/chart_selection_test.dart new file mode 100644 index 0000000..102aa92 --- /dev/null +++ b/client/test/chart_selection_test.dart @@ -0,0 +1,118 @@ +// Copyright (C) 2026 言灸 +// SPDX-License-Identifier: AGPL-3.0-only + +// The plot surface is not part of fl_chart's public API. The tap has to land +// on that surface, not on the axis titles around it. +// ignore_for_file: implementation_imports + +import 'package:fl_chart/src/chart/line_chart/line_chart_renderer.dart'; +import 'package:flutter/gestures.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:netquality/probe/probe_controller.dart'; +import 'package:netquality/ui/chart_selection.dart'; +import 'package:netquality/ui/quality_charts.dart'; + +void main() { + test('a tap pins the nearest time and a later tap replaces it', () { + final first = nextChartSelection( + current: null, + sessionId: 3, + sampleSecs: const [1, 2, 4], + tappedSec: 2.2, + ); + expect(first, isNotNull); + expect(first!.sec, 2); + expect(first.sessionId, 3); + + final second = nextChartSelection( + current: first, + sessionId: 3, + sampleSecs: const [1, 2, 4], + tappedSec: 3.6, + ); + expect(second!.sec, 4); + + final kept = nextChartSelection( + current: second, + sessionId: 3, + sampleSecs: const [1, 2, 4], + ); + expect(kept, same(second)); + }); + + test('a new session or a point that rolled off does not jump', () { + const pin = ChartSelection(sec: 1, sessionId: 3); + expect( + nextChartSelection(current: pin, sessionId: 4, sampleSecs: const [1, 2]), + isNull, + ); + expect( + nextChartSelection(current: pin, sessionId: 3, sampleSecs: const [2, 3]), + isNull, + ); + expect( + nextChartSelection(current: pin, sessionId: 3, sampleSecs: const []), + isNull, + ); + }); + + testWidgets('releasing a chart tap keeps the pinned readout', (tester) async { + const samples = [ + ChartSample(sec: 0, rttMs: 10, ratePct: 0), + ChartSample(sec: 1, rttMs: 30, ratePct: 50), + ]; + await tester.pumpWidget( + const MaterialApp( + home: Scaffold( + body: QualityCharts( + samples: samples, + rateLabel: '探测无应答', + running: false, + sessionId: 1, + ), + ), + ), + ); + await tester.pumpAndSettle(); + expect(find.textContaining('点曲线上的一点'), findsOneWidget); + + final box = tester.getRect(find.byType(LineChartLeaf).first); + const maxY = 30 * 1.2; + Offset at(double sec, double value) { + final x = box.left + box.width * sec; + final y = box.top + (1 - value / maxY) * box.height; + return Offset(x.clamp(box.left + 2, box.right - 2), y); + } + + await tester.tapAt(at(0, 10)); + await tester.pump(); + expect(find.textContaining('已选 0.0 秒'), findsOneWidget); + expect(find.textContaining('延迟'), findsWidgets); + + final mouse = await tester.startGesture( + at(1, 30), + kind: PointerDeviceKind.mouse, + ); + await mouse.up(); + await tester.pump(); + expect(find.textContaining('已选 1.0 秒'), findsOneWidget); + expect(find.textContaining('已选 0.0 秒'), findsNothing); + + await tester.pumpWidget( + const MaterialApp( + home: Scaffold( + body: QualityCharts( + samples: [ChartSample(sec: 2, rttMs: 8, ratePct: 0)], + rateLabel: '探测无应答', + running: false, + sessionId: 1, + ), + ), + ), + ); + await tester.pump(); + expect(find.textContaining('已选'), findsNothing); + expect(find.textContaining('点曲线上的一点'), findsOneWidget); + }); +} diff --git a/client/test/latency_test.dart b/client/test/latency_test.dart new file mode 100644 index 0000000..21c3037 --- /dev/null +++ b/client/test/latency_test.dart @@ -0,0 +1,102 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:netquality/metrics/latency.dart'; +import 'package:netquality/metrics/sample_book.dart'; +import 'package:netquality/ui/metrics_panel.dart'; + +void main() { + test('percentile ranks match the documented integer formula', () { + expect(nearestRank95(1), 1); + expect(nearestRank95(2), 2); + expect(nearestRank95(4), 4); + expect(nearestRank95(5), 5); + expect(nearestRank95(20), 19); + + final one = LatencyStats.from([10]); + expect(one!.min, 10); + expect(one.mean, 10); + expect(one.median, 10); + expect(one.p95, 10); + expect(one.max, 10); + + final two = LatencyStats.from([10, 30]); + expect(two!.median, 20); + expect(two.p95, 30); + expect(two.mean, 20); + + final four = LatencyStats.from([1, 2, 3, 4]); + expect(four!.median, 2.5); + expect(four.p95, 4); + expect(four.min, 1); + expect(four.max, 4); + + final five = LatencyStats.from([1, 2, 3, 4, 10]); + expect(five!.median, 3); + expect(five.p95, 10); + expect(five.mean, 4); + }); + + test('sample book keeps current rtt separate from the distribution', () { + final book = SampleBook('nq-udp'); + book.add(const Outcome(status: Outcome.ok, kind: Outcome.probe, rttMs: 1)); + book.add(const Outcome(status: Outcome.ok, kind: Outcome.probe, rttMs: 2)); + book.add(const Outcome(status: Outcome.ok, kind: Outcome.probe, rttMs: 3)); + book.add(const Outcome(status: Outcome.ok, kind: Outcome.probe, rttMs: 4)); + final summary = book.summary(); + expect(summary.rttMs, 4); + expect(summary.minRttMs, 1); + expect(summary.maxRttMs, 4); + expect(summary.medianRttMs, 2.5); + expect(summary.p95RttMs, 4); + expect(summary.meanRttMs, 2.5); + expect(summary.latencyStats, isTrue); + expect(summary.jitterMs, isNotNull); + }); + + test('unsupported rates stay blank instead of a false zero', () { + final silent = SampleBook('udp-generic') + ..add(const Outcome(status: Outcome.unknown, kind: Outcome.query)); + final lines = metricLines( + summary: silent.summary(), + profileId: 'udp-generic', + elapsed: const Duration(seconds: 2), + ); + expect(_value(lines, '超时'), '不适用'); + expect(_value(lines, '无应答(未知)'), '100%'); + expect(_value(lines, '探测无应答'), isNull); + expect(_value(lines, '当前延迟'), '—'); + + final connect = SampleBook('tcp-connect'); + final empty = metricLines( + summary: connect.summary(), + profileId: 'tcp-connect', + elapsed: Duration.zero, + ); + expect(_value(empty, '失败率'), '—'); + expect(_value(empty, '探测无应答'), isNull); + + final oldNode = ProbeSummary.fromJson({ + 'sent': 1, + 'completed': 1, + 'ok': 1, + 'rttMs': 8, + 'probeLossPct': 0, + }); + final remote = metricLines( + summary: oldNode, + profileId: 'nq-udp', + elapsed: const Duration(seconds: 3), + ); + expect(_value(remote, '当前延迟'), '8.00 ms'); + expect(_value(remote, 'P95 延迟'), '节点未提供'); + expect(_value(remote, '探测无应答'), '0%'); + }); +} + +String? _value(List lines, String label) { + for (final line in lines) { + if (line.label == label) { + return line.value; + } + } + return null; +} diff --git a/client/test/node_edit_test.dart b/client/test/node_edit_test.dart new file mode 100644 index 0000000..0bf355b --- /dev/null +++ b/client/test/node_edit_test.dart @@ -0,0 +1,101 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:netquality/models/node_edit.dart'; +import 'package:netquality/models/port_field.dart'; +import 'package:netquality/models/probe_profile.dart'; +import 'package:netquality/models/session_config.dart'; +import 'package:netquality/storage/token_vault.dart'; + +void main() { + test( + 'editing keeps the node id and replaces only a non-blank token', + () async { + const original = RemoteNode( + id: 'node-7', + name: '机房', + baseUrl: 'https://probe.example:8443', + visibility: 'private', + ); + final renamed = applyNodeEdit( + original, + name: '机房乙', + baseUrl: 'https://probe.example:8443', + ); + expect(renamed.id, 'node-7'); + expect(renamed.name, '机房乙'); + expect(renamed.visibility, 'private'); + + final moved = applyNodeEdit( + renamed, + name: '机房乙', + baseUrl: 'https://other.example:8443', + ); + expect(moved.id, 'node-7'); + expect(moved.visibility, ''); + + final vault = MemoryTokenVault(); + await vault.write(nodeTokenKey('node-7'), 'saved-secret'); + expect(await saveEditedNodeToken(vault, 'node-7', ' '), isFalse); + expect(await vault.read(nodeTokenKey('node-7')), 'saved-secret'); + expect(replacementNodeToken(' '), isNull); + expect( + await saveEditedNodeToken(vault, 'node-7', ' next-secret '), + isTrue, + ); + expect(await vault.read(nodeTokenKey('node-7')), 'next-secret'); + final encoded = moved.toJson(); + expect(encoded.containsKey('token'), isFalse); + expect(encoded.values, isNot(contains('saved-secret'))); + expect(encoded.values, isNot(contains('next-secret'))); + }, + ); + + test( + 'port follows the profile default unless the user typed another port', + () { + expect(ProbeProfile.nqTcp.port, 19090); + expect(ProbeProfile.nqUdp.port, 19091); + expect(portHint(ProbeProfile.nqTcp), '合作反射 TCP 默认 19090'); + expect(portHint(ProbeProfile.nqUdp), '合作反射 UDP 默认 19091'); + expect( + portAfterProfileChange( + current: '19090', + previousDefault: 19090, + nextDefault: 19091, + ), + '19091', + ); + expect( + portAfterProfileChange( + current: ' 19090 ', + previousDefault: 19090, + nextDefault: 19132, + ), + '19132', + ); + expect( + portAfterProfileChange( + current: '', + previousDefault: 19090, + nextDefault: 19091, + ), + '19091', + ); + expect( + portAfterProfileChange( + current: '25565', + previousDefault: 19090, + nextDefault: 19091, + ), + '25565', + ); + expect( + portAfterProfileChange( + current: '25565', + previousDefault: 19091, + nextDefault: 80, + ), + '25565', + ); + }, + ); +} diff --git a/client/test/selection_ledger_test.dart b/client/test/selection_ledger_test.dart new file mode 100644 index 0000000..055575a --- /dev/null +++ b/client/test/selection_ledger_test.dart @@ -0,0 +1,135 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:netquality/metrics/sample_book.dart'; +import 'package:netquality/metrics/selection_ledger.dart'; + +void main() { + const v6 = Outcome( + status: Outcome.timeout, + kind: Outcome.probe, + note: '未收到回显', + detail: {'address': '2001:db8::1', 'family': 'IPv6'}, + ); + const v4ok = Outcome( + status: Outcome.ok, + kind: Outcome.probe, + rttMs: 5, + note: '收到回显', + detail: {'address': '203.0.113.5', 'family': 'IPv4'}, + ); + const v4timeout = Outcome( + status: Outcome.timeout, + kind: Outcome.probe, + note: '未收到回显', + detail: {'address': '203.0.113.5', 'family': 'IPv4'}, + ); + + test('ipv6 miss then ipv4 success is not formal loss', () { + final ledger = SelectionLedger(2); + expect(ledger.take(v6, responded: false).commit, isEmpty); + final decision = ledger.take(v4ok, responded: true); + expect(decision.skipped, 1); + expect(decision.commit.single.selectionSkipped, 1); + expect(decision.commit.single.detail['fallback'], contains('2001:db8::1')); + expect(decision.commit.single.detail['fallback'], contains('203.0.113.5')); + expect(decision.commit.single.detail['fallback'], contains('没有计入')); + final book = SampleBook('nq-udp'); + book.add(decision.commit.single); + final summary = book.summary(); + expect(summary.sent, 1); + expect(summary.ok, 1); + expect(summary.timeout, 0); + expect(summary.probeLossPct, 0); + expect(summary.selectionSkipped, 1); + }); + + test('every candidate failing is loss, not a false zero', () { + final ledger = SelectionLedger(2); + expect(ledger.take(v6, responded: false).commit, isEmpty); + final decision = ledger.take(v4timeout, responded: false); + expect(decision.commit, hasLength(2)); + final book = SampleBook('nq-udp'); + for (final outcome in decision.commit) { + book.add(outcome); + } + final summary = book.summary(); + expect(summary.ok, 0); + expect(summary.sent, 2); + expect(summary.timeout, 2); + expect(summary.probeLossPct, 100); + expect(summary.selectionSkipped, 0); + }); + + test('timeouts after an address is selected still count', () { + final ledger = SelectionLedger(2); + final first = ledger.take(v4ok, responded: true); + expect(ledger.measuring, isTrue); + final again = ledger.take(v4timeout, responded: false); + expect(again.commit, hasLength(1)); + final book = SampleBook('nq-udp'); + book.add(first.commit.single); + book.add(again.commit.single); + expect(book.summary().probeLossPct, 50); + expect(book.summary().sent, 2); + }); + + test('one address never hides a failure', () { + final ledger = SelectionLedger(1); + final decision = ledger.take(v4timeout, responded: false); + expect(decision.commit, hasLength(1)); + final book = SampleBook('nq-tcp'); + book.add(decision.commit.single); + expect(book.summary().probeLossPct, 100); + }); + + test('a stopped in-flight attempt is not recorded', () { + expect( + countsAfterStop( + const Outcome( + status: Outcome.timeout, + kind: Outcome.probe, + note: '已停止', + ), + cancelled: false, + ), + isFalse, + ); + expect( + countsAfterStop( + const Outcome(status: Outcome.timeout, kind: Outcome.probe), + cancelled: true, + ), + isFalse, + ); + expect( + countsAfterStop( + const Outcome(status: Outcome.ok, kind: Outcome.probe, rttMs: 1), + cancelled: false, + ), + isTrue, + ); + expect(countsAfterStop(null, cancelled: false), isFalse); + }); + + test( + 'leaving a measured address does not drop the failure that caused it', + () { + final ledger = SelectionLedger(2); + ledger.take(v4ok, responded: true); + final miss = ledger.take(v4timeout, responded: false); + expect(miss.commit, hasLength(1)); + ledger.endMeasurement(); + expect(ledger.measuring, isFalse); + final held = ledger.take(v6, responded: false); + expect(held.commit, isEmpty); + final recovered = ledger.take(v4ok, responded: true); + expect(recovered.skipped, 1); + final book = SampleBook('nq-udp'); + book.add(v4ok); + book.add(miss.commit.single); + book.add(recovered.commit.single); + expect(book.summary().sent, 3); + expect(book.summary().timeout, 1); + expect(book.summary().probeLossPct, closeTo(100 / 3, 0.0001)); + }, + ); +}