Skip to content

Commit c5ce3a6

Browse files
committed
chore: normalize indentation to 2 spaces
1 parent ef487d8 commit c5ce3a6

26 files changed

+2780
-2841
lines changed

.prettierignore

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,4 @@ demos/**
33
dist/**
44
coverage/**
55
lib/wasm_exec.js
6-
package-lock.json
7-
package.json
86
README.md
9-
tsconfig.json
10-
tslint.json
11-
webpack.config.js

.prettierrc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"singleQuote": true,
3-
"tabWidth": 4,
3+
"tabWidth": 2,
44
"semi": true,
55
"trailingComma": "none",
6-
"bracketSpacing": true
7-
}
6+
"bracketSpacing": true,
7+
"printWidth": 80
8+
}

lib/api/createRpc.test.ts

Lines changed: 149 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -6,173 +6,170 @@ import { createRpc } from './createRpc';
66

77
// Mock the external dependency
88
vi.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
1717
const mockLnc = {
18-
request: vi.fn(),
19-
subscribe: vi.fn()
18+
request: vi.fn(),
19+
subscribe: vi.fn()
2020
} as unknown as Mocked<LNC>;
2121

2222
describe('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
});

lib/api/createRpc.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,30 @@ const capitalize = (s: string) => s && s[0].toUpperCase() + s.slice(1);
99
* subscribe methods depending on which function is called on the object
1010
*/
1111
export function createRpc<T extends object>(packageName: string, lnc: LNC): T {
12-
const rpc = {};
13-
return new Proxy(rpc, {
14-
get(target, key, c) {
15-
const methodName = capitalize(key.toString());
16-
// the full name of the method (ex: lnrpc.Lightning.OpenChannel)
17-
const method = `${packageName}.${methodName}`;
12+
const rpc = {};
13+
return new Proxy(rpc, {
14+
get(target, key, c) {
15+
const methodName = capitalize(key.toString());
16+
// the full name of the method (ex: lnrpc.Lightning.OpenChannel)
17+
const method = `${packageName}.${methodName}`;
1818

19-
if (subscriptionMethods.includes(method)) {
20-
// call subscribe for streaming methods
21-
return (
22-
request: object,
23-
callback: (msg: object) => void,
24-
errCallback?: (err: Error) => void
25-
): void => {
26-
lnc.subscribe(method, request, callback, errCallback);
27-
};
28-
} else {
29-
// call request for unary methods
30-
return async (request: object): Promise<any> => {
31-
return await lnc.request(method, request);
32-
};
33-
}
34-
}
35-
}) as T;
19+
if (subscriptionMethods.includes(method)) {
20+
// call subscribe for streaming methods
21+
return (
22+
request: object,
23+
callback: (msg: object) => void,
24+
errCallback?: (err: Error) => void
25+
): void => {
26+
lnc.subscribe(method, request, callback, errCallback);
27+
};
28+
} else {
29+
// call request for unary methods
30+
return async (request: object): Promise<any> => {
31+
return await lnc.request(method, request);
32+
};
33+
}
34+
}
35+
}) as T;
3636
}
3737

3838
export default createRpc;

0 commit comments

Comments
 (0)