Files
pipelines/app/test/features/chat/domain/chat_usage_limit_test.dart
T
alan 53d8b911a6 Schedule a message for when the usage limit resets
When the open chat's account has an exhausted quota window, a banner above
the composer reads "Usage limit reached · resets at HH:MM" and offers to
send the last message (or the current draft) a minute after the reset, via
chat_prompt send_at. Queued prompts with a future send_at read
"Scheduled · sends at HH:MM" and keep Remove.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 00:59:33 +00:00

161 lines
4.6 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,
);
});
test('the last prompt is the newest user text and notes its files', () {
final last = chatLastPrompt([
{
'role': 'user',
'parts': [
{'type': 'text', 'text': 'first'},
],
},
{
'role': 'user',
'parts': [
{'type': 'text', 'text': ' fix the build '},
{'type': 'file', 'name': 'log.txt'},
],
},
{
'role': 'assistant',
'parts': [
{'type': 'text', 'text': 'ok'},
],
},
])!;
expect(last.text, 'fix the build');
expect(last.hadAttachments, isTrue);
expect(chatLastPrompt(const []), 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);
});
}