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() }) }) 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') 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') expect(reviewed.map(({ id, orgName }) => ({ id, orgName }))).toEqual([ { id: denied!.id, orgName: null }, ]) }) }) describe('countPending', () => { test('counts only the organization pending requests', async () => { const db = TestApp.database() await VerifiedTokenRequests.create(db, input) const denied = await VerifiedTokenRequests.create(db, { ...input, address: '0x20c00000000000000000000000000000000000bb', symbol: 'tEUR', }) await VerifiedTokenRequests.review(db, denied!.id, { reviewedBy: 'admin@tempo.xyz', status: 'denied', }) await VerifiedTokenRequests.create(db, { ...input, address: '0x20c00000000000000000000000000000000000cc', orgId: 'org_2b3c4d5e6f7g8h9j0k1m2n3p', symbol: 'tGBP', }) expect(await VerifiedTokenRequests.countPending(db, input.orgId)).toBe(1) }) }) 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() }) })