-
Notifications
You must be signed in to change notification settings - Fork 305
/
adapter.test.ts
178 lines (157 loc) · 6 KB
/
adapter.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { WebSocketClassProvider } from '@chainlink/external-adapter-framework/transports'
import {
TestAdapter,
setEnvVariables,
mockWebSocketProvider,
MockWebsocketServer,
} from '@chainlink/external-adapter-framework/util/testing-utils'
import { Adapter } from '@chainlink/external-adapter-framework/adapter'
import FakeTimers from '@sinonjs/fake-timers'
import {
mockCryptoWebSocketServer,
mockForexResponse,
mockForexWebSocketServer,
mockMarketStatusWebSocketServer,
} from './fixtures'
describe('websocket', () => {
let mockWsServer: MockWebsocketServer | undefined
let mockWsServerForex: MockWebsocketServer | undefined
let mockWsServerMarketStatus: MockWebsocketServer | undefined
let testAdapter: TestAdapter
const wsEndpoint = 'ws://localhost:9090'
const wsEndpointForex = 'ws://localhost:9091'
const wsEndpointMarketStatus = 'ws://localhost:9092'
let oldEnv: NodeJS.ProcessEnv
const cryptoData = {
base: 'eth',
quote: 'usd',
}
const cryptoDataLwba = {
endpoint: 'crypto-lwba',
base: 'avax',
quote: 'usd',
}
const cryptoDataLwbaInvariantViolation = {
endpoint: 'crypto-lwba',
base: 'btc',
quote: 'usd',
}
const forexData = {
base: 'CAD',
quote: 'USD',
endpoint: 'forex',
}
const marketStatusOpenData = {
market: 'forex',
endpoint: 'market-status',
}
const marketStatusClosedData = {
market: 'metals',
endpoint: 'market-status',
}
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['METRICS_ENABLED'] = 'false'
process.env['WS_API_ENDPOINT'] = wsEndpoint
process.env['FOREX_WS_API_ENDPOINT'] = wsEndpointForex
process.env['MARKET_STATUS_WS_API_ENDPOINT'] = wsEndpointMarketStatus
process.env['API_USERNAME'] = 'test-api-username'
process.env['API_PASSWORD'] = 'test-api-password'
process.env['FOREX_WS_API_KEY'] = 'test-api-key'
process.env['MARKET_STATUS_WS_API_KEY'] = 'test-api-key'
mockWebSocketProvider(WebSocketClassProvider)
mockWsServer = mockCryptoWebSocketServer(wsEndpoint)
mockWsServerForex = mockForexWebSocketServer(wsEndpointForex)
mockWsServerMarketStatus = mockMarketStatusWebSocketServer(wsEndpointMarketStatus)
const adapter = (await import('./../../src')).adapter as unknown as 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(cryptoData)
await testAdapter.request(cryptoDataLwba)
await testAdapter.request(cryptoDataLwbaInvariantViolation)
await testAdapter.request(forexData)
await testAdapter.request(marketStatusOpenData)
await testAdapter.request(marketStatusClosedData)
await testAdapter.waitForCache(6 + Object.keys(mockForexResponse).length)
})
afterAll(async () => {
setEnvVariables(oldEnv)
mockWsServer?.close()
mockWsServerForex?.close()
mockWsServerMarketStatus?.close()
testAdapter.clock?.uninstall()
await testAdapter.api.close()
})
describe('crypto endpoint', () => {
it('should return success', async () => {
const response = await testAdapter.request(cryptoData)
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: 'BTC' })
expect(response.statusCode).toEqual(400)
})
it('should return error (empty quote)', async () => {
const response = await testAdapter.request({ base: 'ETH' })
expect(response.statusCode).toEqual(400)
})
})
describe('lwba endpoint', () => {
it('should return success', async () => {
const response = await testAdapter.request(cryptoDataLwba)
expect(response.json()).toMatchSnapshot()
})
it('should return error (LWBA invariant violation)', async () => {
const response = await testAdapter.request(cryptoDataLwbaInvariantViolation)
expect(response.json()).toMatchSnapshot()
})
})
describe('forex endpoint', () => {
it('should return success', async () => {
const response = await testAdapter.request(forexData)
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: 'BTC' })
expect(response.statusCode).toEqual(400)
})
it('should return error (empty quote)', async () => {
const response = await testAdapter.request({ base: 'ETH' })
expect(response.statusCode).toEqual(400)
})
})
describe('market status endpoint', () => {
it('should return success with open', async () => {
const response = await testAdapter.request(marketStatusOpenData)
expect(response.statusCode).toEqual(200)
expect(response.json()).toMatchSnapshot()
})
it('should return success with closed', async () => {
const response = await testAdapter.request(marketStatusClosedData)
expect(response.statusCode).toEqual(200)
expect(response.json()).toMatchSnapshot()
})
it('should return error (empty market)', async () => {
const response = await testAdapter.request({ ...marketStatusOpenData, market: undefined })
expect(response.statusCode).toEqual(400)
})
it('should return error (unknown market)', async () => {
const response = await testAdapter.request({ ...marketStatusOpenData, market: 'unknown' })
expect(response.statusCode).toEqual(400)
})
})
})