Skip to content

Commit e4dca70

Browse files
fix: Use correct SI units for speed test conversion
- Change "Kb" to "kb" for kilo prefix (SI standard uses lowercase k) - Replace binary conversion (* 1024) with SI conversion (* 1000) for Kbps to bps - Update related test expectations Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 6dda14e commit e4dca70

5 files changed

Lines changed: 11 additions & 11 deletions

File tree

lib/page/health_check/providers/health_check_provider.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class HealthCheckProvider extends Notifier<HealthCheckState> {
155155
});
156156
} else {
157157
// Add a random value to simulate a fluctuating meter during tests.
158-
final randomValue = (_random.nextDouble() * (15 - (-3)) + (-3)) * 1024;
158+
final randomValue = (_random.nextDouble() * (15 - (-3)) + (-3)) * 1000;
159159
meterValue += randomValue;
160160
}
161161

lib/page/health_check/services/health_check_service.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,11 @@ class SpeedTestService {
186186
return SpeedTestUIModel.empty();
187187
}
188188
final download = NetworkUtils.formatBitsWithUnit(
189-
(speedTestResult.downloadBandwidth ?? 0) * 1024,
189+
(speedTestResult.downloadBandwidth ?? 0) * 1000,
190190
decimals: 1,
191191
);
192192
final upload = NetworkUtils.formatBitsWithUnit(
193-
(speedTestResult.uploadBandwidth ?? 0) * 1024,
193+
(speedTestResult.uploadBandwidth ?? 0) * 1000,
194194
decimals: 1,
195195
);
196196
final (formattedTimestamp, _) = _formatTimestamp(timestamp ?? '');

lib/page/health_check/shared_widgets/speed_test_widget.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,13 @@ class SpeedTestWidget extends ConsumerWidget {
225225
final result = state.result ?? SpeedTestUIModel.empty();
226226
// Format the live meter value for display.
227227
final formattedLiveValue = NetworkUtils.formatBitsWithUnit(
228-
(state.meterValue * 1024).toInt(),
228+
(state.meterValue * 1000).toInt(),
229229
decimals: 1);
230230

231231
final bandwidthValue = formattedLiveValue.value;
232232
final bandwidthUnit = formattedLiveValue.unit;
233233

234-
final meterValueMbps = (state.meterValue / 1024);
234+
final meterValueMbps = (state.meterValue / 1000);
235235

236236
return Center(
237237
child: AppGauge(

lib/utils.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ extension MediaQueryUtils on Utils {
330330
extension NetworkUtils on Utils {
331331
/// Formats a bit count into a human-readable string with SI units (base 1000).
332332
///
333-
/// Example: 1000 bits -> "1 Kb"
333+
/// Example: 1000 bits -> "1 kb"
334334
static String formatBits(int bits, {int decimals = 0}) {
335335
final result = formatBitsWithUnit(bits, decimals: decimals);
336336
return '${result.value} ${result.unit}';
@@ -342,7 +342,7 @@ extension NetworkUtils on Utils {
342342
static ({String value, String unit}) formatBitsWithUnit(int bits,
343343
{int decimals = 0}) {
344344
if (bits <= 0) return (value: '0', unit: "b");
345-
const suffixes = ["b", "Kb", "Mb", "Gb", "Tb", "Pb"];
345+
const suffixes = ["b", "kb", "Mb", "Gb", "Tb", "Pb"];
346346
var i = (log(bits) / log(1000)).floor();
347347
var number = (bits / pow(1000, i));
348348
return (

test/utils_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ void main() {
510510
test('formatBits: formats bits in kilobytes range with specified decimals',
511511
() {
512512
const bits = 1234;
513-
const expected = '1.234 Kb';
513+
const expected = '1.234 kb';
514514

515515
final formattedBits = NetworkUtils.formatBits(bits, decimals: 3);
516516
expect(formattedBits, expected);
@@ -565,18 +565,18 @@ void main() {
565565
expect(result.unit, 'b');
566566
});
567567

568-
test('formats bits (less than 1Kb)', () {
568+
test('formats bits (less than 1kb)', () {
569569
const bits = 500;
570570
final result = NetworkUtils.formatBitsWithUnit(bits);
571571
expect(result.value, '500');
572572
expect(result.unit, 'b');
573573
});
574574

575575
test('formats kilobytes with 0 decimal places', () {
576-
const bits = 2000; // 2 Kb
576+
const bits = 2000; // 2 kb
577577
final result = NetworkUtils.formatBitsWithUnit(bits);
578578
expect(result.value, '2');
579-
expect(result.unit, 'Kb');
579+
expect(result.unit, 'kb');
580580
});
581581

582582
test('formats megabytes with 2 decimal places', () {

0 commit comments

Comments
 (0)