App: Claude account auto-switch setting; the limit banner leaves a refusal the server is continuing alone

The Claude account dialog shows 'Auto-switch account when usage runs out'
(claude_account_auto_switch) when the server reports it. The usage-limit
banner schedules no auto-continue while the server's auto-switch continue
prompt is queued. A chat refused after another chat already switched the
Claude home gets a fresh switch stamp, so its banner clears too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 00:31:31 +00:00
parent ed9072b79a
commit 18e0e565ca
10 changed files with 386 additions and 11 deletions
@@ -0,0 +1,122 @@
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_claude_account_dialog.dart';
import 'package:pipelines_app/features/chat/conversation/chat_claude_accounts_controller.dart';
import 'package:pipelines_app/features/chat/domain/chat_claude_accounts.dart';
import 'chat_thread_fixtures.dart';
/// A Claude home with two saved accounts and an automatic-switch choice;
/// [reports] false models a server that does not report one.
class _Gateway extends FakeChatGateway {
_Gateway({this.reports = true});
final bool reports;
bool autoSwitch = true;
final requests = <JsonMap>[];
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
if (!type.startsWith('claude_account')) return super.call(type, fields);
final body = fields['claude_account'] as JsonMap;
requests.add({'type': type, ...body});
if (type == 'claude_account_auto_switch') {
autoSwitch = body['auto_switch'] as bool;
}
JsonMap account(String email, bool active) => {
'name': email,
'email': email,
'active': active,
'saved': true,
};
return {
'claude_accounts': {
'config_dir': '/home/op/.claude-profile',
'active': account('a@example.invalid', true),
'accounts': [
account('a@example.invalid', true),
account('b@example.invalid', false),
],
if (reports) 'auto_switch': autoSwitch,
},
};
}
}
Future<void> _open(WidgetTester tester, _Gateway gateway) async {
final accounts = ChatClaudeAccountsController(
gateway: gateway,
sessionId: 's',
);
addTearDown(gateway.close);
addTearDown(accounts.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Builder(
builder: (context) => TextButton(
onPressed: () => showChatClaudeAccountsDialog(context, accounts),
child: const Text('open'),
),
),
),
),
);
await tester.tap(find.text('open'));
await tester.pumpAndSettle();
}
final _setting = find.byKey(const Key('chat-claude-account-auto-switch'));
bool _checked(WidgetTester tester) =>
tester.widget<CheckboxListTile>(_setting).value!;
void main() {
test('the accounts reply carries the automatic-switch choice', () {
expect(
ChatClaudeAccounts.fromJson({
'accounts': [],
'auto_switch': false,
})!.autoSwitch,
isFalse,
);
expect(ChatClaudeAccounts.fromJson({'accounts': []})!.autoSwitch, isNull);
});
testWidgets('the Claude account dialog turns automatic switching off and '
'on', (tester) async {
final gateway = _Gateway();
await _open(tester, gateway);
expect(
find.text('Auto-switch account when usage runs out'),
findsOneWidget,
);
expect(_checked(tester), isTrue);
await tester.tap(_setting);
await tester.pumpAndSettle();
expect(gateway.requests.last, {
'type': 'claude_account_auto_switch',
'session_id': 's',
'auto_switch': false,
});
expect(_checked(tester), isFalse);
await tester.tap(_setting);
await tester.pumpAndSettle();
expect(gateway.requests.last['auto_switch'], isTrue);
expect(_checked(tester), isTrue);
});
testWidgets('a server that does not switch automatically shows no setting', (
tester,
) async {
await _open(tester, _Gateway(reports: false));
expect(
find.byKey(const Key('chat-claude-account-b@example.invalid')),
findsOneWidget,
);
expect(_setting, findsNothing);
});
}
@@ -0,0 +1,183 @@
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_thread_view.dart';
import 'chat_thread_fixtures.dart';
/// A thread gateway that keeps the server's prompt queue, including prompts
/// the server queued itself ([serverQueue]).
class _Gateway extends FakeChatGateway {
final promptBodies = <JsonMap>[];
final held = <JsonMap>[];
List<JsonMap> messages = [];
_Gateway() {
history = (_) async => chatPage(messages);
}
void pushQueue({String? removed}) => emitRaw({
'type': 'chat_queue',
'chat_queue': {
'session_id': 's',
'prompts': [...held],
'removed': ?removed,
},
});
/// The server switched accounts: its continue prompt is queued and the app's
/// auto-continue prompts are removed.
void serverSwitched() {
final removed = [
for (final p in held)
if ('${p['operation_id']}'.startsWith('auto-continue-'))
'${p['operation_id']}',
];
held.removeWhere((p) => removed.contains(p['operation_id']));
held.add({
'operation_id': 'auto-switch-r1',
'text': '[Sent automatically by Pipelines] Usage ran out…',
'client': 'Pipelines (automatic)',
});
for (final id in removed) {
pushQueue(removed: id);
}
pushQueue();
emitEvent({
'session_id': 's',
'kind': 'usage',
'usage': {
'account': {
'harness': 'Claude Code',
'email': 'b@example.invalid',
'switched_at': DateTime.now().millisecondsSinceEpoch,
},
},
});
}
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
if (type == 'chat_prompt') {
final body = fields['chat_prompt'] as JsonMap;
promptBodies.add(body);
held.add({
'operation_id': body['operation_id'],
'text': body['text'],
'send_at': body['send_at'],
});
pushQueue();
return {'chat_queued': true};
}
return super.call(type, fields);
}
}
const _hit =
"You've hit your 5-hour limit · resets 5:20pm (Europe/London) · out of "
'usage credits';
JsonMap _exhausted() => {
'session_id': 's',
'kind': 'usage',
'usage': {
'account': {
'harness': 'Claude Code',
'email': 'a@example.invalid',
'windows': [
{
'id': 'five_hour',
'label': '5-hour',
'used_percent': 100,
'resets_at': DateTime.now()
.add(const Duration(hours: 2))
.millisecondsSinceEpoch,
},
],
},
},
};
Future<void> _pump(WidgetTester tester) async {
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
await tester.pump();
}
Future<_Gateway> _host(WidgetTester tester) async {
final gateway = _Gateway()
..messages = [
chatMessage(
'u1',
'user',
parts: [
{'type': 'text', 'text': 'Fix it'},
],
),
{
...chatMessage(
'r1',
'assistant',
parts: [
{'type': 'text', 'text': _hit},
],
),
'created_at': DateTime.now()
.subtract(const Duration(seconds: 5))
.millisecondsSinceEpoch,
},
];
addTearDown(gateway.close);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: ChatThreadView(
gateway: gateway,
sessionId: 's',
onBack: () {},
drafts: MemoryChatDraftStore(),
autoContinue: ChatAutoContinue(),
),
),
),
);
await _pump(tester);
return gateway;
}
final _banner = find.byKey(const Key('chat-usage-limit'));
void main() {
testWidgets('the server switching accounts clears the banner and its '
'auto-continue stays removed', (tester) async {
final gateway = await _host(tester);
gateway.emitEvent(_exhausted());
await _pump(tester);
expect(_banner, findsOneWidget);
expect(gateway.promptBodies.single['operation_id'], 'auto-continue-r1');
gateway.serverSwitched();
await _pump(tester);
expect(_banner, findsNothing);
// The removed auto-continue is not asked for again.
expect(gateway.promptBodies, hasLength(1));
expect(
gateway.held.map((p) => p['operation_id']),
equals(['auto-switch-r1']),
);
});
testWidgets('a refusal the server is already continuing schedules no '
'auto-continue', (tester) async {
final gateway = await _host(tester);
// The server's continue prompt is queued before the quota report lands.
gateway.held.add({'operation_id': 'auto-switch-r1', 'text': 'continue'});
gateway.pushQueue();
await _pump(tester);
gateway.emitEvent(_exhausted());
await _pump(tester);
expect(gateway.promptBodies, isEmpty);
});
}