Files
pipelines/app/test/features/chat/domain/chat_usage_limit_test.dart
T
alan 293474ac3c 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>
2026-11-30 16:13:33 +00:00

237 lines
7.1 KiB
Dart

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_limit.dart';
ChatAccountUsage _usage(List<(double, DateTime?)> windows) => ChatAccountUsage(
harness: 'Claude Code',
plan: 'max',
asOf: 0,
unsupported: false,
windows: [
for (final (index, (percent, reset)) in windows.indexed)
ChatQuotaWindow(
id: 'w$index',
label: 'W$index',
usedPercent: percent,
resetsAt: reset?.millisecondsSinceEpoch ?? 0,
),
],
);
void main() {
final now = DateTime(2026, 11, 25, 12);
group('chatUsageLimit', () {
test('an exhausted window with a future reset is a limit', () {
final reset = now.add(const Duration(hours: 3));
final limit = chatUsageLimit(
_usage([(42, now.add(const Duration(days: 2))), (100, reset)]),
now,
)!;
expect(limit.resetsAt, reset);
expect(limit.sendAt, reset.add(const Duration(minutes: 1)));
});
test('below 100% or unknown quota is not a limit', () {
expect(chatUsageLimit(null, now), isNull);
expect(
chatUsageLimit(
_usage([(99.9, now.add(const Duration(hours: 1)))]),
now,
),
isNull,
);
final unsupported = ChatAccountUsage(
harness: 'OpenCode',
plan: '',
asOf: 0,
windows: const [],
unsupported: true,
);
expect(chatUsageLimit(unsupported, now), isNull);
});
test('a reset that has passed, or is unknown, is not a limit', () {
expect(
chatUsageLimit(
_usage([(100, now.subtract(const Duration(minutes: 1)))]),
now,
),
isNull,
);
expect(chatUsageLimit(_usage([(100, null)]), now), isNull);
});
test('several exhausted windows wait for the latest reset', () {
final early = now.add(const Duration(hours: 1));
final late = now.add(const Duration(days: 3));
final limit = chatUsageLimit(
_usage([
(100, early),
(104, late),
(60, now.add(const Duration(days: 9))),
(100, now.subtract(const Duration(hours: 1))),
]),
now,
)!;
expect(limit.resetsAt, late);
});
});
test('Claude Code limit text carries the reset in epoch seconds', () {
final reset = now.add(const Duration(hours: 2));
final seconds = reset.millisecondsSinceEpoch ~/ 1000;
expect(
chatUsageLimitFromText(
'Claude AI usage limit reached|$seconds',
now,
)!.resetsAt,
DateTime.fromMillisecondsSinceEpoch(seconds * 1000),
);
expect(chatUsageLimitFromText('Hello there', now), isNull);
final past = now.subtract(const Duration(hours: 1));
expect(
chatUsageLimitFromText(
'Claude AI usage limit reached|${past.millisecondsSinceEpoch ~/ 1000}',
now,
),
isNull,
);
});
test('the thread limit prefers quota, then the last reply text', () {
final seconds =
now.add(const Duration(hours: 2)).millisecondsSinceEpoch ~/ 1000;
final refused = {
'role': 'assistant',
'parts': [
{'type': 'text', 'text': 'Claude AI usage limit reached|$seconds'},
],
};
expect(chatThreadUsageLimit(null, [refused], now), isNotNull);
expect(
chatThreadUsageLimit(null, [
refused,
{
'role': 'user',
'parts': [
{'type': 'text', 'text': 'continue'},
],
},
], now),
isNull,
);
});
group('chatRefusedTurn', () {
Map<String, Object?> message(
String id,
String role,
String text, {
bool file = false,
int? sendAt,
}) => {
'id': id,
'role': role,
'send_at': ?sendAt,
'parts': [
{'type': 'text', 'text': text},
if (file) {'type': 'file', 'name': 'log.txt'},
],
};
const hit =
"You've hit your 5-hour limit · resets 5:20pm (Europe/London) · "
'out of usage credits';
test('a refused operator message is sent again, without its files', () {
final turn = chatRefusedTurn([
message('u1', 'user', 'first'),
message('a1', 'assistant', 'done'),
message('u2', 'user', ' fix the build ', file: true),
message('r1', 'assistant', hit),
], null)!;
expect(turn.text, 'fix the build');
expect(turn.resends, isTrue);
expect(turn.hadAttachments, isTrue);
expect(turn.wording, "You've hit your 5-hour limit");
expect(turn.autoContinueId, 'auto-continue-r1');
});
test('a refusal after the turn worked continues it', () {
final turn = chatRefusedTurn([
message('u1', 'user', 'fix it'),
message('a1', 'assistant', 'Working on it.'),
message('r1', 'assistant', 'Claude AI usage limit reached|1900000000'),
// A prompt scheduled since is not part of the conversation yet.
message('q1', 'user', 'continue', sendAt: 1900000060000),
], null)!;
expect(turn.text, 'continue');
expect(turn.resends, isFalse);
expect(turn.wording, isNull);
});
test('anything but a refusal last is not a refused turn', () {
expect(chatRefusedTurn(const [], null), isNull);
expect(
chatRefusedTurn([message('a1', 'assistant', 'All done.')], null),
isNull,
);
expect(
chatRefusedTurn([
message('r1', 'assistant', hit),
message('u1', 'user', 'continue'),
], null),
isNull,
);
});
test('a refusal from before an account switch no longer counts', () {
final refusal = {...message('r1', 'assistant', hit), 'created_at': 10};
final switched = ChatAccountUsage(
harness: 'Claude Code',
plan: 'max',
asOf: 0,
windows: const [],
unsupported: false,
switchedAt: 20,
);
expect(chatRefusedTurn([refusal], switched), isNull);
expect(chatRefusedTurn([refusal], null), isNotNull);
});
});
test('extra usage reads as available or in use, else nothing', () {
ChatAccountUsage? parse(Map<String, Object?> extra) => chatAccountUsage({
'harness': 'Claude Code',
'windows': [
{'id': 'five_hour', 'label': '5-hour', 'used_percent': 10},
],
'extra_usage': extra,
});
expect(
chatExtraUsageNote(parse({'status': 'allowed'})),
'Extra usage available',
);
expect(
chatExtraUsageNote(parse({'status': 'allowed', 'in_use': true})),
'Using extra usage',
);
expect(
chatExtraUsageNote(
parse({'status': 'rejected', 'disabled_reason': 'out_of_credits'}),
),
isNull,
);
expect(chatExtraUsageNote(parse({})), isNull);
expect(chatExtraUsageNote(null), isNull);
});
test('scheduled send_at is a future epoch-ms time only', () {
final later = now.add(const Duration(minutes: 5));
expect(chatScheduledSendAt(later.millisecondsSinceEpoch, now), later);
expect(chatScheduledSendAt(now.millisecondsSinceEpoch - 1, now), isNull);
expect(chatScheduledSendAt(null, now), isNull);
expect(chatScheduledSendAt('x', now), isNull);
});
}