|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | + |
| 3 | +import { BioForestApiClient, BioForestApiError } from './client' |
| 4 | + |
| 5 | +describe('BioForestApiClient.getBalance', () => { |
| 6 | + it('uses the provided magic from genesisBlock after trim', async () => { |
| 7 | + const requests: Array<{ url: string; init: RequestInit | undefined }> = [] |
| 8 | + const fetchFn: typeof fetch = async (input, init) => { |
| 9 | + requests.push({ url: String(input), init }) |
| 10 | + return new Response( |
| 11 | + JSON.stringify({ |
| 12 | + success: true, |
| 13 | + result: { amount: '100' }, |
| 14 | + }), |
| 15 | + { |
| 16 | + status: 200, |
| 17 | + headers: { 'Content-Type': 'application/json' }, |
| 18 | + }, |
| 19 | + ) |
| 20 | + } |
| 21 | + |
| 22 | + const client = new BioForestApiClient({ |
| 23 | + rpcUrl: 'https://walletapi.bfmeta.info', |
| 24 | + chainId: 'bfm', |
| 25 | + fetch: fetchFn, |
| 26 | + }) |
| 27 | + |
| 28 | + await client.getBalance('b-test', 'BFM', ' LLLQL ') |
| 29 | + const request = requests[0] |
| 30 | + expect(request?.url).toBe('https://walletapi.bfmeta.info/wallet/bfm/address/balance') |
| 31 | + expect(request?.init?.method).toBe('POST') |
| 32 | + expect(request?.init?.body).toBe( |
| 33 | + JSON.stringify({ |
| 34 | + address: 'b-test', |
| 35 | + magic: 'LLLQL', |
| 36 | + assetType: 'BFM', |
| 37 | + }), |
| 38 | + ) |
| 39 | + }) |
| 40 | + |
| 41 | + it('throws when magic is empty', async () => { |
| 42 | + let called = false |
| 43 | + const fetchFn: typeof fetch = async () => { |
| 44 | + called = true |
| 45 | + return new Response(JSON.stringify({ success: true, result: { amount: '0' } }), { |
| 46 | + status: 200, |
| 47 | + headers: { 'Content-Type': 'application/json' }, |
| 48 | + }) |
| 49 | + } |
| 50 | + |
| 51 | + const client = new BioForestApiClient({ |
| 52 | + rpcUrl: 'https://walletapi.bfmeta.info', |
| 53 | + chainId: 'bfm', |
| 54 | + fetch: fetchFn, |
| 55 | + }) |
| 56 | + |
| 57 | + await expect(client.getBalance('b-test', 'BFM', ' ')).rejects.toBeInstanceOf(BioForestApiError) |
| 58 | + expect(called).toBe(false) |
| 59 | + }) |
| 60 | +}) |
0 commit comments