|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import type { GatewayBrowserClient } from "../../api/gateway.ts"; |
| 3 | +import type { ApplicationContext, ApplicationGatewaySnapshot } from "../../app/context.ts"; |
| 4 | +import type { NewSessionRouteData } from "./location.ts"; |
| 5 | +import { page } from "./route.ts"; |
| 6 | + |
| 7 | +// The loader is exercised through the page contract so route.ts keeps its |
| 8 | +// internals unexported; new-session routes never resolve to RouteNotFound. |
| 9 | +const loadNewSessionData = (context: ApplicationContext, search: string) => |
| 10 | + page.loader?.(context, { location: { search } } as never) as Promise<NewSessionRouteData>; |
| 11 | + |
| 12 | +function createContext(params: { |
| 13 | + assistantAgentId: string | null; |
| 14 | + agentsList: ApplicationContext["agents"]["state"]["agentsList"]; |
| 15 | + staleRosterClient?: boolean; |
| 16 | +}) { |
| 17 | + const request = vi.fn(async (method: string) => { |
| 18 | + if (method !== "sessions.catalog.list") { |
| 19 | + throw new Error(`unexpected request: ${method}`); |
| 20 | + } |
| 21 | + return { catalogs: [] }; |
| 22 | + }); |
| 23 | + const client = { request } as unknown as GatewayBrowserClient; |
| 24 | + const snapshot: ApplicationGatewaySnapshot = { |
| 25 | + client, |
| 26 | + phase: "connected", |
| 27 | + offlineStable: false, |
| 28 | + canvasPluginSurfaceUrl: null, |
| 29 | + hello: { |
| 30 | + type: "hello-ok", |
| 31 | + protocol: 1, |
| 32 | + auth: { role: "operator", scopes: [] }, |
| 33 | + features: { methods: ["sessions.catalog.list"] }, |
| 34 | + }, |
| 35 | + assistantAgentId: params.assistantAgentId, |
| 36 | + sessionKey: "main", |
| 37 | + lastError: null, |
| 38 | + lastErrorCode: null, |
| 39 | + }; |
| 40 | + const ensureList = vi.fn(async () => params.agentsList); |
| 41 | + const agentsState = { |
| 42 | + client: params.staleRosterClient |
| 43 | + ? ({ request: vi.fn() } as unknown as GatewayBrowserClient) |
| 44 | + : client, |
| 45 | + connected: true, |
| 46 | + agentsList: params.agentsList, |
| 47 | + }; |
| 48 | + const context = { |
| 49 | + gateway: { snapshot }, |
| 50 | + agents: { |
| 51 | + state: agentsState, |
| 52 | + ensureList, |
| 53 | + }, |
| 54 | + } as unknown as ApplicationContext; |
| 55 | + return { agentsState, client, context, ensureList, request }; |
| 56 | +} |
| 57 | + |
| 58 | +describe("new-session route catalog target", () => { |
| 59 | + it("defers an unvalidated route agent before roster hydration", async () => { |
| 60 | + const { context, request } = createContext({ |
| 61 | + assistantAgentId: "roboclaw", |
| 62 | + agentsList: null, |
| 63 | + }); |
| 64 | + |
| 65 | + const data = await loadNewSessionData(context, "?agent=main&catalog=claude"); |
| 66 | + |
| 67 | + expect(data.agentId).toBe(""); |
| 68 | + expect(request).not.toHaveBeenCalled(); |
| 69 | + }); |
| 70 | + |
| 71 | + it("reconciles a retired route agent against the loaded roster", async () => { |
| 72 | + const { context, request } = createContext({ |
| 73 | + assistantAgentId: "main", |
| 74 | + agentsList: { |
| 75 | + defaultId: "roboclaw", |
| 76 | + mainKey: "main", |
| 77 | + scope: "agent", |
| 78 | + agents: [{ id: "roboclaw" }], |
| 79 | + }, |
| 80 | + }); |
| 81 | + |
| 82 | + const data = await loadNewSessionData(context, "?agent=main&catalog=claude"); |
| 83 | + |
| 84 | + expect(data.agentId).toBe("roboclaw"); |
| 85 | + expect(request).toHaveBeenCalledWith("sessions.catalog.list", { |
| 86 | + agentId: "roboclaw", |
| 87 | + catalogId: "claude", |
| 88 | + limitPerHost: 1, |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it("defers catalog retries when neither roster nor hello supplies an agent", async () => { |
| 93 | + const { context, request } = createContext({ assistantAgentId: null, agentsList: null }); |
| 94 | + |
| 95 | + const data = await loadNewSessionData(context, "?agent=main&catalog=claude"); |
| 96 | + |
| 97 | + expect(data.agentId).toBe(""); |
| 98 | + expect(request).not.toHaveBeenCalled(); |
| 99 | + }); |
| 100 | + |
| 101 | + it("waits for the replacement client's roster before preserving a valid route agent", async () => { |
| 102 | + const { agentsState, client, context, request } = createContext({ |
| 103 | + assistantAgentId: "roboclaw", |
| 104 | + agentsList: { |
| 105 | + defaultId: "main", |
| 106 | + mainKey: "main", |
| 107 | + scope: "agent", |
| 108 | + agents: [{ id: "main" }], |
| 109 | + }, |
| 110 | + staleRosterClient: true, |
| 111 | + }); |
| 112 | + |
| 113 | + const pending = await loadNewSessionData(context, "?agent=research&catalog=claude"); |
| 114 | + |
| 115 | + expect(pending.agentId).toBe(""); |
| 116 | + expect(request).not.toHaveBeenCalled(); |
| 117 | + |
| 118 | + agentsState.client = client; |
| 119 | + agentsState.agentsList = { |
| 120 | + defaultId: "roboclaw", |
| 121 | + mainKey: "main", |
| 122 | + scope: "agent", |
| 123 | + agents: [{ id: "roboclaw" }, { id: "research" }], |
| 124 | + }; |
| 125 | + const data = await loadNewSessionData(context, "?agent=research&catalog=claude"); |
| 126 | + |
| 127 | + expect(data.agentId).toBe("research"); |
| 128 | + expect(request).toHaveBeenCalledWith("sessions.catalog.list", { |
| 129 | + agentId: "research", |
| 130 | + catalogId: "claude", |
| 131 | + limitPerHost: 1, |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + it("reuses the current gateway client across route retries", async () => { |
| 136 | + const { client, context, request } = createContext({ |
| 137 | + assistantAgentId: "roboclaw", |
| 138 | + agentsList: null, |
| 139 | + }); |
| 140 | + |
| 141 | + await loadNewSessionData(context, "?catalog=claude"); |
| 142 | + await loadNewSessionData(context, "?catalog=claude"); |
| 143 | + |
| 144 | + expect(context.gateway.snapshot.client).toBe(client); |
| 145 | + expect(request).toHaveBeenCalledTimes(2); |
| 146 | + }); |
| 147 | +}); |
0 commit comments