|
2 | 2 |
|
3 | 3 | import type { OverlayHandle } from "@earendil-works/pi-tui"; |
4 | 4 | import { expectDefined } from "@openclaw/normalization-core"; |
| 5 | +import type { Result } from "@openclaw/normalization-core/result"; |
5 | 6 | import { describe, expect, it, vi } from "vitest"; |
6 | 7 | import { |
7 | 8 | createSessionProjection, |
@@ -35,6 +36,7 @@ type SetActivityStatusMock = ReturnType<typeof vi.fn> & ((text: string) => void) |
35 | 36 | type SetSessionMock = ReturnType<typeof vi.fn> & ((key: string) => Promise<void>); |
36 | 37 | type ConsumeCompletedRunMock = ReturnType<typeof vi.fn> & ((runId: string) => boolean); |
37 | 38 | type FlushPendingHistoryRefreshMock = ReturnType<typeof vi.fn> & (() => void); |
| 39 | +type RefreshAgentsMock = ReturnType<typeof vi.fn> & (() => Promise<Result<void, string>>); |
38 | 40 |
|
39 | 41 | function createOverlayHandle(): OverlayHandle { |
40 | 42 | return { |
@@ -131,6 +133,9 @@ function createHarness(params?: { |
131 | 133 | consumeCompletedRunForPendingSend?: ConsumeCompletedRunMock; |
132 | 134 | isRunObserved?: (runId: string) => boolean; |
133 | 135 | flushPendingHistoryRefreshIfIdle?: FlushPendingHistoryRefreshMock; |
| 136 | + refreshAgents?: RefreshAgentsMock; |
| 137 | + agentDefaultId?: string; |
| 138 | + agents?: Array<{ id: string; kind?: "agent" | "system"; name?: string }>; |
134 | 139 | }) { |
135 | 140 | const sendChat = |
136 | 141 | params?.sendChat ?? |
@@ -172,12 +177,17 @@ function createHarness(params?: { |
172 | 177 | const requestExit = vi.fn(); |
173 | 178 | const abortActive = |
174 | 179 | params?.abortActive ?? (vi.fn().mockResolvedValue(undefined) as AbortActiveMock); |
| 180 | + const refreshAgents = |
| 181 | + params?.refreshAgents ?? |
| 182 | + (vi.fn().mockResolvedValue({ ok: true, value: undefined }) as RefreshAgentsMock); |
175 | 183 | const runAuthFlow: RunAuthFlow | undefined = |
176 | 184 | params?.runAuthFlow ?? |
177 | 185 | (params?.opts?.local |
178 | 186 | ? (vi.fn().mockResolvedValue({ exitCode: 0, signal: null }) as unknown as RunAuthFlow) |
179 | 187 | : undefined); |
180 | 188 | const state = { |
| 189 | + agentDefaultId: params?.agentDefaultId ?? "main", |
| 190 | + agents: params?.agents ?? [], |
181 | 191 | currentAgentId: params?.currentAgentId ?? "main", |
182 | 192 | currentSessionKey: params?.currentSessionKey ?? "agent:main:main", |
183 | 193 | currentSessionId: params?.currentSessionId ?? null, |
@@ -219,7 +229,7 @@ function createHarness(params?: { |
219 | 229 | refreshSessionInfo: refreshSessionInfo as never, |
220 | 230 | loadHistory, |
221 | 231 | setSession, |
222 | | - refreshAgents: vi.fn(), |
| 232 | + refreshAgents, |
223 | 233 | abortActive, |
224 | 234 | setActivityStatus, |
225 | 235 | formatSessionKey: vi.fn(), |
@@ -272,11 +282,55 @@ function createHarness(params?: { |
272 | 282 | forgetLocalBtwRunId, |
273 | 283 | requestExit, |
274 | 284 | abortActive, |
| 285 | + refreshAgents, |
275 | 286 | state, |
276 | 287 | }; |
277 | 288 | } |
278 | 289 |
|
279 | 290 | describe("tui command handlers", () => { |
| 291 | + it("does not open the agent picker from a cached roster after refresh failure", async () => { |
| 292 | + const refreshAgents = vi |
| 293 | + .fn() |
| 294 | + .mockResolvedValue({ ok: false, error: "gateway unavailable" }) as RefreshAgentsMock; |
| 295 | + const { handleCommand, openOverlay, requestRender } = createHarness({ |
| 296 | + refreshAgents, |
| 297 | + agents: [{ id: "cached", name: "Cached Agent" }], |
| 298 | + }); |
| 299 | + |
| 300 | + await handleCommand("/agents"); |
| 301 | + |
| 302 | + expect(refreshAgents).toHaveBeenCalledTimes(1); |
| 303 | + expect(openOverlay).not.toHaveBeenCalled(); |
| 304 | + expect(requestRender).toHaveBeenCalled(); |
| 305 | + }); |
| 306 | + |
| 307 | + it("opens the agent picker only after a successful refresh", async () => { |
| 308 | + const refreshAgents = vi |
| 309 | + .fn() |
| 310 | + .mockResolvedValue({ ok: true, value: undefined }) as RefreshAgentsMock; |
| 311 | + const { handleCommand, openOverlay } = createHarness({ |
| 312 | + refreshAgents, |
| 313 | + agentDefaultId: "team-lead", |
| 314 | + agents: [ |
| 315 | + { id: "team-lead", name: "Lead Agent" }, |
| 316 | + { id: "system-agent", kind: "system", name: "System Agent" }, |
| 317 | + ], |
| 318 | + }); |
| 319 | + |
| 320 | + await handleCommand("/agents"); |
| 321 | + |
| 322 | + expect(refreshAgents).toHaveBeenCalledTimes(1); |
| 323 | + expect(openOverlay).toHaveBeenCalledTimes(1); |
| 324 | + const selector = firstMockArg(openOverlay, "openOverlay") as SelectableOverlay; |
| 325 | + expect(selector.items).toEqual([ |
| 326 | + { |
| 327 | + value: "team-lead", |
| 328 | + label: "team-lead (Lead Agent)", |
| 329 | + description: "default", |
| 330 | + }, |
| 331 | + ]); |
| 332 | + }); |
| 333 | + |
280 | 334 | it("bounds session picker hydration to recent TUI sessions", async () => { |
281 | 335 | const listSessions = vi.fn().mockResolvedValue({ |
282 | 336 | sessions: [ |
|
0 commit comments