|
| 1 | +import { Lightning } from '@lightninglabs/lnc-core/dist/types/proto/lnrpc'; |
| 2 | +import { WalletKit } from '@lightninglabs/lnc-core/dist/types/proto/walletrpc'; |
| 3 | +import { beforeEach, describe, expect, it, Mocked, vi } from 'vitest'; |
| 4 | +import LNC from '../lnc'; |
| 5 | +import { createRpc } from './createRpc'; |
| 6 | + |
| 7 | +// Mock the external dependency |
| 8 | +vi.mock('@lightninglabs/lnc-core', () => ({ |
| 9 | + subscriptionMethods: [ |
| 10 | + 'lnrpc.Lightning.SubscribeInvoices', |
| 11 | + 'lnrpc.Lightning.SubscribeChannelEvents', |
| 12 | + 'lnrpc.Lightning.ChannelAcceptor' |
| 13 | + ] |
| 14 | +})); |
| 15 | + |
| 16 | +// Create the mocked LNC instance |
| 17 | +const mockLnc = { |
| 18 | + request: vi.fn(), |
| 19 | + subscribe: vi.fn() |
| 20 | +} as unknown as Mocked<LNC>; |
| 21 | + |
| 22 | +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 | + |
| 41 | + beforeEach(() => { |
| 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 | + }); |
| 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 | + }); |
| 177 | + }); |
| 178 | +}); |
0 commit comments