-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathreserves.test.ts
145 lines (124 loc) · 3.96 KB
/
reserves.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
import { AdapterRequest, FastifyInstance } from '@chainlink/ea-bootstrap'
import request, { SuperTest, Test } from 'supertest'
import process from 'process'
import nock from 'nock'
import { server as startServer } from '../../src'
import {
mockSubscribeResponse,
mockUnsubscribeResponse,
mockWootradeResponseSuccess,
} from './fixtures'
import { DEFAULT_BASE_URL } from '../../src/config'
import { AddressInfo } from 'net'
import {
mockWebSocketProvider,
mockWebSocketServer,
MockWsServer,
mockWebSocketFlow,
setEnvVariables,
} from '@chainlink/ea-test-helpers'
import { WebSocketClassProvider } from '@chainlink/ea-bootstrap/dist/lib/middleware/ws/recorder'
import { DEFAULT_WS_API_ENDPOINT } from '../../src/config'
import { util } from '@chainlink/ea-bootstrap'
import { setupExternalAdapterTest } from '@chainlink/ea-test-helpers'
import type { SuiteContext } from '@chainlink/ea-test-helpers'
describe('execute', () => {
const context: SuiteContext = {
req: null,
server: startServer,
}
const envVariables = {
API_ENDPOINT: process.env.API_ENDPOINT || DEFAULT_BASE_URL,
API_VERBOSE: 'true',
}
setupExternalAdapterTest(envVariables, context)
describe('with token', () => {
const data: AdapterRequest = {
id: '1',
data: {
base: 'ETH',
quote: 'USDT',
},
}
it('should return success', async () => {
mockWootradeResponseSuccess()
const response = await (context.req as SuperTest<Test>)
.post('/')
.send(data)
.set('Accept', '*/*')
.set('Content-Type', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
expect(response.body).toMatchSnapshot()
})
})
})
describe('websocket', () => {
let mockedWsServer: InstanceType<typeof MockWsServer>
let fastify: FastifyInstance
let req: SuperTest<Test>
let oldEnv: NodeJS.ProcessEnv
beforeAll(async () => {
if (!process.env.RECORD) {
process.env.CACHE_ENABLED = 'true'
process.env.API_KEY = process.env.API_KEY || 'fake-api-key'
mockedWsServer = mockWebSocketServer(`${DEFAULT_WS_API_ENDPOINT}/${process.env.API_KEY}`)
mockWebSocketProvider(WebSocketClassProvider)
}
oldEnv = JSON.parse(JSON.stringify(process.env))
process.env.WS_ENABLED = 'true'
process.env.WS_SUBSCRIPTION_TTL = '100'
fastify = await startServer()
req = request(`localhost:${(fastify.server.address() as AddressInfo).port}`)
})
afterAll((done) => {
setEnvVariables(oldEnv)
nock.restore()
nock.cleanAll()
nock.enableNetConnect()
fastify.close(done)
})
describe('WS crypto endpoint', () => {
const jobID = '1'
it('should return success', async () => {
const data: AdapterRequest = {
id: jobID,
data: {
base: 'ETH',
quote: 'USDT',
},
}
let flowFulfilled = Promise.resolve(true)
if (!process.env.RECORD) {
mockWootradeResponseSuccess() // For the first response
flowFulfilled = mockWebSocketFlow(mockedWsServer, [
mockSubscribeResponse,
mockUnsubscribeResponse,
])
}
const makeRequest = () =>
req
.post('/')
.send(data)
.set('Accept', '*/*')
.set('Content-Type', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
// We don't care about the first response, coming from http request
// This first request will start both batch warmer & websocket
await makeRequest()
// This final request should disable the cache warmer, sleep is used to make sure that the data is pulled from the websocket
// populated cache entries.
await util.sleep(50)
const response = await makeRequest()
expect(response.body).toEqual({
jobRunID: '1',
result: 2907.395,
statusCode: 200,
maxAge: 30000,
data: { result: 2907.395 },
})
await flowFulfilled
}, 30000)
})
})