Chat usage limit: Claude Code-style banner with auto-continue; drop the approaching-limit banner
- The usage-limit banner now reads 'Out of usage credits' (or the refusal's own 'You've hit your 5-hour limit'), 'Auto-resuming at <time>', an 'Auto-continue when limits reset' checkbox and Try again, keeping the account switcher's 'Switch to <account>'. - Auto-continue (a shared, persisted preference, on by default) hands the server a durable scheduled prompt (send_at = reset + 1 min) with an idempotent auto-continue-<refusal id> operation id: the refused operator message again, or 'continue' when the refused turn had already worked. Unchecking unqueues it; Try again unqueues it and sends now. - Remove the approaching-limit banner, its SnackBar announcements and their marks; the send-button usage ring replaces them. - Claude Code's rate_limit_event overage fields (overageStatus, isUsingOverage/overageInUse, overageDisabledReason, overageResetsAt) reach clients as usage.account.extra_usage; the ring tooltip and the banner say 'Extra usage available' or 'Using extra usage'. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,9 +7,8 @@ import 'package:pipelines_app/features/chat/conversation/chat_composer_controlle
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_thread_controller.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_auto_continue.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_usage_limit_banner.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:pipelines_app/features/chat/domain/chat_account_usage.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_usage_limit.dart';
|
||||
import 'package:pipelines_app/ui/adaptive/segmented_bar_overflow.dart';
|
||||
@@ -290,8 +289,8 @@ void main() {
|
||||
(thread, accounts) => ChatUsageLimitBanner(
|
||||
thread: thread,
|
||||
composer: composer,
|
||||
sendDraft: (_) async {},
|
||||
accounts: accounts,
|
||||
autoContinue: ChatAutoContinue(),
|
||||
),
|
||||
);
|
||||
gateway.emitEvent(_limited());
|
||||
@@ -316,7 +315,7 @@ void main() {
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('with several other accounts the warning banner offers a menu', (
|
||||
testWidgets('with several other accounts the limit banner offers a menu', (
|
||||
tester,
|
||||
) async {
|
||||
final gateway = _AccountsGateway([
|
||||
@@ -324,18 +323,21 @@ void main() {
|
||||
'b@example.invalid',
|
||||
'c@example.invalid',
|
||||
]);
|
||||
final composer = ChatComposerController(gateway, 's');
|
||||
addTearDown(composer.dispose);
|
||||
await _pump(
|
||||
tester,
|
||||
gateway,
|
||||
(thread, accounts) => ChatUsageWarningBanner(
|
||||
(thread, accounts) => ChatUsageLimitBanner(
|
||||
thread: thread,
|
||||
warnings: ChatUsageWarnings(announce: (_) => true),
|
||||
composer: composer,
|
||||
accounts: accounts,
|
||||
autoContinue: ChatAutoContinue(),
|
||||
),
|
||||
);
|
||||
gateway.emitEvent(_limited(five: 90));
|
||||
gateway.emitEvent(_limited());
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byKey(const Key('chat-usage-warning')), findsOneWidget);
|
||||
expect(find.byKey(const Key('chat-usage-limit')), findsOneWidget);
|
||||
await tester.tap(find.text('Switch account'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
@@ -353,18 +355,21 @@ void main() {
|
||||
tester,
|
||||
) async {
|
||||
final gateway = _AccountsGateway(['a@example.invalid']);
|
||||
final composer = ChatComposerController(gateway, 's');
|
||||
addTearDown(composer.dispose);
|
||||
await _pump(
|
||||
tester,
|
||||
gateway,
|
||||
(thread, accounts) => ChatUsageWarningBanner(
|
||||
(thread, accounts) => ChatUsageLimitBanner(
|
||||
thread: thread,
|
||||
warnings: ChatUsageWarnings(announce: (_) => true),
|
||||
composer: composer,
|
||||
accounts: accounts,
|
||||
autoContinue: ChatAutoContinue(),
|
||||
),
|
||||
);
|
||||
gateway.emitEvent(_limited(five: 90));
|
||||
gateway.emitEvent(_limited());
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byKey(const Key('chat-usage-warning')), findsOneWidget);
|
||||
expect(find.byKey(const Key('chat-usage-limit')), findsOneWidget);
|
||||
expect(find.byKey(const Key('chat-claude-switch')), findsNothing);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,28 +1,56 @@
|
||||
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_auto_continue.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_schedule_time.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'chat_thread_fixtures.dart';
|
||||
|
||||
/// A thread gateway that records prompt bodies and answers them as queued,
|
||||
/// as the server does for one carrying `send_at`.
|
||||
/// A thread gateway that keeps the server's prompt queue: a prompt carrying
|
||||
/// `send_at` is held (and reported in a `chat_queue` frame) until removed.
|
||||
class _Gateway extends FakeChatGateway {
|
||||
final promptBodies = <JsonMap>[];
|
||||
final unqueued = <String>[];
|
||||
final held = <JsonMap>[];
|
||||
List<JsonMap> messages = [];
|
||||
|
||||
_Gateway() {
|
||||
history = (_) async => chatPage(messages);
|
||||
}
|
||||
|
||||
JsonMap get _queue => {
|
||||
'session_id': 's',
|
||||
'prompts': [...held],
|
||||
};
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
if (type == 'chat_prompt') {
|
||||
final body = fields['chat_prompt'] as JsonMap;
|
||||
promptBodies.add(body);
|
||||
return {'chat_queued': body['send_at'] != null};
|
||||
final scheduled = body['send_at'] != null;
|
||||
if (scheduled &&
|
||||
!held.any((p) => p['operation_id'] == body['operation_id'])) {
|
||||
held.add({
|
||||
'operation_id': body['operation_id'],
|
||||
'native_message_id': body['native_message_id'],
|
||||
'text': body['text'],
|
||||
'send_at': body['send_at'],
|
||||
});
|
||||
emitRaw({'type': 'chat_queue', 'chat_queue': _queue});
|
||||
}
|
||||
return {'chat_queued': scheduled};
|
||||
}
|
||||
if (type == 'chat_unqueue') {
|
||||
final id = (fields['chat_unqueue'] as JsonMap)['operation_id'];
|
||||
unqueued.add('$id');
|
||||
held.removeWhere((p) => p['operation_id'] == id);
|
||||
return {
|
||||
'chat_queue': {..._queue, 'removed': id},
|
||||
};
|
||||
}
|
||||
return super.call(type, fields);
|
||||
}
|
||||
@@ -30,7 +58,11 @@ class _Gateway extends FakeChatGateway {
|
||||
|
||||
JsonMap _text(String text) => {'type': 'text', 'text': text};
|
||||
|
||||
JsonMap _limitFrame(DateTime reset, {double percent = 100}) => {
|
||||
const _hit =
|
||||
"You've hit your 5-hour limit · resets 5:20pm (Europe/London) · out of "
|
||||
'usage credits';
|
||||
|
||||
JsonMap _limitFrame(DateTime reset, {double percent = 100, JsonMap? extra}) => {
|
||||
'session_id': 's',
|
||||
'kind': 'usage',
|
||||
'usage': {
|
||||
@@ -44,15 +76,8 @@ JsonMap _limitFrame(DateTime reset, {double percent = 100}) => {
|
||||
'used_percent': percent,
|
||||
'resets_at': reset.millisecondsSinceEpoch,
|
||||
},
|
||||
{
|
||||
'id': 'seven_day',
|
||||
'label': 'Weekly',
|
||||
'used_percent': 40,
|
||||
'resets_at': reset
|
||||
.add(const Duration(days: 2))
|
||||
.millisecondsSinceEpoch,
|
||||
},
|
||||
],
|
||||
'extra_usage': ?extra,
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -63,27 +88,27 @@ Future<void> _pump(WidgetTester tester) async {
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
/// A refused operator message: the refusal answers it directly.
|
||||
List<JsonMap> _refusedPrompt() => [
|
||||
chatMessage('u1', 'user', parts: [_text('first')]),
|
||||
chatMessage('a1', 'assistant', parts: [_text('Done.')]),
|
||||
chatMessage(
|
||||
'u2',
|
||||
'user',
|
||||
parts: [
|
||||
_text('Fix the flaky build'),
|
||||
{'type': 'file', 'name': 'log.txt', 'uri': 'file://log.txt'},
|
||||
],
|
||||
),
|
||||
chatMessage('r1', 'assistant', parts: [_text(_hit)]),
|
||||
];
|
||||
|
||||
Future<_Gateway> _host(
|
||||
WidgetTester tester, {
|
||||
List<JsonMap>? messages,
|
||||
Future<JsonMap> Function()? subscribe,
|
||||
ChatAutoContinue? autoContinue,
|
||||
}) async {
|
||||
final gateway = _Gateway()
|
||||
..messages =
|
||||
messages ??
|
||||
[
|
||||
chatMessage('u1', 'user', parts: [_text('first')]),
|
||||
chatMessage(
|
||||
'u2',
|
||||
'user',
|
||||
parts: [
|
||||
_text('Fix the flaky build'),
|
||||
{'type': 'file', 'name': 'log.txt', 'uri': 'file://log.txt'},
|
||||
],
|
||||
),
|
||||
chatMessage('a1', 'assistant', parts: [_text('On it.')]),
|
||||
]
|
||||
..subscribe = subscribe;
|
||||
final gateway = _Gateway()..messages = messages ?? _refusedPrompt();
|
||||
addTearDown(gateway.close);
|
||||
var sequence = 0;
|
||||
await tester.pumpWidget(
|
||||
@@ -95,6 +120,9 @@ Future<_Gateway> _host(
|
||||
onBack: () {},
|
||||
drafts: MemoryChatDraftStore(),
|
||||
newOperationId: () => 'op-${++sequence}',
|
||||
autoContinue:
|
||||
autoContinue ??
|
||||
ChatAutoContinue(preferences: SharedPreferences.getInstance),
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -106,104 +134,167 @@ Future<_Gateway> _host(
|
||||
String _time(WidgetTester tester, DateTime at) =>
|
||||
chatScheduleTime(tester.element(find.byType(Scaffold)), at);
|
||||
|
||||
/// A reset on a whole millisecond, as the wire carries it.
|
||||
DateTime _resetIn(Duration offset) => DateTime.fromMillisecondsSinceEpoch(
|
||||
DateTime.now().add(offset).millisecondsSinceEpoch,
|
||||
);
|
||||
|
||||
final _banner = find.byKey(const Key('chat-usage-limit'));
|
||||
final _button = find.byKey(const Key('chat-usage-limit-schedule'));
|
||||
final _status = find.byKey(const Key('chat-usage-limit-status'));
|
||||
final _checkbox = find.byKey(const Key('chat-usage-limit-auto-continue'));
|
||||
final _tryAgain = find.byKey(const Key('chat-usage-limit-try-again'));
|
||||
|
||||
String _statusText(WidgetTester tester) => tester.widget<Text>(_status).data!;
|
||||
|
||||
bool _checked(WidgetTester tester) => tester
|
||||
.widget<Checkbox>(
|
||||
find.descendant(of: _checkbox, matching: find.byType(Checkbox)),
|
||||
)
|
||||
.value!;
|
||||
|
||||
void main() {
|
||||
setUp(() => SharedPreferences.setMockInitialValues({}));
|
||||
|
||||
testWidgets('no banner until a window is exhausted', (tester) async {
|
||||
final gateway = await _host(tester);
|
||||
expect(_banner, findsNothing);
|
||||
final reset = DateTime.now().add(const Duration(hours: 2));
|
||||
gateway.emitEvent(_limitFrame(reset, percent: 97));
|
||||
gateway.emitEvent(
|
||||
_limitFrame(_resetIn(const Duration(hours: 2)), percent: 97),
|
||||
);
|
||||
await _pump(tester);
|
||||
expect(_banner, findsNothing);
|
||||
expect(gateway.promptBodies, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('the button schedules the last message a minute after reset', (
|
||||
tester,
|
||||
) async {
|
||||
testWidgets('auto-continue hands the server the refused message for a '
|
||||
'minute after the reset, once', (tester) async {
|
||||
final gateway = await _host(tester);
|
||||
final reset = DateTime.fromMillisecondsSinceEpoch(
|
||||
DateTime.now().add(const Duration(hours: 2)).millisecondsSinceEpoch,
|
||||
);
|
||||
final reset = _resetIn(const Duration(hours: 2));
|
||||
final sendAt = reset.add(const Duration(minutes: 1));
|
||||
gateway.emitEvent(_limitFrame(reset));
|
||||
await _pump(tester);
|
||||
|
||||
final sendAt = reset.add(const Duration(minutes: 1));
|
||||
expect(
|
||||
find.text('Usage limit reached · resets at ${_time(tester, reset)}'),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(
|
||||
find.text('Send last message at ${_time(tester, sendAt)}'),
|
||||
findsOneWidget,
|
||||
);
|
||||
// The earlier message's file is not repeated, and the banner says so.
|
||||
expect(find.text("You've hit your 5-hour limit"), findsOneWidget);
|
||||
expect(_statusText(tester), 'Auto-resuming at ${_time(tester, sendAt)}');
|
||||
expect(_checked(tester), isTrue);
|
||||
expect(find.text('Its attachments are not sent again.'), findsOneWidget);
|
||||
|
||||
await tester.tap(_button);
|
||||
await _pump(tester);
|
||||
|
||||
final body = gateway.promptBodies.single;
|
||||
expect(body['text'], 'Fix the flaky build');
|
||||
expect(body['send_at'], sendAt.millisecondsSinceEpoch);
|
||||
expect(body['operation_id'], 'auto-continue-r1');
|
||||
expect(body.containsKey('attachments'), isFalse);
|
||||
// The new bubble reads as scheduled rather than plain "Queued".
|
||||
expect(find.byKey(const Key('chat-delivery-scheduled')), findsOneWidget);
|
||||
// The server holds it: it reads as scheduled and is not asked for again.
|
||||
await _pump(tester);
|
||||
expect(
|
||||
find.text('Scheduled · sends at ${_time(tester, sendAt)}'),
|
||||
findsOneWidget,
|
||||
);
|
||||
// It waits for its time, not the current turn, and is not offered twice.
|
||||
expect(find.byKey(const Key('chat-composer-queued')), findsNothing);
|
||||
expect(find.textContaining('1 message scheduled'), findsOneWidget);
|
||||
expect(_button, findsNothing);
|
||||
gateway.emitEvent(_limitFrame(reset));
|
||||
await _pump(tester);
|
||||
expect(gateway.promptBodies, hasLength(1));
|
||||
});
|
||||
|
||||
testWidgets('with a draft the button schedules the draft and clears it', (
|
||||
testWidgets('a refusal after the turn worked sends "continue"', (
|
||||
tester,
|
||||
) async {
|
||||
final gateway = await _host(
|
||||
tester,
|
||||
messages: [
|
||||
chatMessage('u1', 'user', parts: [_text('Fix it')]),
|
||||
chatMessage('a1', 'assistant', parts: [_text('Working on it.')]),
|
||||
chatMessage('r1', 'assistant', parts: [_text(_hit)]),
|
||||
],
|
||||
);
|
||||
gateway.emitEvent(_limitFrame(_resetIn(const Duration(hours: 1))));
|
||||
await _pump(tester);
|
||||
expect(gateway.promptBodies.single['text'], 'continue');
|
||||
expect(find.text('Its attachments are not sent again.'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('unchecking is remembered and takes the prompt back', (
|
||||
tester,
|
||||
) async {
|
||||
final gateway = await _host(tester);
|
||||
final reset = DateTime.now().add(const Duration(minutes: 40));
|
||||
final reset = _resetIn(const Duration(hours: 2));
|
||||
gateway.emitEvent(_limitFrame(reset));
|
||||
await _pump(tester);
|
||||
final sendAt = DateTime.fromMillisecondsSinceEpoch(
|
||||
reset.millisecondsSinceEpoch,
|
||||
).add(const Duration(minutes: 1));
|
||||
expect(gateway.held, hasLength(1));
|
||||
|
||||
await tester.enterText(
|
||||
find.byKey(const Key('chat-composer-field')),
|
||||
'Then run the tests',
|
||||
);
|
||||
await tester.tap(_checkbox);
|
||||
await _pump(tester);
|
||||
expect(find.text('Send this at ${_time(tester, sendAt)}'), findsOneWidget);
|
||||
expect(find.text('Its attachments are not sent again.'), findsNothing);
|
||||
|
||||
await tester.tap(_button);
|
||||
expect(_checked(tester), isFalse);
|
||||
expect(gateway.unqueued, ['auto-continue-r1']);
|
||||
expect(gateway.held, isEmpty);
|
||||
expect(find.textContaining('Scheduled · sends at'), findsNothing);
|
||||
expect(_statusText(tester), 'Resets at ${_time(tester, reset)}');
|
||||
final preferences = await SharedPreferences.getInstance();
|
||||
expect(preferences.getBool(chatAutoContinuePreferenceKey), isFalse);
|
||||
// Off, nothing is scheduled again.
|
||||
gateway.emitEvent(_limitFrame(reset));
|
||||
await _pump(tester);
|
||||
expect(gateway.promptBodies, hasLength(1));
|
||||
|
||||
final body = gateway.promptBodies.single;
|
||||
expect(body['text'], 'Then run the tests');
|
||||
expect(body['send_at'], sendAt.millisecondsSinceEpoch);
|
||||
final field = tester.widget<TextField>(
|
||||
find.byKey(const Key('chat-composer-field')),
|
||||
);
|
||||
expect(field.controller!.text, isEmpty);
|
||||
// Checking it again schedules the same prompt again.
|
||||
await tester.tap(_checkbox);
|
||||
await _pump(tester);
|
||||
expect(_checked(tester), isTrue);
|
||||
expect(preferences.getBool(chatAutoContinuePreferenceKey), isTrue);
|
||||
expect(gateway.promptBodies, hasLength(2));
|
||||
expect(gateway.held.single['operation_id'], 'auto-continue-r1');
|
||||
});
|
||||
|
||||
testWidgets('nothing to send hides the button', (tester) async {
|
||||
testWidgets('a stored "off" schedules nothing', (tester) async {
|
||||
SharedPreferences.setMockInitialValues({
|
||||
chatAutoContinuePreferenceKey: false,
|
||||
});
|
||||
final gateway = await _host(tester);
|
||||
final reset = _resetIn(const Duration(hours: 2));
|
||||
gateway.emitEvent(_limitFrame(reset));
|
||||
await _pump(tester);
|
||||
expect(_banner, findsOneWidget);
|
||||
expect(_checked(tester), isFalse);
|
||||
expect(_statusText(tester), 'Resets at ${_time(tester, reset)}');
|
||||
expect(gateway.promptBodies, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('Try again takes the scheduled prompt back and sends now', (
|
||||
tester,
|
||||
) async {
|
||||
final gateway = await _host(tester);
|
||||
gateway.emitEvent(_limitFrame(_resetIn(const Duration(hours: 2))));
|
||||
await _pump(tester);
|
||||
expect(gateway.held, hasLength(1));
|
||||
|
||||
await tester.tap(_tryAgain);
|
||||
await _pump(tester);
|
||||
expect(gateway.unqueued, ['auto-continue-r1']);
|
||||
final now = gateway.promptBodies.last;
|
||||
expect(now['text'], 'Fix the flaky build');
|
||||
expect(now.containsKey('send_at'), isFalse);
|
||||
expect(now['operation_id'], isNot('auto-continue-r1'));
|
||||
expect(gateway.promptBodies, hasLength(2));
|
||||
});
|
||||
|
||||
testWidgets('a quota limit with no refused turn only says when it resets', (
|
||||
tester,
|
||||
) async {
|
||||
final gateway = await _host(
|
||||
tester,
|
||||
messages: [
|
||||
chatMessage('a1', 'assistant', parts: [_text('Hello.')]),
|
||||
],
|
||||
);
|
||||
gateway.emitEvent(
|
||||
_limitFrame(DateTime.now().add(const Duration(hours: 1))),
|
||||
);
|
||||
final reset = _resetIn(const Duration(hours: 1));
|
||||
gateway.emitEvent(_limitFrame(reset, extra: {'status': 'allowed'}));
|
||||
await _pump(tester);
|
||||
expect(_banner, findsOneWidget);
|
||||
expect(_button, findsNothing);
|
||||
expect(find.text('Out of usage credits'), findsOneWidget);
|
||||
expect(
|
||||
_statusText(tester),
|
||||
'Resets at ${_time(tester, reset)} · Extra usage available',
|
||||
);
|
||||
expect(_tryAgain, findsNothing);
|
||||
expect(gateway.promptBodies, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('a passed reset is not a limit', (tester) async {
|
||||
@@ -213,42 +304,7 @@ void main() {
|
||||
);
|
||||
await _pump(tester);
|
||||
expect(_banner, findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('a queued prompt with send_at reads Scheduled with Remove', (
|
||||
tester,
|
||||
) async {
|
||||
final sendAt = DateTime.fromMillisecondsSinceEpoch(
|
||||
DateTime.now().add(const Duration(hours: 1)).millisecondsSinceEpoch,
|
||||
);
|
||||
final gateway = await _host(
|
||||
tester,
|
||||
subscribe: () async => {
|
||||
'chat_queue': {
|
||||
'session_id': 's',
|
||||
'prompts': [
|
||||
{
|
||||
'operation_id': 'later',
|
||||
'text': 'continue',
|
||||
'send_at': sendAt.millisecondsSinceEpoch,
|
||||
},
|
||||
{'operation_id': 'next', 'text': 'and then this'},
|
||||
],
|
||||
},
|
||||
},
|
||||
);
|
||||
expect(
|
||||
find.text('Scheduled · sends at ${_time(tester, sendAt)}'),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(find.text('Queued'), findsOneWidget);
|
||||
expect(find.byKey(const Key('chat-delivery-remove')), findsNWidgets(2));
|
||||
|
||||
// With a message already scheduled the banner offers no second re-send.
|
||||
gateway.emitEvent(_limitFrame(sendAt.subtract(const Duration(minutes: 1))));
|
||||
await _pump(tester);
|
||||
expect(find.textContaining('1 message scheduled'), findsOneWidget);
|
||||
expect(_button, findsNothing);
|
||||
expect(gateway.promptBodies, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('chatScheduleTime names another day', (tester) async {
|
||||
|
||||
@@ -1,233 +0,0 @@
|
||||
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);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user