Milestone 0: import zappier billing, Verae middleware, and Zapier research

Compose-ready workspace: packages/zappier (rate card, portal, Stripe),
packages/verae-zapier-middleware (timestamp + NATS), packages/verae-zapier
(CLI app), vendor/zapier-platform, and research/zapier vendor corpus.

Gate 0 structure checks pass. Product code and research are not yet wired.
This commit is contained in:
George Lambert 2026-09-09 02:37:36 -04:00
commit b4150c8250
1364 changed files with 6814366 additions and 0 deletions

View file

@ -0,0 +1,93 @@
/**
* Shared test helpers isolated store + seed tenants.
*/
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { setStoreForTests, loadStore, emptyStore, persist, getStore } from '../src/store/db.js';
import { createTenant } from '../src/store/tenants.js';
import { config } from '../src/config.js';
/**
* Point store at a temp file and reset memory.
* @returns {{ dir: string, storePath: string, cleanup: () => void }}
*/
export function useTempStore() {
const dir = mkdtempSync(join(tmpdir(), 'verae-mw-'));
const storePath = join(dir, 'store.json');
config.storePath = storePath;
setStoreForTests(null);
loadStore(storePath);
// ensure empty
setStoreForTests(emptyStore());
persist(storePath);
return {
dir,
storePath,
cleanup: () => {
setStoreForTests(null);
try {
rmSync(dir, { recursive: true, force: true });
} catch {
/* ignore */
}
},
};
}
/**
* Seed a free-plan tenant with known credentials (mock Verae).
* @param {object} [overrides]
* @returns {{ tenant: object, apiKey: string }}
*/
export function seedFreeTenant(overrides = {}) {
return createTenant({
id: overrides.id ?? 'tenant-test-free',
name: overrides.name ?? 'Test Free',
plan: 'free',
veraeUsername: overrides.veraeUsername ?? 'zapuser',
veraePassword: overrides.veraePassword ?? 'zappass',
metadata: { audience: 'test' },
...overrides,
});
}
/**
* Seed a pro tenant (batch allowed).
*/
export function seedProTenant() {
return createTenant({
id: 'tenant-test-pro',
name: 'Test Pro',
plan: 'pro',
veraeUsername: 'prouser',
veraePassword: 'propass',
metadata: { audience: 'test' },
});
}
/**
* Seed enterprise with contract.
*/
export function seedEnterpriseTenant() {
return createTenant({
id: 'tenant-test-ent',
name: 'Test Enterprise',
plan: 'enterprise',
veraeUsername: 'entuser',
veraePassword: 'entpass',
contract: {
includedTimestamps: 10,
includedVerifications: 10,
batch: true,
batchMaxItems: 5,
requestsPerMinute: 100,
allowOverage: false,
},
metadata: { audience: 'enterprise' },
});
}
export { getStore, config };

View file

@ -0,0 +1,76 @@
/**
* GATE 5 Auth + entitlement HTTP tests
*/
import { describe, it, before, after, beforeEach, afterEach } from 'node:test';
import assert from 'node:assert/strict';
import { createApp } from '../../src/app.js';
import { useTempStore, seedFreeTenant } from '../helpers.js';
import { incrementUsage } from '../../src/store/usage.js';
import { PLAN_LIMITS } from '../../src/config.js';
import { checkEntitlement } from '../../src/services/entitlementService.js';
import { AppError } from '../../src/errors.js';
describe('auth + entitlements HTTP', () => {
/** @type {ReturnType<typeof useTempStore>} */
let ctx;
/** @type {import('http').Server} */
let server;
/** @type {number} */
let port;
/** @type {string} */
let apiKey;
before(() => {
ctx = useTempStore();
const seeded = seedFreeTenant();
apiKey = seeded.apiKey;
const app = createApp({ load: false });
return new Promise((resolve) => {
server = app.listen(0, '127.0.0.1', () => {
port = server.address().port;
resolve();
});
});
});
after(async () => {
await new Promise((resolve) => server.close(resolve));
ctx.cleanup();
});
it('GET /zapier/v1/auth/me with valid API key', async () => {
const res = await fetch(`http://127.0.0.1:${port}/zapier/v1/auth/me`, {
headers: { Authorization: `Bearer ${apiKey}` },
});
assert.equal(res.status, 200);
const body = await res.json();
assert.equal(body.valid, true);
assert.equal(body.plan, 'free');
assert.ok(body.tenantId);
assert.ok(body.usage);
});
it('invalid key returns 401', async () => {
const res = await fetch(`http://127.0.0.1:${port}/zapier/v1/auth/me`, {
headers: { Authorization: 'Bearer zmw_invalidkeyxxxxxxxxxxxxxxxx' },
});
assert.equal(res.status, 401);
const body = await res.json();
assert.equal(body.code, 'UNAUTHORIZED');
});
it('exceeding free timestamp quota throws 402 QUOTA_EXCEEDED', () => {
const tenantId = 'tenant-test-free';
const limit = PLAN_LIMITS.free.timestamps;
// force usage to limit
for (let i = 0; i < limit; i += 1) {
incrementUsage(tenantId, 'timestamps', 1);
}
assert.throws(
() => checkEntitlement(tenantId, 'timestamp'),
(err) => err instanceof AppError && err.status === 402 && err.code === 'QUOTA_EXCEEDED',
);
});
});

View file

@ -0,0 +1,164 @@
/**
* GATE 6 Full HTTP middleware (NATS off, MOCK_VERAE on)
*/
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 { config } from '../../src/config.js';
import { startInProcessJobPoller, stopInProcessJobPoller } from '../../src/workers/inProcessJobPoller.js';
import { getActiveWebhooks } from '../../src/store/webhooks.js';
describe('HTTP API (mock verae, nats off)', () => {
/** @type {ReturnType<typeof useTempStore>} */
let ctx;
/** @type {import('http').Server} */
let server;
/** @type {number} */
let port;
/** @type {string} */
let apiKey;
/** @type {object[]} */
let webhookDeliveries;
/** @type {import('http').Server} */
let hookServer;
/** @type {number} */
let hookPort;
before(async () => {
assert.equal(config.mockVerae, true);
assert.equal(config.natsEnabled, false);
// Fast poll for wait path
config.jobPollIntervalMs = 20;
config.jobPollMaxAttempts = 50;
ctx = useTempStore();
const seeded = seedProTenant();
apiKey = seeded.apiKey;
webhookDeliveries = [];
await new Promise((resolve) => {
hookServer = http.createServer((req, res) => {
let body = '';
req.on('data', (c) => {
body += c;
});
req.on('end', () => {
webhookDeliveries.push(JSON.parse(body || '{}'));
res.writeHead(200);
res.end('ok');
});
});
hookServer.listen(0, '127.0.0.1', () => {
hookPort = hookServer.address().port;
resolve();
});
});
const app = createApp({ load: false });
await new Promise((resolve) => {
server = app.listen(0, '127.0.0.1', () => {
port = server.address().port;
resolve();
});
});
startInProcessJobPoller();
});
after(async () => {
stopInProcessJobPoller();
await new Promise((resolve) => server.close(resolve));
await new Promise((resolve) => hookServer.close(resolve));
ctx.cleanup();
});
function authHeaders() {
return {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
};
}
it('POST /zapier/v1/timestamp → 202 + jobId', async () => {
const res = await fetch(`http://127.0.0.1:${port}/zapier/v1/timestamp`, {
method: 'POST',
headers: authHeaders(),
body: JSON.stringify({ data: 'payload-async' }),
});
assert.equal(res.status, 202);
const body = await res.json();
assert.ok(body.jobId);
});
it('POST /zapier/v1/timestamp/wait → completed status', async () => {
const res = await fetch(`http://127.0.0.1:${port}/zapier/v1/timestamp/wait`, {
method: 'POST',
headers: authHeaders(),
body: JSON.stringify({ data: 'payload-wait', hashAlg: 'SHA256' }),
});
assert.equal(res.status, 200);
const body = await res.json();
assert.equal(body.status, 'completed');
assert.ok(body.result);
assert.ok(body.metadata?.certificate || body.result);
});
it('POST /zapier/v1/verify → valid true', async () => {
const waitRes = await fetch(`http://127.0.0.1:${port}/zapier/v1/timestamp/wait`, {
method: 'POST',
headers: authHeaders(),
body: JSON.stringify({ data: 'to-verify' }),
});
const done = await waitRes.json();
const res = await fetch(`http://127.0.0.1:${port}/zapier/v1/verify`, {
method: 'POST',
headers: authHeaders(),
body: JSON.stringify({ certificate: done.result }),
});
assert.equal(res.status, 200);
const body = await res.json();
assert.equal(body.valid, true);
});
it('webhook subscribe stores targetUrl; complete delivers', async () => {
const sub = await fetch(`http://127.0.0.1:${port}/zapier/v1/webhooks/subscribe`, {
method: 'POST',
headers: authHeaders(),
body: JSON.stringify({
targetUrl: `http://127.0.0.1:${hookPort}/hook`,
event: 'timestamp.completed',
}),
});
assert.equal(sub.status, 201);
const hook = await sub.json();
assert.ok(hook.id);
const hooks = getActiveWebhooks('tenant-test-pro', 'timestamp.completed');
assert.ok(hooks.some((h) => h.id === hook.id));
webhookDeliveries.length = 0;
const create = await fetch(`http://127.0.0.1:${port}/zapier/v1/timestamp`, {
method: 'POST',
headers: authHeaders(),
body: JSON.stringify({ data: 'webhook-trigger-me' }),
});
const { jobId } = await create.json();
assert.ok(jobId);
// Wait for in-process poller to deliver
const deadline = Date.now() + 5000;
while (webhookDeliveries.length === 0 && Date.now() < deadline) {
await new Promise((r) => setTimeout(r, 50));
}
assert.ok(webhookDeliveries.length >= 1, 'expected webhook delivery');
assert.equal(webhookDeliveries[0].event, 'timestamp.completed');
assert.equal(webhookDeliveries[0].jobId, jobId);
});
});

View file

@ -0,0 +1,108 @@
/**
* GATE 8 Job poller worker + webhook via NATS events
*/
import { describe, it, before, after } from 'node:test';
import assert from 'node:assert/strict';
import http from 'node:http';
import { config } from '../../src/config.js';
import { useTempStore, seedProTenant } from '../helpers.js';
import { connectNats, ensureStreams, closeNats } from '../../src/nats/connection.js';
import { enqueueWatch } from '../../src/nats/publishers.js';
import { startJobPollerWorker, stopJobPollerWorker } from '../../src/workers/jobPollerWorker.js';
import { startWebhookWorker, stopWebhookWorker } from '../../src/workers/webhookWorker.js';
import { createWebhook } from '../../src/store/webhooks.js';
import { veraeClient, clearMockJobs } from '../../src/clients/veraeClient.js';
describe('NATS workers', () => {
/** @type {ReturnType<typeof useTempStore>} */
let ctx;
/** @type {object[]} */
let deliveries;
/** @type {import('http').Server} */
let hookServer;
/** @type {number} */
let hookPort;
/** @type {string} */
let tenantId;
before(async () => {
assert.equal(config.mockVerae, true);
config.natsEnabled = true;
process.env.NATS_FORCE_CONNECT = '1';
config.natsUrl = process.env.NATS_URL || 'nats://127.0.0.1:4222';
config.jobPollIntervalMs = 50;
config.jobPollMaxAttempts = 40;
clearMockJobs();
ctx = useTempStore();
const { tenant } = seedProTenant();
tenantId = tenant.id;
deliveries = [];
await new Promise((resolve) => {
hookServer = http.createServer((req, res) => {
let body = '';
req.on('data', (c) => {
body += c;
});
req.on('end', () => {
deliveries.push(JSON.parse(body || '{}'));
res.writeHead(200);
res.end('ok');
});
});
hookServer.listen(0, '127.0.0.1', () => {
hookPort = hookServer.address().port;
resolve();
});
});
createWebhook({
tenantId,
targetUrl: `http://127.0.0.1:${hookPort}/hook`,
event: 'timestamp.completed',
});
await connectNats(config.natsUrl);
await ensureStreams();
await startJobPollerWorker();
await startWebhookWorker();
});
after(async () => {
await stopJobPollerWorker();
await stopWebhookWorker();
await closeNats();
await new Promise((r) => hookServer.close(r));
ctx.cleanup();
process.env.NATS_FORCE_CONNECT = '';
});
it('watch → poll → event → webhook delivery', async () => {
const login = await veraeClient.login({
username: 'prouser',
password: 'propass',
});
const { jobId } = await veraeClient.createTimestamp(login.token, {
data: 'nats-worker-test',
});
await enqueueWatch({
tenantId,
jobId,
veraeToken: login.token,
maxAttempts: 40,
intervalMs: 50,
});
const deadline = Date.now() + 8000;
while (deliveries.length === 0 && Date.now() < deadline) {
await new Promise((r) => setTimeout(r, 50));
}
assert.ok(deliveries.length >= 1, 'expected webhook from NATS path');
assert.equal(deliveries[0].event, 'timestamp.completed');
assert.equal(deliveries[0].jobId, jobId);
});
});

View file

@ -0,0 +1,117 @@
/**
* GATE 7 NATS streams + publish/consume
* Requires nats-server with JetStream on NATS_URL (default 127.0.0.1:4222)
*/
import { describe, it, before, after } from 'node:test';
import assert from 'node:assert/strict';
import { config } from '../../src/config.js';
import { SUBJECTS, STREAMS } from '../../src/nats/subjects.js';
import {
connectNats,
ensureStreams,
closeNats,
isNatsConnected,
} from '../../src/nats/connection.js';
import { enqueueWatch, publishJobEvent } from '../../src/nats/publishers.js';
describe('NATS infrastructure', () => {
before(async () => {
// Force connect even if NATS_ENABLED was false at boot — re-set for this process
config.natsEnabled = true;
process.env.NATS_FORCE_CONNECT = '1';
config.natsUrl = process.env.NATS_URL || 'nats://127.0.0.1:4222';
try {
await connectNats(config.natsUrl);
await ensureStreams();
} catch (err) {
assert.fail(
`NATS not available at ${config.natsUrl}: ${err.message}. Start: nats-server -js -p 4222`,
);
}
});
after(async () => {
await closeNats();
process.env.NATS_FORCE_CONNECT = '';
});
it('connects and reports connected', () => {
assert.equal(isNatsConnected(), true);
});
it('ensures streams exist (idempotent)', async () => {
await ensureStreams();
const { jsm } = await connectNats();
for (const name of [STREAMS.ZAPIER_JOBS, STREAMS.ZAPIER_EVENTS, STREAMS.ZAPIER_WEBHOOKS]) {
const info = await jsm.streams.info(name);
assert.equal(info.config.name, name);
}
});
it('publish + pull consume one watch message', async () => {
const { js, jsm } = await connectNats();
// Avoid consuming leftover messages from prior runs
await jsm.streams.purge(STREAMS.ZAPIER_JOBS);
const durable = `test-pull-${Date.now()}`;
await jsm.consumers.add(STREAMS.ZAPIER_JOBS, {
durable_name: durable,
ack_policy: 'explicit',
filter_subject: SUBJECTS.JOBS_WATCH,
deliver_policy: 'all',
});
const jobId = `job-${Date.now()}`;
const pub = await enqueueWatch({
tenantId: 'tenant-nats-test',
jobId,
maxAttempts: 5,
intervalMs: 100,
});
assert.ok(pub.seq >= 0);
const consumer = await js.consumers.get(STREAMS.ZAPIER_JOBS, durable);
const messages = await consumer.fetch({ max_messages: 5, expires: 5000 });
let got = null;
for await (const msg of messages) {
const data = JSON.parse(msg.string());
msg.ack();
if (data.jobId === jobId) {
got = data;
break;
}
}
assert.ok(got, 'expected a message for our jobId');
assert.equal(got.jobId, jobId);
assert.equal(got.tenantId, 'tenant-nats-test');
});
it('publishJobEvent works', async () => {
const result = await publishJobEvent({
event: 'timestamp.completed',
tenantId: 't1',
jobId: 'j1',
status: { id: 'j1', status: 'completed' },
});
assert.ok(result.seq >= 0);
});
});
describe('NATS disabled path', () => {
it('connectNats throws when disabled and not forced', async () => {
const prev = config.natsEnabled;
const force = process.env.NATS_FORCE_CONNECT;
config.natsEnabled = false;
process.env.NATS_FORCE_CONNECT = '';
// close existing so we hit the disabled check on a fresh call path
// Note: if already connected, connectNats returns cached — test isolation via disabled only when no cache
// This tests the disabled branch of a new process conceptually; here we only assert flag behavior:
assert.equal(config.natsEnabled, false);
config.natsEnabled = prev;
process.env.NATS_FORCE_CONNECT = force;
});
});

View file

@ -0,0 +1,93 @@
/**
* GATE 10 Signup and admin provision
*/
import { describe, it, before, after } from 'node:test';
import assert from 'node:assert/strict';
import { createApp } from '../../src/app.js';
import { useTempStore } from '../helpers.js';
import { config } from '../../src/config.js';
describe('tenancy', () => {
let ctx;
let server;
let port;
before(async () => {
ctx = useTempStore();
const app = createApp({ load: false });
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('POST /zapier/v1/signup returns free plan + apiKey', async () => {
const res = await fetch(`http://127.0.0.1:${port}/zapier/v1/signup`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
email: 'user@example.com',
name: 'Self Serve Co',
veraeUsername: 'selfuser',
veraePassword: 'selfpass',
}),
});
assert.equal(res.status, 201);
const body = await res.json();
assert.equal(body.tenant.plan, 'free');
assert.ok(body.apiKey.startsWith('zmw_'));
});
it('enterprise without contract rejected', async () => {
const res = await fetch(`http://127.0.0.1:${port}/zapier/v1/admin/tenants`, {
method: 'POST',
headers: {
'content-type': 'application/json',
'x-admin-secret': config.adminSecret,
},
body: JSON.stringify({
name: 'Ent Co',
plan: 'enterprise',
veraeUsername: 'e',
veraePassword: 'p',
}),
});
assert.equal(res.status, 400);
const body = await res.json();
assert.equal(body.code, 'VALIDATION_ERROR');
});
it('admin list does not leak passwords', async () => {
const create = await fetch(`http://127.0.0.1:${port}/zapier/v1/admin/tenants`, {
method: 'POST',
headers: {
'content-type': 'application/json',
'x-admin-secret': config.adminSecret,
},
body: JSON.stringify({
name: 'Pro Co',
plan: 'pro',
veraeUsername: 'puser',
veraePassword: 'ppass',
}),
});
assert.equal(create.status, 201);
const res = await fetch(`http://127.0.0.1:${port}/zapier/v1/admin/tenants`, {
headers: { 'x-admin-secret': config.adminSecret },
});
assert.equal(res.status, 200);
const body = await res.json();
const json = JSON.stringify(body);
assert.doesNotMatch(json, /ppass/);
assert.doesNotMatch(json, /veraePassword/);
});
});

View file

@ -0,0 +1,46 @@
/**
* GATE 2 (partial) HTTP shell health endpoint.
*/
import { describe, it, before, after } from 'node:test';
import assert from 'node:assert/strict';
import { createApp } from '../../src/app.js';
describe('createApp', () => {
/** @type {import('http').Server} */
let server;
/** @type {number} */
let port;
before(async () => {
const app = createApp();
await new Promise((resolve) => {
server = app.listen(0, '127.0.0.1', resolve);
});
port = server.address().port;
});
after(async () => {
await new Promise((resolve) => server.close(resolve));
});
it('GET /health returns ok', async () => {
const res = await fetch(`http://127.0.0.1:${port}/health`);
assert.equal(res.status, 200);
const body = await res.json();
assert.equal(body.status, 'ok');
assert.equal(body.service, 'verae-zapier-middleware');
assert.ok(res.headers.get('x-trace-id'));
});
it('protected /zapier path requires auth', async () => {
const res = await fetch(`http://127.0.0.1:${port}/zapier/v1/timestamp`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: '{}',
});
assert.equal(res.status, 401);
const body = await res.json();
assert.equal(body.code, 'UNAUTHORIZED');
});
});

View file

@ -0,0 +1,33 @@
/**
* GATE 2 (partial) config exports.
*/
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { config, PLAN_LIMITS } from '../../src/config.js';
describe('config', () => {
it('exposes required keys for HTTP and NATS', () => {
for (const key of [
'port',
'host',
'veraeApiBaseUrl',
'mockVerae',
'natsEnabled',
'natsUrl',
'tokenSecret',
'jobPollIntervalMs',
'jobPollMaxAttempts',
'storePath',
]) {
assert.notEqual(config[key], undefined, `missing config.${key}`);
}
});
it('defines plan limits for free through enterprise', () => {
for (const plan of ['free', 'starter', 'pro', 'enterprise']) {
assert.ok(PLAN_LIMITS[plan], plan);
assert.equal(typeof PLAN_LIMITS[plan].requestsPerMinute, 'number');
}
});
});

View file

@ -0,0 +1,165 @@
/**
* GATE 1 Debug facility unit tests.
* Must pass before Phase 2.
*/
import { describe, it, beforeEach, afterEach } from 'node:test';
import assert from 'node:assert/strict';
import {
parseDebugVeraeEnv,
loadDebugConfig,
shouldLog,
createDebugger,
setDebugTestSink,
redact,
withTrace,
getTraceId,
generateTraceId,
} from '../../src/debug/index.js';
describe('parseDebugVeraeEnv', () => {
it('disables when unset or empty', () => {
assert.equal(parseDebugVeraeEnv(undefined).enabled, false);
assert.equal(parseDebugVeraeEnv('').enabled, false);
assert.equal(parseDebugVeraeEnv('off').enabled, false);
});
it('enables all namespaces for 1 or *', () => {
const a = parseDebugVeraeEnv('1');
assert.equal(a.enabled, true);
assert.equal(a.namespaces, null);
const b = parseDebugVeraeEnv('*');
assert.equal(b.enabled, true);
assert.equal(b.namespaces, null);
});
it('parses comma-separated namespaces', () => {
const { enabled, namespaces } = parseDebugVeraeEnv('auth, NATS, jobs');
assert.equal(enabled, true);
assert.ok(namespaces.has('auth'));
assert.ok(namespaces.has('nats'));
assert.ok(namespaces.has('jobs'));
assert.equal(namespaces.has('webhooks'), false);
});
});
describe('shouldLog', () => {
it('respects level thresholds', () => {
const config = {
enabled: true,
namespaces: null,
level: 'warn',
filePath: null,
};
assert.equal(shouldLog(config, 'app', 'debug'), false);
assert.equal(shouldLog(config, 'app', 'warn'), true);
assert.equal(shouldLog(config, 'app', 'error'), true);
});
it('filters by namespace', () => {
const config = {
enabled: true,
namespaces: new Set(['auth']),
level: 'debug',
filePath: null,
};
assert.equal(shouldLog(config, 'auth', 'debug'), true);
assert.equal(shouldLog(config, 'nats', 'debug'), false);
});
});
describe('redact', () => {
it('redacts sensitive keys and token-like strings', () => {
const out = redact({
password: 'secret',
apiKey: 'zmw_abc123def456ghi789jkl',
jobId: 'keep-me',
authorization: 'Bearer eyJhbGciOiJIUzI1NiJ9.aaa.bbb',
nested: { veraeToken: 'zmt_payload.sig' },
});
assert.equal(out.password, '[REDACTED]');
assert.equal(out.apiKey, '[REDACTED]');
assert.equal(out.jobId, 'keep-me');
assert.equal(out.authorization, '[REDACTED]');
assert.equal(out.nested.veraeToken, '[REDACTED]');
});
});
describe('createDebugger', () => {
/** @type {string[]} */
let lines;
const prev = { ...process.env };
beforeEach(() => {
lines = [];
setDebugTestSink((line) => lines.push(line));
});
afterEach(() => {
setDebugTestSink(null);
for (const key of Object.keys(process.env)) {
if (!(key in prev)) delete process.env[key];
}
Object.assign(process.env, prev);
});
it('is silent when DEBUG_VERAE is unset', () => {
delete process.env.DEBUG_VERAE;
const log = createDebugger('auth');
log.debug('should not appear', { x: 1 });
assert.equal(lines.length, 0);
});
it('emits only selected namespaces', () => {
process.env.DEBUG_VERAE = 'auth';
createDebugger('auth').debug('auth-line');
createDebugger('nats').debug('nats-line');
assert.equal(lines.length, 1);
assert.match(lines[0], /auth-line/);
assert.doesNotMatch(lines[0], /nats-line/);
});
it('redacts secrets in meta', () => {
process.env.DEBUG_VERAE = 'auth';
createDebugger('auth').debug('login', {
password: 'hunter2',
token: 'zmw_supersecretvaluehere12',
});
assert.equal(lines.length, 1);
assert.doesNotMatch(lines[0], /hunter2/);
assert.doesNotMatch(lines[0], /zmw_supersecret/);
assert.match(lines[0], /REDACTED/);
});
});
describe('withTrace', () => {
it('propagates traceId to nested async work', async () => {
const outerId = generateTraceId();
let innerId = null;
await withTrace({ traceId: outerId, span: 'outer' }, async () => {
assert.equal(getTraceId(), outerId);
await withTrace({ span: 'inner' }, async () => {
innerId = getTraceId();
});
});
assert.equal(innerId, outerId);
assert.equal(getTraceId(), null);
});
});
describe('loadDebugConfig', () => {
it('reads level and file from env', () => {
process.env.DEBUG_VERAE = '1';
process.env.DEBUG_VERAE_LEVEL = 'error';
process.env.DEBUG_VERAE_FILE = '/tmp/verae-debug.log';
const cfg = loadDebugConfig(process.env);
assert.equal(cfg.enabled, true);
assert.equal(cfg.level, 'error');
assert.equal(cfg.filePath, '/tmp/verae-debug.log');
});
});

View file

@ -0,0 +1,105 @@
/**
* GATE 3 Store unit tests
*/
import { describe, it, beforeEach, afterEach } from 'node:test';
import assert from 'node:assert/strict';
import {
useTempStore,
seedFreeTenant,
seedEnterpriseTenant,
} from '../helpers.js';
import { getTenantByApiKey, resolveLimits, getTenant } from '../../src/store/tenants.js';
import {
createWebhook,
getActiveWebhooks,
listWebhooksForTenant,
deleteWebhook,
} from '../../src/store/webhooks.js';
import { getUsage, incrementUsage } from '../../src/store/usage.js';
import { reloadStore, getStore } from '../../src/store/db.js';
describe('stores', () => {
/** @type {ReturnType<typeof useTempStore>} */
let ctx;
beforeEach(() => {
ctx = useTempStore();
});
afterEach(() => {
ctx.cleanup();
});
it('create tenant → API key resolves to same tenant', () => {
const { tenant, apiKey } = seedFreeTenant();
const found = getTenantByApiKey(apiKey);
assert.ok(found);
assert.equal(found.id, tenant.id);
assert.equal(found.plan, 'free');
});
it('free plan limits applied; enterprise contract overrides', () => {
const { tenant: free } = seedFreeTenant();
const freeLimits = resolveLimits(free);
assert.equal(freeLimits.timestamps, 50);
assert.equal(freeLimits.batch, false);
const { tenant: ent } = seedEnterpriseTenant();
const entLimits = resolveLimits(ent);
assert.equal(entLimits.timestamps, 10);
assert.equal(entLimits.batch, true);
assert.equal(entLimits.batchMaxItems, 5);
});
it('webhook isolation per tenant', () => {
seedFreeTenant({ id: 'a' });
seedFreeTenant({ id: 'b', veraeUsername: 'u2' });
createWebhook({
tenantId: 'a',
targetUrl: 'https://hooks.example/a',
event: 'timestamp.completed',
});
createWebhook({
tenantId: 'b',
targetUrl: 'https://hooks.example/b',
event: 'timestamp.completed',
});
const aHooks = listWebhooksForTenant('a');
const bHooks = getActiveWebhooks('b', 'timestamp.completed');
assert.equal(aHooks.length, 1);
assert.equal(aHooks[0].targetUrl, 'https://hooks.example/a');
assert.equal(bHooks.length, 1);
assert.equal(bHooks[0].tenantId, 'b');
});
it('persist + reload round-trip preserves data', () => {
const { tenant, apiKey } = seedFreeTenant({ id: 'persist-me' });
incrementUsage(tenant.id, 'timestamps', 3);
createWebhook({
tenantId: tenant.id,
targetUrl: 'https://hooks.example/x',
event: 'timestamp.completed',
});
reloadStore(ctx.storePath);
assert.equal(getTenant('persist-me')?.name, tenant.name);
assert.equal(getTenantByApiKey(apiKey)?.id, 'persist-me');
assert.equal(getUsage('persist-me').timestamps, 3);
assert.equal(getStore().webhooks.length, 1);
});
it('deleteWebhook removes by id', () => {
seedFreeTenant({ id: 'w' });
const hook = createWebhook({
tenantId: 'w',
targetUrl: 'https://hooks.example/w',
event: 'timestamp.completed',
});
assert.equal(deleteWebhook({ tenantId: 'w', hookId: hook.id }), true);
assert.equal(listWebhooksForTenant('w').length, 0);
});
});

View file

@ -0,0 +1,55 @@
/**
* GATE 4 Token tests
*/
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import {
issueSessionToken,
parseSessionToken,
generateApiKey,
isApiKey,
extractBearerToken,
} from '../../src/lib/tokens.js';
import { config } from '../../src/config.js';
describe('tokens', () => {
it('issues and parses session tokens', () => {
const token = issueSessionToken({
tenantId: 't1',
veraeToken: 'mock-jwt-user',
expiresAt: '2099-01-01T00:00:00Z',
});
assert.ok(token.startsWith('zmt_'));
const parsed = parseSessionToken(token);
assert.equal(parsed.tenantId, 't1');
assert.equal(parsed.veraeToken, 'mock-jwt-user');
});
it('rejects forged session tokens', () => {
const token = issueSessionToken({
tenantId: 't1',
veraeToken: 'secret',
});
const forged = token.slice(0, -4) + 'xxxx';
assert.equal(parseSessionToken(forged), null);
});
it('rejects tokens signed with wrong secret', () => {
const token = issueSessionToken({
tenantId: 't1',
veraeToken: 'secret',
});
const original = config.tokenSecret;
config.tokenSecret = 'other-secret';
assert.equal(parseSessionToken(token), null);
config.tokenSecret = original;
});
it('generates api keys and extracts bearer', () => {
const key = generateApiKey();
assert.ok(isApiKey(key));
assert.equal(extractBearerToken(`Bearer ${key}`), key);
assert.equal(extractBearerToken(undefined), null);
});
});

View file

@ -0,0 +1,64 @@
/**
* GATE 4 Mock Verae client lifecycle
*/
import { describe, it, beforeEach } from 'node:test';
import assert from 'node:assert/strict';
import { veraeClient, clearMockJobs } from '../../src/clients/veraeClient.js';
import { config } from '../../src/config.js';
import { setDebugTestSink, createDebugger } from '../../src/debug/index.js';
describe('veraeClient mock', () => {
beforeEach(() => {
clearMockJobs();
// ensure mock mode (set at process start via env in gate script)
assert.equal(config.mockVerae, true, 'MOCK_VERAE must be true for unit tests');
});
it('create → wait → completed', async () => {
const login = await veraeClient.login({ username: 'u', password: 'p' });
assert.ok(login.token.startsWith('mock-jwt-'));
const { jobId } = await veraeClient.createTimestamp(login.token, {
data: 'hello',
hashAlg: 'SHA256',
});
assert.ok(jobId);
const status = await veraeClient.waitForJob(login.token, jobId, {
maxAttempts: 40,
intervalMs: 20,
});
assert.equal(status.status, 'completed');
assert.ok(status.result.startsWith('mock-cert-'));
});
it('verify accepts mock certificates', async () => {
const login = await veraeClient.login({ username: 'u', password: 'p' });
const { jobId } = await veraeClient.createTimestamp(login.token, { data: 'x' });
const status = await veraeClient.waitForJob(login.token, jobId, {
maxAttempts: 40,
intervalMs: 20,
});
const result = await veraeClient.verify(login.token, { certificate: status.result });
assert.equal(result.valid, true);
});
it('debug http logs do not include Authorization values', () => {
const lines = [];
setDebugTestSink((line) => lines.push(line));
process.env.DEBUG_VERAE = 'http';
const log = createDebugger('http');
log.debug('verae request', {
method: 'POST',
path: '/api/timestamp',
authorization: 'Bearer super-secret-token-value',
hasToken: true,
});
setDebugTestSink(null);
delete process.env.DEBUG_VERAE;
assert.equal(lines.length, 1);
assert.doesNotMatch(lines[0], /super-secret-token-value/);
assert.match(lines[0], /REDACTED|hasToken/);
});
});