From 4dfed75ddb3f5d63d564d2521dd0b1a84ac8b2f6 Mon Sep 17 00:00:00 2001 From: jamaljsr <1356600+jamaljsr@users.noreply.github.com> Date: Thu, 4 Aug 2022 00:00:52 -0400 Subject: [PATCH] test(unit): add unit tests for Code Lightning GRPC info --- .../lightning/LightningDetails.spec.tsx | 22 +++++++++++++++++++ src/lib/docker/composeFile.spec.ts | 14 ++++++++++++ src/utils/network.spec.ts | 16 +++++++++++++- src/utils/network.ts | 2 +- 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/components/designer/lightning/LightningDetails.spec.tsx b/src/components/designer/lightning/LightningDetails.spec.tsx index 0bdaffa5d..40bcb7b11 100644 --- a/src/components/designer/lightning/LightningDetails.spec.tsx +++ b/src/components/designer/lightning/LightningDetails.spec.tsx @@ -4,6 +4,7 @@ import { fireEvent, waitFor } from '@testing-library/react'; import { LightningNode, Status } from 'shared/types'; import { Network } from 'types'; import * as files from 'utils/files'; +import { createCLightningNetworkNode } from 'utils/network'; import { defaultStateBalances, defaultStateInfo, @@ -303,6 +304,27 @@ describe('LightningDetails', () => { expect(getByText('http://127.0.0.1:8182')).toBeInTheDocument(); }); + it('should display the GRPC Host', async () => { + const { getByText, findByText } = renderComponent(Status.Started); + fireEvent.click(await findByText('Connect')); + expect(getByText('GRPC Host')).toBeInTheDocument(); + expect(getByText('127.0.0.1:11002')).toBeInTheDocument(); + }); + + it('should not display grpc host for unsupported versions', async () => { + // add an older version c-lightning node that doesn't support GRPC + node = createCLightningNetworkNode( + network, + '0.10.0', // version before GRPC is supported + undefined, + { image: '', command: '' }, + ); + network.nodes.lightning.push(node); + const { queryByText, findByText } = renderComponent(Status.Started); + fireEvent.click(await findByText('Connect')); + expect(queryByText('GRPC Host')).not.toBeInTheDocument(); + }); + it('should open API Doc links in the browser', async () => { shell.openExternal = jest.fn().mockResolvedValue(true); const { getByText, findByText } = renderComponent(Status.Started); diff --git a/src/lib/docker/composeFile.spec.ts b/src/lib/docker/composeFile.spec.ts index a69e86a08..37af5617c 100644 --- a/src/lib/docker/composeFile.spec.ts +++ b/src/lib/docker/composeFile.spec.ts @@ -86,6 +86,20 @@ describe('ComposeFile', () => { expect(service.volumes[0]).toContain('/bob/lightningd:'); }); + it('should have the grpc port for c-lightning', () => { + composeFile.addClightning(clnNode, btcNode); + const service = composeFile.content.services['bob']; + expect(service.command).toContain('--grpc-port'); + }); + + it('should not have the grpc port for c-lightning', () => { + clnNode.version = '0.10.1'; + clnNode.ports.grpc = 0; + composeFile.addClightning(clnNode, btcNode); + const service = composeFile.content.services['bob']; + expect(service.command).not.toContain('--grpc-port'); + }); + it('should use the c-lightning nodes docker data', () => { clnNode.docker = { image: 'my-image', command: 'my-command' }; composeFile.addClightning(clnNode, btcNode); diff --git a/src/utils/network.spec.ts b/src/utils/network.spec.ts index 22aee732f..13b85881d 100644 --- a/src/utils/network.spec.ts +++ b/src/utils/network.spec.ts @@ -1,5 +1,5 @@ import detectPort from 'detect-port'; -import { LndNode, NodeImplementation, Status } from 'shared/types'; +import { CLightningNode, LndNode, NodeImplementation, Status } from 'shared/types'; import { Network } from 'types'; import { defaultRepoState } from './constants'; import { getImageCommand, getOpenPortRange, getOpenPorts, OpenPorts } from './network'; @@ -127,6 +127,20 @@ describe('Network Utils', () => { expect(ports[network.nodes.lightning[3].name].grpc).toBe(10004); }); + it("should not update zero'd grpc port for c-lightning nodes", async () => { + const portsInUse = [8182, 10001]; + mockDetectPort.mockImplementation(port => + Promise.resolve(portsInUse.includes(port) ? port + 1 : port), + ); + network.nodes.bitcoin = []; + // set port to 0, mimicking an old c-lightning node + (network.nodes.lightning[1] as CLightningNode).ports.grpc = 0; + const ports = (await getOpenPorts(network)) as OpenPorts; + expect(ports).toBeDefined(); + expect(ports[network.nodes.lightning[1].name].rest).toBe(8183); + expect(ports[network.nodes.lightning[1].name].grpc).toBeUndefined(); + }); + it('should update the rest ports for lightning nodes', async () => { const portsInUse = [8081]; mockDetectPort.mockImplementation(port => diff --git a/src/utils/network.ts b/src/utils/network.ts index 642d49754..fc9e76db6 100644 --- a/src/utils/network.ts +++ b/src/utils/network.ts @@ -416,7 +416,7 @@ export const getOpenPortRange = async (requestedPorts: number[]): Promise