|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | + |
| 4 | +process.env.TURSO_DATABASE_URL = "file::memory:"; |
| 5 | + |
| 6 | +const { |
| 7 | + db, |
| 8 | + deleteProjectInboundWebhook, |
| 9 | + ensureSchema, |
| 10 | + setProjectInboundWebhookActive, |
| 11 | +} = await import("../lib/db.ts"); |
| 12 | + |
| 13 | +await ensureSchema(); |
| 14 | + |
| 15 | +async function insertReceiver(projectId, suffix) { |
| 16 | + const id = `receiver-${suffix}`; |
| 17 | + await db().execute({ |
| 18 | + sql: `INSERT INTO inbound_webhooks (id, project_id, provider, secret) |
| 19 | + VALUES (?, ?, ?, ?)`, |
| 20 | + args: [id, projectId, `provider-${suffix}`, `whrcv_${suffix}`], |
| 21 | + }); |
| 22 | + return id; |
| 23 | +} |
| 24 | + |
| 25 | +test("inbound receivers pause and resume without losing configuration", async () => { |
| 26 | + const id = await insertReceiver("project-pause", "pause"); |
| 27 | + |
| 28 | + assert.equal(await setProjectInboundWebhookActive(id, "project-pause", false), true); |
| 29 | + const paused = await db().execute({ |
| 30 | + sql: `SELECT provider, secret, active FROM inbound_webhooks WHERE id = ?`, |
| 31 | + args: [id], |
| 32 | + }); |
| 33 | + assert.deepEqual( |
| 34 | + { |
| 35 | + provider: String(paused.rows[0].provider), |
| 36 | + secret: String(paused.rows[0].secret), |
| 37 | + active: Number(paused.rows[0].active), |
| 38 | + }, |
| 39 | + { provider: "provider-pause", secret: "whrcv_pause", active: 0 }, |
| 40 | + ); |
| 41 | + |
| 42 | + assert.equal(await setProjectInboundWebhookActive(id, "project-pause", true), true); |
| 43 | + const resumed = await db().execute({ |
| 44 | + sql: `SELECT active FROM inbound_webhooks WHERE id = ?`, |
| 45 | + args: [id], |
| 46 | + }); |
| 47 | + assert.equal(Number(resumed.rows[0].active), 1); |
| 48 | +}); |
| 49 | + |
| 50 | +test("project scoping prevents cross-project receiver changes", async () => { |
| 51 | + const id = await insertReceiver("project-owner", "scoped"); |
| 52 | + await db().execute({ |
| 53 | + sql: `INSERT INTO inbound_events |
| 54 | + (id, inbound_id, provider, idempotency_key, status) |
| 55 | + VALUES (?, ?, ?, ?, 'accepted')`, |
| 56 | + args: ["inbound-event-scoped", id, "provider-scoped", "event-scoped"], |
| 57 | + }); |
| 58 | + |
| 59 | + assert.equal(await setProjectInboundWebhookActive(id, "project-other", false), false); |
| 60 | + assert.equal(await deleteProjectInboundWebhook(id, "project-other"), false); |
| 61 | + |
| 62 | + const receiver = await db().execute({ |
| 63 | + sql: `SELECT active FROM inbound_webhooks WHERE id = ?`, |
| 64 | + args: [id], |
| 65 | + }); |
| 66 | + assert.equal(Number(receiver.rows[0].active), 1); |
| 67 | + const events = await db().execute({ |
| 68 | + sql: `SELECT 1 FROM inbound_events WHERE inbound_id = ?`, |
| 69 | + args: [id], |
| 70 | + }); |
| 71 | + assert.equal(events.rows.length, 1); |
| 72 | +}); |
| 73 | + |
| 74 | +test("deleting an inbound receiver also deletes its event history", async () => { |
| 75 | + const id = await insertReceiver("project-delete", "delete"); |
| 76 | + await db().execute({ |
| 77 | + sql: `INSERT INTO inbound_events |
| 78 | + (id, inbound_id, provider, idempotency_key, status) |
| 79 | + VALUES (?, ?, ?, ?, 'accepted')`, |
| 80 | + args: ["inbound-event-delete", id, "provider-delete", "event-delete"], |
| 81 | + }); |
| 82 | + |
| 83 | + assert.equal(await deleteProjectInboundWebhook(id, "project-delete"), true); |
| 84 | + assert.equal(await deleteProjectInboundWebhook(id, "project-delete"), false); |
| 85 | + |
| 86 | + const receivers = await db().execute({ |
| 87 | + sql: `SELECT 1 FROM inbound_webhooks WHERE id = ?`, |
| 88 | + args: [id], |
| 89 | + }); |
| 90 | + const events = await db().execute({ |
| 91 | + sql: `SELECT 1 FROM inbound_events WHERE inbound_id = ?`, |
| 92 | + args: [id], |
| 93 | + }); |
| 94 | + assert.equal(receivers.rows.length, 0); |
| 95 | + assert.equal(events.rows.length, 0); |
| 96 | +}); |
0 commit comments