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': 56900000, 'output': 4000000, 'reasoning': 1400000, 'cache': {'read': 1266000000, 'write': 0}, }, '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(); } 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))) { // A Row of childless ColoredBoxes collapses to zero height unless the // cross axis is stretched; that is what made the bars look empty. expect(box.size.height, greaterThan(0), reason: 'token segments must have height, got ${box.size}'); widest = box.size.width > widest ? box.size.width : widest; } } // The top model has most 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); }); }