|
1 | 1 | import { Value } from "typebox/value"; |
2 | 2 | import { describe, expect, it, vi } from "vitest"; |
3 | 3 | import type { BoardCommand, BoardSnapshot } from "../../../packages/gateway-protocol/src/index.js"; |
| 4 | +import { |
| 5 | + createGatewayMethodDescriptorsFromHandlers, |
| 6 | + createGatewayMethodRegistry, |
| 7 | +} from "../../gateway/methods/registry.js"; |
| 8 | +import type { |
| 9 | + GatewayRequestContext, |
| 10 | + GatewayRequestHandlerOptions, |
| 11 | + GatewayRequestHandlers, |
| 12 | +} from "../../gateway/server-methods/types.js"; |
4 | 13 | import { withPluginRuntimeGatewayRequestScope } from "../../plugins/runtime/gateway-request-scope.js"; |
5 | 14 | import { createDashboardTool } from "./dashboard-tool.js"; |
| 15 | +import { withGatewayToolCallerIdentity } from "./gateway-caller-context.js"; |
6 | 16 | import type { InProcessGatewayCaller } from "./in-process-gateway.js"; |
7 | 17 |
|
8 | 18 | const snapshot: BoardSnapshot = { |
@@ -33,6 +43,32 @@ function recorder(boardSnapshot: BoardSnapshot = snapshot) { |
33 | 43 | }; |
34 | 44 | } |
35 | 45 |
|
| 46 | +function createGatewayAffinityHarness(revision: number) { |
| 47 | + const requests: GatewayRequestHandlerOptions["req"][] = []; |
| 48 | + const broadcastToConnIds = vi.fn(); |
| 49 | + const handlers: GatewayRequestHandlers = { |
| 50 | + "board.get": ({ req, respond }) => { |
| 51 | + requests.push(req); |
| 52 | + respond(true, { ...snapshot, revision }); |
| 53 | + }, |
| 54 | + }; |
| 55 | + const methodRegistry = createGatewayMethodRegistry( |
| 56 | + createGatewayMethodDescriptorsFromHandlers({ |
| 57 | + handlers, |
| 58 | + owner: { kind: "core", area: "dashboard-affinity-test" }, |
| 59 | + defaultScope: "operator.read", |
| 60 | + }), |
| 61 | + ); |
| 62 | + const context = { |
| 63 | + broadcastToConnIds, |
| 64 | + getClientConnIds: () => new Set([`control-ui-${revision}`]), |
| 65 | + getGatewayMethodRegistry: () => methodRegistry, |
| 66 | + getRuntimeConfig: () => ({}), |
| 67 | + resolveGatewayContext: () => context, |
| 68 | + } as unknown as GatewayRequestContext; |
| 69 | + return { broadcastToConnIds, context, requests }; |
| 70 | +} |
| 71 | + |
36 | 72 | describe("dashboard tool", () => { |
37 | 73 | it("declares every action, no client capability guard, sizing, and the dashboard threshold", () => { |
38 | 74 | const tool = createDashboardTool(); |
@@ -94,6 +130,58 @@ describe("dashboard tool", () => { |
94 | 130 | }); |
95 | 131 | }); |
96 | 132 |
|
| 133 | + it("dispatches through the admitted Gateway and fences replacement or retirement", async () => { |
| 134 | + const admitted = createGatewayAffinityHarness(11); |
| 135 | + const replacement = createGatewayAffinityHarness(22); |
| 136 | + let current: GatewayRequestContext | undefined = admitted.context; |
| 137 | + const tool = createDashboardTool({ agentSessionKey: "agent:main:main" }); |
| 138 | + |
| 139 | + await withPluginRuntimeGatewayRequestScope( |
| 140 | + { |
| 141 | + context: replacement.context, |
| 142 | + isWebchatConnect: () => false, |
| 143 | + }, |
| 144 | + async () => |
| 145 | + await withGatewayToolCallerIdentity( |
| 146 | + { |
| 147 | + agentId: "main", |
| 148 | + sessionKey: "agent:main:main", |
| 149 | + gatewayContextResolver: () => current, |
| 150 | + }, |
| 151 | + async () => { |
| 152 | + const read = await tool.execute("read", { action: "read" }); |
| 153 | + const command = await tool.execute("focus", { |
| 154 | + action: "focus_tab", |
| 155 | + tabId: "main", |
| 156 | + }); |
| 157 | + expect(read.details).toMatchObject({ revision: 11 }); |
| 158 | + expect(command.details).toEqual({ ok: true, delivered: 1 }); |
| 159 | + |
| 160 | + current = replacement.context; |
| 161 | + await expect(tool.execute("late-read", { action: "read" })).rejects.toThrow( |
| 162 | + /dashboard|Gateway|gateway|unavailable/u, |
| 163 | + ); |
| 164 | + current = undefined; |
| 165 | + await expect( |
| 166 | + tool.execute("late-focus", { action: "focus_tab", tabId: "main" }), |
| 167 | + ).rejects.toThrow(/dashboard|Gateway|gateway|unavailable/u); |
| 168 | + }, |
| 169 | + ), |
| 170 | + ); |
| 171 | + |
| 172 | + expect(admitted.requests).toEqual([ |
| 173 | + expect.objectContaining({ |
| 174 | + type: "req", |
| 175 | + id: expect.stringMatching(/^plugin-subagent-/u), |
| 176 | + method: "board.get", |
| 177 | + params: expect.objectContaining({ sessionKey: "agent:main:main" }), |
| 178 | + }), |
| 179 | + ]); |
| 180 | + expect(replacement.requests).toEqual([]); |
| 181 | + expect(admitted.broadcastToConnIds).toHaveBeenCalledOnce(); |
| 182 | + expect(replacement.broadcastToConnIds).not.toHaveBeenCalled(); |
| 183 | + }); |
| 184 | + |
97 | 185 | it("returns content ownership and valid update paths in model-visible snapshot details", async () => { |
98 | 186 | const widget = ( |
99 | 187 | name: string, |
|
0 commit comments