-
Notifications
You must be signed in to change notification settings - Fork 308
/
adapter-ws.test.ts
121 lines (104 loc) · 4.1 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
import {
TestAdapter,
setEnvVariables,
mockWebSocketProvider,
MockWebsocketServer,
} from '@chainlink/external-adapter-framework/util/testing-utils'
import { WebSocketClassProvider } from '@chainlink/external-adapter-framework/transports'
import { mockWebSocketServer, mockCryptoLwbaWebSocketServer } from './fixtures'
import FakeTimers from '@sinonjs/fake-timers'
describe('websocket', () => {
let mockWsServer: MockWebsocketServer | undefined
let mockWsServerLwba: MockWebsocketServer | undefined
let testAdapter: TestAdapter
let oldEnv: NodeJS.ProcessEnv
const wsEndpoint = 'ws://localhost:9090/v4/timeseries-stream/asset-metrics'
const wsEndpointLwba = 'ws://localhost:9090/v4/timeseries-stream/asset-quotes'
const data = {
base: 'ETH',
quote: 'USD',
}
const dataLwba = {
endpoint: 'crypto-lwba',
base: 'ETH',
quote: 'USD',
}
beforeAll(async () => {
oldEnv = JSON.parse(JSON.stringify(process.env))
process.env['WS_SUBSCRIPTION_TTL'] = '5000'
process.env['CACHE_MAX_AGE'] = '5000'
process.env['CACHE_POLLING_MAX_RETRIES'] = '0'
process.env['WS_API_ENDPOINT'] = wsEndpoint
process.env['API_KEY'] = 'fake-api-key'
// Start mock web socket server
mockWebSocketProvider(WebSocketClassProvider)
mockWsServer = mockWebSocketServer(wsEndpoint)
mockWsServerLwba = mockCryptoLwbaWebSocketServer(wsEndpointLwba)
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(data)
await testAdapter.request(dataLwba)
await testAdapter.waitForCache(2)
})
afterAll(async () => {
setEnvVariables(oldEnv)
mockWsServer?.close()
mockWsServerLwba?.close()
testAdapter.clock?.uninstall()
await testAdapter.api.close()
})
describe('price endpoint', () => {
it('should return success', async () => {
const response = await testAdapter.request(data)
expect(response.json()).toMatchSnapshot()
})
it('should return error (empty data)', async () => {
const response = await testAdapter.request({})
expect(response.statusCode).toEqual(400)
})
it('should return error (empty base)', async () => {
const response = await testAdapter.request({ quote: 'USD' })
expect(response.statusCode).toEqual(400)
})
it('should return error (empty quote)', async () => {
const response = await testAdapter.request({ base: 'ETH' })
expect(response.statusCode).toEqual(400)
})
it('should return error (bad quote)', async () => {
const response = await testAdapter.request({ base: 'ETH', quote: 'INVALID' })
expect(response.statusCode).toEqual(400)
})
})
describe('lwba endpoint', () => {
it('should return success', async () => {
const response = await testAdapter.request(dataLwba)
expect(response.json()).toMatchSnapshot()
})
it('should return error (empty body)', async () => {
const response = await testAdapter.request({})
expect(response.statusCode).toEqual(400)
})
it('should return error (empty data)', async () => {
const response = await testAdapter.request({ endpoint: 'crypto-lwba' })
expect(response.statusCode).toEqual(400)
})
it('should return error (empty base)', async () => {
const response = await testAdapter.request({ endpoint: 'crypto-lwba', quote: 'USD' })
expect(response.statusCode).toEqual(400)
})
it('should return error (empty quote)', async () => {
const response = await testAdapter.request({ endpoint: 'crypto-lwba', base: 'ETH' })
expect(response.statusCode).toEqual(400)
})
it('should return error (invariant violation)', async () => {
// fast forward to next message (which contains an invariant violation)
testAdapter.clock.tick(1000)
const response = await testAdapter.request(dataLwba)
expect(response.json()).toMatchSnapshot()
})
})
})