-
Notifications
You must be signed in to change notification settings - Fork 308
/
adapter.test.ts
162 lines (139 loc) · 4.21 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
import { AdapterRequest, FastifyInstance, util } from '@chainlink/ea-bootstrap'
import request, { SuperTest, Test } from 'supertest'
import * as process from 'process'
import { server as startServer } from '../../src'
import * as nock from 'nock'
import {
mockRateResponseFailure,
mockRateResponseSuccess,
mockSubscribeResponse,
mockUnsubscribeResponse,
} from './fixtures'
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 { setupExternalAdapterTest } from '@chainlink/ea-test-helpers'
import type { SuiteContext } from '@chainlink/ea-test-helpers'
describe('execute', () => {
const id = '1'
const context: SuiteContext = {
req: null,
server: startServer,
}
const envVariables = {}
setupExternalAdapterTest(envVariables, context)
describe('rate api', () => {
const data: AdapterRequest = {
id,
data: {
base: 'ETH',
quote: 'BTC',
},
}
it('should return success', async () => {
mockRateResponseSuccess()
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('rate api with invalid symbol', () => {
const data: AdapterRequest = {
id,
data: {
base: 'NON',
quote: 'EXISTING',
},
}
it('should return failure', async () => {
mockRateResponseFailure()
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) {
mockedWsServer = mockWebSocketServer(DEFAULT_WS_API_ENDPOINT)
mockWebSocketProvider(WebSocketClassProvider)
}
oldEnv = JSON.parse(JSON.stringify(process.env))
process.env.WS_ENABLED = 'true'
process.env.WS_SUBSCRIPTION_TTL = '3000'
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 api', () => {
const jobID = '1'
it('should return success', async () => {
const data: AdapterRequest = {
id: jobID,
data: {
from: 'ETH',
to: 'BTC',
},
}
let flowFulfilled = Promise.resolve(true)
if (!process.env.RECORD) {
mockRateResponseSuccess() // 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(1000)
const response = await makeRequest()
expect(response.body).toEqual({
jobRunID: '1',
result: 0.070773,
statusCode: 200,
maxAge: 30000,
data: { result: 0.070773 },
})
await flowFulfilled
}, 30000)
})
})