Skip to content

Commit f744945

Browse files
adds unit tests
1 parent 65a702d commit f744945

1 file changed

Lines changed: 139 additions & 0 deletions

File tree

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import { EndpointContext } from '@chainlink/external-adapter-framework/adapter'
2+
import { SubscriptionDeltas } from '@chainlink/external-adapter-framework/transports/abstract/streaming'
3+
import { WebsocketReverseMappingTransport } from '@chainlink/external-adapter-framework/transports/websocket'
4+
import { LoggerFactoryProvider } from '@chainlink/external-adapter-framework/util'
5+
import { transport as lwbaTransport } from '../../src/transport/crypto-lwba'
6+
import { transport as priceTransport } from '../../src/transport/price'
7+
import { transport as vwapTransport } from '../../src/transport/vwap'
8+
9+
LoggerFactoryProvider.set()
10+
11+
type TickerParam = { base: string; quote: string }
12+
13+
type CustomSubscriptionMessagesBuilder = (
14+
context: EndpointContext<unknown>,
15+
subscriptions: SubscriptionDeltas<TickerParam>,
16+
) => unknown[]
17+
18+
const getCustomSubscriptionMessages = (
19+
transport: WebsocketReverseMappingTransport<unknown, string>,
20+
): CustomSubscriptionMessagesBuilder =>
21+
(
22+
transport as unknown as {
23+
config: { builders: { customSubscriptionMessages: CustomSubscriptionMessagesBuilder } }
24+
}
25+
).config.builders.customSubscriptionMessages
26+
27+
const context = {} as EndpointContext<unknown>
28+
29+
describe.each([
30+
['price', priceTransport, 'vwap_subscribe', 'vwap_unsubscribe'],
31+
['vwap', vwapTransport, 'fixedvwap_subscribe', 'fixedvwap_unsubscribe'],
32+
['crypto-lwba', lwbaTransport, 'bidask_subscribe', 'bidask_unsubscribe'],
33+
] as const)(
34+
'%s transport customSubscriptionMessages',
35+
(_name, transport, subscribeMethod, unsubscribeMethod) => {
36+
const newParams: TickerParam[] = [
37+
{ base: 'ETH', quote: 'EUR' },
38+
{ base: 'LINK', quote: 'ETH' },
39+
]
40+
const staleParams: TickerParam[] = [{ base: 'BTC', quote: 'USD' }]
41+
42+
it('sends one subscribe message containing all new tickers', () => {
43+
const setReverseMapping = jest.spyOn(transport, 'setReverseMapping')
44+
const subscriptions: SubscriptionDeltas<TickerParam> = {
45+
desired: newParams,
46+
new: newParams,
47+
stale: [],
48+
}
49+
50+
const messages = getCustomSubscriptionMessages(transport)(context, subscriptions)
51+
52+
expect(messages).toEqual([
53+
{
54+
jsonrpc: '2.0',
55+
method: subscribeMethod,
56+
params: { tickers: ['ETHEUR', 'LINKETH'] },
57+
},
58+
])
59+
expect(setReverseMapping).toHaveBeenCalledTimes(2)
60+
expect(setReverseMapping).toHaveBeenCalledWith('ETHEUR', newParams[0])
61+
expect(setReverseMapping).toHaveBeenCalledWith('LINKETH', newParams[1])
62+
63+
setReverseMapping.mockRestore()
64+
})
65+
66+
it('sends one unsubscribe message containing all stale tickers', () => {
67+
const subscriptions: SubscriptionDeltas<TickerParam> = {
68+
desired: [],
69+
new: [],
70+
stale: staleParams,
71+
}
72+
73+
const messages = getCustomSubscriptionMessages(transport)(context, subscriptions)
74+
75+
expect(messages).toEqual([
76+
{
77+
jsonrpc: '2.0',
78+
method: unsubscribeMethod,
79+
params: { tickers: ['BTCUSD'] },
80+
},
81+
])
82+
})
83+
84+
it('batches subscribe and unsubscribe into separate messages when both change', () => {
85+
const setReverseMapping = jest.spyOn(transport, 'setReverseMapping')
86+
const subscriptions: SubscriptionDeltas<TickerParam> = {
87+
desired: newParams,
88+
new: newParams,
89+
stale: staleParams,
90+
}
91+
92+
const messages = getCustomSubscriptionMessages(transport)(context, subscriptions)
93+
94+
expect(messages).toEqual([
95+
{
96+
jsonrpc: '2.0',
97+
method: subscribeMethod,
98+
params: { tickers: ['ETHEUR', 'LINKETH'] },
99+
},
100+
{
101+
jsonrpc: '2.0',
102+
method: unsubscribeMethod,
103+
params: { tickers: ['BTCUSD'] },
104+
},
105+
])
106+
expect(setReverseMapping).toHaveBeenCalledTimes(2)
107+
108+
setReverseMapping.mockRestore()
109+
})
110+
111+
it('returns no messages when there are no subscription changes', () => {
112+
const subscriptions: SubscriptionDeltas<TickerParam> = {
113+
desired: newParams,
114+
new: [],
115+
stale: [],
116+
}
117+
118+
expect(getCustomSubscriptionMessages(transport)(context, subscriptions)).toEqual([])
119+
})
120+
121+
it('uses customSubscriptionMessages instead of per-ticker subscribeMessage', () => {
122+
const builders = (
123+
transport as unknown as {
124+
config: {
125+
builders: {
126+
customSubscriptionMessages?: unknown
127+
subscribeMessage?: unknown
128+
unsubscribeMessage?: unknown
129+
}
130+
}
131+
}
132+
).config.builders
133+
134+
expect(builders.customSubscriptionMessages).toBeDefined()
135+
expect(builders.subscribeMessage).toBeUndefined()
136+
expect(builders.unsubscribeMessage).toBeUndefined()
137+
})
138+
},
139+
)

0 commit comments

Comments
 (0)