// `?claudeAccounts=N` (with `?chats=1&harness=claude`): the chat's Claude // home has N saved accounts (alex@caplens.example, sam@caplens.example, // ops@caplens.example), the first one active, and `claude_account_use` // switches between them. `?claudeUsage=limit` pushes the active account's // quota at 100% on subscribe (the usage-limit banner); `?claudeUsage=warn` // pushes it at 88% (the usage warning banner). import 'package:pipelines_app/core/api_gateway.dart'; Map _params = const {}; int _active = 0; const _emails = [ 'alex@caplens.example', 'sam@caplens.example', 'ops@caplens.example', ]; void configureChatClaudeAccountsFixture(Map params) { _params = params; } int get _count => (int.tryParse(_params['claudeAccounts'] ?? '') ?? 0).clamp(0, 3); JsonMap _account(int i) => { 'name': _emails[i], 'email': _emails[i], 'organization': 'Caplens', 'plan': i == 2 ? 'pro' : 'max', 'active': i == _active, 'saved': true, }; JsonMap _list({JsonMap? usage}) => { 'config_dir': '/home/alex/caplens/.claude-profile', 'active': _account(_active), 'accounts': [for (var i = 0; i < _count; i++) _account(i)], 'usage': ?usage, }; /// Replies to the claude_account* requests, or null for any other request. JsonMap? chatClaudeAccountsReply(JsonMap request) { final type = '${request['type']}'; if (_count == 0 || !type.startsWith('claude_account')) return null; final body = request['claude_account']; final name = body is Map ? '${body['name'] ?? ''}' : ''; JsonMap? usage; if (type == 'claude_account_use') { final index = _emails.indexOf(name); if (index < 0 || index >= _count) { return { 'id': request['id'], 'type': 'error', 'error': 'no saved Claude account "$name"', }; } _active = index; final now = DateTime.now().millisecondsSinceEpoch; usage = _named({ 'harness': 'Claude Code', 'plan': 'max', 'as_of': now, 'switched_at': now, }); } return { 'id': request['id'], 'type': type, 'claude_accounts': _list(usage: usage), }; } /// The account usage.account names: the active one, as the server sends it. JsonMap _named(JsonMap account) => { ...account, 'email': _emails[_active], 'organization': 'Caplens', }; /// The usage.account of other fixtures (`?quota=`) with the active account's /// name when the chat has Claude accounts. JsonMap chatClaudeNamedAccount(JsonMap account) => _count == 0 ? account : _named(account); /// The active account's frame on subscribe: its quota for `?claudeUsage=`, /// else the account alone (as a process start reports it); null without /// Claude accounts. JsonMap? chatClaudeUsageFrame(String sessionId) { if (_count == 0) return null; final percent = switch (_params['claudeUsage']) { 'limit' => 100.0, 'warn' => 88.0, _ => null, }; // `?quota=` already reports the (named) account's quota. if (percent == null && _params['quota'] != null) return null; final now = DateTime.now(); return { 'type': 'chat_event', 'chat_event': { 'session_id': sessionId, 'kind': 'usage', 'usage': { 'account': _named({ 'harness': 'Claude Code', 'plan': 'max', 'as_of': now.millisecondsSinceEpoch, 'windows': [ if (percent != null) { 'id': 'five_hour', 'label': '5-hour', 'used_percent': percent, 'resets_at': now .add(const Duration(hours: 2, minutes: 10)) .millisecondsSinceEpoch, }, ], }), }, }, }; }