forked from smartcontractkit/external-adapters-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixtures.ts
109 lines (105 loc) · 2.72 KB
/
fixtures.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
import nock from 'nock'
import { MockWebsocketServer } from '@chainlink/external-adapter-framework/util/testing-utils'
export const mockSubscribeResponse = (apiKey: string, symbol: string) => {
nock(`https://api.chk.elwood.systems`, { encodedQueryParams: true })
.persist()
.post(`/v1/stream?apiKey=${apiKey}`, {
action: 'subscribe',
stream: 'index',
symbol,
index_freq: 1000,
})
.reply(200, {}, [])
}
export const mockUnsubscribeResponse = (apiKey: string, symbol: string) => {
nock(`https://api.chk.elwood.systems`, { encodedQueryParams: true })
.persist()
.post(`/v1/stream?apiKey=${apiKey}`, {
action: 'unsubscribe',
stream: 'index',
symbol,
index_freq: 1000,
})
.reply(200, {}, [])
}
export const mockSubscribeError = (apiKey: string, symbol: string) => {
nock(`https://api.chk.elwood.systems`, { encodedQueryParams: true })
.persist()
.post(`/v1/stream?apiKey=${apiKey}`, {
action: 'subscribe',
stream: 'index',
symbol,
index_freq: 1000,
})
.reply(400, {
error: {
code: 400,
message: 'Symbol is not supported',
errors: [
{
domain: 'stream',
},
],
},
})
}
export const mockWebSocketServer = (URL: string) => {
const mockWsServer = new MockWebsocketServer(URL, { mock: false })
mockWsServer.on('connection', (socket) => {
const parseMessage = () => {
setTimeout(
() =>
socket.send(
JSON.stringify({
type: 'Index',
data: {
bid: '10000',
price: '10001',
ask: '10002',
symbol: 'ETH-USD',
timestamp: '2022-11-08T04:18:18.736534617Z',
},
sequence: 123,
}),
),
10,
)
setTimeout(
() =>
socket.send(
JSON.stringify({
type: 'Index',
data: {
bid: '10001',
price: '10000',
ask: '10002',
symbol: 'BTC-USD',
timestamp: '2022-11-08T04:18:19.736534617Z',
},
sequence: 123,
}),
),
10,
)
setTimeout(
() =>
socket.send(
JSON.stringify({
type: 'Index',
data: {
bid: '32.16',
price: '32.17',
ask: '32.18',
symbol: 'AVAX-USD',
timestamp: '2022-11-08T04:18:20.736534617Z',
},
sequence: 123,
}),
),
10,
)
}
parseMessage()
})
return mockWsServer
}