-
Notifications
You must be signed in to change notification settings - Fork 308
/
price-ws.test.ts
131 lines (122 loc) · 4.35 KB
/
price-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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { EndpointContext } from '@chainlink/external-adapter-framework/adapter'
import * as queryString from 'querystring'
import { config, VALID_QUOTES } from '../../src/config'
import {
calculateAssetMetricsUrl,
handleAssetMetricsMessage,
WsAssetMetricsErrorResponse,
WsAssetMetricsSuccessResponse,
WsAssetMetricsWarningResponse,
invalidBaseAssets,
} from '../../src/transport/price-ws'
import { assetMetricsInputParameters, BaseEndpointTypes } from '../../src/endpoint/price'
import { LoggerFactoryProvider } from '@chainlink/external-adapter-framework/util'
//Since the test is directly using endpoint functions, we need to initialize the logger here
LoggerFactoryProvider.set()
const EXAMPLE_SUCCESS_MESSAGE: WsAssetMetricsSuccessResponse = {
time: Date.now().toString(),
asset: 'eth',
height: 99999,
hash: 'nwiiwefepnfpnwiwiwfi',
parent_hash: 'iriwwfnpfpuffp',
type: 'price',
cm_sequence_id: 9,
ReferenceRateUSD: '1500',
}
const EXAMPLE_WARNING_MESSAGE: WsAssetMetricsWarningResponse = {
warning: {
type: 'warning',
message: 'This is a warning message',
},
}
const EXAMPLE_ERROR_MESSAGE: WsAssetMetricsErrorResponse = {
error: {
type: 'error',
message: 'This is an error message',
},
}
const EXAMPLE_REORG_MESSAGE = {
...EXAMPLE_SUCCESS_MESSAGE,
type: 'reorg',
}
config.initialize()
const EXAMPLE_CONTEXT: EndpointContext<BaseEndpointTypes> = {
endpointName: 'price',
inputParameters: assetMetricsInputParameters,
adapterSettings: config.settings,
}
describe('price-ws url generator', () => {
let oldEnv: NodeJS.ProcessEnv
const invalid_currencies = invalidBaseAssets
beforeAll(() => {
oldEnv = JSON.parse(JSON.stringify(process.env))
process.env['API_KEY'] = 'someKey'
})
afterAll(() => {
process.env = oldEnv
})
it('should correct casing in url', async () => {
const url = await calculateAssetMetricsUrl(EXAMPLE_CONTEXT, [
{
base: 'ETH'.toUpperCase(), //Deliberately use the wrong case
//@ts-expect-error since we are testing the failure exactly we need this so that the pipeline won't fail
quote: 'usd'.toLowerCase(), //Deliberately use the wrong case
},
])
expect(url).toContain(queryString.stringify({ assets: 'eth' }))
expect(url).toContain(queryString.stringify({ metrics: 'ReferenceRateUSD' }))
})
it('should compose the url using all desired subs', async () => {
const url = await calculateAssetMetricsUrl(EXAMPLE_CONTEXT, [
{
base: 'btc', //Deliberately use the wrong case
//@ts-expect-error since we are testing the failure exactly we need this so that the pipeline won't fail
quote: 'usd', //Deliberately use the wrong case
},
{
base: 'eth', //Deliberately use the wrong case
//@ts-expect-error since we are testing the failure exactly we need this so that the pipeline won't fail
quote: 'EUR', //Deliberately use the wrong case
},
])
expect(url).toContain(new URLSearchParams({ assets: 'btc,eth' }).toString())
expect(url).toContain(
new URLSearchParams({ metrics: 'ReferenceRateEUR,ReferenceRateUSD' }).toString(),
)
})
it('invalid request, should compose url with invalid pair', async () => {
invalid_currencies.push('usd')
const url = await calculateAssetMetricsUrl(EXAMPLE_CONTEXT, [
{
base: 'BTC'.toUpperCase(),
quote: VALID_QUOTES.USD,
},
{
base: 'USD',
quote: VALID_QUOTES.BTC,
},
])
expect(url).toContain(new URLSearchParams({ assets: 'btc' }).toString())
expect(url).toContain(new URLSearchParams({ metrics: 'ReferenceRateUSD' }).toString())
})
})
describe('price-ws message handler', () => {
it('success message results in value', () => {
const res = handleAssetMetricsMessage({ ...EXAMPLE_SUCCESS_MESSAGE })
expect(res).toBeDefined()
expect(res?.length).toEqual(1)
expect(res?.[0].response.result).toEqual(1500)
})
it('warning message results in undefined', () => {
const res = handleAssetMetricsMessage(EXAMPLE_WARNING_MESSAGE)
expect(res).toEqual([])
})
it('error message results in undefined', () => {
const res = handleAssetMetricsMessage(EXAMPLE_ERROR_MESSAGE)
expect(res).toEqual([])
})
it('reorg message results in undefined', () => {
const res = handleAssetMetricsMessage(EXAMPLE_REORG_MESSAGE)
expect(res).toEqual([])
})
})