switchroom 0.8.1 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/README.md +54 -61
  2. package/bin/timezone-hook.sh +9 -7
  3. package/dist/agent-scheduler/index.js +285 -45
  4. package/dist/auth-broker/index.js +13932 -0
  5. package/dist/cli/drive-write-pretool.mjs +5418 -0
  6. package/dist/cli/switchroom.js +8890 -5560
  7. package/dist/host-control/main.js +582 -43
  8. package/dist/vault/approvals/kernel-server.js +276 -47
  9. package/dist/vault/broker/server.js +333 -69
  10. package/examples/minimal.yaml +63 -0
  11. package/examples/personal-google-workspace-mcp/.env.example +34 -0
  12. package/examples/personal-google-workspace-mcp/README.md +194 -0
  13. package/examples/personal-google-workspace-mcp/compose.yaml +66 -0
  14. package/examples/switchroom.yaml +220 -0
  15. package/package.json +6 -4
  16. package/profiles/_base/start.sh.hbs +3 -3
  17. package/profiles/_shared/agent-self-service.md.hbs +126 -0
  18. package/profiles/default/CLAUDE.md +10 -0
  19. package/profiles/default/CLAUDE.md.hbs +16 -0
  20. package/skills/buildkite-agent-infrastructure/SKILL.md +30 -11
  21. package/skills/buildkite-agent-runtime/SKILL.md +44 -11
  22. package/skills/buildkite-api/SKILL.md +31 -8
  23. package/skills/buildkite-cli/SKILL.md +27 -9
  24. package/skills/buildkite-migration/SKILL.md +22 -9
  25. package/skills/buildkite-pipelines/SKILL.md +26 -9
  26. package/skills/buildkite-secure-delivery/SKILL.md +23 -9
  27. package/skills/buildkite-test-engine/SKILL.md +25 -8
  28. package/skills/docx/SKILL.md +1 -1
  29. package/skills/file-bug/SKILL.md +34 -6
  30. package/skills/humanizer/SKILL.md +15 -0
  31. package/skills/humanizer-calibrate/SKILL.md +7 -1
  32. package/skills/mcp-builder/SKILL.md +1 -1
  33. package/skills/pdf/SKILL.md +1 -1
  34. package/skills/pptx/SKILL.md +1 -1
  35. package/skills/skill-creator/SKILL.md +21 -1
  36. package/skills/skill-creator/scripts/__pycache__/__init__.cpython-313.pyc +0 -0
  37. package/skills/skill-creator/scripts/__pycache__/generate_report.cpython-313.pyc +0 -0
  38. package/skills/skill-creator/scripts/__pycache__/improve_description.cpython-313.pyc +0 -0
  39. package/skills/skill-creator/scripts/__pycache__/run_eval.cpython-313.pyc +0 -0
  40. package/skills/skill-creator/scripts/__pycache__/run_loop.cpython-313.pyc +0 -0
  41. package/skills/skill-creator/scripts/__pycache__/utils.cpython-313.pyc +0 -0
  42. package/skills/switchroom-cli/SKILL.md +63 -64
  43. package/skills/switchroom-health/SKILL.md +23 -10
  44. package/skills/switchroom-install/SKILL.md +3 -3
  45. package/skills/switchroom-manage/SKILL.md +26 -19
  46. package/skills/switchroom-runtime/SKILL.md +67 -15
  47. package/skills/switchroom-status/SKILL.md +26 -1
  48. package/skills/telegram-test-harness/SKILL.md +3 -0
  49. package/skills/webapp-testing/SKILL.md +31 -1
  50. package/skills/xlsx/SKILL.md +1 -1
  51. package/telegram-plugin/admin-commands/dispatch.test.ts +1 -1
  52. package/telegram-plugin/admin-commands/index.ts +9 -5
  53. package/telegram-plugin/auth-snapshot-format.ts +612 -0
  54. package/telegram-plugin/auto-fallback-fleet.ts +215 -0
  55. package/telegram-plugin/auto-fallback.ts +28 -301
  56. package/telegram-plugin/dist/gateway/gateway.js +17453 -15100
  57. package/telegram-plugin/fleet-fallback-gate.ts +105 -0
  58. package/telegram-plugin/gateway/approval-callback.test.ts +104 -0
  59. package/telegram-plugin/gateway/approval-callback.ts +31 -3
  60. package/telegram-plugin/gateway/auth-add-flow.ts +326 -0
  61. package/telegram-plugin/gateway/auth-broker-client.ts +75 -0
  62. package/telegram-plugin/gateway/auth-command.ts +905 -0
  63. package/telegram-plugin/gateway/auth-line.ts +123 -0
  64. package/telegram-plugin/gateway/auth-status-adapter.ts +101 -0
  65. package/telegram-plugin/gateway/boot-card.ts +23 -37
  66. package/telegram-plugin/gateway/boot-probes.ts +9 -12
  67. package/telegram-plugin/gateway/diff-preview-card.test.ts +192 -0
  68. package/telegram-plugin/gateway/diff-preview-card.ts +170 -0
  69. package/telegram-plugin/gateway/drive-write-approval.test.ts +312 -0
  70. package/telegram-plugin/gateway/drive-write-approval.ts +243 -0
  71. package/telegram-plugin/gateway/folder-picker-handler.test.ts +314 -0
  72. package/telegram-plugin/gateway/folder-picker-handler.ts +348 -0
  73. package/telegram-plugin/gateway/gateway.ts +1156 -938
  74. package/telegram-plugin/gateway/hostd-dispatch.ts +244 -0
  75. package/telegram-plugin/gateway/ipc-protocol.ts +83 -2
  76. package/telegram-plugin/gateway/ipc-server.ts +69 -0
  77. package/telegram-plugin/hooks/sandbox-hint-posttool.mjs +103 -12
  78. package/telegram-plugin/hooks/tool-label-pretool.mjs +11 -0
  79. package/telegram-plugin/hooks/wedge-detect-posttool.mjs +303 -0
  80. package/telegram-plugin/model-unavailable.ts +28 -12
  81. package/telegram-plugin/permission-title.ts +56 -0
  82. package/telegram-plugin/quota-check.ts +19 -41
  83. package/telegram-plugin/scripts/build.mjs +0 -1
  84. package/telegram-plugin/shared/bot-runtime.ts +5 -4
  85. package/telegram-plugin/silence-poke.ts +153 -1
  86. package/telegram-plugin/tests/auth-add-flow.test.ts +559 -0
  87. package/telegram-plugin/tests/auth-code-redact.test.ts +8 -4
  88. package/telegram-plugin/tests/auth-command-format2.test.ts +156 -0
  89. package/telegram-plugin/tests/auth-command-vernacular.test.ts +531 -0
  90. package/telegram-plugin/tests/auth-snapshot-format.test.ts +429 -0
  91. package/telegram-plugin/tests/auth-status-adapter.test.ts +129 -0
  92. package/telegram-plugin/tests/auto-fallback-fleet.test.ts +211 -0
  93. package/telegram-plugin/tests/auto-fallback.test.ts +60 -358
  94. package/telegram-plugin/tests/boot-probes.test.ts +27 -22
  95. package/telegram-plugin/tests/fleet-fallback-gate.test.ts +197 -0
  96. package/telegram-plugin/tests/model-unavailable.test.ts +30 -5
  97. package/telegram-plugin/tests/permission-title.test.ts +31 -0
  98. package/telegram-plugin/tests/quota-check.test.ts +5 -35
  99. package/telegram-plugin/tests/sandbox-hint-posttool.test.ts +212 -2
  100. package/telegram-plugin/tests/silence-poke.test.ts +237 -0
  101. package/telegram-plugin/tests/turn-flush-safety.test.ts +112 -0
  102. package/telegram-plugin/turn-flush-safety.ts +55 -1
  103. package/telegram-plugin/uat/SETUP.md +35 -1
  104. package/telegram-plugin/uat/runners/agent-self-sufficiency.ts +457 -0
  105. package/telegram-plugin/uat/runners/paraphrases.ts +231 -0
  106. package/telegram-plugin/uat/runners/report.ts +150 -0
  107. package/telegram-plugin/uat/runners/run-agent-self-sufficiency.sh +50 -0
  108. package/telegram-plugin/uat/runners/scorer.test.ts +196 -0
  109. package/telegram-plugin/uat/runners/scorer.ts +106 -0
  110. package/telegram-plugin/uat/runners/skill-coverage.test.ts +100 -0
  111. package/telegram-plugin/uat/runners/skill-coverage.ts +620 -0
  112. package/telegram-plugin/uat/scenarios/jtbd-interrupt-marker-dm.test.ts +7 -1
  113. package/telegram-plugin/uat/scenarios/jtbd-rapid-followup-dm.test.ts +7 -1
  114. package/telegram-plugin/auth-dashboard.ts +0 -1104
  115. package/telegram-plugin/auth-slot-parser.ts +0 -497
  116. package/telegram-plugin/auto-fallback-dispatcher.ts +0 -68
  117. package/telegram-plugin/dist/foreman/foreman.js +0 -31358
  118. package/telegram-plugin/foreman/foreman-create-flow.ts +0 -202
  119. package/telegram-plugin/foreman/foreman-handlers.ts +0 -493
  120. package/telegram-plugin/foreman/foreman.ts +0 -1165
  121. package/telegram-plugin/foreman/setup-flow.ts +0 -345
  122. package/telegram-plugin/foreman/setup-state.ts +0 -239
  123. package/telegram-plugin/foreman/state.ts +0 -203
  124. package/telegram-plugin/tests/auth-account-identity-surface.test.ts +0 -118
  125. package/telegram-plugin/tests/auth-dashboard-edge-cases.test.ts +0 -260
  126. package/telegram-plugin/tests/auth-dashboard-restart-flow.test.ts +0 -140
  127. package/telegram-plugin/tests/auth-dashboard-v3b.test.ts +0 -559
  128. package/telegram-plugin/tests/auth-dashboard.test.ts +0 -1045
  129. package/telegram-plugin/tests/auth-slot-commands.test.ts +0 -640
  130. package/telegram-plugin/tests/auto-fallback-dispatcher.e2e.test.ts +0 -183
  131. package/telegram-plugin/tests/boot-card-account-quota.test.ts +0 -137
  132. package/telegram-plugin/tests/foreman-create-flow.test.ts +0 -359
  133. package/telegram-plugin/tests/foreman-handlers.test.ts +0 -347
  134. package/telegram-plugin/tests/foreman-state.test.ts +0 -164
  135. package/telegram-plugin/tests/foreman-write-ops.test.ts +0 -214
  136. package/telegram-plugin/tests/setup-flow.test.ts +0 -510
  137. package/telegram-plugin/tests/setup-state.test.ts +0 -146
@@ -0,0 +1,156 @@
1
+ /**
2
+ * Integration test for the Format 2 wiring through `renderShowText` +
3
+ * `handleAuthCommand`. The pure formatter has dedicated tests in
4
+ * auth-snapshot-format.test.ts; here we cover the seam between the
5
+ * legacy ASCII-table path and the new health-grouped path.
6
+ *
7
+ * Headline guarantees:
8
+ *
9
+ * 1. With no liveQuotas, renderShowText produces the legacy ASCII
10
+ * table shape (back-compat preserved).
11
+ * 2. With liveQuotas matching state.accounts.length, renderShowText
12
+ * produces the Format 2 health-grouped shape (Recommendation
13
+ * footer present, ASCII column header absent).
14
+ * 3. handleAuthCommand attaches a keyboard ONLY when liveQuotas is
15
+ * supplied AND yields one quota per account (no half-rendered
16
+ * buttons under partial-failure).
17
+ * 4. The keyboard emitted by handleAuthCommand never references a
18
+ * blocked or unknown-health account in a switch button (smart-
19
+ * hide rule, integration variant of the unit test in
20
+ * auth-snapshot-format.test.ts).
21
+ */
22
+ import { describe, it, expect, vi } from 'vitest';
23
+ import { renderShowText, handleAuthCommand } from '../gateway/auth-command.js';
24
+ import type { AuthBrokerClient, AuthCommandContext } from '../gateway/auth-command.js';
25
+ import type { ListStateData } from '../../src/auth/broker/client.js';
26
+ import type { QuotaResult, QuotaUtilization } from '../quota-check.js';
27
+
28
+ function quota(part: Partial<QuotaUtilization>): QuotaUtilization {
29
+ return {
30
+ fiveHourUtilizationPct: 0,
31
+ sevenDayUtilizationPct: 0,
32
+ fiveHourResetAt: null,
33
+ sevenDayResetAt: null,
34
+ representativeClaim: null,
35
+ overageStatus: null,
36
+ overageDisabledReason: null,
37
+ ...part,
38
+ };
39
+ }
40
+
41
+ function qOk(part: Partial<QuotaUtilization>): QuotaResult {
42
+ return { ok: true, data: quota(part) };
43
+ }
44
+
45
+ const NOW_MS = new Date('2026-05-15T00:53:00Z').getTime();
46
+
47
+ const FIXTURE_STATE: ListStateData = {
48
+ active: 'pixsoul@x',
49
+ fallback_order: ['ken@x', 'me@x', 'pixsoul@x'],
50
+ accounts: [
51
+ { label: 'ken@x', exhausted: false },
52
+ { label: 'me@x', exhausted: false },
53
+ { label: 'pixsoul@x', exhausted: false },
54
+ ],
55
+ agents: [{ name: 'carrie', account: 'pixsoul@x', override: null }],
56
+ consumers: [],
57
+ };
58
+
59
+ const FIXTURE_QUOTAS: QuotaResult[] = [
60
+ qOk({ fiveHourUtilizationPct: 0, sevenDayUtilizationPct: 23 }),
61
+ qOk({ sevenDayUtilizationPct: 100 }), // blocked
62
+ qOk({ fiveHourUtilizationPct: 8, sevenDayUtilizationPct: 20 }),
63
+ ];
64
+
65
+ function mockClient(over: Partial<AuthBrokerClient> = {}): AuthBrokerClient {
66
+ return {
67
+ listState: vi.fn(async () => FIXTURE_STATE),
68
+ setActive: vi.fn(async (label: string) => ({ active: label, fanned: ['carrie'] })),
69
+ rmAccount: vi.fn(async (label: string) => ({ label })),
70
+ refreshAccount: vi.fn(async (label: string) => ({ account: label })),
71
+ setOverride: vi.fn(async (agent: string, account: string | null) => ({ agent, account })),
72
+ ...over,
73
+ };
74
+ }
75
+
76
+ describe('renderShowText — Format 2 vs legacy', () => {
77
+ it('falls back to legacy ASCII table when no liveQuotas given', () => {
78
+ const out = renderShowText(FIXTURE_STATE, NOW_MS);
79
+ expect(out).toContain('<b>Auth — fleet snapshot</b>');
80
+ expect(out).toContain('ACCOUNT');
81
+ expect(out).toContain('STATUS');
82
+ expect(out).toContain('EXPIRES');
83
+ expect(out).not.toContain('🔋');
84
+ expect(out).not.toContain('Recommendation:');
85
+ });
86
+
87
+ it('renders Format 2 when liveQuotas length matches accounts length', () => {
88
+ const out = renderShowText(FIXTURE_STATE, NOW_MS, {
89
+ liveQuotas: FIXTURE_QUOTAS,
90
+ tz: 'UTC',
91
+ liveProbedAtMs: NOW_MS,
92
+ });
93
+ expect(out).toContain('🔋 <b>Auth — fleet status</b>');
94
+ expect(out).toContain('Recommendation:');
95
+ expect(out).toContain('🔴 <b>BLOCKED</b>');
96
+ expect(out).toContain('🟢 <b>HEALTHY</b>');
97
+ // Legacy ASCII column headers should be absent
98
+ expect(out).not.toContain('ACCOUNT STATUS');
99
+ });
100
+
101
+ it('falls back to legacy when liveQuotas length disagrees with accounts (defensive)', () => {
102
+ const out = renderShowText(FIXTURE_STATE, NOW_MS, {
103
+ liveQuotas: FIXTURE_QUOTAS.slice(0, 2), // wrong length
104
+ });
105
+ expect(out).not.toContain('🔋');
106
+ expect(out).toContain('ACCOUNT');
107
+ });
108
+ });
109
+
110
+ describe('handleAuthCommand — keyboard attachment', () => {
111
+ function makeCtx(overrides: Partial<AuthCommandContext> = {}): AuthCommandContext {
112
+ return {
113
+ agentName: 'carrie',
114
+ isAdmin: true,
115
+ client: mockClient(),
116
+ chatId: 'chat-1',
117
+ ...overrides,
118
+ };
119
+ }
120
+
121
+ it('attaches NO keyboard when liveQuotas is omitted (legacy callers)', async () => {
122
+ const reply = await handleAuthCommand({ kind: 'show' }, makeCtx());
123
+ expect(reply.keyboard).toBeUndefined();
124
+ expect(reply.text).toContain('ACCOUNT'); // legacy table
125
+ });
126
+
127
+ it('attaches a smart keyboard when liveQuotas yields one result per account', async () => {
128
+ const reply = await handleAuthCommand(
129
+ { kind: 'show' },
130
+ makeCtx({ liveQuotas: async () => FIXTURE_QUOTAS, tz: 'UTC' }),
131
+ );
132
+ expect(reply.keyboard).toBeDefined();
133
+ const allButtonText = reply.keyboard!.flat().map((b) => b.text);
134
+ // Switch button should exist for ken@x (healthy, not active)
135
+ expect(allButtonText).toContain('Switch fleet → ken@x');
136
+ // me@x is blocked — must NOT appear as a switch target
137
+ expect(allButtonText).not.toContain('Switch fleet → me@x');
138
+ // Bottom row hardware
139
+ expect(allButtonText).toContain('↻ Refresh');
140
+ expect(allButtonText).toContain('/usage');
141
+ expect(allButtonText).toContain('+ Add');
142
+ });
143
+
144
+ it('attaches no keyboard when the live probe throws (graceful degrade)', async () => {
145
+ const reply = await handleAuthCommand(
146
+ { kind: 'show' },
147
+ makeCtx({
148
+ liveQuotas: async () => {
149
+ throw new Error('network down');
150
+ },
151
+ }),
152
+ );
153
+ expect(reply.keyboard).toBeUndefined();
154
+ expect(reply.text).toContain('ACCOUNT'); // legacy table fallback
155
+ });
156
+ });
@@ -0,0 +1,531 @@
1
+ /**
2
+ * `/auth` CLI-vernacular alignment coverage (RFC H Decision 11 —
3
+ * "same shape on the CLI and in Telegram").
4
+ *
5
+ * Pins the post-/auth-add verb tree that mirrors `switchroom auth`:
6
+ *
7
+ * list / show [<agent>] / rm <label> [confirm] / refresh [<label>]
8
+ * / agent override <agent> <label|clear> / help
9
+ *
10
+ * The headline guarantees:
11
+ *
12
+ * 1. Every verb resolves through the pure parser to the right
13
+ * ParsedAuthCommand kind (no I/O in `parseAuthCommand`).
14
+ * 2. Read verbs (`show`, `list`, `show <agent>`, `help`) are open
15
+ * to any agent; mutating verbs are admin-gated.
16
+ * 3. The `rm` two-step confirm is paired by chat id + label and
17
+ * respects the 60s TTL.
18
+ * 4. `rm` refuses to even prompt when the label is the fleet active
19
+ * (broker enforces too, but the chat surface short-circuits for
20
+ * a cleaner error).
21
+ * 5. `refresh` (no label) iterates every known account, once each.
22
+ * 6. `override` set vs clear translates the chat-ergonomic `clear`
23
+ * keyword to a `null` broker argument.
24
+ * 7. Help text lists every verb (string-contains).
25
+ *
26
+ * Sibling to `auth-add-flow.test.ts` — keeps the new surface's tests
27
+ * scoped to a dedicated file rather than ballooning that one further.
28
+ */
29
+
30
+ import { describe, it, expect, beforeEach, vi } from 'vitest'
31
+
32
+ import {
33
+ parseAuthCommand,
34
+ handleAuthCommand,
35
+ pendingAuthRmFlows,
36
+ AUTH_RM_CONFIRM_TTL_MS,
37
+ type AuthBrokerClient,
38
+ type ListStateData,
39
+ } from '../gateway/auth-command.js'
40
+
41
+ /* ── Fixture builders ─────────────────────────────────────────────────── */
42
+
43
+ function fakeState(over: Partial<ListStateData> = {}): ListStateData {
44
+ return {
45
+ active: 'primary',
46
+ fallback_order: ['primary', 'spare'],
47
+ accounts: [
48
+ {
49
+ label: 'primary',
50
+ expiresAt: Date.now() + 6 * 3600_000,
51
+ exhausted: false,
52
+ last_refreshed_at: Date.now() - 600_000,
53
+ },
54
+ {
55
+ label: 'spare',
56
+ expiresAt: Date.now() + 4 * 3600_000,
57
+ exhausted: false,
58
+ },
59
+ ],
60
+ agents: [
61
+ { name: 'clerk', account: 'primary', override: null },
62
+ { name: 'researcher', account: 'spare', override: 'spare' },
63
+ ],
64
+ consumers: [],
65
+ ...over,
66
+ }
67
+ }
68
+
69
+ interface MockClient extends AuthBrokerClient {
70
+ listState: ReturnType<typeof vi.fn>
71
+ setActive: ReturnType<typeof vi.fn>
72
+ rmAccount: ReturnType<typeof vi.fn>
73
+ refreshAccount: ReturnType<typeof vi.fn>
74
+ setOverride: ReturnType<typeof vi.fn>
75
+ }
76
+
77
+ function mockClient(state: ListStateData = fakeState()): MockClient {
78
+ return {
79
+ listState: vi.fn().mockResolvedValue(state),
80
+ setActive: vi.fn().mockResolvedValue({ active: 'spare', fanned: ['clerk'] }),
81
+ rmAccount: vi.fn().mockImplementation(async (label: string) => ({ label })),
82
+ refreshAccount: vi.fn().mockImplementation(async (label: string) => ({
83
+ account: label,
84
+ expiresAt: Date.now() + 8 * 3600_000,
85
+ })),
86
+ setOverride: vi
87
+ .fn()
88
+ .mockImplementation(async (agent: string, account: string | null) => ({
89
+ agent,
90
+ account,
91
+ })),
92
+ }
93
+ }
94
+
95
+ beforeEach(() => {
96
+ pendingAuthRmFlows.clear()
97
+ })
98
+
99
+ /* ── 1. Parser ────────────────────────────────────────────────────────── */
100
+
101
+ describe('parseAuthCommand — new verbs', () => {
102
+ it('parses /auth list as { kind: "list" }', () => {
103
+ expect(parseAuthCommand('/auth list')).toEqual({ kind: 'list' })
104
+ })
105
+
106
+ it('parses /auth show <agent> as { kind: "show", agent }', () => {
107
+ expect(parseAuthCommand('/auth show clerk')).toEqual({
108
+ kind: 'show',
109
+ agent: 'clerk',
110
+ })
111
+ })
112
+
113
+ it('bare /auth show stays kindshow with no agent field set', () => {
114
+ const p = parseAuthCommand('/auth show')
115
+ expect(p?.kind).toBe('show')
116
+ expect((p as { agent?: string }).agent).toBeUndefined()
117
+ })
118
+
119
+ it('parses /auth rm <label> as rm-prompt', () => {
120
+ expect(parseAuthCommand('/auth rm spare')).toEqual({
121
+ kind: 'rm-prompt',
122
+ label: 'spare',
123
+ })
124
+ })
125
+
126
+ it('parses /auth rm <label> confirm as rm-confirmed (case-insensitive)', () => {
127
+ expect(parseAuthCommand('/auth rm spare confirm')).toEqual({
128
+ kind: 'rm-confirmed',
129
+ label: 'spare',
130
+ })
131
+ expect(parseAuthCommand('/auth rm spare CONFIRM')).toEqual({
132
+ kind: 'rm-confirmed',
133
+ label: 'spare',
134
+ })
135
+ })
136
+
137
+ it('rejects /auth rm <label> <bogus> with a help reason', () => {
138
+ const p = parseAuthCommand('/auth rm spare yesplease')
139
+ expect(p?.kind).toBe('help')
140
+ expect((p as { reason?: string }).reason).toMatch(/confirm/i)
141
+ })
142
+
143
+ it('rejects /auth rm with no label', () => {
144
+ const p = parseAuthCommand('/auth rm')
145
+ expect(p?.kind).toBe('help')
146
+ expect((p as { reason?: string }).reason).toMatch(/usage/i)
147
+ })
148
+
149
+ it('parses /auth refresh (no label)', () => {
150
+ expect(parseAuthCommand('/auth refresh')).toEqual({ kind: 'refresh' })
151
+ })
152
+
153
+ it('parses /auth refresh <label>', () => {
154
+ expect(parseAuthCommand('/auth refresh primary')).toEqual({
155
+ kind: 'refresh',
156
+ label: 'primary',
157
+ })
158
+ })
159
+
160
+ it('parses /auth agent override <agent> <label>', () => {
161
+ expect(parseAuthCommand('/auth agent override clerk primary')).toEqual({
162
+ kind: 'override-set',
163
+ agent: 'clerk',
164
+ label: 'primary',
165
+ })
166
+ })
167
+
168
+ it('parses /auth agent override <agent> clear as override-clear', () => {
169
+ expect(parseAuthCommand('/auth agent override clerk clear')).toEqual({
170
+ kind: 'override-clear',
171
+ agent: 'clerk',
172
+ })
173
+ // case-insensitive
174
+ expect(parseAuthCommand('/auth agent override clerk CLEAR')).toEqual({
175
+ kind: 'override-clear',
176
+ agent: 'clerk',
177
+ })
178
+ })
179
+
180
+ it('rejects /auth agent override with missing args', () => {
181
+ const a = parseAuthCommand('/auth agent override')
182
+ const b = parseAuthCommand('/auth agent override clerk')
183
+ expect(a?.kind).toBe('help')
184
+ expect(b?.kind).toBe('help')
185
+ })
186
+
187
+ it('rejects /auth agent <unknown-sub>', () => {
188
+ const p = parseAuthCommand('/auth agent pin clerk primary')
189
+ expect(p?.kind).toBe('help')
190
+ expect((p as { reason?: string }).reason).toMatch(/override/i)
191
+ })
192
+
193
+ it('parses /auth help explicitly', () => {
194
+ expect(parseAuthCommand('/auth help')).toEqual({ kind: 'help' })
195
+ })
196
+
197
+ it('routes unknown verbs to help with a reason', () => {
198
+ const p = parseAuthCommand('/auth nonsense')
199
+ expect(p?.kind).toBe('help')
200
+ expect((p as { reason?: string }).reason).toMatch(/unknown/i)
201
+ })
202
+
203
+ it('tolerates extra whitespace and bot-suffix', () => {
204
+ expect(parseAuthCommand(' /auth list ')).toEqual({ kind: 'list' })
205
+ expect(parseAuthCommand('/auth@switchroombot list')).toEqual({ kind: 'list' })
206
+ expect(parseAuthCommand('/auth\tshow\tclerk')).toEqual({
207
+ kind: 'show',
208
+ agent: 'clerk',
209
+ })
210
+ })
211
+
212
+ it('is case-insensitive on the verb', () => {
213
+ expect(parseAuthCommand('/auth LIST')?.kind).toBe('list')
214
+ expect(parseAuthCommand('/auth REFRESH')?.kind).toBe('refresh')
215
+ expect(parseAuthCommand('/auth Agent OVERRIDE clerk clear')).toEqual({
216
+ kind: 'override-clear',
217
+ agent: 'clerk',
218
+ })
219
+ })
220
+ })
221
+
222
+ /* ── 2. Read-verb open access ─────────────────────────────────────────── */
223
+
224
+ describe('handleAuthCommand — read verbs are open to any agent', () => {
225
+ it('/auth list renders the fleet snapshot without an admin gate', async () => {
226
+ const client = mockClient()
227
+ const reply = await handleAuthCommand(
228
+ { kind: 'list' },
229
+ { agentName: 'random-agent', isAdmin: false, client },
230
+ )
231
+ expect(reply.html).toBe(true)
232
+ expect(reply.text).toMatch(/Auth — fleet snapshot/)
233
+ expect(reply.text).not.toMatch(/Not authorized/i)
234
+ expect(client.listState).toHaveBeenCalledTimes(1)
235
+ })
236
+
237
+ it('/auth show <agent> renders per-agent detail for any agent', async () => {
238
+ const client = mockClient()
239
+ const reply = await handleAuthCommand(
240
+ { kind: 'show', agent: 'researcher' },
241
+ { agentName: 'random', isAdmin: false, client },
242
+ )
243
+ expect(reply.text).toMatch(/researcher/)
244
+ expect(reply.text).toMatch(/override/)
245
+ expect(reply.text).toMatch(/spare/)
246
+ })
247
+
248
+ it('/auth show <unknown-agent> returns a friendly error', async () => {
249
+ const client = mockClient()
250
+ const reply = await handleAuthCommand(
251
+ { kind: 'show', agent: 'ghost' },
252
+ { agentName: 'random', isAdmin: false, client },
253
+ )
254
+ expect(reply.text).toMatch(/no agent named/i)
255
+ expect(reply.text).toMatch(/ghost/)
256
+ })
257
+ })
258
+
259
+ /* ── 3. Admin gating ──────────────────────────────────────────────────── */
260
+
261
+ describe('handleAuthCommand — admin gating', () => {
262
+ const nonAdmin = { agentName: 'snooper', isAdmin: false }
263
+
264
+ it('refuses /auth rm <label> for non-admin', async () => {
265
+ const client = mockClient()
266
+ const reply = await handleAuthCommand(
267
+ { kind: 'rm-prompt', label: 'spare' },
268
+ { ...nonAdmin, client },
269
+ )
270
+ expect(reply.text).toMatch(/Not authorized/i)
271
+ expect(client.listState).not.toHaveBeenCalled()
272
+ })
273
+
274
+ it('refuses /auth rm <label> confirm for non-admin', async () => {
275
+ const client = mockClient()
276
+ const reply = await handleAuthCommand(
277
+ { kind: 'rm-confirmed', label: 'spare' },
278
+ { ...nonAdmin, client },
279
+ )
280
+ expect(reply.text).toMatch(/Not authorized/i)
281
+ expect(client.rmAccount).not.toHaveBeenCalled()
282
+ })
283
+
284
+ it('refuses /auth refresh for non-admin', async () => {
285
+ const client = mockClient()
286
+ const reply = await handleAuthCommand(
287
+ { kind: 'refresh' },
288
+ { ...nonAdmin, client },
289
+ )
290
+ expect(reply.text).toMatch(/Not authorized/i)
291
+ expect(client.refreshAccount).not.toHaveBeenCalled()
292
+ })
293
+
294
+ it('refuses /auth agent override <set> for non-admin', async () => {
295
+ const client = mockClient()
296
+ const reply = await handleAuthCommand(
297
+ { kind: 'override-set', agent: 'clerk', label: 'spare' },
298
+ { ...nonAdmin, client },
299
+ )
300
+ expect(reply.text).toMatch(/Not authorized/i)
301
+ expect(client.setOverride).not.toHaveBeenCalled()
302
+ })
303
+
304
+ it('refuses /auth agent override <clear> for non-admin', async () => {
305
+ const client = mockClient()
306
+ const reply = await handleAuthCommand(
307
+ { kind: 'override-clear', agent: 'clerk' },
308
+ { ...nonAdmin, client },
309
+ )
310
+ expect(reply.text).toMatch(/Not authorized/i)
311
+ expect(client.setOverride).not.toHaveBeenCalled()
312
+ })
313
+ })
314
+
315
+ /* ── 4. rm two-step confirm flow ──────────────────────────────────────── */
316
+
317
+ describe('handleAuthCommand — /auth rm two-step confirm', () => {
318
+ const admin = { agentName: 'clerk', isAdmin: true }
319
+
320
+ it('prompt phase succeeds for a valid non-active label and stashes a pending entry', async () => {
321
+ const client = mockClient()
322
+ const reply = await handleAuthCommand(
323
+ { kind: 'rm-prompt', label: 'spare' },
324
+ { ...admin, client, chatId: '999' },
325
+ )
326
+ expect(reply.text).toMatch(/about to remove/i)
327
+ expect(reply.text).toMatch(/spare/)
328
+ expect(reply.text).toMatch(/confirm/i)
329
+ expect(pendingAuthRmFlows.get('999')?.label).toBe('spare')
330
+ })
331
+
332
+ it('refuses to prompt when the label is unknown', async () => {
333
+ const client = mockClient()
334
+ const reply = await handleAuthCommand(
335
+ { kind: 'rm-prompt', label: 'doesnotexist' },
336
+ { ...admin, client, chatId: '999' },
337
+ )
338
+ expect(reply.text).toMatch(/no account named/i)
339
+ expect(client.rmAccount).not.toHaveBeenCalled()
340
+ expect(pendingAuthRmFlows.size).toBe(0)
341
+ })
342
+
343
+ it('refuses to prompt when the label is the fleet active', async () => {
344
+ const client = mockClient()
345
+ const reply = await handleAuthCommand(
346
+ { kind: 'rm-prompt', label: 'primary' },
347
+ { ...admin, client, chatId: '999' },
348
+ )
349
+ expect(reply.text).toMatch(/fleet active/i)
350
+ expect(reply.text).toMatch(/use/)
351
+ expect(client.rmAccount).not.toHaveBeenCalled()
352
+ expect(pendingAuthRmFlows.size).toBe(0)
353
+ })
354
+
355
+ it('confirm phase only fires when a matching pending entry exists', async () => {
356
+ const client = mockClient()
357
+ // Phase 1
358
+ await handleAuthCommand(
359
+ { kind: 'rm-prompt', label: 'spare' },
360
+ { ...admin, client, chatId: 'C' },
361
+ )
362
+ // Phase 2
363
+ const reply = await handleAuthCommand(
364
+ { kind: 'rm-confirmed', label: 'spare' },
365
+ { ...admin, client, chatId: 'C' },
366
+ )
367
+ expect(reply.text).toMatch(/Removed/i)
368
+ expect(client.rmAccount).toHaveBeenCalledTimes(1)
369
+ expect(client.rmAccount).toHaveBeenCalledWith('spare')
370
+ expect(pendingAuthRmFlows.has('C')).toBe(false)
371
+ })
372
+
373
+ it('confirm refuses when no prompt was issued', async () => {
374
+ const client = mockClient()
375
+ const reply = await handleAuthCommand(
376
+ { kind: 'rm-confirmed', label: 'spare' },
377
+ { ...admin, client, chatId: 'C' },
378
+ )
379
+ expect(reply.text).toMatch(/no pending confirm/i)
380
+ expect(client.rmAccount).not.toHaveBeenCalled()
381
+ })
382
+
383
+ it('confirm refuses when the pending label does not match', async () => {
384
+ const client = mockClient()
385
+ pendingAuthRmFlows.set('C', {
386
+ label: 'other-label',
387
+ expiresAt: Date.now() + AUTH_RM_CONFIRM_TTL_MS,
388
+ })
389
+ const reply = await handleAuthCommand(
390
+ { kind: 'rm-confirmed', label: 'spare' },
391
+ { ...admin, client, chatId: 'C' },
392
+ )
393
+ expect(reply.text).toMatch(/no pending confirm/i)
394
+ expect(client.rmAccount).not.toHaveBeenCalled()
395
+ })
396
+
397
+ it('confirm refuses when the pending entry has expired', async () => {
398
+ const client = mockClient()
399
+ pendingAuthRmFlows.set('C', {
400
+ label: 'spare',
401
+ expiresAt: Date.now() - 1, // expired
402
+ })
403
+ const reply = await handleAuthCommand(
404
+ { kind: 'rm-confirmed', label: 'spare' },
405
+ { ...admin, client, chatId: 'C' },
406
+ )
407
+ expect(reply.text).toMatch(/expired|no pending confirm/i)
408
+ expect(client.rmAccount).not.toHaveBeenCalled()
409
+ // Stale entry should be reaped.
410
+ expect(pendingAuthRmFlows.has('C')).toBe(false)
411
+ })
412
+
413
+ it('TTL is the documented 60 seconds', () => {
414
+ expect(AUTH_RM_CONFIRM_TTL_MS).toBe(60_000)
415
+ })
416
+ })
417
+
418
+ /* ── 5. refresh ───────────────────────────────────────────────────────── */
419
+
420
+ describe('handleAuthCommand — /auth refresh', () => {
421
+ const admin = { agentName: 'clerk', isAdmin: true }
422
+
423
+ it('without a label refreshes every account, once each', async () => {
424
+ const client = mockClient()
425
+ const reply = await handleAuthCommand(
426
+ { kind: 'refresh' },
427
+ { ...admin, client },
428
+ )
429
+ expect(reply.text).toMatch(/Refreshed/)
430
+ expect(client.refreshAccount).toHaveBeenCalledTimes(2)
431
+ expect(client.refreshAccount).toHaveBeenCalledWith('primary')
432
+ expect(client.refreshAccount).toHaveBeenCalledWith('spare')
433
+ })
434
+
435
+ it('with a label refreshes that account once', async () => {
436
+ const client = mockClient()
437
+ const reply = await handleAuthCommand(
438
+ { kind: 'refresh', label: 'spare' },
439
+ { ...admin, client },
440
+ )
441
+ expect(reply.text).toMatch(/Refreshed/)
442
+ expect(reply.text).toMatch(/spare/)
443
+ expect(client.refreshAccount).toHaveBeenCalledTimes(1)
444
+ expect(client.refreshAccount).toHaveBeenCalledWith('spare')
445
+ })
446
+
447
+ it('with an unknown label returns a friendly error and does not call the broker', async () => {
448
+ const client = mockClient()
449
+ const reply = await handleAuthCommand(
450
+ { kind: 'refresh', label: 'ghost' },
451
+ { ...admin, client },
452
+ )
453
+ expect(reply.text).toMatch(/no account named/i)
454
+ expect(client.refreshAccount).not.toHaveBeenCalled()
455
+ })
456
+
457
+ it('reports per-account failures without aborting the whole sweep', async () => {
458
+ const client = mockClient()
459
+ client.refreshAccount.mockImplementation(async (label: string) => {
460
+ if (label === 'primary') throw new Error('rate-limited')
461
+ return { account: label, expiresAt: Date.now() + 1000 }
462
+ })
463
+ const reply = await handleAuthCommand(
464
+ { kind: 'refresh' },
465
+ { ...admin, client },
466
+ )
467
+ expect(client.refreshAccount).toHaveBeenCalledTimes(2)
468
+ expect(reply.text).toMatch(/Failures/i)
469
+ expect(reply.text).toMatch(/rate-limited/)
470
+ })
471
+ })
472
+
473
+ /* ── 6. override set + clear ──────────────────────────────────────────── */
474
+
475
+ describe('handleAuthCommand — /auth agent override', () => {
476
+ const admin = { agentName: 'clerk', isAdmin: true }
477
+
478
+ it('set calls setOverride(agent, label)', async () => {
479
+ const client = mockClient()
480
+ const reply = await handleAuthCommand(
481
+ { kind: 'override-set', agent: 'researcher', label: 'primary' },
482
+ { ...admin, client },
483
+ )
484
+ expect(client.setOverride).toHaveBeenCalledTimes(1)
485
+ expect(client.setOverride).toHaveBeenCalledWith('researcher', 'primary')
486
+ expect(reply.text).toMatch(/Override set/i)
487
+ expect(reply.text).toMatch(/researcher/)
488
+ expect(reply.text).toMatch(/primary/)
489
+ })
490
+
491
+ it('clear calls setOverride(agent, null) — chat "clear" → null arg', async () => {
492
+ const client = mockClient()
493
+ const reply = await handleAuthCommand(
494
+ { kind: 'override-clear', agent: 'researcher' },
495
+ { ...admin, client },
496
+ )
497
+ expect(client.setOverride).toHaveBeenCalledTimes(1)
498
+ expect(client.setOverride).toHaveBeenCalledWith('researcher', null)
499
+ expect(reply.text).toMatch(/Override cleared/i)
500
+ expect(reply.text).toMatch(/researcher/)
501
+ })
502
+ })
503
+
504
+ /* ── 7. help text contents ────────────────────────────────────────────── */
505
+
506
+ describe('handleAuthCommand — help text lists every verb', () => {
507
+ it('help reply mentions all the load-bearing verbs', async () => {
508
+ const client = mockClient()
509
+ const reply = await handleAuthCommand(
510
+ { kind: 'help' },
511
+ { agentName: 'x', isAdmin: true, client },
512
+ )
513
+ const text = reply.text
514
+ // Verbs (all variants). The help is HTML; <code> wraps each verb.
515
+ for (const fragment of [
516
+ '/auth show',
517
+ '/auth show &lt;agent&gt;',
518
+ '/auth list',
519
+ '/auth use',
520
+ '/auth rotate',
521
+ '/auth add',
522
+ '/auth cancel',
523
+ '/auth rm',
524
+ '/auth refresh',
525
+ '/auth agent override',
526
+ '/auth help',
527
+ ]) {
528
+ expect(text).toContain(fragment)
529
+ }
530
+ })
531
+ })