|
| 1 | +// Signal durable-ingress replay proof: a transient Gateway failure during |
| 2 | +// approval-reaction resolution must release the real SQLite claim so the real |
| 3 | +// drain redelivers the operator's reaction once the Gateway recovers. |
| 4 | +// Everything here is real (SQLite queue, monitor, drain, event handler, |
| 5 | +// approval-reaction resolution) except the one true external boundary: the |
| 6 | +// Gateway approval-resolution call. |
| 7 | +import fs from "node:fs/promises"; |
| 8 | +import os from "node:os"; |
| 9 | +import path from "node:path"; |
| 10 | +import { |
| 11 | + closeOpenClawStateDatabaseForTest, |
| 12 | + createChannelIngressQueueForTests, |
| 13 | +} from "openclaw/plugin-sdk/plugin-state-test-runtime"; |
| 14 | +import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; |
| 15 | +import { |
| 16 | + clearSignalApprovalReactionTargetsForTest, |
| 17 | + registerSignalApprovalReactionTarget, |
| 18 | +} from "./approval-reactions.js"; |
| 19 | +import { startSignalIngressMonitor } from "./signal-ingress.js"; |
| 20 | + |
| 21 | +const resolverMocks = vi.hoisted(() => ({ |
| 22 | + resolveSignalApproval: vi.fn(), |
| 23 | + isApprovalNotFoundError: vi.fn(() => false), |
| 24 | +})); |
| 25 | + |
| 26 | +vi.mock("openclaw/plugin-sdk/approval-gateway-runtime", () => ({ |
| 27 | + resolveApprovalOverGateway: resolverMocks.resolveSignalApproval, |
| 28 | +})); |
| 29 | +vi.mock("openclaw/plugin-sdk/error-runtime", () => ({ |
| 30 | + isApprovalNotFoundError: resolverMocks.isApprovalNotFoundError, |
| 31 | +})); |
| 32 | + |
| 33 | +vi.useRealTimers(); |
| 34 | +let createBaseSignalEventHandlerDeps: typeof import("./monitor/event-handler.test-harness.js").createBaseSignalEventHandlerDeps; |
| 35 | +let createSignalReceiveEvent: typeof import("./monitor/event-handler.test-harness.js").createSignalReceiveEvent; |
| 36 | +let createSignalEventHandler: typeof import("./monitor/event-handler.js").createSignalEventHandler; |
| 37 | + |
| 38 | +type SignalIngressQueue = NonNullable<Parameters<typeof startSignalIngressMonitor>[0]["queue"]>; |
| 39 | +type SignalIngressPayload = Parameters<SignalIngressQueue["enqueue"]>[1]; |
| 40 | + |
| 41 | +beforeAll(async () => { |
| 42 | + const harness = await import("./monitor/event-handler.test-harness.js"); |
| 43 | + createBaseSignalEventHandlerDeps = harness.createBaseSignalEventHandlerDeps; |
| 44 | + createSignalReceiveEvent = harness.createSignalReceiveEvent; |
| 45 | + ({ createSignalEventHandler } = await import("./monitor/event-handler.js")); |
| 46 | +}); |
| 47 | + |
| 48 | +beforeEach(() => { |
| 49 | + clearSignalApprovalReactionTargetsForTest(); |
| 50 | + resolverMocks.resolveSignalApproval.mockReset(); |
| 51 | + resolverMocks.isApprovalNotFoundError.mockReset(); |
| 52 | + resolverMocks.isApprovalNotFoundError.mockReturnValue(false); |
| 53 | +}); |
| 54 | + |
| 55 | +afterEach(() => { |
| 56 | + closeOpenClawStateDatabaseForTest(); |
| 57 | + vi.restoreAllMocks(); |
| 58 | +}); |
| 59 | + |
| 60 | +async function withQueue<T>(fn: (queue: SignalIngressQueue) => Promise<T>): Promise<T> { |
| 61 | + const created = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-signal-ingress-replay-")); |
| 62 | + const stateDir = await fs.realpath(created); |
| 63 | + const queue = createChannelIngressQueueForTests<SignalIngressPayload>({ |
| 64 | + channelId: "signal", |
| 65 | + accountId: "default", |
| 66 | + stateDir, |
| 67 | + }); |
| 68 | + try { |
| 69 | + return await fn(queue); |
| 70 | + } finally { |
| 71 | + closeOpenClawStateDatabaseForTest(); |
| 72 | + await fs.rm(stateDir, { recursive: true, force: true }); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +describe("Signal approval reaction durable replay", () => { |
| 77 | + it( |
| 78 | + "releases the claim and replays a reaction after a transient Gateway failure", |
| 79 | + { timeout: 30_000 }, |
| 80 | + async () => { |
| 81 | + await withQueue(async (queue) => { |
| 82 | + // First resolution attempt fails like a transient Gateway 503; the |
| 83 | + // redelivered attempt resolves the approval. |
| 84 | + resolverMocks.resolveSignalApproval |
| 85 | + .mockRejectedValueOnce(new Error("gateway 503")) |
| 86 | + .mockResolvedValue({ |
| 87 | + applied: true, |
| 88 | + approval: { status: "allowed", decision: "allow-once" }, |
| 89 | + }); |
| 90 | + registerSignalApprovalReactionTarget({ |
| 91 | + accountId: "default", |
| 92 | + // normalizeE164 preserves '+', matching the conversationKey the handler |
| 93 | + // derives from the sender (signal:+15550001111). |
| 94 | + conversationKey: "+15550001111", |
| 95 | + messageId: "1700000000123", |
| 96 | + approvalId: "approval-abc-123", |
| 97 | + approvalKind: "exec", |
| 98 | + allowedDecisions: ["allow-once"], |
| 99 | + targetAuthorKeys: ["+15550001111"], |
| 100 | + route: { deliveryMode: "session" }, |
| 101 | + routeAllowed: true, |
| 102 | + }); |
| 103 | + |
| 104 | + const handler = createSignalEventHandler( |
| 105 | + createBaseSignalEventHandlerDeps({ |
| 106 | + cfg: { |
| 107 | + channels: { signal: { allowFrom: ["+15550001111"] } }, |
| 108 | + approvals: { exec: { enabled: true, mode: "session" } }, |
| 109 | + }, |
| 110 | + isSignalReactionMessage: (reaction) => reaction != null, |
| 111 | + }), |
| 112 | + ); |
| 113 | + const monitor = await startSignalIngressMonitor({ |
| 114 | + accountId: "default", |
| 115 | + queue, |
| 116 | + dispatch: handler, |
| 117 | + runtime: { error: vi.fn(), log: vi.fn() }, |
| 118 | + }); |
| 119 | + const reactionEvent = createSignalReceiveEvent({ |
| 120 | + sourceNumber: "+15550001111", |
| 121 | + reactionMessage: { |
| 122 | + emoji: "👍", |
| 123 | + targetAuthor: "+15550001111", |
| 124 | + targetSentTimestamp: 1700000000123, |
| 125 | + }, |
| 126 | + }); |
| 127 | + |
| 128 | + try { |
| 129 | + await monitor.receive(reactionEvent); |
| 130 | + // First attempt: the handler's throw escapes into the real drain. |
| 131 | + await vi.waitFor( |
| 132 | + () => expect(resolverMocks.resolveSignalApproval).toHaveBeenCalledTimes(1), |
| 133 | + { timeout: 5_000 }, |
| 134 | + ); |
| 135 | + await monitor.waitForIdle(); |
| 136 | + |
| 137 | + // The drain released the claim instead of completing it: the durable |
| 138 | + // row is pending again with the recorded attempt and error. The retry |
| 139 | + // backoff (>=1s from the release) keeps the lane unclaimed here, so |
| 140 | + // this is a produced-state read, never a race against redelivery. |
| 141 | + expect(await queue.listClaims()).toHaveLength(0); |
| 142 | + const pendingAfterFailure = await queue.listPending({ limit: "all" }); |
| 143 | + expect(pendingAfterFailure).toHaveLength(1); |
| 144 | + expect(pendingAfterFailure[0]?.attempts).toBe(1); |
| 145 | + expect(pendingAfterFailure[0]?.lastError).toContain("gateway 503"); |
| 146 | + |
| 147 | + // The real drain reclaims and redelivers the same reaction event. |
| 148 | + await vi.waitFor( |
| 149 | + () => expect(resolverMocks.resolveSignalApproval).toHaveBeenCalledTimes(2), |
| 150 | + { timeout: 15_000 }, |
| 151 | + ); |
| 152 | + await monitor.waitForIdle(); |
| 153 | + |
| 154 | + // Redelivery carried the identical resolution request. |
| 155 | + expect(resolverMocks.resolveSignalApproval.mock.calls[1]?.[0]).toEqual( |
| 156 | + resolverMocks.resolveSignalApproval.mock.calls[0]?.[0], |
| 157 | + ); |
| 158 | + expect(resolverMocks.resolveSignalApproval.mock.calls[1]?.[0]).toEqual( |
| 159 | + expect.objectContaining({ |
| 160 | + approvalId: "approval-abc-123", |
| 161 | + approvalKind: "exec", |
| 162 | + decision: "allow-once", |
| 163 | + channel: "signal", |
| 164 | + accountId: "default", |
| 165 | + }), |
| 166 | + ); |
| 167 | + |
| 168 | + // The replayed attempt resolved the approval and completed the row. |
| 169 | + expect(await queue.listPending({ limit: "all" })).toHaveLength(0); |
| 170 | + expect(await queue.listClaims()).toHaveLength(0); |
| 171 | + |
| 172 | + // The completion tombstone is durable: the same reaction can never |
| 173 | + // dispatch a third time. |
| 174 | + await monitor.receive(reactionEvent); |
| 175 | + await monitor.waitForIdle(); |
| 176 | + expect(resolverMocks.resolveSignalApproval).toHaveBeenCalledTimes(2); |
| 177 | + } finally { |
| 178 | + await monitor.stop(); |
| 179 | + } |
| 180 | + }); |
| 181 | + }, |
| 182 | + ); |
| 183 | +}); |
0 commit comments