From 2e0308f232e6f79b86c31cc0c06ad5b378339332 Mon Sep 17 00:00:00 2001 From: Wang Zichong Date: Thu, 26 Nov 2026 19:56:31 +0800 Subject: [PATCH] fix: keep a token-model bar visible even at a tiny share - model bars are 8px tall and a nonzero share paints at least 8px, so a model that is a fraction of a percent of the report still reads as a sliver on the full-width track instead of an empty bar - an emulator check of the live report showed the shares were correct (the top model is 1.0 and renders full width); the many near-zero rows came from the real data, where eight of ten models are under a percent - widget test covers the tokens bar width and the metric grid --- lib/screens/stats_screen.dart | 21 ++++-- test/stats_screen_test.dart | 134 ++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 test/stats_screen_test.dart diff --git a/lib/screens/stats_screen.dart b/lib/screens/stats_screen.dart index b0d9de0..575f7c0 100644 --- a/lib/screens/stats_screen.dart +++ b/lib/screens/stats_screen.dart @@ -689,7 +689,8 @@ class _StatsScreenState extends State { Widget _modelRow(ThemeData theme, UsageModelStat model, double Function(UsageModelStat) valueOf, double max) { final value = valueOf(model); - final share = max > 0 ? (value / max).clamp(0.0, 1.0) : 0.0; + final share = + max > 0 ? (value / max).clamp(0.0, 1.0).toDouble() : 0.0; final name = app.modelName(ModelRef(model.modelId, model.providerId, model.variant)); final secondary = [ @@ -727,8 +728,14 @@ class _StatsScreenState extends State { style: TextStyle(fontSize: 11, color: theme.hintColor)), ), const SizedBox(height: 4), - SizedBox( - height: 6, + LayoutBuilder(builder: (context, constraints) { + // A nonzero share always paints at least a few pixels, so a small + // model reads as a sliver on the track, never as an empty bar. + final visible = share <= 0 + ? 0.0 + : math.max(share, 8 / constraints.maxWidth); + return SizedBox( + height: 8, child: ClipRRect( borderRadius: BorderRadius.circular(3), child: Stack( @@ -737,11 +744,11 @@ class _StatsScreenState extends State { // A full-width track: a small share still reads as a sliver // of colour on it, not as an empty bar. ColoredBox(color: theme.dividerColor.withValues(alpha: 0.35)), - if (share > 0) + if (visible > 0) Align( alignment: Alignment.centerLeft, child: FractionallySizedBox( - widthFactor: share, + widthFactor: visible, heightFactor: 1, child: _dimension == _ModelDimension.tokens ? Row(children: [ @@ -763,7 +770,9 @@ class _StatsScreenState extends State { ], ), ), - ), + ); + }), + ], ), ); diff --git a/test/stats_screen_test.dart b/test/stats_screen_test.dart new file mode 100644 index 0000000..a369a03 --- /dev/null +++ b/test/stats_screen_test.dart @@ -0,0 +1,134 @@ +import 'dart:convert'; +import 'dart:typed_data'; + +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:openchamber/api/client.dart'; +import 'package:openchamber/l10n.dart'; +import 'package:openchamber/api/transport.dart'; +import 'package:openchamber/screens/stats_screen.dart'; +import 'package:openchamber/state.dart'; + +class _FakeTransport implements Transport { + final Map report; + _FakeTransport(this.report); + + @override + Future send(TransportRequest r) async { + final body = utf8.encode(jsonEncode({'data': report})); + return TransportResponse(200, {'content-type': 'application/json'}, Stream.value(Uint8List.fromList(body))); + } + + @override + Future webSocket(String path, + {String? rawQuery, Map headers = const {}}) async => + throw UnimplementedError(); + + @override + void setBearerToken(String? token) {} + + @override + void close() {} +} + +Map _report() => { + 'range': {'from': 1000, 'to': 2000}, + 'sessions': 2, + 'subagents': 1, + 'prompts': 4, + 'steps': 120, + 'tokens': { + 'input': 110000, + 'output': 52000, + 'reasoning': 20000, + 'cache': {'read': 900000, 'write': 5000}, + }, + 'cost': 0, + 'activeDays': 2, + 'streak': 1, + 'activity': [ + {'date': '2026-11-24', 'steps': 40}, + {'date': '2026-11-25', 'steps': 80}, + ], + 'models': [ + { + 'model': {'providerID': 'axonhub', 'id': 'deepseek-flash'}, + 'steps': 100, + 'tokens': { + 'input': 100000, + 'output': 50000, + 'reasoning': 20000, + 'cache': {'read': 900000, 'write': 5000}, + }, + 'cost': 0, + }, + { + 'model': {'providerID': 'p2', 'id': 'small'}, + 'steps': 20, + 'tokens': { + 'input': 3000, + 'output': 1000, + 'reasoning': 500, + 'cache': {'read': 500, 'write': 0}, + }, + 'cost': 0, + }, + ], + 'tools': {'mode': 'none'}, + }; + +void main() { + setUp(() => L10n.instance.apply(AppLanguage.en)); + tearDown(() => L10n.instance.apply(AppLanguage.zh)); + + Future pump(WidgetTester tester) async { + final app = AppState(); + addTearDown(app.dispose); + tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( + const MethodChannel('openchamber/device'), + (call) async => 'Asia/Shanghai', + ); + app.client = Client(Uri.parse('http://test'), transport: _FakeTransport(_report())); + await tester.pumpWidget(MaterialApp(home: StatsScreen(app: app))); + await tester.pumpAndSettle(); + final texts = tester.widgetList(find.byType(Text)).map((t) => t.data).whereType().toList(); + debugPrint('TEXTS: ${texts.take(20).toList()}'); + } + + testWidgets('the tokens model bar paints a share of the row', (tester) async { + await pump(tester); + + await tester.scrollUntilVisible(find.text('deepseek-flash'), 300, + scrollable: find.byType(Scrollable).last); + await tester.pumpAndSettle(); + + const colors = [ + Color(0xFF7C6CF6), + Color(0xFF3FB6A8), + Color(0xFFE0A03A), + Color(0xFFB06AD6), + ]; + double widest = 0; + for (final color in colors) { + for (final box in tester + .renderObjectList(find.byWidgetPredicate( + (w) => w is ColoredBox && w.color == color))) { + widest = box.size.width > widest ? box.size.width : widest; + } + } + // The top model has 92% of all tokens, so its fill is hundreds of px. + expect(widest, greaterThan(200), + reason: 'token-segment fills must paint; widest was $widest'); + }); + + testWidgets('the metric grid shows the report', (tester) async { + await pump(tester); + expect(find.text('Prompts'), findsOneWidget); + expect(find.text('4'), findsWidgets); // prompts in the report + expect(find.text('1.1M'), findsWidgets); // 1100k tokens compact + await tester.scrollUntilVisible(find.text('deepseek-flash'), 300, + scrollable: find.byType(Scrollable).last); + expect(find.text('deepseek-flash'), findsWidgets); + }); +}