|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { test } from "node:test"; |
| 3 | + |
| 4 | +import { EnvelopeCodec, type Envelope } from "../src/codec.js"; |
| 5 | +import { |
| 6 | + redrive, |
| 7 | + resetForRedrive, |
| 8 | + type RedriveIO, |
| 9 | + type RedriveMessage, |
| 10 | +} from "../src/redrive.js"; |
| 11 | + |
| 12 | +// memoryIO is a tiny in-memory RedriveIO over a Map of queues, with a queue() accessor for assertions. |
| 13 | +function memoryIO(): RedriveIO & { queue(name: string): string[]; failOn?: string } { |
| 14 | + const queues = new Map<string, string[]>(); |
| 15 | + const q = (name: string): string[] => { |
| 16 | + let arr = queues.get(name); |
| 17 | + if (!arr) { |
| 18 | + arr = []; |
| 19 | + queues.set(name, arr); |
| 20 | + } |
| 21 | + return arr; |
| 22 | + }; |
| 23 | + const io: RedriveIO & { queue(name: string): string[]; failOn?: string } = { |
| 24 | + queue: q, |
| 25 | + async pop(queue: string): Promise<RedriveMessage | null> { |
| 26 | + const arr = q(queue); |
| 27 | + if (arr.length === 0) { |
| 28 | + return null; |
| 29 | + } |
| 30 | + const body = arr.shift() as string; |
| 31 | + return { |
| 32 | + body, |
| 33 | + async ack(): Promise<void> {}, |
| 34 | + }; |
| 35 | + }, |
| 36 | + async publish(queue: string, body: string): Promise<void> { |
| 37 | + if (io.failOn && queue === io.failOn) { |
| 38 | + throw new Error(`publish refused for ${queue}`); |
| 39 | + } |
| 40 | + q(queue).push(body); |
| 41 | + }, |
| 42 | + }; |
| 43 | + return io; |
| 44 | +} |
| 45 | + |
| 46 | +function deadLetteredBody(urn: string, originalQueue: string, data: Record<string, unknown> = {}): string { |
| 47 | + const base = EnvelopeCodec.make(urn, data, { queue: originalQueue }); |
| 48 | + const dl: Envelope = { |
| 49 | + ...base, |
| 50 | + attempts: 3, |
| 51 | + dead_letter: { |
| 52 | + reason: "failed", |
| 53 | + error: "boom", |
| 54 | + exception: "Error", |
| 55 | + failed_at: 1, |
| 56 | + original_queue: originalQueue, |
| 57 | + attempts: 3, |
| 58 | + lang: "node", |
| 59 | + }, |
| 60 | + }; |
| 61 | + return EnvelopeCodec.encode(dl); |
| 62 | +} |
| 63 | + |
| 64 | +test("resetForRedrive strips dead_letter and resets attempts, without mutating the input", () => { |
| 65 | + const base = EnvelopeCodec.make("urn:babel:orders:created", { order_id: 1 }, { queue: "orders" }); |
| 66 | + const input: Envelope = { ...base, attempts: 3, dead_letter: { reason: "failed", error: null, exception: null, failed_at: 1, original_queue: "orders", attempts: 3, lang: "node" } }; |
| 67 | + |
| 68 | + const out = resetForRedrive(input); |
| 69 | + assert.equal(out.dead_letter, undefined); |
| 70 | + assert.equal(out.attempts, 0); |
| 71 | + assert.equal(out.trace_id, input.trace_id); |
| 72 | + assert.equal(out.job, input.job); |
| 73 | + // input untouched |
| 74 | + assert.ok(input.dead_letter); |
| 75 | + assert.equal(input.attempts, 3); |
| 76 | +}); |
| 77 | + |
| 78 | +test("redrive sends a message back to its source queue, reset and traceable", async () => { |
| 79 | + const io = memoryIO(); |
| 80 | + io.queue("orders.dlq").push(deadLetteredBody("urn:babel:orders:created", "orders", { order_id: 1 })); |
| 81 | + const traceId = EnvelopeCodec.decode(io.queue("orders.dlq")[0]).trace_id; |
| 82 | + |
| 83 | + const res = await redrive(io, "orders.dlq"); |
| 84 | + assert.deepEqual([res.redriven, res.skipped], [1, 0]); |
| 85 | + |
| 86 | + assert.equal(io.queue("orders.dlq").length, 0, "DLQ should be drained"); |
| 87 | + assert.equal(io.queue("orders").length, 1); |
| 88 | + const back = EnvelopeCodec.decode(io.queue("orders")[0]); |
| 89 | + assert.equal(back.dead_letter, undefined); |
| 90 | + assert.equal(back.attempts, 0); |
| 91 | + assert.equal(back.trace_id, traceId); |
| 92 | +}); |
| 93 | + |
| 94 | +test("redrive routes to a sandbox queue without touching the source", async () => { |
| 95 | + const io = memoryIO(); |
| 96 | + io.queue("orders.dlq").push(deadLetteredBody("urn:babel:orders:created", "orders")); |
| 97 | + |
| 98 | + const res = await redrive(io, "orders.dlq", { toQueue: "sandbox" }); |
| 99 | + assert.equal(res.redriven, 1); |
| 100 | + assert.equal(io.queue("orders").length, 0); |
| 101 | + assert.equal(io.queue("sandbox").length, 1); |
| 102 | +}); |
| 103 | + |
| 104 | +test("dryRun reports the plan and leaves the DLQ unchanged", async () => { |
| 105 | + const io = memoryIO(); |
| 106 | + io.queue("orders.dlq").push(deadLetteredBody("urn:babel:orders:created", "orders")); |
| 107 | + |
| 108 | + const res = await redrive(io, "orders.dlq", { dryRun: true }); |
| 109 | + assert.deepEqual([res.redriven, res.skipped], [0, 1]); |
| 110 | + assert.equal(res.items[0].to, "orders"); |
| 111 | + assert.equal(res.items[0].redriven, false); |
| 112 | + assert.equal(io.queue("orders").length, 0, "source untouched"); |
| 113 | + assert.equal(io.queue("orders.dlq").length, 1, "DLQ unchanged"); |
| 114 | + assert.ok(EnvelopeCodec.decode(io.queue("orders.dlq")[0]).dead_letter, "dead_letter intact"); |
| 115 | +}); |
| 116 | + |
| 117 | +test("select redrives only the matching messages, restoring the rest", async () => { |
| 118 | + const io = memoryIO(); |
| 119 | + io.queue("dlq").push(deadLetteredBody("urn:babel:orders:created", "orders")); |
| 120 | + io.queue("dlq").push(deadLetteredBody("urn:babel:emails:welcome", "emails")); |
| 121 | + |
| 122 | + const res = await redrive(io, "dlq", { |
| 123 | + select: (e) => EnvelopeCodec.urn(e) === "urn:babel:orders:created", |
| 124 | + }); |
| 125 | + assert.deepEqual([res.redriven, res.skipped], [1, 1]); |
| 126 | + assert.equal(io.queue("orders").length, 1); |
| 127 | + assert.equal(io.queue("emails").length, 0); |
| 128 | + assert.equal(io.queue("dlq").length, 1, "unselected restored to the DLQ"); |
| 129 | +}); |
| 130 | + |
| 131 | +test("max caps how many messages are pulled", async () => { |
| 132 | + const io = memoryIO(); |
| 133 | + for (let i = 0; i < 3; i++) { |
| 134 | + io.queue("dlq").push(deadLetteredBody("urn:babel:orders:created", "orders")); |
| 135 | + } |
| 136 | + const res = await redrive(io, "dlq", { max: 2 }); |
| 137 | + assert.equal(res.redriven, 2); |
| 138 | + assert.equal(io.queue("dlq").length, 1); |
| 139 | +}); |
| 140 | + |
| 141 | +test("a publish failure restores the message to the DLQ and re-throws", async () => { |
| 142 | + const io = memoryIO(); |
| 143 | + io.queue("dlq").push(deadLetteredBody("urn:babel:orders:created", "orders")); |
| 144 | + io.failOn = "orders"; |
| 145 | + |
| 146 | + await assert.rejects(redrive(io, "dlq"), /publish refused/); |
| 147 | + assert.equal(io.queue("dlq").length, 1, "message restored to the DLQ"); |
| 148 | + assert.equal(io.queue("orders").length, 0); |
| 149 | +}); |
| 150 | + |
| 151 | +test("an undecodable body is restored, not lost", async () => { |
| 152 | + const io = memoryIO(); |
| 153 | + io.queue("dlq").push("not-json{{{"); |
| 154 | + |
| 155 | + const res = await redrive(io, "dlq"); |
| 156 | + assert.deepEqual([res.redriven, res.skipped], [0, 1]); |
| 157 | + assert.equal(io.queue("dlq").length, 1); |
| 158 | + assert.equal(io.queue("dlq")[0], "not-json{{{"); |
| 159 | +}); |
0 commit comments