/** * Hash idempotent register + lookup (mock Verae) */ import { describe, it, before, after } from 'node:test'; import assert from 'node:assert/strict'; import http from 'node:http'; import { createApp } from '../../src/app.js'; import { useTempStore, seedProTenant } from '../helpers.js'; import { clearMockJobs, sha256Hex } from '../../src/clients/veraeClient.js'; import { config } from '../../src/config.js'; describe('hash register + lookup', () => { let ctx; /** @type {import('http').Server} */ let server; let port; let apiKey; before(async () => { config.mockVerae = true; config.natsEnabled = false; clearMockJobs(); ctx = useTempStore(); const seeded = seedProTenant(); apiKey = seeded.apiKey; const app = createApp(); await new Promise((resolve) => { server = app.listen(0, '127.0.0.1', () => { port = server.address().port; resolve(); }); }); }); after(async () => { await new Promise((r) => server.close(r)); ctx.cleanup(); }); it('registers sha256, second create returns original jobId', async () => { const data = 'hello-hash'; const sha = sha256Hex(data); const r1 = await json(port, 'POST', '/zapier/v1/timestamp', apiKey, { data }); assert.equal(r1.status, 202); const r2 = await json(port, 'POST', '/zapier/v1/timestamp', apiKey, { sha256: sha }); assert.equal(r2.status, 202); assert.equal(r2.body.jobId, r1.body.jobId); assert.equal(r2.body.existing, true); const look = await json(port, 'GET', `/zapier/v1/hashes/${sha}`, apiKey); assert.equal(look.status, 200); assert.equal(look.body.exists, true); assert.equal(look.body.jobId, r1.body.jobId); }); it('lookup miss is 404', async () => { const miss = 'a'.repeat(64); const look = await json(port, 'GET', `/zapier/v1/hashes/${miss}`, apiKey); assert.equal(look.status, 404); }); }); function json(port, method, path, apiKey, body) { return new Promise((resolve, reject) => { const req = http.request( { hostname: '127.0.0.1', port, path, method, headers: { authorization: `Bearer ${apiKey}`, 'content-type': 'application/json', }, }, (res) => { let data = ''; res.on('data', (c) => { data += c; }); res.on('end', () => { let parsed = {}; try { parsed = data ? JSON.parse(data) : {}; } catch { parsed = { raw: data }; } resolve({ status: res.statusCode, body: parsed }); }); }, ); req.on('error', reject); if (body) req.write(JSON.stringify(body)); req.end(); }); }