|
3 | 3 | const test = require('node:test'); |
4 | 4 | const assert = require('node:assert/strict'); |
5 | 5 |
|
6 | | -const handler = require('../api/claim/commandlayer-namespace'); |
7 | | - |
8 | 6 | function makeRes() { |
9 | 7 | return { |
10 | 8 | statusCode: 200, |
@@ -34,71 +32,90 @@ function validBody() { |
34 | 32 | }; |
35 | 33 | } |
36 | 34 |
|
| 35 | +function loadHandlerWithMockQuery(mockQuery) { |
| 36 | + const handlerPath = require.resolve('../api/claim/commandlayer-namespace'); |
| 37 | + const dbPath = require.resolve('../lib/db'); |
| 38 | + delete require.cache[handlerPath]; |
| 39 | + delete require.cache[dbPath]; |
| 40 | + require.cache[dbPath] = { exports: { query: mockQuery, getDatabaseUrl: () => process.env.DATABASE_URL } }; |
| 41 | + return require('../api/claim/commandlayer-namespace'); |
| 42 | +} |
| 43 | + |
37 | 44 | test('rejects non-POST', async () => { |
| 45 | + const handler = loadHandlerWithMockQuery(async () => ({ rows: [] })); |
38 | 46 | const res = makeRes(); |
39 | 47 | await handler({ method: 'GET', body: validBody() }, res); |
40 | 48 | assert.equal(res.statusCode, 405); |
41 | 49 | }); |
42 | 50 |
|
43 | | -test('rejects missing authenticatedAddress', async () => { |
44 | | - const body = validBody(); |
45 | | - delete body.authenticatedAddress; |
| 51 | +test('missing DATABASE_URL returns STORAGE_UNAVAILABLE for valid payload', async () => { |
| 52 | + const original = process.env.DATABASE_URL; |
| 53 | + delete process.env.DATABASE_URL; |
| 54 | + const handler = loadHandlerWithMockQuery(async () => ({ rows: [] })); |
46 | 55 | const res = makeRes(); |
47 | | - await handler({ method: 'POST', body }, res); |
48 | | - assert.equal(res.statusCode, 400); |
49 | | - assert.equal(res.body.error, 'invalid_authenticated_address'); |
| 56 | + await handler({ method: 'POST', body: validBody() }, res); |
| 57 | + assert.equal(res.statusCode, 503); |
| 58 | + assert.equal(res.body.status, 'STORAGE_UNAVAILABLE'); |
| 59 | + process.env.DATABASE_URL = original; |
50 | 60 | }); |
51 | 61 |
|
52 | | -test('rejects invalid tenant', async () => { |
| 62 | +test('invalid tenant fails before DB', async () => { |
| 63 | + process.env.DATABASE_URL = 'postgres://example.com/db'; |
| 64 | + let dbCalled = false; |
| 65 | + const handler = loadHandlerWithMockQuery(async () => { dbCalled = true; return { rows: [] }; }); |
53 | 66 | const body = validBody(); |
54 | 67 | body.tenant = 'Acme'; |
55 | 68 | const res = makeRes(); |
56 | 69 | await handler({ method: 'POST', body }, res); |
57 | 70 | assert.equal(res.statusCode, 400); |
58 | 71 | assert.equal(res.body.error, 'invalid_tenant'); |
| 72 | + assert.equal(dbCalled, false); |
59 | 73 | }); |
60 | 74 |
|
61 | | -test('rejects .eth tenant', async () => { |
| 75 | +test('unsupported pack fails before DB', async () => { |
| 76 | + process.env.DATABASE_URL = 'postgres://example.com/db'; |
| 77 | + let dbCalled = false; |
| 78 | + const handler = loadHandlerWithMockQuery(async () => { dbCalled = true; return { rows: [] }; }); |
62 | 79 | const body = validBody(); |
63 | | - body.tenant = 'acme.eth'; |
| 80 | + body.packId = 'commerce'; |
64 | 81 | const res = makeRes(); |
65 | 82 | await handler({ method: 'POST', body }, res); |
66 | 83 | assert.equal(res.statusCode, 400); |
67 | | - assert.equal(res.body.error, 'invalid_tenant'); |
| 84 | + assert.equal(res.body.error, 'unsupported_pack'); |
| 85 | + assert.equal(dbCalled, false); |
68 | 86 | }); |
69 | 87 |
|
70 | | -test('rejects too many capabilities', async () => { |
71 | | - const body = validBody(); |
72 | | - body.capabilities = ['sign', 'attest', 'authorize', 'approve', 'reject', 'permit', 'grant', 'authenticate', 'endorse', 'verify', 'extra']; |
73 | | - const res = makeRes(); |
74 | | - await handler({ method: 'POST', body }, res); |
75 | | - assert.equal(res.statusCode, 400); |
76 | | - assert.equal(res.body.error, 'invalid_capabilities'); |
77 | | -}); |
| 88 | +test('valid payload with mocked DB returns CLAIM_REQUEST_CREATED', async () => { |
| 89 | + process.env.DATABASE_URL = 'postgres://example.com/db'; |
| 90 | + const calls = []; |
| 91 | + const handler = loadHandlerWithMockQuery(async (text, params) => { |
| 92 | + calls.push({ text, params }); |
| 93 | + return { rows: [] }; |
| 94 | + }); |
78 | 95 |
|
79 | | -test('rejects malformed publicKey', async () => { |
80 | | - const body = validBody(); |
81 | | - body.publicKey = 'nope'; |
82 | 96 | const res = makeRes(); |
83 | | - await handler({ method: 'POST', body }, res); |
84 | | - assert.equal(res.statusCode, 400); |
85 | | - assert.equal(res.body.error, 'invalid_public_key'); |
| 97 | + await handler({ method: 'POST', body: validBody() }, res); |
| 98 | + assert.equal(res.statusCode, 200); |
| 99 | + assert.equal(res.body.ok, true); |
| 100 | + assert.equal(res.body.status, 'CLAIM_REQUEST_CREATED'); |
| 101 | + assert.match(res.body.claimId, /^clm_[a-f0-9]{32}$/); |
| 102 | + assert.equal(Array.isArray(res.body.agents), true); |
| 103 | + assert.equal(calls.length >= 5, true); |
86 | 104 | }); |
87 | 105 |
|
88 | | -test('rejects non-trust pack', async () => { |
89 | | - const body = validBody(); |
90 | | - body.packId = 'commerce'; |
91 | | - const res = makeRes(); |
92 | | - await handler({ method: 'POST', body }, res); |
93 | | - assert.equal(res.statusCode, 400); |
94 | | - assert.equal(res.body.error, 'unsupported_pack'); |
95 | | -}); |
| 106 | +test('claim.created event insertion is attempted', async () => { |
| 107 | + process.env.DATABASE_URL = 'postgres://example.com/db'; |
| 108 | + const calls = []; |
| 109 | + const handler = loadHandlerWithMockQuery(async (text, params) => { |
| 110 | + calls.push({ text, params }); |
| 111 | + return { rows: [] }; |
| 112 | + }); |
96 | 113 |
|
97 | | -test('accepts valid Trust Verification request', async () => { |
98 | 114 | const res = makeRes(); |
99 | 115 | await handler({ method: 'POST', body: validBody() }, res); |
100 | | - assert.equal(res.statusCode, 200); |
101 | | - assert.equal(res.body.ok, true); |
102 | | - assert.equal(res.body.status, 'CLAIM_REQUEST_VALIDATED'); |
103 | | - assert.match(res.body.claimId, /^clm_[a-f0-9]{24}$/); |
| 116 | + |
| 117 | + const eventInsert = calls.find((entry) => String(entry.text).includes('insert into claim_events')); |
| 118 | + assert.ok(eventInsert); |
| 119 | + assert.equal(eventInsert.params[1], 'claim.created'); |
| 120 | + assert.equal(eventInsert.params[2], 'CommandLayer namespace claim request created.'); |
104 | 121 | }); |
0 commit comments