Skip to content

Commit

Permalink
feat: remember node counts when creating new networks
Browse files Browse the repository at this point in the history
  • Loading branch information
jamaljsr committed May 6, 2024
1 parent c25b429 commit 4b2c287
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 13 deletions.
4 changes: 1 addition & 3 deletions src/components/network/NewNetwork.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ describe('NewNetwork component', () => {
it('should disable c-lightning input on Windows', () => {
mockOS.platform.mockReturnValue('win32');
const { getByLabelText, getByText } = renderComponent();
expect(getByLabelText('Core Lightning')).toHaveValue('0');
expect(getByLabelText('LND')).toHaveValue('2');
expect(getByLabelText('Eclair')).toHaveValue('1');
expect(getByLabelText('Core Lightning')).toBeDisabled();
expect(getByText('Not supported on Windows yet.')).toBeInTheDocument();
});

Expand Down
8 changes: 4 additions & 4 deletions src/components/network/NewNetwork.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ const NewNetwork: React.FC = () => {
layout="vertical"
colon={false}
initialValues={{
lndNodes: isWindows() ? 2 : 1,
clightningNodes: isWindows() ? 0 : 1,
eclairNodes: 1,
bitcoindNodes: 1,
lndNodes: settings.newNodeCounts.LND,
clightningNodes: settings.newNodeCounts['c-lightning'],
eclairNodes: settings.newNodeCounts.eclair,
bitcoindNodes: settings.newNodeCounts.bitcoind,
customNodes: initialCustomValues,
}}
onFinish={createAsync.execute}
Expand Down
8 changes: 8 additions & 0 deletions src/lib/settings/settingsService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ describe('SettingsService', () => {
checkForUpdatesOnStartup: false,
theme: 'dark',
nodeImages: { custom: [], managed: [] },
newNodeCounts: {
LND: 1,
'c-lightning': 1,
eclair: 1,
bitcoind: 1,
btcd: 0,
tapd: 0,
},
};
});

Expand Down
51 changes: 46 additions & 5 deletions src/store/models/app.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { notification } from 'antd';
import { createStore } from 'easy-peasy';
import os from 'os';
import { defaultRepoState } from 'utils/constants';
import { injections } from 'utils/tests';
import appModel from './app';
import designerModel from './designer';
import modalsModel from './modals';
import networkModel from './network';

jest.mock('os');

const mockOS = os as jest.Mocked<typeof os>;
const mockDockerService = injections.dockerService as jest.Mocked<
typeof injections.dockerService
>;
Expand Down Expand Up @@ -43,6 +47,14 @@ describe('App model', () => {
checkForUpdatesOnStartup: false,
theme: 'dark',
nodeImages: { custom: [], managed: [] },
newNodeCounts: {
LND: 1,
'c-lightning': 1,
eclair: 1,
bitcoind: 1,
btcd: 0,
tapd: 0,
},
});
mockRepoService.load.mockResolvedValue({
...defaultRepoState,
Expand All @@ -53,9 +65,30 @@ describe('App model', () => {
it('should initialize', async () => {
await store.getActions().app.initialize();
expect(store.getState().app.initialized).toBe(true);
expect(mockSettingsService.load).toBeCalledTimes(1);
expect(mockDockerService.getVersions).toBeCalledTimes(1);
expect(mockDockerService.loadNetworks).toBeCalledTimes(1);
expect(mockSettingsService.load).toHaveBeenCalledTimes(1);
expect(mockDockerService.getVersions).toHaveBeenCalledTimes(1);
expect(mockDockerService.loadNetworks).toHaveBeenCalledTimes(1);
});

it('should have correct default node counts on Windows', async () => {
mockOS.platform.mockReturnValue('win32');
mockSettingsService.load.mockResolvedValue({
lang: 'en-US',
showAllNodeVersions: true,
checkForUpdatesOnStartup: false,
theme: 'dark',
nodeImages: { custom: [], managed: [] },
} as any);
await store.getActions().app.initialize();
expect(store.getState().app.initialized).toBe(true);
expect(store.getState().app.settings.newNodeCounts).toEqual({
LND: 2,
'c-lightning': 0,
eclair: 1,
bitcoind: 1,
btcd: 0,
tapd: 0,
});
});

it('should initialize with missing settings', async () => {
Expand Down Expand Up @@ -97,6 +130,14 @@ describe('App model', () => {
checkForUpdatesOnStartup: true,
theme: 'dark',
nodeImages: { custom: [], managed: [] },
newNodeCounts: {
LND: 1,
'c-lightning': 1,
eclair: 1,
bitcoind: 1,
btcd: 1,
tapd: 1,
},
});
});

Expand All @@ -106,7 +147,7 @@ describe('App model', () => {
});
await store.getActions().app.initialize();
expect(store.getState().app.initialized).toBe(true);
expect(mockRepoService.checkForUpdates).toBeCalledTimes(1);
expect(mockRepoService.checkForUpdates).toHaveBeenCalledTimes(1);
expect(store.getState().modals.imageUpdates.visible).toBe(false);
});

Expand All @@ -125,7 +166,7 @@ describe('App model', () => {

await store.getActions().app.initialize();
expect(store.getState().app.initialized).toBe(true);
expect(mockRepoService.checkForUpdates).toBeCalledTimes(1);
expect(mockRepoService.checkForUpdates).toHaveBeenCalledTimes(1);
expect(store.getState().modals.imageUpdates.visible).toBe(true);
});

Expand Down
22 changes: 21 additions & 1 deletion src/store/models/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
StoreInjections,
} from 'types';
import { defaultRepoState } from 'utils/constants';
import { isWindows } from 'utils/system';
import { changeTheme } from 'utils/theme';
import { NETWORK_VIEW } from 'components/routing';
import { RootModel } from './';
Expand Down Expand Up @@ -79,6 +80,14 @@ const appModel: AppModel = {
managed: [],
custom: [],
},
newNodeCounts: {
LND: 1,
'c-lightning': 1,
eclair: 1,
bitcoind: 1,
btcd: 0,
tapd: 0,
},
},
dockerVersions: { docker: '', compose: '' },
dockerImages: [],
Expand Down Expand Up @@ -123,7 +132,18 @@ const appModel: AppModel = {
...settings,
};
}),
loadSettings: thunk(async (actions, _, { injections }) => {
loadSettings: thunk(async (actions, _, { injections, getState }) => {
// before loading settings, set the default CLN count to 0 on Windows
if (isWindows()) {
actions.setSettings({
newNodeCounts: {
...getState().settings.newNodeCounts,
LND: 2,
'c-lightning': 0,
},
});
}

const settings = await injections.settingsService.load();
if (settings) {
actions.setSettings(settings);
Expand Down
12 changes: 12 additions & 0 deletions src/store/models/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,18 @@ const networkModel: NetworkModel = {
getStoreActions().designer.setChart({ id: newNetwork.id, chart });
getStoreActions().designer.setActiveId(newNetwork.id);
await actions.save();

await getStoreActions().app.updateSettings({
newNodeCounts: {
LND: lndNodes,
'c-lightning': clightningNodes,
eclair: eclairNodes,
bitcoind: bitcoindNodes,
btcd: 0,
tapd: 0,
},
});

dispatch(push(NETWORK_VIEW(newNetwork.id)));
},
),
Expand Down
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export interface AppSettings {
managed: ManagedImage[];
custom: CustomImage[];
};
/** The default number of each node when creating a new network */
newNodeCounts: Record<NodeImplementation, number>;
}

export interface SettingsInjection {
Expand Down

0 comments on commit 4b2c287

Please sign in to comment.