Skip to content

Commit

Permalink
test(unit): add unit tests for Code Lightning GRPC info
Browse files Browse the repository at this point in the history
  • Loading branch information
jamaljsr committed Aug 4, 2022
1 parent 856ed04 commit 4dfed75
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/components/designer/lightning/LightningDetails.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions src/lib/docker/composeFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 15 additions & 1 deletion src/utils/network.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 =>
Expand Down
2 changes: 1 addition & 1 deletion src/utils/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ export const getOpenPortRange = async (requestedPorts: number[]): Promise<number
const openPorts: number[] = [];

for (let port of requestedPorts) {
// keep 0 port as this indicates the port isn't type isn't supported for the node
// keep 0 port as this indicates the port isn't supported for the node
if (port === 0) {
openPorts.push(0);
continue;
Expand Down

0 comments on commit 4dfed75

Please sign in to comment.