55 lines
1.7 KiB
JavaScript
55 lines
1.7 KiB
JavaScript
import test, { beforeEach } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import request from 'supertest';
|
|
import { createApp } from '../src/app.js';
|
|
import { __resetIdempotencyStoreForTests } from '../src/services/idempotency-store.js';
|
|
|
|
process.env.AUTH_BYPASS = 'true';
|
|
|
|
beforeEach(() => {
|
|
process.env.IDEMPOTENCY_STORE = 'memory';
|
|
delete process.env.IDEMPOTENCY_DATABASE_URL;
|
|
__resetIdempotencyStoreForTests();
|
|
});
|
|
|
|
test('GET /healthz returns healthy response', async () => {
|
|
const app = createApp();
|
|
const res = await request(app).get('/healthz');
|
|
|
|
assert.equal(res.status, 200);
|
|
assert.equal(res.body.ok, true);
|
|
assert.equal(typeof res.body.requestId, 'string');
|
|
});
|
|
|
|
test('command route requires idempotency key', async () => {
|
|
const app = createApp();
|
|
const res = await request(app)
|
|
.post('/commands/orders/create')
|
|
.set('Authorization', 'Bearer test-token')
|
|
.send({ payload: {} });
|
|
|
|
assert.equal(res.status, 400);
|
|
assert.equal(res.body.code, 'MISSING_IDEMPOTENCY_KEY');
|
|
});
|
|
|
|
test('command route is idempotent by key', async () => {
|
|
const app = createApp();
|
|
|
|
const first = await request(app)
|
|
.post('/commands/orders/create')
|
|
.set('Authorization', 'Bearer test-token')
|
|
.set('Idempotency-Key', 'abc-123')
|
|
.send({ payload: { order: 'x' } });
|
|
|
|
const second = await request(app)
|
|
.post('/commands/orders/create')
|
|
.set('Authorization', 'Bearer test-token')
|
|
.set('Idempotency-Key', 'abc-123')
|
|
.send({ payload: { order: 'x' } });
|
|
|
|
assert.equal(first.status, 200);
|
|
assert.equal(second.status, 200);
|
|
assert.equal(first.body.commandId, second.body.commandId);
|
|
assert.equal(first.body.idempotencyKey, 'abc-123');
|
|
});
|