|
| 1 | +import type { AliasService, CommandHandlerArgs, CoreApi } from '@/core'; |
| 2 | +import type { ContractErc20CallSymbolOutput } from '@/plugins/contract-erc20/commands/symbol/output'; |
| 3 | + |
| 4 | +import { ZodError } from 'zod'; |
| 5 | + |
| 6 | +import { makeArgs, makeLogger } from '@/__tests__/mocks/mocks'; |
| 7 | +import { Status } from '@/core/shared/constants'; |
| 8 | +import { symbol as erc20SymbolHandler } from '@/plugins/contract-erc20/commands/symbol/handler'; |
| 9 | +import { ContractErc20CallSymbolInputSchema } from '@/plugins/contract-erc20/commands/symbol/input'; |
| 10 | + |
| 11 | +jest.mock('@hashgraph/sdk', () => ({ |
| 12 | + ContractId: { |
| 13 | + fromString: jest.fn(() => ({ |
| 14 | + toEvmAddress: jest.fn(() => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'), |
| 15 | + })), |
| 16 | + }, |
| 17 | + TokenType: { |
| 18 | + NonFungibleUnique: 'NonFungibleUnique', |
| 19 | + FungibleCommon: 'FungibleCommon', |
| 20 | + }, |
| 21 | +})); |
| 22 | + |
| 23 | +const mockEncodeFunctionData = jest.fn().mockReturnValue('0xencoded'); |
| 24 | +const mockDecodeFunctionResult = jest |
| 25 | + .fn() |
| 26 | + .mockReturnValue(['HBAR'] as unknown[]); |
| 27 | + |
| 28 | +jest.mock('@/plugins/contract-erc20/utils/erc20-abi-resolver', () => ({ |
| 29 | + getAbiErc20Interface: jest.fn(() => ({ |
| 30 | + encodeFunctionData: mockEncodeFunctionData, |
| 31 | + decodeFunctionResult: mockDecodeFunctionResult, |
| 32 | + })), |
| 33 | +})); |
| 34 | + |
| 35 | +jest.mock('@/core/utils/contract-resolver', () => ({ |
| 36 | + resolveContractId: jest.fn(() => '0.0.1234'), |
| 37 | +})); |
| 38 | + |
| 39 | +describe('contract-erc20 plugin - symbol command (unit)', () => { |
| 40 | + let api: CommandHandlerArgs['api']; |
| 41 | + let logger: ReturnType<typeof makeLogger>; |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + jest.clearAllMocks(); |
| 45 | + |
| 46 | + logger = makeLogger(); |
| 47 | + |
| 48 | + api = { |
| 49 | + network: { |
| 50 | + getCurrentNetwork: jest.fn(() => 'testnet'), |
| 51 | + }, |
| 52 | + alias: {} as unknown as AliasService, |
| 53 | + mirror: { |
| 54 | + postContractCall: jest.fn(), |
| 55 | + }, |
| 56 | + } as unknown as CoreApi; |
| 57 | + }); |
| 58 | + |
| 59 | + test('calls ERC-20 symbol successfully and returns expected output', async () => { |
| 60 | + (api.mirror.postContractCall as jest.Mock).mockResolvedValue({ |
| 61 | + result: '0xencodedResult', |
| 62 | + }); |
| 63 | + |
| 64 | + const args = makeArgs(api, logger, { |
| 65 | + contract: 'some-alias-or-id', |
| 66 | + }); |
| 67 | + |
| 68 | + const result = await erc20SymbolHandler(args); |
| 69 | + |
| 70 | + expect(result.status).toBe(Status.Success); |
| 71 | + expect(result.outputJson).toBeDefined(); |
| 72 | + |
| 73 | + const parsed = JSON.parse( |
| 74 | + result.outputJson as string, |
| 75 | + ) as ContractErc20CallSymbolOutput; |
| 76 | + |
| 77 | + expect(parsed.contractId).toBe('0.0.1234'); |
| 78 | + expect(parsed.contractSymbol).toBe('HBAR'); |
| 79 | + expect(parsed.network).toBe('testnet'); |
| 80 | + |
| 81 | + expect(mockEncodeFunctionData).toHaveBeenCalledWith('symbol'); |
| 82 | + expect(api.mirror.postContractCall).toHaveBeenCalledWith({ |
| 83 | + to: `0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`, |
| 84 | + data: '0xencoded', |
| 85 | + }); |
| 86 | + expect(mockDecodeFunctionResult).toHaveBeenCalledWith( |
| 87 | + 'symbol', |
| 88 | + '0xencodedResult', |
| 89 | + ); |
| 90 | + expect(logger.info).toHaveBeenCalledWith( |
| 91 | + 'Calling ERC-20 "symbol" function on contract 0.0.1234 (network: testnet)', |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + test('returns failure when mirror returns no result', async () => { |
| 96 | + (api.mirror.postContractCall as jest.Mock).mockResolvedValue({}); |
| 97 | + |
| 98 | + const args = makeArgs(api, logger, { |
| 99 | + contract: 'some-alias-or-id', |
| 100 | + }); |
| 101 | + |
| 102 | + const result = await erc20SymbolHandler(args); |
| 103 | + |
| 104 | + expect(result.status).toBe(Status.Failure); |
| 105 | + expect(result.errorMessage).toContain( |
| 106 | + 'There was a problem with calling contract 0.0.1234 "symbol" function', |
| 107 | + ); |
| 108 | + }); |
| 109 | + |
| 110 | + test('returns failure when decodeFunctionResult returns empty array', async () => { |
| 111 | + (api.mirror.postContractCall as jest.Mock).mockResolvedValue({ |
| 112 | + result: '0xencodedResult', |
| 113 | + }); |
| 114 | + mockDecodeFunctionResult.mockReturnValueOnce([]); |
| 115 | + |
| 116 | + const args = makeArgs(api, logger, { |
| 117 | + contract: 'some-alias-or-id', |
| 118 | + }); |
| 119 | + |
| 120 | + const result = await erc20SymbolHandler(args); |
| 121 | + |
| 122 | + expect(result.status).toBe(Status.Failure); |
| 123 | + expect(result.errorMessage).toContain( |
| 124 | + 'There was a problem with decoding contract 0.0.1234 "symbol" function result', |
| 125 | + ); |
| 126 | + }); |
| 127 | + |
| 128 | + test('returns failure when postContractCall throws', async () => { |
| 129 | + (api.mirror.postContractCall as jest.Mock).mockRejectedValue( |
| 130 | + new Error('mirror node error'), |
| 131 | + ); |
| 132 | + |
| 133 | + const args = makeArgs(api, logger, { |
| 134 | + contract: 'some-alias-or-id', |
| 135 | + }); |
| 136 | + |
| 137 | + const result = await erc20SymbolHandler(args); |
| 138 | + |
| 139 | + expect(result.status).toBe(Status.Failure); |
| 140 | + expect(result.errorMessage).toContain( |
| 141 | + 'Failed to call "symbol" function: mirror node error', |
| 142 | + ); |
| 143 | + }); |
| 144 | + |
| 145 | + test('schema validation fails when contract is missing', () => { |
| 146 | + expect(() => { |
| 147 | + ContractErc20CallSymbolInputSchema.parse({}); |
| 148 | + }).toThrow(ZodError); |
| 149 | + }); |
| 150 | +}); |
0 commit comments