|
| 1 | +import { abciQuery } from './rpc'; |
| 2 | +import { ProtocolError, AbciError } from '@interchainjs/types'; |
| 3 | + |
| 4 | +// Mock fetch globally |
| 5 | +const mockFetch = jest.fn(); |
| 6 | +global.fetch = mockFetch as any; |
| 7 | + |
| 8 | +describe('abciQuery', () => { |
| 9 | + const endpoint = { url: 'http://localhost:26657', headers: {} }; |
| 10 | + const path = '/cosmos.tx.v1beta1.Service/Simulate'; |
| 11 | + const data = new Uint8Array([1, 2, 3]); |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + mockFetch.mockReset(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should throw ProtocolError on JSON-RPC error', async () => { |
| 18 | + mockFetch.mockResolvedValue({ |
| 19 | + json: () => Promise.resolve({ |
| 20 | + error: { code: -32600, message: 'Invalid Request' }, |
| 21 | + }), |
| 22 | + }); |
| 23 | + |
| 24 | + await expect(abciQuery(endpoint, path, data)).rejects.toThrow(ProtocolError); |
| 25 | + await expect(abciQuery(endpoint, path, data)).rejects.toThrow('JSON-RPC error'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should throw AbciError on non-zero ABCI response code', async () => { |
| 29 | + mockFetch.mockResolvedValue({ |
| 30 | + json: () => Promise.resolve({ |
| 31 | + result: { |
| 32 | + response: { |
| 33 | + code: 11, |
| 34 | + log: 'out of gas in location: ReadFlat; gasWanted: 0, gasUsed: 1000', |
| 35 | + value: '', |
| 36 | + }, |
| 37 | + }, |
| 38 | + }), |
| 39 | + }); |
| 40 | + |
| 41 | + await expect(abciQuery(endpoint, path, data)).rejects.toThrow(AbciError); |
| 42 | + |
| 43 | + try { |
| 44 | + await abciQuery(endpoint, path, data); |
| 45 | + } catch (e) { |
| 46 | + expect(e).toBeInstanceOf(AbciError); |
| 47 | + const abciError = e as AbciError; |
| 48 | + expect(abciError.abciCode).toBe(11); |
| 49 | + expect(abciError.log).toBe('out of gas in location: ReadFlat; gasWanted: 0, gasUsed: 1000'); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + it('should throw AbciError with generic message when ABCI error has no log', async () => { |
| 54 | + mockFetch.mockResolvedValue({ |
| 55 | + json: () => Promise.resolve({ |
| 56 | + result: { |
| 57 | + response: { |
| 58 | + code: 5, |
| 59 | + log: '', |
| 60 | + value: '', |
| 61 | + }, |
| 62 | + }, |
| 63 | + }), |
| 64 | + }); |
| 65 | + |
| 66 | + await expect(abciQuery(endpoint, path, data)).rejects.toThrow(AbciError); |
| 67 | + |
| 68 | + try { |
| 69 | + await abciQuery(endpoint, path, data); |
| 70 | + } catch (e) { |
| 71 | + const abciError = e as AbciError; |
| 72 | + expect(abciError.abciCode).toBe(5); |
| 73 | + expect(abciError.log).toBe('Unknown error'); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + it('should return decoded value on success (code 0)', async () => { |
| 78 | + // Base64 encoded [4, 5, 6] |
| 79 | + const base64Value = 'BAUG'; |
| 80 | + mockFetch.mockResolvedValue({ |
| 81 | + json: () => Promise.resolve({ |
| 82 | + result: { |
| 83 | + response: { |
| 84 | + code: 0, |
| 85 | + log: '', |
| 86 | + value: base64Value, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }), |
| 90 | + }); |
| 91 | + |
| 92 | + const result = await abciQuery(endpoint, path, data); |
| 93 | + expect(result).toEqual(new Uint8Array([4, 5, 6])); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should preserve instanceof Error for backward compatibility', async () => { |
| 97 | + mockFetch.mockResolvedValue({ |
| 98 | + json: () => Promise.resolve({ |
| 99 | + result: { |
| 100 | + response: { |
| 101 | + code: 11, |
| 102 | + log: 'test error', |
| 103 | + value: '', |
| 104 | + }, |
| 105 | + }, |
| 106 | + }), |
| 107 | + }); |
| 108 | + |
| 109 | + await expect(abciQuery(endpoint, path, data)).rejects.toThrow(Error); |
| 110 | + }); |
| 111 | +}); |
0 commit comments