|
| 1 | +import { WebSocketClassProvider } from '@chainlink/external-adapter-framework/transports' |
| 2 | +import { |
| 3 | + mockWebSocketProvider, |
| 4 | + MockWebsocketServer, |
| 5 | + setEnvVariables, |
| 6 | + TestAdapter, |
| 7 | +} from '@chainlink/external-adapter-framework/util/testing-utils' |
| 8 | +import FakeTimers from '@sinonjs/fake-timers' |
| 9 | +import { mockStockQuotesWebSocketServer } from './fixtures' |
| 10 | + |
| 11 | +describe('stock quotes websocket', () => { |
| 12 | + let mockWsServerStockQuotes: MockWebsocketServer | undefined |
| 13 | + let testAdapter: TestAdapter |
| 14 | + const wsEndpointStockQuotes = 'ws://localhost:9094' |
| 15 | + let oldEnv: NodeJS.ProcessEnv |
| 16 | + const data = { |
| 17 | + endpoint: 'stock_quotes', |
| 18 | + base: 'AAPL', |
| 19 | + } |
| 20 | + const fallBackData = { |
| 21 | + endpoint: 'stock_quotes', |
| 22 | + base: 'FALLBACK', |
| 23 | + } |
| 24 | + |
| 25 | + beforeAll(async () => { |
| 26 | + oldEnv = JSON.parse(JSON.stringify(process.env)) |
| 27 | + process.env['STOCK_QUOTES_WS_API_ENDPOINT'] = wsEndpointStockQuotes |
| 28 | + process.env['WS_SOCKET_KEY'] = 'fake-api-key' |
| 29 | + process.env['API_KEY'] = 'fake-api-key' |
| 30 | + |
| 31 | + // Start mock web socket server |
| 32 | + mockWebSocketProvider(WebSocketClassProvider) |
| 33 | + mockWsServerStockQuotes = mockStockQuotesWebSocketServer(wsEndpointStockQuotes) |
| 34 | + |
| 35 | + const adapter = (await import('./../../src')).adapter |
| 36 | + testAdapter = await TestAdapter.startWithMockedCache(adapter, { |
| 37 | + clock: FakeTimers.install(), |
| 38 | + testAdapter: {} as TestAdapter<never>, |
| 39 | + }) |
| 40 | + |
| 41 | + // Send initial request to start background execute and wait for cache to be filled with results |
| 42 | + await testAdapter.request(data) |
| 43 | + await testAdapter.request(fallBackData) |
| 44 | + await testAdapter.waitForCache(2) |
| 45 | + }) |
| 46 | + |
| 47 | + afterAll(async () => { |
| 48 | + setEnvVariables(oldEnv) |
| 49 | + mockWsServerStockQuotes?.close() |
| 50 | + testAdapter.clock?.uninstall() |
| 51 | + await testAdapter.api.close() |
| 52 | + }) |
| 53 | + |
| 54 | + describe('stock quotes endpoint', () => { |
| 55 | + it('should return success', async () => { |
| 56 | + const response = await testAdapter.request(data) |
| 57 | + expect(response.json()).toMatchSnapshot() |
| 58 | + }) |
| 59 | + |
| 60 | + it('missing a and b fields should fallback', async () => { |
| 61 | + const response = await testAdapter.request(fallBackData) |
| 62 | + expect(response.json()).toMatchSnapshot() |
| 63 | + }) |
| 64 | + }) |
| 65 | +}) |
0 commit comments