-
Notifications
You must be signed in to change notification settings - Fork 308
/
adapter.test.ts
120 lines (98 loc) · 4.04 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
import * as process from 'process'
import { AddressInfo } from 'net'
import {
mockWebSocketProvider,
mockPriceWebSocketServer,
createAdapter,
setEnvVariables,
} from './setup'
import request, { SuperTest, Test } from 'supertest'
import { Server } from 'mock-socket'
import { expose, ServerInstance } from '@chainlink/external-adapter-framework'
import { sleep } from '@chainlink/external-adapter-framework/util'
import { WebSocketClassProvider } from '@chainlink/external-adapter-framework/transports'
import { mockConnectionTime } from './fixtures'
describe('Price Endpoint', () => {
let fastify: ServerInstance | undefined
let req: SuperTest<Test>
let mockPriceWsServer: Server | undefined
let spy: jest.SpyInstance
const makeRequest = (body: any) =>
req
.post('/')
.send(body)
.set('Accept', '*/*')
.set('Content-Type', 'application/json')
.expect('Content-Type', /json/)
jest.setTimeout(10000)
let oldEnv: NodeJS.ProcessEnv
beforeAll(async () => {
const wsEndpoint = 'ws://localhost:9090'
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_USERNAME'] = 'test-username'
process.env['WS_API_PASSWORD'] = 'test-password'
process.env['WS_API_ENDPOINT'] = wsEndpoint
spy = jest.spyOn(Date, 'now').mockReturnValue(mockConnectionTime.getTime())
mockWebSocketProvider(WebSocketClassProvider)
mockPriceWsServer = mockPriceWebSocketServer(wsEndpoint)
fastify = await expose(createAdapter())
req = request(`http://localhost:${(fastify?.server.address() as AddressInfo).port}`)
// Send initial request to start background execute
await req.post('/').send({ data: { base: 'JPY', quote: 'USD' } })
await sleep(5000)
})
afterAll((done) => {
spy.mockRestore()
setEnvVariables(oldEnv)
mockPriceWsServer?.close()
fastify?.close(done())
})
it('should succeed with CAD/USD', async () => {
const response = await makeRequest({ data: { base: 'CAD', quote: 'USD' } })
expect(response.body).toMatchSnapshot()
}, 30000)
it('should succeed with USD/CAD', async () => {
const response = await makeRequest({ data: { base: 'USD', quote: 'CAD' } })
expect(response.body).toMatchSnapshot()
}, 30000)
it('should return price', async () => {
const response = await makeRequest({ data: { base: 'EUR', quote: 'USD' } })
expect(response.body).toMatchSnapshot()
}, 30000)
it('should return price for inverse pair', async () => {
const response = await makeRequest({ data: { base: 'IDR', quote: 'USD' } })
expect(response.body).toMatchSnapshot()
}, 30000)
it('should return price for specific source', async () => {
const response = await makeRequest({ data: { base: 'EUR', quote: 'USD', icapSource: 'BGK' } })
expect(response.body).toMatchSnapshot()
}, 30000)
it('should return error when queried for TP price', async () => {
const response = await makeRequest({ data: { base: 'ABC', quote: 'USD' } })
expect(response.body).toMatchSnapshot()
}, 30000)
it('should return error when queried for stale price', async () => {
const response = await makeRequest({ data: { base: 'JPY', quote: 'USD' } })
expect(response.body).toMatchSnapshot()
}, 30000)
it('should return error on empty body', async () => {
const response = await makeRequest({})
expect(response.body).toMatchSnapshot()
}, 30000)
it('should return error on empty data', async () => {
const response = await makeRequest({ data: {} })
expect(response.body).toMatchSnapshot()
}, 30000)
it('should return error on empty base', async () => {
const response = await makeRequest({ data: { quote: 'USD' } })
expect(response.body).toMatchSnapshot()
}, 30000)
it('should return error on empty quote', async () => {
const response = await makeRequest({ data: { base: 'EUR' } })
expect(response.body).toMatchSnapshot()
}, 30000)
})