Files
wangjia cb6fc25bc9 test(client): L1 数值格式化纯函数单测 + 从 widget 抽出(支柱3)
- lib/util/format.dart:抽出 formatSpeed/formatDuration(原私有于 connect_page
  _speed / connect_button _fmt,混在 widget 里没法单测)
- connect_page/connect_button 改用公共纯函数(渲染输出不变)
- test/unit/format_test.dart:10 用例覆盖速率 B/s→GB/s 各单位边界 +
  时长 HH:MM:SS 补零/小时不封顶/取模 60
- 验证:format 10 + widget 8 全过、analyze 无 error/warning

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-25 12:06:34 +08:00

59 lines
1.9 KiB
Dart

// format_test.dart — L1 数值格式化纯函数单测(支柱 3:纯逻辑、确定性、零依赖)。
import 'package:flutter_test/flutter_test.dart';
import 'package:pangolin_vpn/util/format.dart';
void main() {
group('formatSpeed(字节/秒 → 数值+单位)', () {
test('null → —/KB/s(未取到)', () {
expect(formatSpeed(null), ('', 'KB/s'));
});
test('0 → 0 B/s', () {
expect(formatSpeed(0), ('0', 'B/s'));
});
test('< 1KB 用 B/s 且取整', () {
expect(formatSpeed(512), ('512', 'B/s'));
expect(formatSpeed(1023), ('1023', 'B/s'));
});
test('KB/s 边界(1024)与一位小数', () {
expect(formatSpeed(1024), ('1.0', 'KB/s'));
expect(formatSpeed(1536), ('1.5', 'KB/s'));
});
test('MB/s 边界(1024^2)', () {
expect(formatSpeed(1024 * 1024), ('1.0', 'MB/s'));
expect(formatSpeed(1024 * 1024 * 5 + 512 * 1024), ('5.5', 'MB/s'));
});
test('GB/s 边界(1024^3)与两位小数', () {
expect(formatSpeed(1024 * 1024 * 1024), ('1.00', 'GB/s'));
expect(formatSpeed(2.5 * 1024 * 1024 * 1024), ('2.50', 'GB/s'));
});
});
group('formatDuration(时长 → HH:MM:SS)', () {
test('0 → 00:00:00', () {
expect(formatDuration(Duration.zero), '00:00:00');
});
test('秒/分补零', () {
expect(formatDuration(const Duration(seconds: 5)), '00:00:05');
expect(formatDuration(const Duration(minutes: 3, seconds: 9)), '00:03:09');
});
test('小时不封顶(对齐连接计时 01:04:59)', () {
expect(
formatDuration(const Duration(hours: 1, minutes: 4, seconds: 59)),
'01:04:59',
);
expect(formatDuration(const Duration(hours: 100)), '100:00:00');
});
test('分/秒取模 60(不溢出进位错乱)', () {
expect(formatDuration(const Duration(minutes: 90)), '01:30:00');
});
});
}