import * as TestApp from '../../../test/App.js' import * as Organizations from './organizations.js' import * as VerifiedTokenRequests from './verifiedTokenRequests.js' const input = { address: '0x20c00000000000000000000000000000000000aa', chainId: 42431, currency: 'USD', decimals: 6, name: 'Test USD', orgId: 'org_1a2b3c4d5e6f7g8h9j0k1m2n', requestedBy: 'usr_1a2b3c4d5e6f7g8h9j0k1m2n', symbol: 'tUSD', } as const describe('create', () => { test('inserts a pending request', async () => { const db = TestApp.database() const record = await VerifiedTokenRequests.create(db, { ...input, note: 'We are the issuer.' }) expect(record).toBeDefined() const { createdAt, id, updatedAt, ...rest } = record! expect(id).toMatch(/^vtr_/) expect(Date.parse(createdAt)).not.toBeNaN() expect(updatedAt).toBe(createdAt) expect(rest).toMatchInlineSnapshot(` { "address": "0x20c00000000000000000000000000000000000aa", "chainId": 42431, "currency": "USD", "decimals": 6, "logo": null, "logoUri": null, "name": "Test USD", "note": "We are the issuer.", "orgId": "org_1a2b3c4d5e6f7g8h9j0k1m2n", "requestedBy": "usr_1a2b3c4d5e6f7g8h9j0k1m2n", "reviewNote": null, "reviewedAt": null, "reviewedBy": null, "status": "pending", "symbol": "tUSD", } `) }) test('a duplicate pending (chain, address) yields undefined, even cross-org', async () => { const db = TestApp.database() await VerifiedTokenRequests.create(db, input) const duplicate = await VerifiedTokenRequests.create(db, { ...input, orgId: 'org_2b3c4d5e6f7g8h9j0k1m2n3p', }) expect(duplicate).toBeUndefined() }) test('a reviewed request frees the (chain, address) slot', async () => { const db = TestApp.database() const first = await VerifiedTokenRequests.create(db, input) await VerifiedTokenRequests.review(db, first!.id, { reviewedBy: 'admin@tempo.xyz', status: 'denied', }) const second = await VerifiedTokenRequests.create(db, input) expect(second).toBeDefined() }) test('the same address on another chain is independent', async () => { const db = TestApp.database() await VerifiedTokenRequests.create(db, input) const other = await VerifiedTokenRequests.create(db, { ...input, chainId: 4217 }) expect(other).toBeDefined() }) test("atomically caps pending requests across a requester's organizations", async () => { const db = TestApp.database() const attempts = await Promise.allSettled( Array.from({ length: VerifiedTokenRequests.maxPendingPerRequester + 1 }, (_, index) => VerifiedTokenRequests.create(db, { ...input, address: `0x${(index + 1).toString(16).padStart(40, '0')}`, orgId: `org_${index}`, symbol: `T${index}`, }), ), ) expect(attempts.filter((attempt) => attempt.status === 'fulfilled')).toHaveLength( VerifiedTokenRequests.maxPendingPerRequester, ) expect(attempts.filter((attempt) => attempt.status === 'rejected')).toMatchObject([ { reason: { name: 'VerifiedTokenRequests.PendingLimitError' } }, ]) }) }) describe('list', () => { test('returns only the organization, newest first', async () => { const db = TestApp.database() const first = await VerifiedTokenRequests.create(db, input) const second = await VerifiedTokenRequests.create(db, { ...input, address: '0x20c00000000000000000000000000000000000bb', symbol: 'tEUR', }) await VerifiedTokenRequests.create(db, { ...input, address: '0x20c00000000000000000000000000000000000cc', orgId: 'org_2b3c4d5e6f7g8h9j0k1m2n3p', symbol: 'tGBP', }) const records = await VerifiedTokenRequests.list(db, input.orgId) expect(records.map(({ id }) => id).sort()).toEqual([first!.id, second!.id].sort()) expect(records.every(({ orgId }) => orgId === input.orgId)).toBe(true) }) }) describe('listByStatus', () => { test('filters by status and joins the current org name', async () => { const db = TestApp.database() const org = await Organizations.create(db, { name: 'Acme' }) const pending = await VerifiedTokenRequests.create(db, { ...input, orgId: org.id }) const denied = await VerifiedTokenRequests.create(db, { ...input, address: '0x20c00000000000000000000000000000000000bb', symbol: 'tEUR', }) await VerifiedTokenRequests.review(db, denied!.id, { reviewedBy: 'admin@tempo.xyz', status: 'denied', }) const queue = await VerifiedTokenRequests.listByStatus(db, 'pending', { limit: 10 }) expect(queue.map(({ id, orgName }) => ({ id, orgName }))).toEqual([ { id: pending!.id, orgName: 'Acme' }, ]) // The denied row's org has no organizations row; the join yields null. const reviewed = await VerifiedTokenRequests.listByStatus(db, 'denied', { limit: 10 }) expect(reviewed.map(({ id, orgName }) => ({ id, orgName }))).toEqual([ { id: denied!.id, orgName: null }, ]) }) test('continues after a stable oldest-first cursor', async () => { const db = TestApp.database() for (const [suffix, symbol] of [ ['bb', 'tEUR'], ['cc', 'tGBP'], ] as const) await VerifiedTokenRequests.create(db, { ...input, address: `0x20c00000000000000000000000000000000000${suffix}`, symbol, }) await VerifiedTokenRequests.create(db, input) const all = await VerifiedTokenRequests.listByStatus(db, 'pending', { limit: 10 }) const first = await VerifiedTokenRequests.listByStatus(db, 'pending', { limit: 2 }) const last = first.at(-1)! const second = await VerifiedTokenRequests.listByStatus(db, 'pending', { cursor: { createdAt: last.createdAt, id: last.id }, limit: 2, }) expect([...first, ...second].map(({ id }) => id)).toEqual(all.map(({ id }) => id)) }) }) describe('remove', () => { test('deletes only in-org pending requests', async () => { const db = TestApp.database() const record = await VerifiedTokenRequests.create(db, input) expect( await VerifiedTokenRequests.remove(db, 'org_2b3c4d5e6f7g8h9j0k1m2n3p', record!.id), ).toBeUndefined() expect(await VerifiedTokenRequests.remove(db, input.orgId, record!.id)).toBeDefined() expect(await VerifiedTokenRequests.get(db, record!.id)).toBeUndefined() }) test('leaves reviewed requests in place', async () => { const db = TestApp.database() const record = await VerifiedTokenRequests.create(db, input) await VerifiedTokenRequests.review(db, record!.id, { reviewedBy: 'admin@tempo.xyz', status: 'approved', }) expect(await VerifiedTokenRequests.remove(db, input.orgId, record!.id)).toBeUndefined() expect(await VerifiedTokenRequests.get(db, record!.id)).toBeDefined() }) }) describe('review', () => { test('marks a pending request with reviewer identity', async () => { const db = TestApp.database() const record = await VerifiedTokenRequests.create(db, input) const reviewed = await VerifiedTokenRequests.review(db, record!.id, { reviewNote: 'Logo unreachable.', reviewedBy: 'admin@tempo.xyz', status: 'denied', }) expect(reviewed).toMatchObject({ reviewNote: 'Logo unreachable.', reviewedBy: 'admin@tempo.xyz', status: 'denied', }) expect(Date.parse(reviewed!.reviewedAt!)).not.toBeNaN() }) test('yields undefined once reviewed', async () => { const db = TestApp.database() const record = await VerifiedTokenRequests.create(db, input) await VerifiedTokenRequests.review(db, record!.id, { reviewedBy: 'admin@tempo.xyz', status: 'approved', }) expect( await VerifiedTokenRequests.review(db, record!.id, { reviewedBy: 'admin@tempo.xyz', status: 'denied', }), ).toBeUndefined() }) })