import * as TestApp from '../../../test/App.js' import * as TestFunding from '../../../test/Funding.js' import * as Transfer from '../../internal/funding/Transfer.js' import * as FundingTransferTransactions from './fundingTransferTransactions.js' const db = TestApp.database() function createTransfer(id: string) { return Transfer.create(db, { apiKeyId: 'key_test', environment: 'production', id, orgId: 'org_test', snapshot: TestFunding.transferSnapshot(), }) } describe('insert', () => { test('canonicalizes EVM hashes before enforcing source uniqueness', async () => { const first = await createTransfer('ftr_evm_first') const second = await createTransfer('ftr_evm_second') const transactionRef = `0x${'aB'.repeat(32)}` const inserted = await FundingTransferTransactions.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( FundingTransferTransactions.insert(db, { chainId: 'eip155:8453', createdAt: '2026-01-01T00:00:02.000Z', role: 'source', transactionRef: transactionRef.toUpperCase(), transferId: second.id, }), ).rejects.toMatchObject({ constraint: 'funding_transfer_transactions_source_unique' }) }) test('preserves case-sensitive non-EVM transaction references', async () => { const first = await createTransfer('ftr_solana_first') const second = await createTransfer('ftr_solana_second') const transactionRef = 'A'.repeat(87) const uppercase = await FundingTransferTransactions.insert(db, { chainId: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp', createdAt: '2026-01-01T00:00:01.000Z', role: 'source', transactionRef, transferId: first.id, }) const lowercase = await FundingTransferTransactions.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('ftr_claim_first') const second = await createTransfer('ftr_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 FundingTransferTransactions.claimSource(db, record) const replayed = await FundingTransferTransactions.claimSource(db, record) const conflict = await FundingTransferTransactions.claimSource(db, { ...record, transferId: second.id, }) expect([claimed.type, replayed.type, conflict.type]).toEqual([ 'claimed', 'replayed', 'conflict', ]) }) }) describe('getSource', () => { test('resolves EVM hashes case-insensitively', async () => { const transfer = await createTransfer('ftr_get_source') await FundingTransferTransactions.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 FundingTransferTransactions.getSource(db, { chainId: 'eip155:8453', transactionRef: `0x${'CE'.repeat(32)}`, }) expect(record?.transferId).toBe(transfer.id) }) })