import * as TestApp from '../../../test/App.js' import * as TestRoutes from '../../../test/Routes.js' import * as Transfer from '../../internal/routes/Transfer.js' import * as RoutesTransferTransactions from './routesTransferTransactions.js' const db = TestApp.database() function createTransfer(id: string) { return Transfer.create(db, { apiKeyId: 'key_test', environment: 'production', id, orgId: 'org_test', snapshot: TestRoutes.transferSnapshot(), }) } describe('insert', () => { test('canonicalizes EVM hashes before enforcing source uniqueness', async () => { const first = await createTransfer('rtr_evm_first') const second = await createTransfer('rtr_evm_second') const transactionRef = `0x${'aB'.repeat(32)}` const inserted = await RoutesTransferTransactions.insert(db, { chainId: 'eip155:8453', createdAt: '2026-01-01T00:00:01.000Z', role: 'source', transactionRef, transferId: first.id, }) expect(inserted?.transactionRef).toBe(transactionRef.toLowerCase()) await expect( RoutesTransferTransactions.insert(db, { chainId: 'eip155:8453', createdAt: '2026-01-01T00:00:02.000Z', role: 'source', transactionRef: transactionRef.toUpperCase(), transferId: second.id, }), ).rejects.toMatchObject({ constraint: 'routes_transfer_transactions_source_owner_unique' }) }) test('preserves case-sensitive non-EVM transaction references', async () => { const first = await createTransfer('rtr_solana_first') const second = await createTransfer('rtr_solana_second') const transactionRef = 'A'.repeat(87) const uppercase = await RoutesTransferTransactions.insert(db, { chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp', createdAt: '2026-01-01T00:00:01.000Z', role: 'source', transactionRef, transferId: first.id, }) const lowercase = await RoutesTransferTransactions.insert(db, { chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp', createdAt: '2026-01-01T00:00:02.000Z', role: 'source', transactionRef: transactionRef.toLowerCase(), transferId: second.id, }) expect([uppercase?.transactionRef, lowercase?.transactionRef]).toEqual([ transactionRef, transactionRef.toLowerCase(), ]) }) }) describe('claimSource', () => { test('returns claimed, replayed, and conflicting ownership outcomes', async () => { const first = await createTransfer('rtr_claim_first') const second = await createTransfer('rtr_claim_second') const record = { chainId: 'eip155:8453', createdAt: '2026-01-01T00:00:01.000Z', role: 'source' as const, transactionRef: `0x${'cd'.repeat(32)}`, transferId: first.id, } const claimed = await RoutesTransferTransactions.claimSource(db, record) const replayed = await RoutesTransferTransactions.claimSource(db, record) const conflict = await RoutesTransferTransactions.claimSource(db, { ...record, transferId: second.id, }) expect([claimed.type, replayed.type, conflict.type]).toEqual([ 'claimed', 'replayed', 'conflict', ]) }) }) describe('getSource', () => { test('selects the tenant owner when source references are shared', async () => { const database = TestApp.database() const owners = [] for (const orgId of ['org_first', 'org_second']) owners.push( await Transfer.create(database, { apiKeyId: `key_${orgId}`, environment: 'production', orgId, snapshot: TestRoutes.transferSnapshot(), }), ) const transactionRef = `0x${'ef'.repeat(32)}` for (const owner of owners) await RoutesTransferTransactions.insert(database, { chainId: 'eip155:8453', createdAt: owner.createdAt, role: 'source', transactionRef, transferId: owner.id, }) for (const owner of owners) expect( ( await RoutesTransferTransactions.getSource(database, { chainId: 'eip155:8453', transactionRef, transferId: owner.id, }) )?.transferId, ).toBe(owner.id) }) test('scopes ownership through transfer tenancy without depending on evidence columns', async () => { const database = TestApp.database() const owner = await Transfer.create(database, { apiKeyId: 'key_original', environment: 'production', orgId: 'org_owner', projectId: 'prj_owner', snapshot: TestRoutes.transferSnapshot(), }) const transactionRef = `0x${'de'.repeat(32)}` await RoutesTransferTransactions.claimSource(database, { chainId: 'eip155:8453', createdAt: owner.createdAt, role: 'source', transactionRef, transferId: owner.id, }) const scopes = [ { environment: 'production', orgId: 'org_owner', projectId: 'prj_owner' }, { environment: 'production', orgId: 'org_foreign', projectId: 'prj_owner' }, { environment: 'sandbox', orgId: 'org_owner', projectId: 'prj_owner' }, { environment: 'production', orgId: 'org_owner', projectId: 'prj_foreign' }, { environment: 'production', orgId: 'org_owner' }, ] as const const records = [] for (const scope of scopes) { const transfer = await Transfer.create(database, { ...scope, apiKeyId: 'key_rotated', snapshot: TestRoutes.transferSnapshot(), }) const record = await RoutesTransferTransactions.getSource(database, { chainId: 'eip155:8453', transactionRef, transferId: transfer.id, }) records.push(record?.transferId === owner.id) } expect(records).toMatchInlineSnapshot(` [ true, false, false, false, false, ] `) }) test('resolves EVM hashes case-insensitively', async () => { const transfer = await createTransfer('rtr_get_source') await RoutesTransferTransactions.claimSource(db, { chainId: 'eip155:8453', createdAt: '2026-01-01T00:00:01.000Z', role: 'source', transactionRef: `0x${'ce'.repeat(32)}`, transferId: transfer.id, }) const record = await RoutesTransferTransactions.getSource(db, { chainId: 'eip155:8453', transactionRef: `0x${'CE'.repeat(32)}`, transferId: transfer.id, }) expect(record?.transferId).toBe(transfer.id) }) })