import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; import 'package:pipelines_app/features/chat/domain/chat_account_usage.dart'; import 'chat_thread_fixtures.dart'; Widget _host(Widget child) => MaterialApp( theme: ThemeData(platform: TargetPlatform.android), home: Scaffold(body: child), ); JsonMap _usageFrame(JsonMap usage) => { 'session_id': 's', 'kind': 'usage', 'usage': usage, }; /// Synthetic numbers in the server's `usage.account` wire shape. JsonMap _claudeAccount(DateTime now) => { 'harness': 'Claude Code', 'source': 'rate_limit_event', 'plan': 'max', 'as_of': now.millisecondsSinceEpoch, 'windows': [ { 'id': 'five_hour', 'label': '5-hour', 'used_percent': 42, 'resets_at': now .add(const Duration(hours: 2, minutes: 14, seconds: 40)) .millisecondsSinceEpoch, }, { 'id': 'seven_day', 'label': 'Weekly', 'used_percent': 93.4, 'resets_at': now .add(const Duration(days: 3, hours: 5, minutes: 10)) .millisecondsSinceEpoch, }, ], }; Future _thread(WidgetTester tester) async { final gateway = FakeChatGateway(); addTearDown(gateway.close); gateway.history = (_) async => chatPage([]); final thread = ChatThreadView( gateway: gateway, sessionId: 's', onBack: () {}, ); await tester.pumpWidget(_host(thread)); await tester.pumpAndSettle(); return gateway; } Future _openInfo(WidgetTester tester) async { await tester.tap(find.byKey(const Key('chat-info'))); await tester.pumpAndSettle(); } void main() { group('domain', () { final now = DateTime.utc(2026, 11, 2, 12); test('parses reported windows and drops unusable ones', () { final usage = chatAccountUsage({ 'harness': 'Codex', 'plan': 'plus', 'as_of': now.millisecondsSinceEpoch, 'windows': [ {'id': 'primary', 'label': '5-hour', 'used_percent': 37}, {'id': 'bad', 'label': 'Bad', 'used_percent': 'x'}, {'id': 'neg', 'label': 'Neg', 'used_percent': -1}, {'id': 'nolabel', 'used_percent': 5}, ], })!; expect(usage.harness, 'Codex'); expect(usage.windows.map((w) => w.id), ['primary']); expect(usage.windows.single.resetsAt, 0); expect(chatQuotaSummary(usage.windows.single, now), '37% used'); }); test('unknown quota is null; an unsupported harness is explicit', () { expect(chatAccountUsage(null), isNull); expect(chatAccountUsage({'harness': 'Codex', 'windows': []}), isNull); final none = chatAccountUsage({ 'harness': 'OpenCode', 'unsupported': true, })!; expect(none.unsupported, isTrue); expect(none.harness, 'OpenCode'); }); test('countdown and report age wording', () { ChatQuotaWindow window(Duration left) => ChatQuotaWindow( id: 'w', label: 'W', usedPercent: 99.6, resetsAt: now.add(left).millisecondsSinceEpoch, ); expect( chatQuotaSummary(window(const Duration(hours: 2, minutes: 14)), now), '100% used · resets in 2h 14m', ); expect( chatQuotaSummary(window(const Duration(minutes: 9)), now), '100% used · resets in 9m', ); expect( chatQuotaSummary(window(const Duration(seconds: 20)), now), '100% used · resets in under a minute', ); expect( chatQuotaSummary(window(const Duration(days: 2, hours: 3)), now), '100% used · resets in 2d 3h', ); expect( chatQuotaSummary(window(const Duration(minutes: -5)), now), '100% used · reset since this report', ); final at = now.millisecondsSinceEpoch; expect(chatQuotaAsOf(at, now), 'as of just now'); expect(chatQuotaAsOf(at - 5 * 60000, now), 'as of 5 min ago'); expect(chatQuotaAsOf(at - 3 * 3600000, now), 'as of 3 h ago'); expect(chatQuotaAsOf(0, now), ''); expect(chatPlanLabel('self_serve_business'), 'Self serve business'); }); }); // Chat details is Material only (a dialog, or a sheet on a phone). testWidgets('account usage shows windows, plan and age', (tester) async { final gateway = await _thread(tester); gateway.emitEvent( _usageFrame({ 'scope': 'session', 'total_tokens': 1200, 'account': _claudeAccount(DateTime.now()), }), ); await tester.pump(); await _openInfo(tester); expect(find.text('Account usage'), findsOneWidget); expect(find.text('Claude Code · Max plan'), findsOneWidget); expect(find.text('5-hour'), findsOneWidget); expect(find.text('42% used · resets in 2h 14m'), findsOneWidget); expect(find.text('Weekly'), findsOneWidget); expect(find.text('93% used · resets in 3d 5h'), findsOneWidget); expect(find.text('as of just now'), findsOneWidget); final bars = tester .widgetList( find.byType(LinearProgressIndicator), ) .toList(); expect(bars.map((bar) => bar.value), [ closeTo(.42, 1e-9), closeTo(.934, 1e-9), ]); expect(bars.first.semanticsLabel, '5-hour quota'); // Token metrics are still shown beside the quota. expect(find.text('1,200'), findsOneWidget); }); testWidgets('unknown quota hides the section; no-API harness says so', ( tester, ) async { final gateway = await _thread(tester); gateway.emitEvent(_usageFrame({'scope': 'session', 'total_tokens': 5})); await tester.pump(); await _openInfo(tester); expect(find.byKey(const Key('chat-account-usage')), findsNothing); expect(find.text('Account usage'), findsNothing); expect(find.byType(LinearProgressIndicator), findsNothing); gateway.emitEvent( _usageFrame({ 'scope': 'session', 'total_tokens': 6, 'account': {'harness': 'OpenCode', 'unsupported': true}, }), ); await tester.pumpAndSettle(); expect(find.text('Account usage'), findsOneWidget); expect(find.text('Not reported by OpenCode'), findsOneWidget); expect(find.byType(LinearProgressIndicator), findsNothing); }); testWidgets('an account-only frame keeps the recorded token metrics', ( tester, ) async { final gateway = await _thread(tester); gateway.emitEvent( _usageFrame({'scope': 'session', 'total_tokens': 132408}), ); await tester.pump(); gateway.emitEvent(_usageFrame({'account': _claudeAccount(DateTime.now())})); await tester.pump(); await _openInfo(tester); expect(find.text('132,408'), findsOneWidget); expect(find.text('42% used · resets in 2h 14m'), findsOneWidget); // A later frame without an account keeps the last known quota. gateway.emitEvent( _usageFrame({'scope': 'session', 'total_tokens': 140000}), ); await tester.pumpAndSettle(); expect(find.text('140,000'), findsOneWidget); expect(find.text('42% used · resets in 2h 14m'), findsOneWidget); }); }