|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { SessionsListParams } from "../../../packages/gateway-protocol/src/index.js"; |
| 3 | +import type { ModelCatalogEntry } from "../../agents/model-catalog.types.js"; |
| 4 | +import { upsertSessionEntryCore } from "../../config/sessions/session-accessor.js"; |
| 5 | +import type { OpenClawConfig } from "../../config/types.openclaw.js"; |
| 6 | +import { resetAgentEventsForTest } from "../../infra/agent-events.js"; |
| 7 | +import { withOpenClawTestState } from "../../test-utils/openclaw-test-state.js"; |
| 8 | +import type { GatewaySessionRow } from "../session-utils.types.js"; |
| 9 | +import type { GatewayClient, GatewayRequestContext, RespondFn } from "./types.js"; |
| 10 | + |
| 11 | +const { sessionReadHandlers } = await import("./sessions-read.js"); |
| 12 | + |
| 13 | +function identifiedClient(profileId: string): GatewayClient { |
| 14 | + return { |
| 15 | + connect: { |
| 16 | + minProtocol: 1, |
| 17 | + maxProtocol: 1, |
| 18 | + client: { id: "openclaw-control-ui", version: "test", platform: "test", mode: "webchat" }, |
| 19 | + role: "operator", |
| 20 | + scopes: ["operator.read", "operator.write"], |
| 21 | + }, |
| 22 | + authenticatedUserProfile: { |
| 23 | + profileId, |
| 24 | + displayName: profileId, |
| 25 | + hasAvatar: false, |
| 26 | + updatedAt: 1, |
| 27 | + }, |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +function requestContext(config: OpenClawConfig): GatewayRequestContext { |
| 32 | + return { |
| 33 | + chatAbortControllers: new Map(), |
| 34 | + getRuntimeConfig: () => config, |
| 35 | + getSessionEventSubscriberConnIds: () => new Set(), |
| 36 | + loadGatewayModelCatalog: async () => [], |
| 37 | + logGateway: { debug: vi.fn() }, |
| 38 | + } as unknown as GatewayRequestContext; |
| 39 | +} |
| 40 | + |
| 41 | +async function listSessions(params: { |
| 42 | + client: GatewayClient; |
| 43 | + context: GatewayRequestContext; |
| 44 | + request: SessionsListParams; |
| 45 | +}) { |
| 46 | + const responses: Parameters<RespondFn>[] = []; |
| 47 | + await sessionReadHandlers["sessions.list"]?.({ |
| 48 | + params: params.request, |
| 49 | + client: params.client, |
| 50 | + context: params.context, |
| 51 | + respond: (...response: Parameters<RespondFn>) => responses.push(response), |
| 52 | + } as never); |
| 53 | + expect(responses).toHaveLength(1); |
| 54 | + expect(responses[0]?.[0]).toBe(true); |
| 55 | + return responses[0]?.[1] as { |
| 56 | + count: number; |
| 57 | + nextOffset: number | null; |
| 58 | + sessions: GatewaySessionRow[]; |
| 59 | + totalCount: number; |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +async function seedSessions(): Promise<OpenClawConfig> { |
| 64 | + const config: OpenClawConfig = { |
| 65 | + agents: { list: [{ id: "main", default: true }, { id: "work" }] }, |
| 66 | + }; |
| 67 | + await upsertSessionEntryCore( |
| 68 | + { agentId: "main", sessionKey: "agent:main:active" }, |
| 69 | + { |
| 70 | + sessionId: "main-active", |
| 71 | + updatedAt: 400, |
| 72 | + createdActor: { type: "human", id: "[email protected]" }, |
| 73 | + visibility: "shared", |
| 74 | + }, |
| 75 | + ); |
| 76 | + await upsertSessionEntryCore( |
| 77 | + { agentId: "work", sessionKey: "agent:work:active" }, |
| 78 | + { |
| 79 | + sessionId: "work-active", |
| 80 | + updatedAt: 100, |
| 81 | + createdActor: { type: "human", id: "[email protected]" }, |
| 82 | + visibility: "shared", |
| 83 | + }, |
| 84 | + ); |
| 85 | + return config; |
| 86 | +} |
| 87 | + |
| 88 | +beforeEach(() => { |
| 89 | + resetAgentEventsForTest(); |
| 90 | +}); |
| 91 | + |
| 92 | +afterEach(() => { |
| 93 | + resetAgentEventsForTest(); |
| 94 | + vi.restoreAllMocks(); |
| 95 | +}); |
| 96 | + |
| 97 | +describe("sessions.list catalog scoping", () => { |
| 98 | + it("keeps unscoped listings owner-scoped when agents have distinct completed catalogs", async () => { |
| 99 | + await withOpenClawTestState({ scenario: "minimal" }, async () => { |
| 100 | + const config = await seedSessions(); |
| 101 | + config.agents = { |
| 102 | + ...config.agents, |
| 103 | + defaults: { model: { primary: "dynamic-router/reasoner" } }, |
| 104 | + }; |
| 105 | + const mainCatalog: ModelCatalogEntry[] = [ |
| 106 | + { |
| 107 | + provider: "dynamic-router", |
| 108 | + id: "reasoner", |
| 109 | + name: "Reasoner", |
| 110 | + reasoning: true, |
| 111 | + compat: { supportedReasoningEfforts: ["low", "high", "max"] }, |
| 112 | + }, |
| 113 | + ]; |
| 114 | + const workCatalog: ModelCatalogEntry[] = [ |
| 115 | + { |
| 116 | + provider: "dynamic-router", |
| 117 | + id: "reasoner", |
| 118 | + name: "Reasoner", |
| 119 | + reasoning: true, |
| 120 | + compat: { supportedReasoningEfforts: ["medium"] }, |
| 121 | + }, |
| 122 | + ]; |
| 123 | + const context = { |
| 124 | + ...requestContext(config), |
| 125 | + readPreparedGatewayModelCatalog: vi.fn(async (options?: { agentId?: string }) => |
| 126 | + options?.agentId === "work" ? workCatalog : mainCatalog, |
| 127 | + ), |
| 128 | + }; |
| 129 | + const client = identifiedClient("[email protected]"); |
| 130 | + const request = { archived: "all" as const, limit: 100 }; |
| 131 | + |
| 132 | + const result = await listSessions({ client, context, request }); |
| 133 | + |
| 134 | + const mainRow = result.sessions.find((session) => session.agentId === "main"); |
| 135 | + const workRow = result.sessions.find((session) => session.agentId === "work"); |
| 136 | + expect(mainRow).toBeDefined(); |
| 137 | + expect(workRow).toBeDefined(); |
| 138 | + expect(mainRow?.thinkingOptions).toEqual( |
| 139 | + expect.arrayContaining(["off", "low", "high", "max"]), |
| 140 | + ); |
| 141 | + expect(workRow?.thinkingOptions).toEqual(expect.arrayContaining(["off", "medium"])); |
| 142 | + expect(workRow?.thinkingOptions).not.toEqual(expect.arrayContaining(["low", "high", "max"])); |
| 143 | + expect(context.readPreparedGatewayModelCatalog).toHaveBeenCalledWith({ |
| 144 | + agentId: "main", |
| 145 | + }); |
| 146 | + expect(context.readPreparedGatewayModelCatalog).toHaveBeenCalledWith({ |
| 147 | + agentId: "work", |
| 148 | + }); |
| 149 | + }); |
| 150 | + }); |
| 151 | + |
| 152 | + it("uses only the requested agent's catalog for scoped listings", async () => { |
| 153 | + await withOpenClawTestState({ scenario: "minimal" }, async () => { |
| 154 | + const config = await seedSessions(); |
| 155 | + config.agents = { |
| 156 | + ...config.agents, |
| 157 | + defaults: { model: { primary: "dynamic-router/reasoner" } }, |
| 158 | + }; |
| 159 | + const mainCatalog: ModelCatalogEntry[] = [ |
| 160 | + { |
| 161 | + provider: "dynamic-router", |
| 162 | + id: "reasoner", |
| 163 | + name: "Reasoner", |
| 164 | + reasoning: true, |
| 165 | + compat: { supportedReasoningEfforts: ["low", "high", "max"] }, |
| 166 | + }, |
| 167 | + ]; |
| 168 | + const context = { |
| 169 | + ...requestContext(config), |
| 170 | + readPreparedGatewayModelCatalog: vi.fn(async () => mainCatalog), |
| 171 | + }; |
| 172 | + const client = identifiedClient("[email protected]"); |
| 173 | + |
| 174 | + const result = await listSessions({ |
| 175 | + client, |
| 176 | + context, |
| 177 | + request: { agentId: "main", archived: "all" as const, limit: 100 }, |
| 178 | + }); |
| 179 | + |
| 180 | + expect(result.sessions.every((session) => session.agentId === "main")).toBe(true); |
| 181 | + expect(result.sessions[0]?.thinkingOptions).toEqual( |
| 182 | + expect.arrayContaining(["off", "low", "high", "max"]), |
| 183 | + ); |
| 184 | + expect(context.readPreparedGatewayModelCatalog).toHaveBeenCalledTimes(1); |
| 185 | + expect(context.readPreparedGatewayModelCatalog).toHaveBeenCalledWith({ |
| 186 | + agentId: "main", |
| 187 | + }); |
| 188 | + }); |
| 189 | + }); |
| 190 | +}); |
0 commit comments