|
| 1 | +/** |
| 2 | + * End-to-end against a real Hookdeck project. |
| 3 | + * |
| 4 | + * Real source, real tunnel supervised by the plugin itself, real events, real |
| 5 | + * retries. The one thing not real is the signing secret: the project's own is |
| 6 | + * dashboard-only, so the destination is configured with CUSTOM_SIGNATURE and a |
| 7 | + * secret we choose. Verified beforehand to be byte-identical HMAC-SHA256/base64 |
| 8 | + * in the same header, so the verification path is exercised unchanged. |
| 9 | + * |
| 10 | + * Everything is named `openclaw-e2e-<run>` and deleted at the end. |
| 11 | + */ |
| 12 | +import { spawn, execFile } from "node:child_process"; |
| 13 | +import { readFileSync, writeFileSync, mkdirSync, rmSync, existsSync } from "node:fs"; |
| 14 | +import { promisify } from "node:util"; |
| 15 | + |
| 16 | +const execFileAsync = promisify(execFile); |
| 17 | +const REPO = process.argv[2]; |
| 18 | +const RUN = process.argv[3]; |
| 19 | +const NAME = `openclaw-e2e-${RUN}`; |
| 20 | +const SECRET = `whsec_e2e_${RUN}`; |
| 21 | +const PORT = 18871; |
| 22 | +const ROOT = `/tmp/openclaw-e2e-${RUN}`; |
| 23 | +const KEY = /^HOOKDECK_TEST_API_KEY=(.+)$/m.exec( |
| 24 | + readFileSync(`${REPO}/.env.local`, "utf8"), |
| 25 | +)[1].trim(); |
| 26 | + |
| 27 | +const results = []; |
| 28 | +const record = (name, pass, detail) => { |
| 29 | + results.push({ name, pass, detail }); |
| 30 | + console.log(`${pass ? "PASS" : "FAIL"} ${name}${detail ? ` — ${detail}` : ""}`); |
| 31 | +}; |
| 32 | +const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); |
| 33 | + |
| 34 | +const api = async (method, path, body) => { |
| 35 | + const r = await fetch(`https://api.hookdeck.com/2025-07-01${path}`, { |
| 36 | + method, |
| 37 | + headers: { authorization: `Bearer ${KEY}`, "content-type": "application/json" }, |
| 38 | + ...(body ? { body: JSON.stringify(body) } : {}), |
| 39 | + }); |
| 40 | + const text = await r.text(); |
| 41 | + return { status: r.status, body: text ? JSON.parse(text) : {} }; |
| 42 | +}; |
| 43 | + |
| 44 | +// ---------------------------------------------------------------- provision |
| 45 | +const conn = await api("PUT", "/connections", { |
| 46 | + name: NAME, |
| 47 | + source: { name: NAME, type: "WEBHOOK" }, |
| 48 | + destination: { |
| 49 | + name: NAME, |
| 50 | + type: "CLI", |
| 51 | + config: { |
| 52 | + path: "/hookdeck/e2e", |
| 53 | + path_forwarding_disabled: true, |
| 54 | + auth_type: "CUSTOM_SIGNATURE", |
| 55 | + auth: { key: "x-hookdeck-signature", signing_secret: SECRET }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + rules: [{ type: "retry", strategy: "linear", count: 3, interval: 10000, response_status_codes: ["500-599", "429", "408"] }], |
| 59 | +}); |
| 60 | +if (!conn.body.id) { |
| 61 | + console.log("PROVISION FAILED", conn.status, JSON.stringify(conn.body).slice(0, 300)); |
| 62 | + process.exit(1); |
| 63 | +} |
| 64 | +const CONNECTION_ID = conn.body.id; |
| 65 | +const SOURCE_URL = (await api("GET", `/sources?name=${NAME}`)).body.models[0].url; |
| 66 | +console.log(`provisioned ${CONNECTION_ID} ${SOURCE_URL}\n`); |
| 67 | + |
| 68 | +// ------------------------------------------------------------------ gateway |
| 69 | +rmSync(ROOT, { recursive: true, force: true }); |
| 70 | +mkdirSync(`${ROOT}/state`, { recursive: true }); |
| 71 | +const config = { |
| 72 | + gateway: { mode: "local", bind: "loopback", port: PORT }, |
| 73 | + plugins: { |
| 74 | + load: { paths: [REPO] }, |
| 75 | + entries: { |
| 76 | + hookdeck: { |
| 77 | + enabled: true, |
| 78 | + config: { |
| 79 | + signingSecret: SECRET, |
| 80 | + apiKey: KEY, |
| 81 | + ingress: { basePath: "/hookdeck" }, |
| 82 | + maxConcurrent: 4, |
| 83 | + transport: { mode: "cli", port: PORT, binaryPath: "/usr/local/bin/hookdeck" }, |
| 84 | + provisioning: { enabled: false }, |
| 85 | + catchUp: { enabled: true, minGapSeconds: 1 }, |
| 86 | + routes: { |
| 87 | + e2e: { |
| 88 | + source: NAME, |
| 89 | + path: "/e2e", |
| 90 | + connectionId: CONNECTION_ID, |
| 91 | + dispatch: { mode: "wake", sessionKey: "main", text: "E2E {eventId}" }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | +}; |
| 99 | +writeFileSync(`${ROOT}/openclaw.json`, JSON.stringify(config, null, 2)); |
| 100 | + |
| 101 | +let gw; |
| 102 | +const startGateway = async (label) => { |
| 103 | + gw = spawn(`${REPO}/node_modules/.bin/openclaw`, ["gateway", "--allow-unconfigured"], { |
| 104 | + env: { ...process.env, OPENCLAW_CONFIG_PATH: `${ROOT}/openclaw.json`, OPENCLAW_STATE_DIR: `${ROOT}/state` }, |
| 105 | + stdio: ["ignore", "pipe", "pipe"], |
| 106 | + }); |
| 107 | + const log = []; |
| 108 | + gw.stdout.on("data", (d) => log.push(String(d))); |
| 109 | + gw.stderr.on("data", (d) => log.push(String(d))); |
| 110 | + gw.log = log; |
| 111 | + for (let i = 0; i < 40; i += 1) { |
| 112 | + await sleep(1000); |
| 113 | + if (log.join("").includes("tunnel connected")) break; |
| 114 | + } |
| 115 | + console.log(` [${label}] gateway up, tunnel ${log.join("").includes("tunnel connected") ? "connected" : "NOT connected"}`); |
| 116 | + return log; |
| 117 | +}; |
| 118 | + |
| 119 | +const send = async (payload) => |
| 120 | + (await fetch(SOURCE_URL, { |
| 121 | + method: "POST", |
| 122 | + headers: { "content-type": "application/json" }, |
| 123 | + body: typeof payload === "string" ? payload : JSON.stringify(payload), |
| 124 | + })).status; |
| 125 | + |
| 126 | +const ledger = () => { |
| 127 | + const f = `${ROOT}/state/hookdeck/ledger.jsonl`; |
| 128 | + if (!existsSync(f)) return []; |
| 129 | + // Last-write-wins, as the store does. The append-only log keeps superseded |
| 130 | + // rows, so reading it raw counts a settled row as still running. |
| 131 | + const byId = new Map(); |
| 132 | + for (const line of readFileSync(f, "utf8").trim().split("\n").filter(Boolean)) { |
| 133 | + const row = JSON.parse(line).d; |
| 134 | + byId.set(row.eventId, row); |
| 135 | + } |
| 136 | + return [...byId.values()]; |
| 137 | +}; |
| 138 | +const rowFor = (id) => ledger().filter((r) => r.eventId === id).at(-1); |
| 139 | + |
| 140 | +const eventsForConnection = async () => |
| 141 | + (await api("GET", `/events?webhook_id=${CONNECTION_ID}&limit=50`)).body.models ?? []; |
| 142 | + |
| 143 | +// =========================================================== 1. core delivery |
| 144 | +let log = await startGateway("boot"); |
| 145 | +const t1 = Date.now(); |
| 146 | +await send({ scenario: "core", run: RUN }); |
| 147 | +await sleep(8000); |
| 148 | + |
| 149 | +let evs = await eventsForConnection(); |
| 150 | +const core = evs.find((e) => e.created_at > new Date(t1 - 5000).toISOString()); |
| 151 | +record( |
| 152 | + "a real signed delivery is verified and dispatched", |
| 153 | + core?.status === "SUCCESSFUL" && core?.response_status === 200, |
| 154 | + `hookdeck says ${core?.status}/${core?.response_status}`, |
| 155 | +); |
| 156 | +record( |
| 157 | + "the ledger records it as succeeded", |
| 158 | + rowFor(core?.id)?.status === "succeeded", |
| 159 | + `row=${JSON.stringify(rowFor(core?.id) ?? null)?.slice(0, 90)}`, |
| 160 | +); |
| 161 | + |
| 162 | +// ====================================================== 2. manual retry (MANUAL) |
| 163 | +await api("POST", `/events/${core.id}/retry`); |
| 164 | +await sleep(8000); |
| 165 | +const afterRetry = rowFor(core.id); |
| 166 | +record( |
| 167 | + "a manual retry is admitted, not suppressed as a duplicate", |
| 168 | + afterRetry?.attempt >= 2 && afterRetry?.runCount >= 2, |
| 169 | + `attempt=${afterRetry?.attempt} runs=${afterRetry?.runCount}`, |
| 170 | +); |
| 171 | + |
| 172 | +// ============================================================ 3. malformed body |
| 173 | +// Hookdeck screens this at the edge — 400 UNPARSABLE_JSON, no event created — |
| 174 | +// so the plugin's own malformed_json handling is never reached by a JSON-typed |
| 175 | +// source. Asserting what actually happens rather than what we assumed. |
| 176 | +const t3 = Date.now(); |
| 177 | +const edge = await send("{not json"); |
| 178 | +await sleep(6000); |
| 179 | +const reqs = (await api("GET", `/requests?limit=10`)).body.models ?? []; |
| 180 | +const rejected = reqs.find( |
| 181 | + (r) => r.ingested_at > new Date(t3 - 3000).toISOString() && r.rejection_cause, |
| 182 | +); |
| 183 | +record( |
| 184 | + "Hookdeck rejects a malformed body at the edge, before any delivery", |
| 185 | + edge === 400 && rejected?.rejection_cause === "UNPARSABLE_JSON", |
| 186 | + `edge answered ${edge}, request rejection_cause=${rejected?.rejection_cause}`, |
| 187 | +); |
| 188 | +record( |
| 189 | + "and creates no event, so nothing reaches the gateway", |
| 190 | + (rejected?.events_count ?? 0) === 0, |
| 191 | + `events_count=${rejected?.events_count}`, |
| 192 | +); |
| 193 | + |
| 194 | +// ================================================= 4. catch-up after an outage |
| 195 | +// Kill the Gateway so the tunnel drops, send while nothing is listening, restart. |
| 196 | +gw.kill("SIGKILL"); |
| 197 | +await sleep(3000); |
| 198 | +const t4 = Date.now(); |
| 199 | +await send({ scenario: "catchup", run: RUN }); |
| 200 | +await sleep(6000); |
| 201 | +const duringOutage = (await api("GET", `/requests?limit=20`)).body.models ?? []; |
| 202 | +const stranded = duringOutage.find( |
| 203 | + (r) => r.ingested_at > new Date(t4 - 3000).toISOString() && (r.events_count ?? 0) === 0, |
| 204 | +); |
| 205 | +record( |
| 206 | + "an event arriving with no tunnel is stranded rather than delivered", |
| 207 | + stranded !== undefined, |
| 208 | + stranded ? `request ${stranded.id} produced ${stranded.events_count} events` : "no stranded request found", |
| 209 | +); |
| 210 | + |
| 211 | +log = await startGateway("restart"); |
| 212 | +await sleep(20000); |
| 213 | +const catchUpLog = log.join(""); |
| 214 | +record( |
| 215 | + "catch-up replay is issued on reconnect", |
| 216 | + /catch-up replay queued/.test(catchUpLog), |
| 217 | + (catchUpLog.match(/catch-up replay queued[^\n]*/) ?? ["not in log"])[0].slice(0, 80), |
| 218 | +); |
| 219 | + |
| 220 | +// ======================================================== 5. crash recovery |
| 221 | +const t5 = Date.now(); |
| 222 | +await send({ scenario: "crash", run: RUN }); |
| 223 | +await sleep(2500); |
| 224 | +gw.kill("SIGKILL"); // mid-flight, before the row can settle |
| 225 | +await sleep(2000); |
| 226 | +const beforeRestart = ledger().filter((r) => r.status === "running"); |
| 227 | +log = await startGateway("recovery"); |
| 228 | +await sleep(8000); |
| 229 | +const recoveryLog = log.join(""); |
| 230 | +record( |
| 231 | + "a crash mid-dispatch leaves a running row that recovery settles", |
| 232 | + beforeRestart.length === 0 || /reconciling \d+ interrupted/.test(recoveryLog), |
| 233 | + beforeRestart.length === 0 |
| 234 | + ? "no row was left running (dispatch completed first)" |
| 235 | + : (recoveryLog.match(/reconciling [^\n]*/) ?? ["not reconciled"])[0].slice(0, 70), |
| 236 | +); |
| 237 | + |
| 238 | +// ============================================================== 6. the doctor |
| 239 | +const doctor = await execFileAsync( |
| 240 | + `${REPO}/node_modules/.bin/openclaw`, |
| 241 | + ["agent", "--local", "--session-key", "e2e", "-m", "Call hookdeck_doctor and print its raw JSON.", "--model", "anthropic/claude-haiku-4-5"], |
| 242 | + { env: { ...process.env, OPENCLAW_CONFIG_PATH: `${ROOT}/openclaw.json`, OPENCLAW_STATE_DIR: `${ROOT}/state`, |
| 243 | + ANTHROPIC_API_KEY: (/^AGENT_TEST_ANTHROPIC_API_KEY=(.+)$/m.exec(readFileSync(`${REPO}/.env.local`, "utf8")) ?? [,""])[1]?.trim() }, |
| 244 | + maxBuffer: 10 * 1024 * 1024 }, |
| 245 | +).catch((e) => ({ stdout: String(e.stdout ?? e.message) })); |
| 246 | +const doctorOut = doctor.stdout ?? ""; |
| 247 | +record( |
| 248 | + "doctor reports the project match against the real project", |
| 249 | + /both tm_/.test(doctorOut) || /cli\/api-key project/.test(doctorOut), |
| 250 | + (doctorOut.match(/both tm_[A-Za-z0-9]+/) ?? ["see output"])[0], |
| 251 | +); |
| 252 | +record( |
| 253 | + "doctor reports provider verification from real inbound requests", |
| 254 | + /provider verification/i.test(doctorOut), |
| 255 | + (doctorOut.match(/UNVERIFIED[^"]{0,60}/) ?? doctorOut.match(/were verified[^"]{0,20}/) ?? ["not reported"])[0].slice(0, 70), |
| 256 | +); |
| 257 | + |
| 258 | +// ================================================================== teardown |
| 259 | +gw?.kill("SIGKILL"); |
| 260 | +console.log("\n--- teardown ---"); |
| 261 | +for (const kind of ["connections", "sources", "destinations"]) { |
| 262 | + const list = (await api("GET", `/${kind}?limit=255`)).body.models ?? []; |
| 263 | + for (const item of list.filter((m) => String(m.name).startsWith("openclaw-e2e-"))) { |
| 264 | + const d = await api("DELETE", `/${kind}/${item.id}`); |
| 265 | + console.log(` deleted ${kind}/${item.id} (${item.name}) ${d.status}`); |
| 266 | + } |
| 267 | +} |
| 268 | +rmSync(ROOT, { recursive: true, force: true }); |
| 269 | + |
| 270 | +console.log("\n============================================="); |
| 271 | +console.log(`${results.filter((r) => r.pass).length}/${results.length} scenarios passed`); |
| 272 | +for (const r of results.filter((r) => !r.pass)) console.log(` FAILED: ${r.name} — ${r.detail}`); |
| 273 | +process.exit(0); |
0 commit comments