|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import crypto from "node:crypto"; |
| 3 | +import { EventEmitter } from "node:events"; |
| 4 | +import { test } from "node:test"; |
| 5 | +import { makeReceiver } from "../src/receiver.mjs"; |
| 6 | + |
| 7 | +// Integration against a real Valkey: the receiver's verify->filter->enqueue path must land a real job |
| 8 | +// in Valkey under its exact-per-delivery GUID jobId (REQ-DEDUP-BY-DELIVERY-GUID). The fake-queue tests |
| 9 | +// in receiver.test.mjs prove the routing; this proves the wire -- a real BullMQ add against a real |
| 10 | +// Redis-protocol server, so a regression in the shared enqueue contract cannot pass unnoticed. |
| 11 | +// |
| 12 | +// Runs when VALKEY_TEST_URL is set (CI provides a service). PI_DISPATCH_REQUIRE_RECEIVER_TESTS=1 turns |
| 13 | +// a skip into a hard failure: a skipped assertion is an UNVERIFIED assertion, so where the flag is set |
| 14 | +// a missing Valkey is a red build, never a silent green. |
| 15 | +const url = process.env.VALKEY_TEST_URL; |
| 16 | +const required = process.env.PI_DISPATCH_REQUIRE_RECEIVER_TESTS === "1"; |
| 17 | +if (!url && required) { |
| 18 | + throw new Error("receiver enqueue integration test is REQUIRED here (PI_DISPATCH_REQUIRE_RECEIVER_TESTS=1) but VALKEY_TEST_URL is not set"); |
| 19 | +} |
| 20 | +const skip = url ? false : "VALKEY_TEST_URL not set; receiver enqueue integration test skipped (CI sets it)"; |
| 21 | + |
| 22 | +const SECRET = "test-webhook-secret"; |
| 23 | +const SELF_ID = 999; |
| 24 | +const cfg = { |
| 25 | + webhookSecret: SECRET, |
| 26 | + labelFlows: { "pi:frontend": "frontend-fix" }, |
| 27 | + commentTrigger: { phrase: "@pi", defaultFlow: null }, |
| 28 | +}; |
| 29 | + |
| 30 | +/** GitHub's `X-Hub-Signature-256` shape, computed the same way GitHub computes it. */ |
| 31 | +function sign(secret, raw) { |
| 32 | + return "sha256=" + crypto.createHmac("sha256", secret).update(raw).digest("hex"); |
| 33 | +} |
| 34 | + |
| 35 | +/** EventEmitter-backed request mock: real streams are EventEmitters, so `on`/`emit` come for free. */ |
| 36 | +function mockReq({ method = "POST", headers = {} } = {}) { |
| 37 | + const req = new EventEmitter(); |
| 38 | + req.method = method; |
| 39 | + req.headers = headers; |
| 40 | + req.destroyed = false; |
| 41 | + req.destroy = () => { |
| 42 | + req.destroyed = true; |
| 43 | + }; |
| 44 | + return req; |
| 45 | +} |
| 46 | + |
| 47 | +/** Plain object response mock recording writeHead/statusCode/end -- no real socket. */ |
| 48 | +function mockRes() { |
| 49 | + return { |
| 50 | + statusCode: 0, |
| 51 | + headersSent: false, |
| 52 | + body: undefined, |
| 53 | + writeHead(status, headers) { |
| 54 | + this.statusCode = status; |
| 55 | + this.headers = headers; |
| 56 | + this.headersSent = true; |
| 57 | + return this; |
| 58 | + }, |
| 59 | + end(body) { |
| 60 | + this.body = body; |
| 61 | + this.ended = true; |
| 62 | + return this; |
| 63 | + }, |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +/** Drive a handler: attach synchronously, then feed the raw bytes and await completion. */ |
| 68 | +async function drive(handler, req, res, raw) { |
| 69 | + const done = handler(req, res); |
| 70 | + if (raw !== undefined) { |
| 71 | + req.emit("data", Buffer.from(raw, "utf8")); |
| 72 | + req.emit("end"); |
| 73 | + } |
| 74 | + await done; |
| 75 | +} |
| 76 | + |
| 77 | +test("verify->filter->enqueue lands a github job in a real Valkey under the GUID jobId", { skip }, async () => { |
| 78 | + const { Queue } = await import("bullmq"); |
| 79 | + const { parseConnection } = await import("@pi-dispatch/worker/connection"); |
| 80 | + |
| 81 | + // A uniquely-named queue per run so parallel/repeated Valkey tests cannot see each other's jobs. |
| 82 | + // `parseConnection` returns connection OPTIONS (not a shared ioredis instance), so this Queue owns |
| 83 | + // the client it builds from them and `queue.close()` closes it -- no separate connection to quit. |
| 84 | + const connection = parseConnection(url); |
| 85 | + const queue = new Queue(`recv-it-${crypto.randomUUID()}`, { connection }); |
| 86 | + try { |
| 87 | + await queue.obliterate({ force: true }).catch(() => {}); |
| 88 | + |
| 89 | + const delivery = crypto.randomUUID(); |
| 90 | + const payload = { |
| 91 | + action: "labeled", |
| 92 | + sender: { id: 1 }, |
| 93 | + repository: { full_name: "octo/repo" }, |
| 94 | + issue: { number: 42, title: "T", body: "B", labels: [{ name: "pi:frontend" }] }, |
| 95 | + }; |
| 96 | + const raw = JSON.stringify(payload); |
| 97 | + |
| 98 | + const handler = makeReceiver({ queue, selfId: SELF_ID, cfg, log: () => {} }); |
| 99 | + const req = mockReq({ |
| 100 | + headers: { |
| 101 | + "content-type": "application/json", |
| 102 | + "x-hub-signature-256": sign(SECRET, raw), |
| 103 | + "x-github-event": "issues", |
| 104 | + "x-github-delivery": delivery, |
| 105 | + }, |
| 106 | + }); |
| 107 | + const res = mockRes(); |
| 108 | + await drive(handler, req, res, raw); |
| 109 | + |
| 110 | + assert.equal(res.statusCode, 202); |
| 111 | + |
| 112 | + // The job is really in Valkey, under the exact-per-delivery GUID jobId, with the github shape. |
| 113 | + const job = await queue.getJob("gh-" + delivery); |
| 114 | + assert.ok(job, "the enqueued job must be readable back from Valkey under gh-<delivery>"); |
| 115 | + assert.equal(job.id, "gh-" + delivery); |
| 116 | + assert.equal(job.data.kind, "github"); |
| 117 | + assert.equal(job.data.flow, "frontend-fix"); |
| 118 | + } finally { |
| 119 | + await queue.obliterate({ force: true }).catch(() => {}); |
| 120 | + await queue.close(); |
| 121 | + } |
| 122 | +}); |
0 commit comments