|
| 1 | +// Exercises restart-notice retries against the real SQLite outbound queue. |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js"; |
| 4 | +import { getDeliveryQueueEntryStatus } from "../infra/delivery-queue-sqlite.js"; |
| 5 | +import { PlatformMessageNotDispatchedError } from "../infra/outbound/deliver-types.js"; |
| 6 | +import { loadPendingDelivery } from "../infra/outbound/delivery-queue-storage.js"; |
| 7 | +import { markDeliveryPlatformSendAttemptStarted } from "../infra/outbound/delivery-queue.js"; |
| 8 | +import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js"; |
| 9 | +import { captureEnv, setTestEnvValue } from "../test-utils/env.js"; |
| 10 | + |
| 11 | +const mocks = vi.hoisted(() => ({ |
| 12 | + sendDurableMessageBatch: vi.fn(), |
| 13 | + recoveryDeliver: vi.fn(), |
| 14 | + resolveOutboundChannelMessageAdapter: vi.fn(() => undefined), |
| 15 | + sleep: vi.fn(async () => {}), |
| 16 | +})); |
| 17 | + |
| 18 | +vi.mock("../channels/message/runtime.js", () => ({ |
| 19 | + sendDurableMessageBatch: mocks.sendDurableMessageBatch, |
| 20 | +})); |
| 21 | + |
| 22 | +vi.mock("../infra/outbound/deliver.js", () => ({ |
| 23 | + deliverOutboundPayloadsInternal: mocks.recoveryDeliver, |
| 24 | +})); |
| 25 | + |
| 26 | +vi.mock("../infra/outbound/channel-resolution.js", () => ({ |
| 27 | + resolveOutboundChannelMessageAdapter: mocks.resolveOutboundChannelMessageAdapter, |
| 28 | +})); |
| 29 | + |
| 30 | +vi.mock("../utils/sleep.js", () => ({ sleep: mocks.sleep })); |
| 31 | + |
| 32 | +const { deliverRestartSentinelNotice, enqueueRestartSentinelNotice } = |
| 33 | + await import("./server-restart-sentinel-notice.js"); |
| 34 | + |
| 35 | +type DeliveryRequest = { deliveryQueueId?: string; deliveryQueueStateDir?: string }; |
| 36 | + |
| 37 | +describe("restart sentinel notice recovery", () => { |
| 38 | + let envSnapshot: ReturnType<typeof captureEnv> | undefined; |
| 39 | + let stateDir = ""; |
| 40 | + const tempDirs = useAutoCleanupTempDirTracker((cleanup) => { |
| 41 | + afterEach(() => { |
| 42 | + closeOpenClawStateDatabaseForTest(); |
| 43 | + envSnapshot?.restore(); |
| 44 | + envSnapshot = undefined; |
| 45 | + cleanup(); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + beforeEach(() => { |
| 50 | + closeOpenClawStateDatabaseForTest(); |
| 51 | + stateDir = tempDirs.make("openclaw-restart-notice-"); |
| 52 | + envSnapshot = captureEnv(["OPENCLAW_STATE_DIR"]); |
| 53 | + setTestEnvValue("OPENCLAW_STATE_DIR", stateDir); |
| 54 | + mocks.sendDurableMessageBatch.mockReset(); |
| 55 | + mocks.recoveryDeliver.mockReset(); |
| 56 | + mocks.resolveOutboundChannelMessageAdapter.mockClear(); |
| 57 | + mocks.sleep.mockClear(); |
| 58 | + }); |
| 59 | + |
| 60 | + async function enqueueNotice(): Promise<string> { |
| 61 | + const queued = await enqueueRestartSentinelNotice({ |
| 62 | + channel: "whatsapp", |
| 63 | + to: "+15550002", |
| 64 | + message: "restart complete", |
| 65 | + sessionKey: "agent:main:main", |
| 66 | + revision: 123, |
| 67 | + }); |
| 68 | + return queued.id; |
| 69 | + } |
| 70 | + |
| 71 | + async function deliverNotice(queueId: string): Promise<void> { |
| 72 | + await deliverRestartSentinelNotice({ |
| 73 | + deps: {} as never, |
| 74 | + cfg: {}, |
| 75 | + channel: "whatsapp", |
| 76 | + to: "+15550002", |
| 77 | + message: "restart complete", |
| 78 | + sessionKey: "agent:main:main", |
| 79 | + summary: "restart summary", |
| 80 | + queueId, |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + async function markAttempt(request: unknown): Promise<void> { |
| 85 | + const { deliveryQueueId, deliveryQueueStateDir } = request as DeliveryRequest; |
| 86 | + if (!deliveryQueueId) { |
| 87 | + throw new Error("expected durable delivery queue id"); |
| 88 | + } |
| 89 | + await markDeliveryPlatformSendAttemptStarted(deliveryQueueId, deliveryQueueStateDir); |
| 90 | + } |
| 91 | + |
| 92 | + function queueStatus(queueId: string): string | undefined { |
| 93 | + return getDeliveryQueueEntryStatus("outbound", queueId, stateDir); |
| 94 | + } |
| 95 | + |
| 96 | + it("replays a retryable provider-not-dispatched failure after the startup scan", async () => { |
| 97 | + const queueId = await enqueueNotice(); |
| 98 | + mocks.sendDurableMessageBatch.mockImplementationOnce(async (request) => { |
| 99 | + await markAttempt(request); |
| 100 | + return { |
| 101 | + status: "failed", |
| 102 | + error: new PlatformMessageNotDispatchedError("connect failed before dispatch", { |
| 103 | + cause: new Error("connect failed"), |
| 104 | + }), |
| 105 | + }; |
| 106 | + }); |
| 107 | + mocks.recoveryDeliver.mockResolvedValueOnce([ |
| 108 | + { channel: "whatsapp", messageId: "recovered-1" }, |
| 109 | + ]); |
| 110 | + |
| 111 | + await deliverNotice(queueId); |
| 112 | + |
| 113 | + expect(mocks.recoveryDeliver).toHaveBeenCalledOnce(); |
| 114 | + expect(await loadPendingDelivery(queueId)).toBeNull(); |
| 115 | + expect(queueStatus(queueId)).toBe("completed"); |
| 116 | + }); |
| 117 | + |
| 118 | + it("does not blindly resend an ambiguous platform attempt", async () => { |
| 119 | + const queueId = await enqueueNotice(); |
| 120 | + mocks.sendDurableMessageBatch.mockImplementationOnce(async (request) => { |
| 121 | + await markAttempt(request); |
| 122 | + return { status: "failed", error: new Error("platform outcome unknown") }; |
| 123 | + }); |
| 124 | + |
| 125 | + await deliverNotice(queueId); |
| 126 | + |
| 127 | + expect(mocks.recoveryDeliver).not.toHaveBeenCalled(); |
| 128 | + expect(await loadPendingDelivery(queueId)).toBeNull(); |
| 129 | + expect(queueStatus(queueId)).toBe("failed"); |
| 130 | + }); |
| 131 | + |
| 132 | + it("dead-letters a permanent provider rejection without replay", async () => { |
| 133 | + const queueId = await enqueueNotice(); |
| 134 | + mocks.sendDurableMessageBatch.mockImplementationOnce(async (request) => { |
| 135 | + await markAttempt(request); |
| 136 | + return { |
| 137 | + status: "failed", |
| 138 | + error: new PlatformMessageNotDispatchedError("payload rejected", { |
| 139 | + cause: new Error("invalid payload"), |
| 140 | + retryable: false, |
| 141 | + }), |
| 142 | + }; |
| 143 | + }); |
| 144 | + |
| 145 | + await deliverNotice(queueId); |
| 146 | + |
| 147 | + expect(mocks.recoveryDeliver).not.toHaveBeenCalled(); |
| 148 | + expect(await loadPendingDelivery(queueId)).toBeNull(); |
| 149 | + expect(queueStatus(queueId)).toBe("failed"); |
| 150 | + }); |
| 151 | + |
| 152 | + it("preserves the shipped 45-attempt budget before dead-lettering", async () => { |
| 153 | + const queueId = await enqueueNotice(); |
| 154 | + const retryableFailure = () => |
| 155 | + new PlatformMessageNotDispatchedError("transport unavailable before dispatch", { |
| 156 | + cause: new Error("transport unavailable"), |
| 157 | + }); |
| 158 | + mocks.sendDurableMessageBatch.mockImplementationOnce(async (request) => { |
| 159 | + await markAttempt(request); |
| 160 | + return { status: "failed", error: retryableFailure() }; |
| 161 | + }); |
| 162 | + mocks.recoveryDeliver.mockImplementation(async (request) => { |
| 163 | + await markAttempt(request); |
| 164 | + throw retryableFailure(); |
| 165 | + }); |
| 166 | + |
| 167 | + await deliverNotice(queueId); |
| 168 | + |
| 169 | + expect(mocks.sendDurableMessageBatch).toHaveBeenCalledOnce(); |
| 170 | + expect(mocks.recoveryDeliver).toHaveBeenCalledTimes(44); |
| 171 | + expect(queueStatus(queueId)).toBe("failed"); |
| 172 | + }); |
| 173 | +}); |
0 commit comments