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>
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
// format.dart — 纯数值格式化(支柱 3:纯逻辑与 IO 分离)。
|
||||
//
|
||||
// 从 widget 抽出的无副作用函数:不碰 BuildContext / 时钟 / 网络,纯输入→输出,
|
||||
// 可在 test/unit 里直接断言边界(见 test/unit/format_test.dart)。
|
||||
|
||||
/// 字节/秒 → 动态单位 (数值, 单位):B/s · KB/s · MB/s · GB/s(小数据也看得清)。
|
||||
/// null = 未取到 → ('—', 'KB/s')。
|
||||
(String, String) formatSpeed(double? bytesPerSec) {
|
||||
if (bytesPerSec == null) return ('—', 'KB/s');
|
||||
final b = bytesPerSec;
|
||||
if (b < 1024) return (b.toStringAsFixed(0), 'B/s');
|
||||
if (b < 1024 * 1024) return ((b / 1024).toStringAsFixed(1), 'KB/s');
|
||||
if (b < 1024 * 1024 * 1024) return ((b / (1024 * 1024)).toStringAsFixed(1), 'MB/s');
|
||||
return ((b / (1024 * 1024 * 1024)).toStringAsFixed(2), 'GB/s');
|
||||
}
|
||||
|
||||
/// 时长 → HH:MM:SS(连接计时;小时不封顶)。
|
||||
String formatDuration(Duration d) {
|
||||
String two(int n) => n.toString().padLeft(2, '0');
|
||||
return '${two(d.inHours)}:${two(d.inMinutes % 60)}:${two(d.inSeconds % 60)}';
|
||||
}
|
||||
Reference in New Issue
Block a user