|
| 1 | +/** |
| 2 | + * Plan 8 PR C — decision batch ingest integration. |
| 3 | + */ |
| 4 | +process.env.DATABASE_URL ||= |
| 5 | + 'postgresql://verilink:verilink@127.0.0.1:15432/verilink_test'; |
| 6 | +process.env.API_KEY_HMAC_SECRET ||= 'test-hmac-secret-for-integration'; |
| 7 | + |
| 8 | +import { describe, it, before, after, beforeEach } from 'node:test'; |
| 9 | +import assert from 'node:assert/strict'; |
| 10 | +import type pg from 'pg'; |
| 11 | +import { setupTestDb, teardownTestDb, resetTestData } from '../../testutil/testDb.js'; |
| 12 | +import { seedTenant, seedApiKey, authHeaders } from '../../testutil/seedData.js'; |
| 13 | +import { startControlPlane, type ControlPlaneHarness } from '../../testutil/appHarness.js'; |
| 14 | +import { |
| 15 | + batchIDFromPayloadHash, |
| 16 | + canonicalizeDecisionsJSON, |
| 17 | + sha256Hex, |
| 18 | + shouldSample, |
| 19 | + type DecisionWire, |
| 20 | +} from '../../domains/decision/decisionSample.js'; |
| 21 | + |
| 22 | +function buildBatch(decisions: DecisionWire[]) { |
| 23 | + const sum = sha256Hex(canonicalizeDecisionsJSON(decisions)); |
| 24 | + return { |
| 25 | + batch_id: batchIDFromPayloadHash(sum), |
| 26 | + first_wal_seq: decisions[0].wal_seq, |
| 27 | + last_wal_seq: decisions[decisions.length - 1].wal_seq, |
| 28 | + payload_hash: sum, |
| 29 | + decisions, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +describe('decision batch ingest', () => { |
| 34 | + let pool: pg.Pool; |
| 35 | + let harness: ControlPlaneHarness; |
| 36 | + let apiKey: string; |
| 37 | + |
| 38 | + before(async () => { |
| 39 | + pool = await setupTestDb(); |
| 40 | + harness = await startControlPlane(); |
| 41 | + }); |
| 42 | + |
| 43 | + after(async () => { |
| 44 | + await harness.stop(); |
| 45 | + await teardownTestDb(pool); |
| 46 | + }); |
| 47 | + |
| 48 | + beforeEach(async () => { |
| 49 | + await resetTestData(pool); |
| 50 | + const tenant = await seedTenant(pool, `decisions-${Date.now()}`); |
| 51 | + apiKey = await seedApiKey(pool, tenant.id, ['*']); |
| 52 | + }); |
| 53 | + |
| 54 | + it('shouldSample keeps all denies and rate-limits allows', () => { |
| 55 | + const t = '2026-07-31T12:00:00.001Z'; |
| 56 | + assert.equal(shouldSample('deny', t, 0), true); |
| 57 | + assert.equal(shouldSample('allow', t, 0), false); |
| 58 | + assert.equal(shouldSample('allow', t, 1), true); |
| 59 | + }); |
| 60 | + |
| 61 | + it('rejects payload_hash that does not match decisions', async () => { |
| 62 | + const decidedAt = new Date().toISOString(); |
| 63 | + const batch = buildBatch([ |
| 64 | + { |
| 65 | + wal_seq: 1, |
| 66 | + fingerprint: 'fp-a', |
| 67 | + action: 'deny', |
| 68 | + decided_at: decidedAt, |
| 69 | + }, |
| 70 | + ]); |
| 71 | + const resp = await fetch(`${harness.url}/v1/decisions/batch`, { |
| 72 | + method: 'POST', |
| 73 | + headers: { ...authHeaders(apiKey), 'Content-Type': 'application/json' }, |
| 74 | + body: JSON.stringify({ ...batch, payload_hash: '0'.repeat(64) }), |
| 75 | + }); |
| 76 | + assert.equal(resp.status, 400); |
| 77 | + }); |
| 78 | + |
| 79 | + it('accepts a batch and is idempotent on redelivery', async () => { |
| 80 | + const decidedAt = new Date().toISOString(); |
| 81 | + const batch = buildBatch([ |
| 82 | + { |
| 83 | + wal_seq: 1, |
| 84 | + fingerprint: 'fp-deny', |
| 85 | + principal_id: 'vrl:p:1', |
| 86 | + score: 10, |
| 87 | + blacklisted: false, |
| 88 | + action: 'deny', |
| 89 | + decided_at: decidedAt, |
| 90 | + }, |
| 91 | + { |
| 92 | + wal_seq: 2, |
| 93 | + fingerprint: 'fp-allow', |
| 94 | + action: 'allow', |
| 95 | + score: 90, |
| 96 | + decided_at: decidedAt, |
| 97 | + }, |
| 98 | + ]); |
| 99 | + |
| 100 | + const resp = await fetch(`${harness.url}/v1/decisions/batch`, { |
| 101 | + method: 'POST', |
| 102 | + headers: { ...authHeaders(apiKey), 'Content-Type': 'application/json' }, |
| 103 | + body: JSON.stringify(batch), |
| 104 | + }); |
| 105 | + assert.equal(resp.status, 200); |
| 106 | + const body = (await resp.json()) as { ok: boolean; data: { duplicate: boolean; accepted: number } }; |
| 107 | + assert.equal(body.ok, true); |
| 108 | + assert.equal(body.data.duplicate, false); |
| 109 | + assert.equal(body.data.accepted, 2); |
| 110 | + |
| 111 | + const again = await fetch(`${harness.url}/v1/decisions/batch`, { |
| 112 | + method: 'POST', |
| 113 | + headers: { ...authHeaders(apiKey), 'Content-Type': 'application/json' }, |
| 114 | + body: JSON.stringify(batch), |
| 115 | + }); |
| 116 | + assert.equal(again.status, 200); |
| 117 | + const againBody = (await again.json()) as { data: { duplicate: boolean } }; |
| 118 | + assert.equal(againBody.data.duplicate, true); |
| 119 | + |
| 120 | + const samples = await pool.query(`SELECT action FROM decision_samples ORDER BY wal_seq`); |
| 121 | + assert.ok(samples.rows.some((r: { action: string }) => r.action === 'deny')); |
| 122 | + |
| 123 | + const aggs = await pool.query( |
| 124 | + `SELECT SUM(count)::int AS n FROM decision_aggregates WHERE dimension_kind = 'all'` |
| 125 | + ); |
| 126 | + assert.equal(aggs.rows[0].n, 2); |
| 127 | + }); |
| 128 | + |
| 129 | + it('returns 409 when stored batch_id hash conflicts with a redelivery', async () => { |
| 130 | + const decidedAt = new Date().toISOString(); |
| 131 | + const batch = buildBatch([ |
| 132 | + { |
| 133 | + wal_seq: 1, |
| 134 | + fingerprint: 'fp-a', |
| 135 | + action: 'deny', |
| 136 | + decided_at: decidedAt, |
| 137 | + }, |
| 138 | + ]); |
| 139 | + const first = await fetch(`${harness.url}/v1/decisions/batch`, { |
| 140 | + method: 'POST', |
| 141 | + headers: { ...authHeaders(apiKey), 'Content-Type': 'application/json' }, |
| 142 | + body: JSON.stringify(batch), |
| 143 | + }); |
| 144 | + assert.equal(first.status, 200); |
| 145 | + |
| 146 | + // Simulate a corrupted / poisoned stored hash for the same batch_id. |
| 147 | + await pool.query(`UPDATE decision_batches SET payload_hash = $1 WHERE batch_id = $2`, [ |
| 148 | + '1'.repeat(64), |
| 149 | + batch.batch_id, |
| 150 | + ]); |
| 151 | + |
| 152 | + const resp = await fetch(`${harness.url}/v1/decisions/batch`, { |
| 153 | + method: 'POST', |
| 154 | + headers: { ...authHeaders(apiKey), 'Content-Type': 'application/json' }, |
| 155 | + body: JSON.stringify(batch), |
| 156 | + }); |
| 157 | + assert.equal(resp.status, 409); |
| 158 | + }); |
| 159 | + |
| 160 | + it('rejects non-RFC3339 decided_at', async () => { |
| 161 | + const resp = await fetch(`${harness.url}/v1/decisions/batch`, { |
| 162 | + method: 'POST', |
| 163 | + headers: { ...authHeaders(apiKey), 'Content-Type': 'application/json' }, |
| 164 | + body: JSON.stringify({ |
| 165 | + batch_id: '11111111-1111-1111-1111-111111111111', |
| 166 | + first_wal_seq: 1, |
| 167 | + last_wal_seq: 1, |
| 168 | + payload_hash: 'a'.repeat(64), |
| 169 | + decisions: [ |
| 170 | + { |
| 171 | + wal_seq: 1, |
| 172 | + fingerprint: 'fp-a', |
| 173 | + action: 'deny', |
| 174 | + decided_at: '2026-07-31 12:00:00', |
| 175 | + }, |
| 176 | + ], |
| 177 | + }), |
| 178 | + }); |
| 179 | + assert.equal(resp.status, 400); |
| 180 | + }); |
| 181 | +}); |
0 commit comments