Files
openchamber-android-native/test/stats_screen_test.dart
T
blumia 2e0308f232 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
2026-11-26 19:56:31 +08:00

135 lines
4.2 KiB
Dart

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<String, dynamic> report;
_FakeTransport(this.report);
@override
Future<TransportResponse> 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<TransportSocket> webSocket(String path,
{String? rawQuery, Map<String, String> headers = const {}}) async =>
throw UnimplementedError();
@override
void setBearerToken(String? token) {}
@override
void close() {}
}
Map<String, dynamic> _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<void> 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<Text>(find.byType(Text)).map((t) => t.data).whereType<String>().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<RenderBox>(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);
});
}