Chat: warn before the harness account's usage limit runs out

A compact banner above the composer when any quota window of the chat's
harness account reaches 80% (error colours from 95%), naming the most
constrained window and a second one also at 80%+. Dismissing it holds per
harness+window until the next level (80/90/95) or a reset, across chats and
reloads; a newly crossed level is announced once with a root SnackBar. The
usage-limit banner wins. Fixture: quota=warn80|warn95.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-30 12:34:01 +00:00
parent 18807b8ac2
commit b6d06219cd
7 changed files with 956 additions and 9 deletions
@@ -0,0 +1,233 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/application/clipboard_failures.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/conversation/chat_thread_controller.dart';
import 'package:pipelines_app/features/chat/conversation/chat_usage_warning_banner.dart';
import 'package:pipelines_app/features/chat/conversation/chat_usage_warnings.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'chat_thread_fixtures.dart';
final _now = DateTime(2026, 11, 25, 12);
final _fiveReset = _now.add(const Duration(hours: 1, minutes: 20));
JsonMap _frame({
double five = 84,
double week = 40,
DateTime? fiveReset,
String session = 's',
}) => {
'session_id': session,
'kind': 'usage',
'usage': {
'account': {
'harness': 'Claude Code',
'plan': 'max',
'windows': [
{
'id': 'five_hour',
'label': '5-hour',
'used_percent': five,
'resets_at': (fiveReset ?? _fiveReset).millisecondsSinceEpoch,
},
{
'id': 'seven_day',
'label': 'Weekly',
'used_percent': week,
'resets_at': _now.add(const Duration(days: 3)).millisecondsSinceEpoch,
},
],
},
},
};
class _Host {
_Host(this.gateway, this.thread, this.warnings, this.announced);
final FakeChatGateway gateway;
final ChatThreadController thread;
final ChatUsageWarnings warnings;
final List<String> announced;
Future<void> emit(WidgetTester tester, JsonMap frame) async {
gateway.emitEvent(frame);
await tester.pump();
await tester.pump();
}
}
Future<_Host> _host(
WidgetTester tester, {
ChatUsageWarnings? warnings,
int panes = 1,
}) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
final announced = <String>[];
final store =
warnings ??
ChatUsageWarnings(
announce: (message) {
announced.add(message);
return true;
},
);
final threads = [
for (var pane = 0; pane < panes; pane++) ChatThreadController(gateway, 's'),
];
for (final thread in threads) {
addTearDown(thread.dispose);
}
await tester.pumpWidget(
MaterialApp(
scaffoldMessengerKey: appScaffoldMessengerKey,
home: Scaffold(
body: Column(
children: [
for (final thread in threads)
ChatUsageWarningBanner(
thread: thread,
warnings: store,
now: () => _now,
),
],
),
),
),
);
return _Host(gateway, threads.first, store, announced);
}
final _banner = find.byKey(const Key('chat-usage-warning'));
final _status = find.byKey(const Key('chat-usage-warning-status'));
final _second = find.byKey(const Key('chat-usage-warning-second'));
final _dismiss = find.byKey(const Key('chat-usage-warning-dismiss'));
Color? _color(WidgetTester tester) =>
tester.widget<Material>(_banner.first).color;
ColorScheme _scheme(WidgetTester tester) =>
Theme.of(tester.element(find.byType(Scaffold))).colorScheme;
void main() {
testWidgets('80% shows a compact warning in the secondary colours', (
tester,
) async {
final host = await _host(tester);
expect(_banner, findsNothing);
await host.emit(tester, _frame(five: 79));
expect(_banner, findsNothing);
await host.emit(tester, _frame(five: 84.2));
expect(_banner, findsOneWidget);
expect(
tester.widget<Text>(_status).data,
'Claude Code 5-hour limit 84% used · resets in 1\u00a0h\u00a020\u00a0m',
);
expect(_second, findsNothing);
expect(_color(tester), _scheme(tester).secondaryContainer);
});
testWidgets('95% escalates to the error colours and names a second window', (
tester,
) async {
final host = await _host(tester);
await host.emit(tester, _frame(five: 96, week: 88));
expect(_color(tester), _scheme(tester).errorContainer);
expect(
tester.widget<Text>(_status).data,
startsWith('Claude Code 5-hour limit 96% used'),
);
// The weekly reset is days away, so it reads as a local date and time.
final second = tester.widget<Text>(_second).data!;
expect(second, startsWith('Weekly limit 88% used · resets '));
expect(second, isNot(contains(' in ')));
});
testWidgets('a dismissal holds until the next level or a reset', (
tester,
) async {
final host = await _host(tester);
await host.emit(tester, _frame(five: 84));
await tester.tap(_dismiss);
await tester.pump();
expect(_banner, findsNothing);
await host.emit(tester, _frame(five: 88));
expect(_banner, findsNothing);
await host.emit(tester, _frame(five: 91));
expect(_banner, findsOneWidget);
await tester.tap(_dismiss);
await tester.pump();
expect(_banner, findsNothing);
final later = _fiveReset.add(const Duration(hours: 5));
await host.emit(tester, _frame(five: 91, fiveReset: later));
expect(_banner, findsOneWidget);
});
testWidgets('a dismissal is shared by every chat of the account', (
tester,
) async {
final host = await _host(tester, panes: 2);
await host.emit(tester, _frame(five: 84));
expect(_banner, findsNWidgets(2));
await tester.tap(_dismiss.first);
await tester.pump();
expect(_banner, findsNothing);
});
testWidgets('hidden while the usage-limit banner shows', (tester) async {
final host = await _host(tester);
await host.emit(tester, _frame(five: 100, week: 90));
expect(_banner, findsNothing);
});
testWidgets('a crossed level is announced once', (tester) async {
final host = await _host(tester);
await host.emit(tester, _frame(five: 84));
expect(host.announced, ['Claude Code 5-hour limit at 84%']);
await host.emit(tester, _frame(five: 86));
expect(host.announced, hasLength(1));
await host.emit(tester, _frame(five: 92, week: 81));
expect(host.announced, [
'Claude Code 5-hour limit at 84%',
'Claude Code 5-hour limit at 92% · Weekly at 81%',
]);
});
testWidgets('the announcement is a SnackBar in the app messenger', (
tester,
) async {
final host = await _host(tester, warnings: ChatUsageWarnings());
await host.emit(tester, _frame(five: 90));
expect(find.text('Claude Code 5-hour limit at 90%'), findsOneWidget);
expect(find.byType(SnackBar), findsOneWidget);
});
testWidgets('dismissals and announcements survive a reload', (tester) async {
SharedPreferences.setMockInitialValues({});
final first = ChatUsageWarnings(
preferences: SharedPreferences.getInstance,
clock: () => _now,
announce: (_) => true,
);
final host = await _host(tester, warnings: first);
await host.emit(tester, _frame(five: 84));
await tester.tap(_dismiss);
await tester.pumpAndSettle();
final announced = <String>[];
final reloaded = ChatUsageWarnings(
preferences: SharedPreferences.getInstance,
clock: () => _now,
announce: (message) {
announced.add(message);
return true;
},
);
final again = await _host(tester, warnings: reloaded);
await again.emit(tester, _frame(five: 84));
await tester.pump();
expect(_banner, findsNothing);
expect(announced, isEmpty);
});
}