|
| 1 | +import { CLNService, HttpService } from './http.service'; |
| 2 | +import { SCROLL_PAGE_SIZE } from '../utilities/constants'; |
| 3 | +import { spyOnIsCompatibleVersion, spyOnListOffersSQL, spyOnListOffersSQLWithoutDesc } from '../utilities/test-utilities/mockUtilities'; |
| 4 | +import { createMockStore } from '../utilities/test-utilities/mockStore'; |
| 5 | +import { mockNodeInfo, mockRootStoreData } from '../utilities/test-utilities/mockData'; |
| 6 | + |
| 7 | +describe('CLNService', () => { |
| 8 | + beforeEach(() => { |
| 9 | + jest.clearAllMocks(); |
| 10 | + }); |
| 11 | + |
| 12 | + describe('listOffers', () => { |
| 13 | + it('should use ListOffersSQL when version is compatible (>= 26.04)', async () => { |
| 14 | + const mockStore = createMockStore('/', { |
| 15 | + root: { ...mockRootStoreData, nodeInfo: { ...mockNodeInfo, version: '26.04' } }, |
| 16 | + }); |
| 17 | + const isCompatibleVersionSpy = spyOnIsCompatibleVersion(); |
| 18 | + const listOffersSQLSpy = spyOnListOffersSQL('desc'); |
| 19 | + const listOffersSQLWithoutDescSpy = spyOnListOffersSQLWithoutDesc('no desc'); |
| 20 | + jest.spyOn(HttpService, 'clnCall').mockResolvedValue({ rows: [] }); |
| 21 | + await CLNService.listOffers(0, mockStore); |
| 22 | + |
| 23 | + expect(isCompatibleVersionSpy).toHaveBeenCalledWith('26.04', '26.04'); |
| 24 | + expect(listOffersSQLSpy).toHaveBeenCalledWith(SCROLL_PAGE_SIZE, 0); |
| 25 | + expect(listOffersSQLWithoutDescSpy).not.toHaveBeenCalled(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should use ListOffersSQLWithoutDesc when version is not compatible (< 26.04)', async () => { |
| 29 | + const mockStore = createMockStore('/', { |
| 30 | + root: { ...mockRootStoreData, nodeInfo: { ...mockNodeInfo, version: '25.12' } }, |
| 31 | + }); |
| 32 | + const isCompatibleVersionSpy = spyOnIsCompatibleVersion(); |
| 33 | + const listOffersSQLSpy = spyOnListOffersSQL('desc'); |
| 34 | + const listOffersSQLWithoutDescSpy = spyOnListOffersSQLWithoutDesc('no desc'); |
| 35 | + jest.spyOn(HttpService, 'clnCall').mockResolvedValue({ rows: [] }); |
| 36 | + |
| 37 | + await CLNService.listOffers(0, mockStore); |
| 38 | + |
| 39 | + expect(isCompatibleVersionSpy).toHaveBeenCalledWith('25.12', '26.04'); |
| 40 | + expect(listOffersSQLWithoutDescSpy).toHaveBeenCalledWith(SCROLL_PAGE_SIZE, 0); |
| 41 | + expect(listOffersSQLSpy).not.toHaveBeenCalled(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should fallback to ListOffersSQLWithoutDesc when query fails with "no such column: description"', async () => { |
| 45 | + const mockStore = createMockStore('/', { |
| 46 | + root: { ...mockRootStoreData, nodeInfo: { ...mockNodeInfo, version: '26.04' } }, |
| 47 | + }); |
| 48 | + const isCompatibleVersionSpy = spyOnIsCompatibleVersion(); |
| 49 | + const listOffersSQLSpy = spyOnListOffersSQL('desc'); |
| 50 | + const listOffersSQLWithoutDescSpy = spyOnListOffersSQLWithoutDesc('no desc'); |
| 51 | + |
| 52 | + const clnCallSpy = jest |
| 53 | + .spyOn(HttpService, 'clnCall') |
| 54 | + // First call fails (new schema not supported) |
| 55 | + .mockRejectedValueOnce(new Error('no such column: description')) |
| 56 | + // Second call succeeds (fallback query) |
| 57 | + .mockResolvedValueOnce({ rows: [] }); |
| 58 | + const result = await CLNService.listOffers(0, mockStore); |
| 59 | + |
| 60 | + expect(clnCallSpy).toHaveBeenCalledTimes(2); |
| 61 | + expect(isCompatibleVersionSpy).toHaveBeenCalledWith('26.04', '26.04'); |
| 62 | + expect(listOffersSQLSpy).toHaveBeenCalledWith(SCROLL_PAGE_SIZE, 0); |
| 63 | + expect(listOffersSQLWithoutDescSpy).toHaveBeenCalledWith(SCROLL_PAGE_SIZE, 0); |
| 64 | + expect(result.offers).toEqual([]); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should handle empty version gracefully', async () => { |
| 68 | + const mockStore = createMockStore('/', { |
| 69 | + root: { ...mockRootStoreData, nodeInfo: { ...mockNodeInfo, version: '' } }, |
| 70 | + }); |
| 71 | + const isCompatibleVersionSpy = spyOnIsCompatibleVersion(); |
| 72 | + const listOffersSQLSpy = spyOnListOffersSQL('desc'); |
| 73 | + const listOffersSQLWithoutDescSpy = spyOnListOffersSQLWithoutDesc('no desc'); |
| 74 | + jest.spyOn(HttpService, 'clnCall').mockResolvedValue({ rows: [{ offer_id: '456' }] }); |
| 75 | + |
| 76 | + const result = await CLNService.listOffers(0, mockStore); |
| 77 | + |
| 78 | + expect(isCompatibleVersionSpy).toHaveBeenCalledWith('', '26.04'); |
| 79 | + expect(listOffersSQLWithoutDescSpy).toHaveBeenCalledWith(SCROLL_PAGE_SIZE, 0); |
| 80 | + expect(listOffersSQLSpy).not.toHaveBeenCalled(); |
| 81 | + expect(result.offers).toBeDefined(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should return empty offers array when rows is undefined', async () => { |
| 85 | + const mockStore = createMockStore('/', { |
| 86 | + root: { ...mockRootStoreData, nodeInfo: { ...mockNodeInfo, version: '25.12' } }, |
| 87 | + }); |
| 88 | + const isCompatibleVersionSpy = spyOnIsCompatibleVersion(); |
| 89 | + const listOffersSQLSpy = spyOnListOffersSQL('desc'); |
| 90 | + const listOffersSQLWithoutDescSpy = spyOnListOffersSQLWithoutDesc('no desc'); |
| 91 | + jest.spyOn(HttpService, 'clnCall').mockResolvedValue({}); |
| 92 | + const result = await CLNService.listOffers(0, mockStore); |
| 93 | + |
| 94 | + expect(isCompatibleVersionSpy).toHaveBeenCalledWith('25.12', '26.04'); |
| 95 | + expect(listOffersSQLWithoutDescSpy).toHaveBeenCalledWith(SCROLL_PAGE_SIZE, 0); |
| 96 | + expect(listOffersSQLSpy).not.toHaveBeenCalled(); |
| 97 | + expect(result.offers).toEqual([]); |
| 98 | + }); |
| 99 | + }); |
| 100 | +}); |
0 commit comments