|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { GatewayRequestContext } from "../server-methods/types.js"; |
| 3 | +import { createSyntheticPluginRuntimeClient } from "../server-plugin-runtime-client.js"; |
| 4 | +import { createInternalAgentTurnFacade } from "./internal-facade.js"; |
| 5 | + |
| 6 | +const startTurn = vi.hoisted(() => vi.fn()); |
| 7 | + |
| 8 | +vi.mock("../server-methods.js", () => ({ |
| 9 | + authorizeGatewayRequestPreDispatch: async () => ({ error: null }), |
| 10 | + createRequestGatewayMethodRegistry: () => ({ |
| 11 | + isControlPlaneWrite: () => false, |
| 12 | + }), |
| 13 | + runWithGatewayRequestEnvelope: async ( |
| 14 | + _method: string, |
| 15 | + _client: unknown, |
| 16 | + run: () => Promise<unknown>, |
| 17 | + ) => await run(), |
| 18 | +})); |
| 19 | + |
| 20 | +vi.mock("./agent-request-preflight.js", () => ({ |
| 21 | + prepareAgentRequestPreflight: ({ request }: { request: unknown }) => ({ request }), |
| 22 | +})); |
| 23 | + |
| 24 | +vi.mock("./agent-turn-service.js", () => ({ |
| 25 | + createAgentTurnService: () => ({ |
| 26 | + startTurn, |
| 27 | + waitForTurn: vi.fn(), |
| 28 | + }), |
| 29 | +})); |
| 30 | + |
| 31 | +function createFacade() { |
| 32 | + return createInternalAgentTurnFacade({ |
| 33 | + client: createSyntheticPluginRuntimeClient(), |
| 34 | + getContext: () => |
| 35 | + ({ |
| 36 | + dedupe: new Map(), |
| 37 | + getRuntimeConfig: () => ({}), |
| 38 | + logGateway: { error: vi.fn(), warn: vi.fn() }, |
| 39 | + }) as unknown as GatewayRequestContext, |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +describe("createInternalAgentTurnFacade", () => { |
| 44 | + beforeEach(() => { |
| 45 | + startTurn.mockReset(); |
| 46 | + }); |
| 47 | + |
| 48 | + it("preserves accepted/final ordering and acceptance metadata without frames", async () => { |
| 49 | + let emitFinal!: () => void; |
| 50 | + const finalGate = new Promise<void>((resolve) => { |
| 51 | + emitFinal = resolve; |
| 52 | + }); |
| 53 | + startTurn.mockImplementation(async ({ io }) => { |
| 54 | + io.emitAcceptance([true, { runId: "run-1", status: "accepted" }, undefined], { |
| 55 | + runId: "run-1", |
| 56 | + }); |
| 57 | + await finalGate; |
| 58 | + io.emitFinal([true, { runId: "run-1", status: "ok", summary: "done" }, undefined], { |
| 59 | + runId: "run-1", |
| 60 | + terminal: true, |
| 61 | + }); |
| 62 | + }); |
| 63 | + const onAccepted = vi.fn(); |
| 64 | + |
| 65 | + const result = createFacade().dispatchRaw( |
| 66 | + { message: "test", idempotencyKey: "run-1" }, |
| 67 | + { expectFinal: true, onAccepted }, |
| 68 | + ); |
| 69 | + await vi.waitFor(() => |
| 70 | + expect(onAccepted).toHaveBeenCalledWith({ |
| 71 | + runId: "run-1", |
| 72 | + status: "accepted", |
| 73 | + }), |
| 74 | + ); |
| 75 | + emitFinal(); |
| 76 | + |
| 77 | + await expect(result).resolves.toEqual({ |
| 78 | + ok: true, |
| 79 | + payload: { runId: "run-1", status: "ok", summary: "done" }, |
| 80 | + error: undefined, |
| 81 | + meta: { runId: "run-1", terminal: true }, |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it("returns a single acceptance with its metadata when no final is requested", async () => { |
| 86 | + startTurn.mockImplementation(async ({ io }) => { |
| 87 | + io.emitAcceptance([true, { runId: "run-2", status: "in_flight" }, undefined], { |
| 88 | + cached: true, |
| 89 | + runId: "run-2", |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + await expect( |
| 94 | + createFacade().dispatchRaw({ message: "test", idempotencyKey: "run-2" }), |
| 95 | + ).resolves.toEqual({ |
| 96 | + ok: true, |
| 97 | + payload: { runId: "run-2", status: "in_flight" }, |
| 98 | + error: undefined, |
| 99 | + meta: { cached: true, runId: "run-2" }, |
| 100 | + }); |
| 101 | + }); |
| 102 | +}); |
0 commit comments