-
Notifications
You must be signed in to change notification settings - Fork 308
/
adapter-ws.test.ts
109 lines (96 loc) · 3.4 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
import { mockCryptoWebSocketServer } from './fixtures'
import { WebSocketClassProvider } from '@chainlink/external-adapter-framework/transports'
import {
TestAdapter,
setEnvVariables,
mockWebSocketProvider,
MockWebsocketServer,
} from '@chainlink/external-adapter-framework/util/testing-utils'
import FakeTimers from '@sinonjs/fake-timers'
import { Adapter } from '@chainlink/external-adapter-framework/adapter'
import process from 'process'
import { sleep } from '@chainlink/external-adapter-framework/util'
describe('websocket', () => {
let mockWsServer: MockWebsocketServer | undefined
let testAdapter: TestAdapter
const wsEndpoint = 'ws://localhost:9090'
let oldEnv: NodeJS.ProcessEnv
const cryptoData = {
base: 'ETH',
quote: 'USD',
transport: 'ws',
}
const volumeData = {
base: 'AAA',
coinid: 'eth-ethereum',
quote: 'USD',
transport: 'ws',
endpoint: 'volume',
}
const marketcapData = {
base: 'AAA',
coinid: 'eth-ethereum',
quote: 'USD',
endpoint: 'marketcap',
}
beforeAll(async () => {
oldEnv = JSON.parse(JSON.stringify(process.env))
process.env['CACHE_POLLING_MAX_RETRIES'] = process.env['CACHE_POLLING_MAX_RETRIES'] ?? '0'
process.env['METRICS_ENABLED'] = process.env['METRICS_ENABLED'] ?? 'false'
process.env['WS_API_ENDPOINT'] = process.env['WS_API_ENDPOINT'] ?? wsEndpoint
process.env['WS_ENABLED'] = process.env['WS_ENABLED'] ?? 'true'
mockWebSocketProvider(WebSocketClassProvider)
mockWsServer = mockCryptoWebSocketServer(wsEndpoint)
const adapter = (await import('./../../src')).adapter as unknown as Adapter
adapter.rateLimiting = undefined
testAdapter = await TestAdapter.startWithMockedCache(adapter, {
clock: FakeTimers.install(),
testAdapter: {} as TestAdapter<never>,
})
// Send initial requests to start background execute and wait for cache to be filled with results
await testAdapter.request(cryptoData)
await testAdapter.request(volumeData)
await testAdapter.request(marketcapData)
// the cache size should be 15 since for each request we save 5 cache entries based on 5 quotes (see fixtures.ts)
await testAdapter.waitForCache(15)
})
afterAll(async () => {
setEnvVariables(oldEnv)
mockWsServer?.close()
testAdapter.clock?.uninstall()
await testAdapter.api.close()
})
describe('crypto endpoint', () => {
it('should return success', async () => {
const response = await testAdapter.request(cryptoData)
expect(response.statusCode).toBe(200)
expect(response.json()).toMatchSnapshot()
})
})
describe('volume endpoint', () => {
it('should return success', async () => {
const response = await testAdapter.request(volumeData)
expect(response.statusCode).toBe(200)
expect(response.json()).toMatchSnapshot()
})
})
describe('marketcap endpoint', () => {
it('should return success', async () => {
const response = await testAdapter.request(marketcapData)
expect(response.statusCode).toBe(200)
expect(response.json()).toMatchSnapshot()
})
})
describe('invalid quote', () => {
it('should return error', async () => {
const response = await testAdapter.request({
base: 'AAA',
coinid: 'eth-ethereum',
quote: 'LTC',
transport: 'ws',
})
expect(response.statusCode).toBe(400)
expect(response.json()).toMatchSnapshot()
})
})
})