|
| 1 | +import { abciQuery } from './rpc'; |
| 2 | + |
| 3 | +// Mock fetch globally |
| 4 | +const mockFetch = jest.fn(); |
| 5 | +global.fetch = mockFetch as any; |
| 6 | + |
| 7 | +describe('abciQuery', () => { |
| 8 | + const endpoint = { url: 'http://localhost:26657', headers: {} }; |
| 9 | + const path = '/cosmos.tx.v1beta1.Service/Simulate'; |
| 10 | + const data = new Uint8Array([1, 2, 3]); |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + mockFetch.mockReset(); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should throw on JSON-RPC error', async () => { |
| 17 | + mockFetch.mockResolvedValue({ |
| 18 | + json: () => Promise.resolve({ |
| 19 | + error: 'Internal error', |
| 20 | + }), |
| 21 | + }); |
| 22 | + |
| 23 | + await expect(abciQuery(endpoint, path, data)).rejects.toThrow('Request Error: Internal error'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should throw on non-zero ABCI response code', async () => { |
| 27 | + mockFetch.mockResolvedValue({ |
| 28 | + json: () => Promise.resolve({ |
| 29 | + result: { |
| 30 | + response: { |
| 31 | + code: 11, |
| 32 | + log: 'out of gas in location: ReadFlat; gasWanted: 0, gasUsed: 1000', |
| 33 | + value: '', |
| 34 | + }, |
| 35 | + }, |
| 36 | + }), |
| 37 | + }); |
| 38 | + |
| 39 | + await expect(abciQuery(endpoint, path, data)).rejects.toThrow( |
| 40 | + 'ABCI Error (code 11): out of gas in location: ReadFlat; gasWanted: 0, gasUsed: 1000' |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should throw with generic message when ABCI error has no log', async () => { |
| 45 | + mockFetch.mockResolvedValue({ |
| 46 | + json: () => Promise.resolve({ |
| 47 | + result: { |
| 48 | + response: { |
| 49 | + code: 5, |
| 50 | + log: '', |
| 51 | + value: '', |
| 52 | + }, |
| 53 | + }, |
| 54 | + }), |
| 55 | + }); |
| 56 | + |
| 57 | + await expect(abciQuery(endpoint, path, data)).rejects.toThrow('ABCI Error (code 5): Unknown error'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should return decoded value on success (code 0)', async () => { |
| 61 | + // Base64 encoded [4, 5, 6] |
| 62 | + const base64Value = 'BAUG'; |
| 63 | + mockFetch.mockResolvedValue({ |
| 64 | + json: () => Promise.resolve({ |
| 65 | + result: { |
| 66 | + response: { |
| 67 | + code: 0, |
| 68 | + log: '', |
| 69 | + value: base64Value, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }), |
| 73 | + }); |
| 74 | + |
| 75 | + const result = await abciQuery(endpoint, path, data); |
| 76 | + expect(result).toEqual(new Uint8Array([4, 5, 6])); |
| 77 | + }); |
| 78 | +}); |
0 commit comments