|
5 | 5 | type GatewayProtocolSocketHandlers, |
6 | 6 | } from "@openclaw/gateway-client/browser"; |
7 | 7 | import { describe, expect, it, vi } from "vitest"; |
| 8 | +import { createDeferred } from "../../../../test/helpers/promise.js"; |
8 | 9 | import { |
9 | 10 | GatewayRequestError, |
10 | 11 | type GatewayBrowserClient, |
@@ -113,6 +114,116 @@ const targetedSessionReconciliationCases = [ |
113 | 114 | ]; |
114 | 115 |
|
115 | 116 | describe("session connection hydration", () => { |
| 117 | + it("publishes the signed-in owner's roster before loading the shared roster", async () => { |
| 118 | + const ownerResult: SessionsListResult = { |
| 119 | + ...emptySessionsResult(), |
| 120 | + count: 2, |
| 121 | + sessions: [ |
| 122 | + { |
| 123 | + key: "agent:main:mine-new", |
| 124 | + kind: "direct", |
| 125 | + updatedAt: 3, |
| 126 | + createdActor: { type: "human", id: "operator" }, |
| 127 | + }, |
| 128 | + { |
| 129 | + key: "agent:main:mine-old", |
| 130 | + kind: "direct", |
| 131 | + updatedAt: 1, |
| 132 | + createdActor: { type: "human", id: "operator" }, |
| 133 | + }, |
| 134 | + ], |
| 135 | + }; |
| 136 | + const sharedResult: SessionsListResult = { |
| 137 | + ...emptySessionsResult(), |
| 138 | + count: 2, |
| 139 | + sessions: [ |
| 140 | + ownerResult.sessions[0]!, |
| 141 | + { |
| 142 | + key: "agent:main:theirs", |
| 143 | + kind: "direct", |
| 144 | + updatedAt: 2, |
| 145 | + createdActor: { type: "human", id: "collaborator" }, |
| 146 | + }, |
| 147 | + ], |
| 148 | + }; |
| 149 | + const ownerList = createDeferred<SessionsListResult>(); |
| 150 | + const sharedList = createDeferred<SessionsListResult>(); |
| 151 | + const queuedList = createDeferred<SessionsListResult>(); |
| 152 | + const request = vi.fn(async (method: string, params?: Record<string, unknown>) => { |
| 153 | + if (method === "sessions.subscribe") { |
| 154 | + return { subscribed: true }; |
| 155 | + } |
| 156 | + if (method === "sessions.list") { |
| 157 | + if (params?.ownerId === "operator") { |
| 158 | + return await ownerList.promise; |
| 159 | + } |
| 160 | + return params?.search === "queued" ? await queuedList.promise : await sharedList.promise; |
| 161 | + } |
| 162 | + throw new Error(`Unexpected request: ${method}`); |
| 163 | + }); |
| 164 | + const client = { request } as unknown as GatewayBrowserClient; |
| 165 | + let snapshot = { |
| 166 | + client: null as GatewayBrowserClient | null, |
| 167 | + phase: "reconnecting" as "connected" | "reconnecting", |
| 168 | + sessionKey: "agent:main:mine-new", |
| 169 | + assistantAgentId: "main" as string | null, |
| 170 | + hello: null as GatewayHelloOk | null, |
| 171 | + selfUser: { id: "operator", name: "Operator" }, |
| 172 | + }; |
| 173 | + let gatewayListener: ((next: typeof snapshot) => void) | undefined; |
| 174 | + const sessions = createSessionCapability({ |
| 175 | + get snapshot() { |
| 176 | + return snapshot; |
| 177 | + }, |
| 178 | + subscribe(listener) { |
| 179 | + gatewayListener = listener; |
| 180 | + return () => undefined; |
| 181 | + }, |
| 182 | + subscribeEvents: () => () => undefined, |
| 183 | + }); |
| 184 | + |
| 185 | + snapshot = { ...snapshot, client, phase: "connected" }; |
| 186 | + gatewayListener?.(snapshot); |
| 187 | + |
| 188 | + await waitForFast(() => |
| 189 | + expect(request).toHaveBeenCalledWith( |
| 190 | + "sessions.list", |
| 191 | + expect.objectContaining({ ownerId: "operator", limit: 60 }), |
| 192 | + ), |
| 193 | + ); |
| 194 | + expect(request.mock.calls.filter(([method]) => method === "sessions.list")).toHaveLength(1); |
| 195 | + const queuedRefresh = sessions.refresh({ agentId: "other", search: "queued", force: true }); |
| 196 | + |
| 197 | + ownerList.resolve(ownerResult); |
| 198 | + await waitForFast(() => expect(sessions.state.result).toBe(ownerResult)); |
| 199 | + await waitForFast(() => |
| 200 | + expect(request.mock.calls.filter(([method]) => method === "sessions.list")).toHaveLength(2), |
| 201 | + ); |
| 202 | + expect(sessions.state.loading).toBe(false); |
| 203 | + expect(request.mock.calls.at(-1)?.[1]).toEqual( |
| 204 | + expect.objectContaining({ agentId: "main", limit: 60 }), |
| 205 | + ); |
| 206 | + expect(request.mock.calls.at(-1)?.[1]).not.toHaveProperty("ownerId"); |
| 207 | + |
| 208 | + sharedList.resolve(sharedResult); |
| 209 | + await waitForFast(() => |
| 210 | + expect(sessions.state.result?.sessions.map((row) => row.key)).toEqual([ |
| 211 | + "agent:main:mine-new", |
| 212 | + "agent:main:mine-old", |
| 213 | + "agent:main:theirs", |
| 214 | + ]), |
| 215 | + ); |
| 216 | + await waitForFast(() => |
| 217 | + expect(request.mock.calls.filter(([method]) => method === "sessions.list")).toHaveLength(3), |
| 218 | + ); |
| 219 | + expect(request.mock.calls.at(-1)?.[1]).toEqual( |
| 220 | + expect.objectContaining({ agentId: "other", search: "queued" }), |
| 221 | + ); |
| 222 | + queuedList.resolve(emptySessionsResult()); |
| 223 | + await queuedRefresh; |
| 224 | + sessions.dispose(); |
| 225 | + }); |
| 226 | + |
116 | 227 | it("uses the selected agent before a session key is available", async () => { |
117 | 228 | const result: SessionsListResult = { |
118 | 229 | ts: 1, |
|
0 commit comments