|
1 | | -import { Base58EncodedAddress } from '@solana/addresses'; |
2 | 1 | import { createHttpTransport, createJsonRpc } from '@solana/rpc-transport'; |
3 | 2 | import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types'; |
| 3 | +import fs from 'fs'; |
4 | 4 | import fetchMock from 'jest-fetch-mock-fork'; |
| 5 | +import path from 'path'; |
5 | 6 |
|
6 | 7 | import { createSolanaRpcApi, SolanaRpcMethods } from '../index'; |
7 | 8 |
|
8 | | -// TODO: We may want to replace the higher clusters with just the local |
9 | | -// validator, since this is within the live test suite anyhow. |
10 | | -// Also, with further manipulation capabilities over the local validator, |
11 | | -// we can explore error testing as well |
12 | | -const genesisHashMap: [string, Base58EncodedAddress][] = [ |
13 | | - ['https://api.devnet.solana.com', 'EtWTRABZaYq6iMfeYKouRu166VU2xqa1wcaWoxPkrZBG' as Base58EncodedAddress], |
14 | | - ['https://api.testnet.solana.com', '4uhcVJyU9pJkvQyS88uRDiswHXSCkY3zQawwpjk2NsNY' as Base58EncodedAddress], |
15 | | - ['https://api.mainnet-beta.solana.com', '5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d' as Base58EncodedAddress], |
16 | | -]; |
| 9 | +const logFilePath = path.resolve(__dirname, '../../../../../test-ledger/validator.log'); |
| 10 | +const genesisHashPattern = /genesis hash: ([\d\w]{44})/; |
17 | 11 |
|
18 | 12 | describe('getGenesisHash', () => { |
19 | 13 | let rpc: Rpc<SolanaRpcMethods>; |
20 | | - const createRpcForTest = (url: string) => { |
21 | | - rpc = createJsonRpc<SolanaRpcMethods>({ |
22 | | - api: createSolanaRpcApi(), |
23 | | - transport: createHttpTransport({ url }), |
24 | | - }); |
25 | | - }; |
26 | 14 | beforeEach(() => { |
27 | 15 | fetchMock.resetMocks(); |
28 | 16 | fetchMock.dontMock(); |
| 17 | + rpc = createJsonRpc<SolanaRpcMethods>({ |
| 18 | + api: createSolanaRpcApi(), |
| 19 | + transport: createHttpTransport({ url: 'http://127.0.0.1:8899' }), |
| 20 | + }); |
29 | 21 | }); |
30 | 22 |
|
31 | | - genesisHashMap.forEach(([url, expectedGenesisHash]) => { |
32 | | - describe(`when sent to ${url}`, () => { |
33 | | - it(`returns ${expectedGenesisHash}`, async () => { |
34 | | - expect.assertions(1); |
35 | | - createRpcForTest(url); |
36 | | - const genesisHashPromise = rpc.getGenesisHash().send(); |
37 | | - await expect(genesisHashPromise).resolves.toBe(expectedGenesisHash); |
38 | | - }); |
| 23 | + describe('when sent to a local validator', () => { |
| 24 | + it('returns the genesis hash', async () => { |
| 25 | + expect.assertions(1); |
| 26 | + const logFile = fs.readFileSync(logFilePath, 'utf-8'); |
| 27 | + const expectedGenesisHash = logFile.match(genesisHashPattern)?.[1]; |
| 28 | + const genesisHashPromise = rpc.getGenesisHash().send(); |
| 29 | + await expect(genesisHashPromise).resolves.toBe(expectedGenesisHash); |
39 | 30 | }); |
40 | 31 | }); |
41 | 32 | }); |
0 commit comments