|
| 1 | +import { L2Block } from '@aztec/circuit-types'; |
| 2 | +import { times } from '@aztec/foundation/collection'; |
| 3 | +import { promiseWithResolvers } from '@aztec/foundation/promise'; |
| 4 | + |
| 5 | +import { type Server, createServer } from 'http'; |
| 6 | +import { type AddressInfo } from 'net'; |
| 7 | + |
| 8 | +import { HttpQuoteProvider } from './http.js'; |
| 9 | + |
| 10 | +describe('HttpQuoteProvider', () => { |
| 11 | + let server: Server; |
| 12 | + let port: number; |
| 13 | + |
| 14 | + let status: number = 200; |
| 15 | + let response: any = {}; |
| 16 | + let request: any = {}; |
| 17 | + |
| 18 | + let provider: HttpQuoteProvider; |
| 19 | + let blocks: L2Block[]; |
| 20 | + |
| 21 | + beforeAll(async () => { |
| 22 | + server = createServer({ keepAliveTimeout: 60000 }, (req, res) => { |
| 23 | + const chunks: Buffer[] = []; |
| 24 | + req |
| 25 | + .on('data', (chunk: Buffer) => { |
| 26 | + chunks.push(chunk); |
| 27 | + }) |
| 28 | + .on('end', () => { |
| 29 | + request = JSON.parse(Buffer.concat(chunks).toString()); |
| 30 | + }); |
| 31 | + |
| 32 | + res.writeHead(status, { 'Content-Type': 'application/json' }); |
| 33 | + res.end(JSON.stringify(response)); |
| 34 | + }); |
| 35 | + |
| 36 | + const { promise, resolve } = promiseWithResolvers(); |
| 37 | + server.listen(0, '127.0.0.1', () => resolve(null)); |
| 38 | + await promise; |
| 39 | + port = (server.address() as AddressInfo).port; |
| 40 | + }); |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + provider = new HttpQuoteProvider(`http://127.0.0.1:${port}`); |
| 44 | + blocks = times(3, i => L2Block.random(i + 1, 4)); |
| 45 | + response = { basisPointFee: 100, bondAmount: '100000000000000000000', validUntilSlot: '100' }; |
| 46 | + }); |
| 47 | + |
| 48 | + afterAll(() => { |
| 49 | + server?.close(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('requests a quote sending epoch data', async () => { |
| 53 | + const quote = await provider.getQuote(1, blocks); |
| 54 | + |
| 55 | + expect(request).toEqual( |
| 56 | + expect.objectContaining({ epochNumber: 1, fromBlock: 1, toBlock: 3, txCount: 12, totalFees: expect.any(String) }), |
| 57 | + ); |
| 58 | + |
| 59 | + expect(quote).toEqual({ |
| 60 | + basisPointFee: response.basisPointFee, |
| 61 | + bondAmount: BigInt(response.bondAmount), |
| 62 | + validUntilSlot: BigInt(response.validUntilSlot), |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + it('throws an error if the response is missing required fields', async () => { |
| 67 | + response = { basisPointFee: 100 }; |
| 68 | + await expect(provider.getQuote(1, blocks)).rejects.toThrow(/Missing required fields/i); |
| 69 | + }); |
| 70 | + |
| 71 | + it('throws an error if the response is not ok', async () => { |
| 72 | + status = 400; |
| 73 | + await expect(provider.getQuote(1, blocks)).rejects.toThrow(/Failed to fetch quote/i); |
| 74 | + }); |
| 75 | +}); |
0 commit comments