|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { GatewayClientRequestError } from "../../../packages/gateway-client/src/request-error.js"; |
| 3 | +import { |
| 4 | + ErrorCodes, |
| 5 | + GatewayErrorDetailCodes, |
| 6 | +} from "../../../packages/gateway-protocol/src/gateway-error-details.js"; |
| 7 | +import { createMessageTool } from "./message-tool-execution.js"; |
| 8 | + |
| 9 | +const EMPTY_CATALOG = { |
| 10 | + version: 0, |
| 11 | + channels: [], |
| 12 | + getChannel: () => undefined, |
| 13 | +} as const; |
| 14 | + |
| 15 | +function createFailingMessageTool(error: Error) { |
| 16 | + const runMessageAction = vi.fn(async () => { |
| 17 | + throw error; |
| 18 | + }); |
| 19 | + const tool = createMessageTool({ |
| 20 | + config: {}, |
| 21 | + runId: "run-queued-delivery", |
| 22 | + preparedMessageToolCatalog: EMPTY_CATALOG, |
| 23 | + sourceReplyOnly: true, |
| 24 | + sourceReplyDeliveryMode: "message_tool_only", |
| 25 | + currentChannelProvider: "telegram", |
| 26 | + currentChannelId: "chat-123", |
| 27 | + currentMessagingTarget: "chat-123", |
| 28 | + getScopedChannelsCommandSecretTargets: () => ({ targetIds: new Set<string>() }), |
| 29 | + resolveCommandSecretRefsViaGateway: async ({ config }) => ({ |
| 30 | + resolvedConfig: config, |
| 31 | + diagnostics: [], |
| 32 | + targetStatesByPath: {}, |
| 33 | + hadUnresolvedTargets: false, |
| 34 | + }), |
| 35 | + runMessageAction, |
| 36 | + }); |
| 37 | + return { tool, runMessageAction }; |
| 38 | +} |
| 39 | + |
| 40 | +function sentIdempotencyKey(runMessageAction: ReturnType<typeof vi.fn>, call: number) { |
| 41 | + const request = runMessageAction.mock.calls[call]?.[0] as |
| 42 | + | { params?: { idempotencyKey?: string } } |
| 43 | + | undefined; |
| 44 | + return request?.params?.idempotencyKey; |
| 45 | +} |
| 46 | + |
| 47 | +describe("message tool queued gateway delivery", () => { |
| 48 | + it("returns a do-not-resend result when the gateway owns the retry", async () => { |
| 49 | + const { tool, runMessageAction } = createFailingMessageTool( |
| 50 | + new GatewayClientRequestError({ |
| 51 | + code: ErrorCodes.UNAVAILABLE, |
| 52 | + message: "connect ECONNREFUSED", |
| 53 | + details: { code: GatewayErrorDetailCodes.OUTBOUND_DELIVERY_QUEUED }, |
| 54 | + }), |
| 55 | + ); |
| 56 | + |
| 57 | + const result = await tool.execute("queued-send", { action: "send", message: "hello" }); |
| 58 | + |
| 59 | + expect(result).toMatchObject({ |
| 60 | + details: { |
| 61 | + status: "delivery_queued", |
| 62 | + delivered: false, |
| 63 | + message: |
| 64 | + "Message not delivered: connect ECONNREFUSED. The gateway queued it and will retry automatically. Do not resend it.", |
| 65 | + }, |
| 66 | + }); |
| 67 | + |
| 68 | + // A model that resends anyway must reuse the queued key so the gateway's |
| 69 | + // idempotency cache answers instead of a second durable send. |
| 70 | + await tool.execute("queued-send-again", { action: "send", message: "hello" }); |
| 71 | + expect(sentIdempotencyKey(runMessageAction, 0)).toBeDefined(); |
| 72 | + expect(sentIdempotencyKey(runMessageAction, 1)).toBe(sentIdempotencyKey(runMessageAction, 0)); |
| 73 | + }); |
| 74 | + |
| 75 | + it("keeps an unstructured unavailable error throwable", async () => { |
| 76 | + const error = new GatewayClientRequestError({ |
| 77 | + code: ErrorCodes.UNAVAILABLE, |
| 78 | + message: "connect ECONNREFUSED", |
| 79 | + }); |
| 80 | + const { tool } = createFailingMessageTool(error); |
| 81 | + |
| 82 | + await expect( |
| 83 | + tool.execute("ordinary-failure", { action: "send", message: "hello" }), |
| 84 | + ).rejects.toBe(error); |
| 85 | + }); |
| 86 | +}); |
0 commit comments