import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/chat/domain/chat_account_usage.dart'; import 'package:pipelines_app/features/chat/domain/chat_usage_ring.dart'; final _now = DateTime(2026, 11, 30, 12); // a Monday ChatQuotaWindow _window(String id, String label, double used, DateTime? at) => ChatQuotaWindow( id: id, label: label, usedPercent: used, resetsAt: at?.millisecondsSinceEpoch ?? 0, ); ChatAccountUsage _usage(List windows) => ChatAccountUsage( harness: 'Claude Code', plan: 'max', asOf: 0, windows: windows, unsupported: false, ); void main() { test('shows the 5-hour window even when another is higher', () { final ring = chatUsageRing( _usage([ _window('seven_day', 'Weekly', 93, null), _window('five_hour', '5-hour', 42, null), ]), _now, )!; expect(ring.shown.label, '5-hour'); expect(ring.fraction, closeTo(.42, 1e-9)); expect(ring.tone, ChatUsageRingTone.quiet); }); test('a harness labelling its window "5-hour" counts as the 5-hour', () { final ring = chatUsageRing( _usage([ _window('secondary', 'Weekly', 70, null), _window('primary', '5-hour', 10, null), ]), _now, )!; expect(ring.shown.id, 'primary'); }); test('without a 5-hour window the most used one is shown', () { final ring = chatUsageRing( _usage([ _window('a', 'Daily', 20, null), _window('b', 'Weekly', 61, null), ]), _now, )!; expect(ring.shown.label, 'Weekly'); }); test('tones: quiet below 80, warning from 80, urgent from 95', () { ChatUsageRingTone tone(double used) => chatUsageRing( _usage([_window('five_hour', '5-hour', used, null)]), _now, )!.tone; expect(tone(79.4), ChatUsageRingTone.quiet); expect(tone(80), ChatUsageRingTone.warning); expect(tone(94.9), ChatUsageRingTone.warning); expect(tone(95), ChatUsageRingTone.urgent); expect(tone(130), ChatUsageRingTone.urgent); }); test('past a cap the ring is full, never more', () { final ring = chatUsageRing( _usage([_window('five_hour', '5-hour', 130, null)]), _now, )!; expect(ring.fraction, 1); }); test('no ring for unknown, unsupported or entirely reset quota', () { expect(chatUsageRing(null, _now), isNull); expect( chatUsageRing( const ChatAccountUsage( harness: 'OpenCode', plan: '', asOf: 0, windows: [], unsupported: true, ), _now, ), isNull, ); final past = _now.subtract(const Duration(minutes: 1)); expect( chatUsageRing(_usage([_window('five_hour', '5-hour', 90, past)]), _now), isNull, ); }); test('a reset 5-hour window falls back to the live ones', () { final ring = chatUsageRing( _usage([ _window( 'five_hour', '5-hour', 90, _now.subtract(const Duration(hours: 1)), ), _window('seven_day', 'Weekly', 61, null), ]), _now, )!; expect(ring.shown.label, 'Weekly'); expect(ring.windows.map((w) => w.label), ['Weekly']); }); test('summary names every window with its reset time', () { final ring = chatUsageRing( _usage([ _window('five_hour', '5-hour', 42.4, DateTime(2026, 11, 30, 17, 20)), _window('seven_day', 'Weekly', 61, DateTime(2026, 12, 3, 9)), ]), _now, )!; expect( chatUsageRingSummary(ring, _now), '5-hour 42% · resets 17:20 · Weekly 61% · resets Thu 09:00', ); }); test('summary ends with extra usage when the account has it', () { ChatUsageRing ring(ChatExtraUsage extra) => chatUsageRing( ChatAccountUsage( harness: 'Claude Code', plan: 'max', asOf: 0, windows: [_window('five_hour', '5-hour', 42, null)], unsupported: false, extraUsage: extra, ), _now, )!; const available = ChatExtraUsage( status: 'allowed', inUse: false, disabledReason: '', ); expect( chatUsageRingSummary(ring(available), _now), '5-hour 42% · Extra usage available', ); const inUse = ChatExtraUsage( status: 'allowed_warning', inUse: true, disabledReason: '', ); expect( chatUsageRingSummary(ring(inUse), _now), '5-hour 42% · Using extra usage', ); const off = ChatExtraUsage( status: 'rejected', inUse: false, disabledReason: 'out_of_credits', ); expect(chatUsageRingSummary(ring(off), _now), '5-hour 42%'); }); test('reset times: today, this week, further out', () { expect(chatUsageRingResetTime(DateTime(2026, 11, 30, 8, 5), _now), '08:05'); expect( chatUsageRingResetTime(DateTime(2026, 12, 1, 0, 30), _now), 'Tue 00:30', ); expect( chatUsageRingResetTime(DateTime(2026, 12, 9, 9), _now), '9 Dec 09:00', ); }); }