-
Notifications
You must be signed in to change notification settings - Fork 308
/
Copy pathadapter-ws.test.ts
129 lines (119 loc) · 3.93 KB
/
adapter-ws.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import {
TestAdapter,
setEnvVariables,
mockWebSocketProvider,
MockWebsocketServer,
} from '@chainlink/external-adapter-framework/util/testing-utils'
import FakeTimers from '@sinonjs/fake-timers'
import {
mockCryptoWebSocketServer,
mockEtfWebSocketServer,
mockForexWebSocketServer,
mockStockWebSocketServer,
} from './fixtures'
import { WebSocketClassProvider } from '@chainlink/external-adapter-framework/transports'
describe('websocket', () => {
let mockWsServerStock: MockWebsocketServer | undefined
let mockWsServerForex: MockWebsocketServer | undefined
let mockWsServerCrypto: MockWebsocketServer | undefined
let mockWsServerEtf: MockWebsocketServer | undefined
let testAdapter: TestAdapter
const wsEndpointStock = 'ws://localhost:9090'
const wsEndpointForex = 'ws://localhost:9091'
const wsEndpointCrypto = 'ws://localhost:9092'
const wsEndpointEtf = 'ws://localhost:9093'
let oldEnv: NodeJS.ProcessEnv
const stockData = {
base: 'AAPL',
transport: 'ws',
}
const forexData = {
endpoint: 'forex',
base: 'GBP',
quote: 'USD',
transport: 'ws',
}
const cryptoData = {
endpoint: 'crypto',
base: 'BTC',
quote: 'USD',
transport: 'ws',
}
const ukEtfData = {
endpoint: 'uk_etf',
base: 'CSPX',
transport: 'ws',
}
const etfData = {
endpoint: 'etf',
base: 'C3M',
transport: 'ws',
}
beforeAll(async () => {
oldEnv = JSON.parse(JSON.stringify(process.env))
process.env['WS_ENABLED'] = 'true'
process.env['WS_SOCKET_KEY'] = 'fake-api-key'
process.env['STOCK_WS_API_ENDPOINT'] = wsEndpointStock
process.env['FOREX_WS_API_ENDPOINT'] = wsEndpointForex
process.env['CRYPTO_WS_API_ENDPOINT'] = wsEndpointCrypto
process.env['ETF_WS_API_ENDPOINT'] = wsEndpointEtf
process.env['API_KEY'] = 'fake-api-key'
// Start mock web socket server
mockWebSocketProvider(WebSocketClassProvider)
mockWsServerStock = mockStockWebSocketServer(wsEndpointStock)
mockWsServerForex = mockForexWebSocketServer(wsEndpointForex)
mockWsServerCrypto = mockCryptoWebSocketServer(wsEndpointCrypto)
mockWsServerEtf = mockEtfWebSocketServer(wsEndpointEtf)
const adapter = (await import('./../../src')).adapter
testAdapter = await TestAdapter.startWithMockedCache(adapter, {
clock: FakeTimers.install(),
testAdapter: {} as TestAdapter<never>,
})
// Send initial request to start background execute and wait for cache to be filled with results
await testAdapter.request(stockData)
await testAdapter.request(forexData)
await testAdapter.request(cryptoData)
await testAdapter.request(ukEtfData)
await testAdapter.request(etfData)
await testAdapter.waitForCache(5)
})
afterAll(async () => {
setEnvVariables(oldEnv)
mockWsServerStock?.close()
mockWsServerCrypto?.close()
mockWsServerForex?.close()
mockWsServerEtf?.close()
testAdapter.clock?.uninstall()
await testAdapter.api.close()
})
describe('stock endpoint', () => {
it('should return success', async () => {
const response = await testAdapter.request(stockData)
expect(response.json()).toMatchSnapshot()
})
})
describe('forex endpoint', () => {
it('should return success', async () => {
const response = await testAdapter.request(forexData)
expect(response.json()).toMatchSnapshot()
})
})
describe('crypto endpoint', () => {
it('should return success', async () => {
const response = await testAdapter.request(cryptoData)
expect(response.json()).toMatchSnapshot()
})
})
describe('uk etf endpoint', () => {
it('should return success', async () => {
const response = await testAdapter.request(ukEtfData)
expect(response.json()).toMatchSnapshot()
})
})
describe('etf endpoint', () => {
it('should return success', async () => {
const response = await testAdapter.request(etfData)
expect(response.json()).toMatchSnapshot()
})
})
})