Chat: a discreet usage ring around the composer's Send button

A thin ring around Send shows the harness account's 5-hour window used share
(the most used live window when there is no 5-hour one): barely visible
normally, tertiary from 80%, error from 95%. The button's tooltip (hover,
long-press on touch) adds every window with its reset time. No ring when the
quota is unknown or unsupported. Painted behind the unchanged button, so its
size, target, states and animations stay as they were.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-30 14:55:42 +00:00
parent 1772100af2
commit 3951c65a7a
7 changed files with 603 additions and 11 deletions
@@ -0,0 +1,156 @@
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_send_usage_ring.dart';
import 'chat_composer_bar_fixtures.dart';
JsonMap _usage(Object account) => {
'session_id': 's',
'kind': 'usage',
'usage': {'account': account},
};
JsonMap _quota({double five = 42, double week = 61}) {
final now = DateTime.now();
return _usage({
'harness': 'Claude Code',
'plan': 'max',
'windows': [
{
'id': 'five_hour',
'label': '5-hour',
'used_percent': five,
'resets_at': now.add(const Duration(hours: 2)).millisecondsSinceEpoch,
},
{
'id': 'seven_day',
'label': 'Weekly',
'used_percent': week,
'resets_at': now.add(const Duration(days: 3)).millisecondsSinceEpoch,
},
],
});
}
final _primary = find.byKey(const Key('chat-composer-primary'));
ChatUsageRingPainter? _painter(WidgetTester tester) {
final paint = tester.widget<CustomPaint>(
find.ancestor(of: _primary, matching: find.byType(CustomPaint)).first,
);
final painter = paint.painter;
return painter is ChatUsageRingPainter ? painter : null;
}
String? _tooltip(WidgetTester tester) => tester
.widget<Tooltip>(
find.ancestor(
of: find.byIcon(Icons.arrow_upward),
matching: find.byType(Tooltip),
),
)
.message;
ColorScheme _scheme(WidgetTester tester) =>
Theme.of(tester.element(_primary)).colorScheme;
Future<ComposerBarGateway> _open(WidgetTester tester) async {
final gateway = ComposerBarGateway();
addTearDown(gateway.close);
await tester.pumpWidget(composerThread(gateway));
await tester.pumpAndSettle();
return gateway;
}
Future<void> _emit(
WidgetTester tester,
ComposerBarGateway gateway,
JsonMap frame,
) async {
gateway.emitEvent(frame);
await tester.pump();
await tester.pump();
}
void main() {
testWidgets('no report: no ring and the plain Send tooltip', (tester) async {
await _open(tester);
expect(_primary, findsOneWidget);
expect(_painter(tester), isNull);
expect(_tooltip(tester), 'Send');
});
testWidgets('a report draws a quiet ring at the 5-hour share and lists '
'every window in the tooltip, without moving the button', (tester) async {
final gateway = await _open(tester);
final before = tester.getRect(_primary);
await _emit(tester, gateway, _quota());
final painter = _painter(tester)!;
expect(painter.fraction, closeTo(.42, 1e-9));
expect(
painter.color,
_scheme(tester).onSurfaceVariant.withValues(alpha: .45),
);
expect(tester.getRect(_primary), before);
final tooltip = _tooltip(tester)!;
expect(tooltip, startsWith('Send\n5-hour 42% · resets '));
expect(tooltip, contains(' · Weekly 61% · resets '));
// The button is still the same element: a report never remounts it.
expect(find.byType(IconButton), findsWidgets);
});
testWidgets('long-press shows the usage tooltip on touch', (tester) async {
final gateway = await _open(tester);
await _emit(tester, gateway, _quota());
await tester.longPress(_primary);
await tester.pump(const Duration(milliseconds: 300));
expect(find.textContaining('5-hour 42%'), findsOneWidget);
await tester.pump(const Duration(seconds: 2));
await tester.pumpAndSettle();
});
testWidgets('tertiary from 80%, error from 95%', (tester) async {
final gateway = await _open(tester);
await _emit(tester, gateway, _quota(five: 84, week: 99));
expect(_painter(tester)!.color, _scheme(tester).tertiary);
await _emit(tester, gateway, _quota(five: 96));
expect(_painter(tester)!.color, _scheme(tester).error);
expect(_painter(tester)!.fraction, closeTo(.96, 1e-9));
});
testWidgets('an unsupported harness draws no ring', (tester) async {
final gateway = await _open(tester);
await _emit(
tester,
gateway,
_usage({'harness': 'OpenCode', 'unsupported': true}),
);
expect(_painter(tester), isNull);
expect(_tooltip(tester), 'Send');
});
testWidgets('Send still sends with the ring shown', (tester) async {
final gateway = await _open(tester);
await _emit(tester, gateway, _quota());
await tester.enterText(find.byKey(const Key('chat-composer-field')), 'hi');
await tester.pump();
await tester.tap(_primary);
await tester.pumpAndSettle();
expect(gateway.promptBodies.single['text'], 'hi');
});
testWidgets('the ring hugs the visible circle outside it', (tester) async {
final gateway = await _open(tester);
await _emit(tester, gateway, _quota());
final painter = _painter(tester)!;
final circle = tester.getSize(
find.descendant(of: _primary, matching: find.byType(Material)).first,
);
expect(painter.circle, circle.width);
expect(
painter.radius - ChatUsageRingPainter.stroke / 2,
circle.width / 2 + ChatUsageRingPainter.gap,
);
});
}
@@ -0,0 +1,143 @@
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<ChatQuotaWindow> 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('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',
);
});
}