import { describe, it, expect, vi } from 'vitest'; import { queryTransferHistory } from './transfers'; import { fetchGraphData } from './fetch-graph'; import { PrexApiService } from '../api'; import { TransferHistoryQuery } from '../types'; vi.mock('./fetch-graph', () => ({ fetchGraphData: vi.fn(), })); const mockApiService = {} as PrexApiService; const USER_ADDRESS = '0x1234567890abcdef1234567890abcdef12345678'; const TOKEN_ADDRESS = '0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef'; const SENDER_ADDRESS = '0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef'; const RECIPIENT_ADDRESS = '0x1234567890abcdef1234567890abcdef12345678'; describe('queryTransferHistory', () => { it('should fetch transfer history by user', async () => { const mockResponse = { coinMovingHistories: [ { id: '0x01', movingType: 'DIRECT', token: { id: '0x02', }, sender: { id: '0x03', name: 'test', }, recipient: { id: '0x04', name: 'test', }, amount: '1000', createdAt: '1234567890', txHash: '0x05', }, ], }; vi.mocked(fetchGraphData).mockResolvedValue(mockResponse); const result = await queryTransferHistory( mockApiService, { user: USER_ADDRESS }, 0, 10 ); expect(fetchGraphData).toHaveBeenCalledWith( mockApiService, 'coinMovingHistories', expect.any(String), { offset: 0, limit: 10, address: USER_ADDRESS.toLowerCase(), } ); expect(result).toEqual([ { id: '0x01', movingType: 'DIRECT', token: '0x02', sender: '0x03', senderName: 'test', recipient: '0x04', recipientName: 'test', amount: 1000n, createdAt: 1234567890, txHash: '0x05', metadata: {}, }, ]); }); it('should fetch transfer history by token', async () => { const mockResponse = { coinMovingHistories: [ { id: '0x01', movingType: 'DIRECT', token: { id: '0x02', }, sender: { id: '0x03', name: 'test', }, recipient: { id: '0x04', name: 'test', }, amount: '1000', createdAt: '1234567890', txHash: '0x05', metadata: {}, }, ], }; vi.mocked(fetchGraphData).mockResolvedValue(mockResponse); const result = await queryTransferHistory( mockApiService, { token: TOKEN_ADDRESS }, 0, 10 ); expect(fetchGraphData).toHaveBeenCalledWith( mockApiService, 'coinMovingHistories', expect.any(String), { offset: 0, limit: 10, token: TOKEN_ADDRESS.toLowerCase(), } ); expect(result).toEqual([ { id: '0x01', movingType: 'DIRECT', token: '0x02', sender: '0x03', senderName: 'test', recipient: '0x04', recipientName: 'test', amount: 1000n, createdAt: 1234567890, txHash: '0x05', metadata: {}, }, ]); }); it('should fetch transfer history by sender and recipient', async () => { const mockResponse = { coinMovingHistories: [ { id: '0x01', movingType: 'DIRECT', token: { id: '0x02', }, sender: { id: '0x03', name: 'test', }, recipient: { id: '0x04', name: 'test', }, amount: '1000', createdAt: '1234567890', txHash: '0x05', metadata: {}, }, ], }; vi.mocked(fetchGraphData).mockResolvedValue(mockResponse); const result = await queryTransferHistory( mockApiService, { me: SENDER_ADDRESS, other: RECIPIENT_ADDRESS }, 0, 10 ); expect(fetchGraphData).toHaveBeenCalledWith( mockApiService, 'coinMovingHistories', expect.any(String), { offset: 0, limit: 10, me: SENDER_ADDRESS.toLowerCase(), other: RECIPIENT_ADDRESS.toLowerCase(), } ); expect(result).toEqual([ { id: '0x01', movingType: 'DIRECT', token: '0x02', sender: '0x03', senderName: 'test', recipient: '0x04', recipientName: 'test', amount: 1000n, createdAt: 1234567890, txHash: '0x05', metadata: {}, }, ]); }); it('should throw an error for invalid query', async () => { await expect( queryTransferHistory(mockApiService, {} as TransferHistoryQuery, 0, 10) ).rejects.toThrow('Invalid query'); }); });