@@ -6,173 +6,170 @@ import { createRpc } from './createRpc';
66
77// Mock the external dependency
88vi . mock ( '@lightninglabs/lnc-core' , ( ) => ( {
9- subscriptionMethods : [
10- 'lnrpc.Lightning.SubscribeInvoices' ,
11- 'lnrpc.Lightning.SubscribeChannelEvents' ,
12- 'lnrpc.Lightning.ChannelAcceptor'
13- ]
9+ subscriptionMethods : [
10+ 'lnrpc.Lightning.SubscribeInvoices' ,
11+ 'lnrpc.Lightning.SubscribeChannelEvents' ,
12+ 'lnrpc.Lightning.ChannelAcceptor'
13+ ]
1414} ) ) ;
1515
1616// Create the mocked LNC instance
1717const mockLnc = {
18- request : vi . fn ( ) ,
19- subscribe : vi . fn ( )
18+ request : vi . fn ( ) ,
19+ subscribe : vi . fn ( )
2020} as unknown as Mocked < LNC > ;
2121
2222describe ( 'RPC Creation' , ( ) => {
23+ beforeEach ( ( ) => {
24+ vi . clearAllMocks ( ) ;
25+ } ) ;
26+
27+ describe ( 'createRpc function' , ( ) => {
28+ it ( 'should create a proxy object' , ( ) => {
29+ const packageName = 'lnrpc.Lightning' ;
30+ const rpc = createRpc ( packageName , mockLnc ) ;
31+
32+ expect ( typeof rpc ) . toBe ( 'object' ) ;
33+ expect ( rpc ) . toBeInstanceOf ( Object ) ;
34+ } ) ;
35+ } ) ;
36+
37+ describe ( 'Proxy behavior' , ( ) => {
38+ const packageName = 'lnrpc.Lightning' ;
39+ let rpc : Lightning ;
40+
2341 beforeEach ( ( ) => {
24- vi . clearAllMocks ( ) ;
42+ rpc = createRpc ( packageName , mockLnc ) ;
43+ } ) ;
44+
45+ describe ( 'Method name capitalization' , ( ) => {
46+ it ( 'should capitalize method names correctly' , ( ) => {
47+ // Access a property to trigger the proxy get handler
48+ const method = rpc . getInfo ;
49+
50+ expect ( typeof method ) . toBe ( 'function' ) ;
51+
52+ // Call the method to verify capitalization
53+ const request = { includeChannels : true } ;
54+ method ( request ) ;
55+
56+ expect ( mockLnc . request ) . toHaveBeenCalledWith (
57+ 'lnrpc.Lightning.GetInfo' ,
58+ request
59+ ) ;
60+ } ) ;
61+
62+ it ( 'should handle method names with numbers' , ( ) => {
63+ const method = ( rpc as any ) . method123 ;
64+
65+ const request = { } ;
66+ method ( request ) ;
67+
68+ expect ( mockLnc . request ) . toHaveBeenCalledWith (
69+ 'lnrpc.Lightning.Method123' ,
70+ request
71+ ) ;
72+ } ) ;
73+ } ) ;
74+
75+ describe ( 'Unary RPC methods' , ( ) => {
76+ it ( 'should create async functions for non-subscription methods' , async ( ) => {
77+ const method = rpc . getInfo ;
78+ expect ( typeof method ) . toBe ( 'function' ) ;
79+
80+ const mockResponse = { identityPubkey : 'test' } ;
81+ mockLnc . request . mockResolvedValue ( mockResponse ) ;
82+
83+ const request = { } ;
84+ const result = await method ( request ) ;
85+
86+ expect ( result ) . toBe ( mockResponse ) ;
87+ expect ( mockLnc . request ) . toHaveBeenCalledWith (
88+ 'lnrpc.Lightning.GetInfo' ,
89+ request
90+ ) ;
91+ } ) ;
92+
93+ it ( 'should handle empty request objects' , async ( ) => {
94+ const method = rpc . getInfo ;
95+ const request = { } ;
96+
97+ mockLnc . request . mockResolvedValue ( { } ) ;
98+
99+ await method ( request ) ;
100+
101+ expect ( mockLnc . request ) . toHaveBeenCalledWith (
102+ 'lnrpc.Lightning.GetInfo' ,
103+ request
104+ ) ;
105+ } ) ;
25106 } ) ;
26107
27- describe ( 'createRpc function' , ( ) => {
28- it ( 'should create a proxy object' , ( ) => {
29- const packageName = 'lnrpc.Lightning' ;
30- const rpc = createRpc ( packageName , mockLnc ) ;
108+ describe ( 'Streaming RPC methods (subscriptions)' , ( ) => {
109+ it ( 'should create subscription functions for streaming methods' , ( ) => {
110+ // Test with SubscribeInvoices which is in subscriptionMethods
111+ const method = rpc . subscribeInvoices ;
112+
113+ expect ( typeof method ) . toBe ( 'function' ) ;
114+
115+ const request = { addIndex : '1' } ;
116+ const callback = vi . fn ( ) ;
117+ const errCallback = vi . fn ( ) ;
31118
32- expect ( typeof rpc ) . toBe ( 'object' ) ;
33- expect ( rpc ) . toBeInstanceOf ( Object ) ;
34- } ) ;
119+ method ( request , callback , errCallback ) ;
120+
121+ expect ( mockLnc . subscribe ) . toHaveBeenCalledWith (
122+ 'lnrpc.Lightning.SubscribeInvoices' ,
123+ request ,
124+ callback ,
125+ errCallback
126+ ) ;
127+ } ) ;
128+
129+ it ( 'should create subscription functions for ChannelAcceptor' , ( ) => {
130+ const method = rpc . channelAcceptor ;
131+
132+ expect ( typeof method ) . toBe ( 'function' ) ;
133+
134+ const request = { } ;
135+ const callback = vi . fn ( ) ;
136+ const errCallback = vi . fn ( ) ;
137+
138+ method ( request , callback , errCallback ) ;
139+
140+ expect ( mockLnc . subscribe ) . toHaveBeenCalledWith (
141+ 'lnrpc.Lightning.ChannelAcceptor' ,
142+ request ,
143+ callback ,
144+ errCallback
145+ ) ;
146+ } ) ;
35147 } ) ;
36148
37- describe ( 'Proxy behavior' , ( ) => {
38- const packageName = 'lnrpc.Lightning' ;
39- let rpc : Lightning ;
149+ describe ( 'Method classification' , ( ) => {
150+ it ( 'should handle different package names correctly' , ( ) => {
151+ const walletRpc = createRpc < WalletKit > ( 'lnrpc.WalletKit' , mockLnc ) ;
152+ const method = walletRpc . listUnspent ;
40153
41- beforeEach ( ( ) => {
42- rpc = createRpc ( packageName , mockLnc ) ;
43- } ) ;
154+ const request = { minConfs : 1 } ;
155+ method ( request ) ;
156+
157+ expect ( mockLnc . request ) . toHaveBeenCalledWith (
158+ 'lnrpc.WalletKit.ListUnspent' ,
159+ request
160+ ) ;
161+ } ) ;
162+ } ) ;
44163
45- describe ( 'Method name capitalization' , ( ) => {
46- it ( 'should capitalize method names correctly' , ( ) => {
47- // Access a property to trigger the proxy get handler
48- const method = rpc . getInfo ;
164+ describe ( 'Error handling' , ( ) => {
165+ it ( 'should handle LNC request errors' , async ( ) => {
166+ const method = rpc . getInfo ;
167+ const error = new Error ( 'RPC Error' ) ;
168+ mockLnc . request . mockRejectedValueOnce ( error ) ;
49169
50- expect ( typeof method ) . toBe ( 'function' ) ;
51-
52- // Call the method to verify capitalization
53- const request = { includeChannels : true } ;
54- method ( request ) ;
55-
56- expect ( mockLnc . request ) . toHaveBeenCalledWith (
57- 'lnrpc.Lightning.GetInfo' ,
58- request
59- ) ;
60- } ) ;
61-
62- it ( 'should handle method names with numbers' , ( ) => {
63- const method = ( rpc as any ) . method123 ;
64-
65- const request = { } ;
66- method ( request ) ;
67-
68- expect ( mockLnc . request ) . toHaveBeenCalledWith (
69- 'lnrpc.Lightning.Method123' ,
70- request
71- ) ;
72- } ) ;
73- } ) ;
74-
75- describe ( 'Unary RPC methods' , ( ) => {
76- it ( 'should create async functions for non-subscription methods' , async ( ) => {
77- const method = rpc . getInfo ;
78- expect ( typeof method ) . toBe ( 'function' ) ;
79-
80- const mockResponse = { identityPubkey : 'test' } ;
81- mockLnc . request . mockResolvedValue ( mockResponse ) ;
82-
83- const request = { } ;
84- const result = await method ( request ) ;
85-
86- expect ( result ) . toBe ( mockResponse ) ;
87- expect ( mockLnc . request ) . toHaveBeenCalledWith (
88- 'lnrpc.Lightning.GetInfo' ,
89- request
90- ) ;
91- } ) ;
92-
93- it ( 'should handle empty request objects' , async ( ) => {
94- const method = rpc . getInfo ;
95- const request = { } ;
96-
97- mockLnc . request . mockResolvedValue ( { } ) ;
98-
99- await method ( request ) ;
100-
101- expect ( mockLnc . request ) . toHaveBeenCalledWith (
102- 'lnrpc.Lightning.GetInfo' ,
103- request
104- ) ;
105- } ) ;
106- } ) ;
107-
108- describe ( 'Streaming RPC methods (subscriptions)' , ( ) => {
109- it ( 'should create subscription functions for streaming methods' , ( ) => {
110- // Test with SubscribeInvoices which is in subscriptionMethods
111- const method = rpc . subscribeInvoices ;
112-
113- expect ( typeof method ) . toBe ( 'function' ) ;
114-
115- const request = { addIndex : '1' } ;
116- const callback = vi . fn ( ) ;
117- const errCallback = vi . fn ( ) ;
118-
119- method ( request , callback , errCallback ) ;
120-
121- expect ( mockLnc . subscribe ) . toHaveBeenCalledWith (
122- 'lnrpc.Lightning.SubscribeInvoices' ,
123- request ,
124- callback ,
125- errCallback
126- ) ;
127- } ) ;
128-
129- it ( 'should create subscription functions for ChannelAcceptor' , ( ) => {
130- const method = rpc . channelAcceptor ;
131-
132- expect ( typeof method ) . toBe ( 'function' ) ;
133-
134- const request = { } ;
135- const callback = vi . fn ( ) ;
136- const errCallback = vi . fn ( ) ;
137-
138- method ( request , callback , errCallback ) ;
139-
140- expect ( mockLnc . subscribe ) . toHaveBeenCalledWith (
141- 'lnrpc.Lightning.ChannelAcceptor' ,
142- request ,
143- callback ,
144- errCallback
145- ) ;
146- } ) ;
147- } ) ;
148-
149- describe ( 'Method classification' , ( ) => {
150- it ( 'should handle different package names correctly' , ( ) => {
151- const walletRpc = createRpc < WalletKit > (
152- 'lnrpc.WalletKit' ,
153- mockLnc
154- ) ;
155- const method = walletRpc . listUnspent ;
156-
157- const request = { minConfs : 1 } ;
158- method ( request ) ;
159-
160- expect ( mockLnc . request ) . toHaveBeenCalledWith (
161- 'lnrpc.WalletKit.ListUnspent' ,
162- request
163- ) ;
164- } ) ;
165- } ) ;
166-
167- describe ( 'Error handling' , ( ) => {
168- it ( 'should handle LNC request errors' , async ( ) => {
169- const method = rpc . getInfo ;
170- const error = new Error ( 'RPC Error' ) ;
171- mockLnc . request . mockRejectedValueOnce ( error ) ;
172-
173- const request = { } ;
174- await expect ( method ( request ) ) . rejects . toThrow ( 'RPC Error' ) ;
175- } ) ;
176- } ) ;
170+ const request = { } ;
171+ await expect ( method ( request ) ) . rejects . toThrow ( 'RPC Error' ) ;
172+ } ) ;
177173 } ) ;
174+ } ) ;
178175} ) ;
0 commit comments