Skip to content

Commit 37e0ed8

Browse files
chore: change Relay Test
1 parent aa658be commit 37e0ed8

File tree

21 files changed

+197
-88
lines changed

21 files changed

+197
-88
lines changed

packages/suite/src/views/settings/SettingsDebug/SuiteSyncSettings.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { useState } from 'react';
22

3+
import { useServices } from '@suite-common/redux-utils';
34
import {
45
DEFAULT_SUITE_SYNC_RELAY_URL,
5-
changeRelayUrlThunk,
66
selectSuiteSyncRelayUrl,
77
suiteSyncActions,
88
} from '@suite-common/suite-sync';
@@ -17,6 +17,9 @@ import { useLabelingCombined } from 'src/hooks/suite/useLabelingCombined';
1717
export const SuiteSyncSettings = () => {
1818
const [isLoading, setIsLoading] = useState(false);
1919

20+
const dispatch = useDispatch();
21+
const { suiteSync } = useServices();
22+
2023
const {
2124
isSuiteSyncDebugEnabled,
2225
isFeatureSuiteSyncAvailable,
@@ -30,8 +33,6 @@ export const SuiteSyncSettings = () => {
3033

3134
const [relayUrl, setRelayUrl] = useState(suiteSyncRelayUrl ?? '');
3235

33-
const dispatch = useDispatch();
34-
3536
const handleToggleSuiteSyncDebug = () => {
3637
dispatch(
3738
suiteSyncActions.updateSuiteSyncDebugEnabled({
@@ -40,9 +41,10 @@ export const SuiteSyncSettings = () => {
4041
);
4142
};
4243

43-
const onRelayUrlSave = () => {
44+
const onRelayUrlSave = async () => {
4445
setIsLoading(true);
45-
dispatch(changeRelayUrlThunk({ relayUrl }));
46+
47+
await suiteSync.changeRelayUrl({ relayUrl });
4648

4749
// Fake it, to make some UI interaction for the user
4850
setTimeout(() => {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { CreateSuiteSyncOwner } from './Owner';
22
import { SuiteSyncStorageRepository } from './SuiteSyncStorageRepository';
33

4+
export type ChangeRelayUrl = (params: { relayUrl: string | null }) => Promise<void>;
5+
6+
export interface ChangeRelayUrlDep {
7+
changeRelayUrl: ChangeRelayUrl;
8+
}
9+
410
export type SuiteSync = {
11+
changeRelayUrl: ChangeRelayUrl;
512
suiteSyncStorageRepository: SuiteSyncStorageRepository;
613
createSuiteSyncOwner: CreateSuiteSyncOwner;
714
};

suite-common/suite-sync-storage/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
export type {
2+
SuiteSyncStorageRepository,
23
CreateSuiteSyncStorageRepository,
34
CreateSuiteStorage,
45
} from './SuiteSyncStorageRepository';
56
export { createSuiteSyncStorageRepositoryFactory } from './SuiteSyncStorageRepository';
67
export type { SuiteSyncStorage } from './SuiteSyncStorage';
78
export type { CreateSuiteSyncOwner } from './Owner';
8-
export type { SuiteSync } from './SuiteSync';
9+
export type { SuiteSync, ChangeRelayUrlDep, ChangeRelayUrl } from './SuiteSync';
910

1011
// Todo: this shared object shall be handled by Dependency Injection, this is madness
1112
export { subscriptionStorage } from './sharedObjects';

suite-common/suite-sync/src/changeRelayUrlThunk.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1 @@
1-
import { isDevEnv } from '@suite-common/suite-utils';
2-
31
export const SUITE_SYNC_STORAGE_PREFIX = '@suite/suite-sync';
4-
5-
// The `https://suite-sync.trezor.io/` MUST have the last `/` in the URL.
6-
export const DEFAULT_SUITE_SYNC_RELAY_URL = isDevEnv
7-
? 'https://evolu.suite.sldev.cz/evolu/'
8-
: 'https://suite-sync.trezor.io/';
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { Dispatch } from '@reduxjs/toolkit';
2+
3+
import {
4+
CreateSuiteStorage,
5+
CreateSuiteSyncOwner,
6+
SuiteSync,
7+
createSuiteSyncStorageRepositoryFactory,
8+
} from '@suite-common/suite-sync-storage';
9+
import { selectAllDeviceOwners } from '@suite-common/wallet-core';
10+
11+
import { createChangeRelayUrl } from './relay/changeRelayUrl';
12+
import { DEFAULT_SUITE_SYNC_RELAY_URL } from './relay/relayUrl';
13+
import { selectSuiteSyncRelayUrl } from './suiteSyncSelectors';
14+
15+
type CreateSuiteSyncCompositionRootDeps = {
16+
createSuiteStorage: CreateSuiteStorage;
17+
createSuiteSyncOwner: CreateSuiteSyncOwner;
18+
getState: () => any;
19+
dispatch: Dispatch;
20+
};
21+
22+
export const createSuiteSyncCompositionRoot = (
23+
deps: CreateSuiteSyncCompositionRootDeps,
24+
): SuiteSync => {
25+
const suiteSyncStorageRepository = createSuiteSyncStorageRepositoryFactory({
26+
createSuiteStorage: deps.createSuiteStorage,
27+
defaultRelayUrl: DEFAULT_SUITE_SYNC_RELAY_URL,
28+
getRelayUrl: () => selectSuiteSyncRelayUrl(deps.getState()),
29+
})();
30+
31+
return {
32+
changeRelayUrl: createChangeRelayUrl({
33+
suiteSyncStorageRepository,
34+
getAllDevicesOwners: () => selectAllDeviceOwners(deps.getState()),
35+
dispatch: deps.dispatch,
36+
}),
37+
suiteSyncStorageRepository,
38+
createSuiteSyncOwner: deps.createSuiteSyncOwner,
39+
};
40+
};

suite-common/suite-sync/src/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,25 @@ export {
88
} from './suiteSyncSelectors';
99

1010
export type { WithSuiteSyncAndDeviceState } from './suiteSyncSelectors';
11+
export { createSuiteSyncCompositionRoot } from './createSuiteSyncCompositionRoot';
1112
export { useSuiteSync } from './useSuiteSync';
1213
export { prepareSuiteSyncReducer, initialSuiteSyncState } from './suiteSyncReducer';
1314
export type { SuiteSyncState, SuiteSyncSettings } from './suiteSyncReducer';
1415
export { suiteSyncActions } from './suiteSyncActions';
1516
export { subscribeSuiteSyncStorageThunk } from './subscribeSuiteSyncStorageThunk';
17+
export { DEFAULT_SUITE_SYNC_RELAY_URL } from './relay/relayUrl';
18+
19+
// Todo: refactor to services, so they can be isolated & tested!
1620
export { unsubscribeAndDisposeSuiteSyncStorageThunk } from './unsubscribeAndDisposeSuiteSyncStorageThunk';
17-
export { changeRelayUrlThunk } from './changeRelayUrlThunk';
18-
export { DEFAULT_SUITE_SYNC_RELAY_URL } from './constants';
1921
export { disposeAllSuiteSyncStoragesThunk } from './disposeAllSuiteSyncStoragesThunk';
2022

2123
// Labeling
24+
// Todo: refactor to services, so they can be isolated & tested!
2225
export { updateWalletLabelThunk } from './labeling/updateWalletLabelThunk';
2326
export { updateAccountLabelThunk } from './labeling/updateAccountLabelThunk';
2427
export { updateOutputLabelThunk } from './labeling/updateOutputLabelThunk';
2528
export { updateAddressLabelThunk } from './labeling/updateAddressLabelThunk';
29+
2630
export {
2731
selectWalletLabel,
2832
selectAccountLabels,
@@ -36,5 +40,7 @@ export { findAccountLabel, findOutputLabel, findAddressLabel } from './labeling/
3640
export type { WithLabelingState } from './labeling/labelingSelectors';
3741
export { prepareLabelingReducer, initialLabelingState } from './labeling/labelingReducer';
3842
export type { LabelingState } from './labeling/labelingReducer';
39-
export { processMetadataMessageThunk } from './labeling/processMetadataMessageThunk';
4043
export { labelingActions } from './labeling/labelingActions';
44+
45+
// Legacy
46+
export { processMetadataMessageThunk } from './labeling/processMetadataMessageThunk';
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Dispatch } from '@reduxjs/toolkit';
2+
3+
import { ChangeRelayUrl, SuiteSyncStorageRepository } from '@suite-common/suite-sync-storage';
4+
import { SuiteSyncOwner } from '@suite-common/suite-types';
5+
6+
import { setSuiteSyncRelayUrl } from '../suiteSyncActions';
7+
import { DEFAULT_SUITE_SYNC_RELAY_URL } from './relayUrl';
8+
9+
export type ChangeRelayUrlDeps = {
10+
getAllDevicesOwners: () => SuiteSyncOwner[];
11+
dispatch: Dispatch;
12+
suiteSyncStorageRepository: SuiteSyncStorageRepository;
13+
};
14+
15+
export const createChangeRelayUrl =
16+
(deps: ChangeRelayUrlDeps): ChangeRelayUrl =>
17+
async ({ relayUrl }) => {
18+
deps.dispatch(setSuiteSyncRelayUrl({ url: relayUrl }));
19+
20+
// We save empty, but we need to reconnect to DEFAULT in case user clears relay form to empty
21+
const normalizedUrl =
22+
relayUrl === null || relayUrl.trim() === '' ? DEFAULT_SUITE_SYNC_RELAY_URL : relayUrl;
23+
24+
const owners = deps.getAllDevicesOwners();
25+
26+
for (const owner of owners) {
27+
await deps.suiteSyncStorageRepository.get(owner).updateRelayUrl(normalizedUrl);
28+
}
29+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { isDevEnv } from '@suite-common/suite-utils';
2+
3+
// The `https://suite-sync.trezor.io/` MUST have the last `/` in the URL.
4+
5+
export const DEFAULT_SUITE_SYNC_RELAY_URL = isDevEnv
6+
? 'https://evolu.suite.sldev.cz/evolu/'
7+
: 'https://suite-sync.trezor.io/';
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import type { UnknownAction } from '@reduxjs/toolkit';
2+
3+
import { SuiteSyncStorage } from '@suite-common/suite-sync-storage';
4+
import { asSuiteSyncOwnerId, asSuiteSyncOwnerSecretHex } from '@suite-common/suite-types';
5+
6+
import { ChangeRelayUrlDeps, createChangeRelayUrl } from '../../src/relay/changeRelayUrl';
7+
import { mockNotExpected } from '../utils';
8+
9+
const owner1 = {
10+
ownerId: asSuiteSyncOwnerId('OwnerId_1'),
11+
ownerSecret: asSuiteSyncOwnerSecretHex('OwnerSecret_2'),
12+
};
13+
14+
describe(createChangeRelayUrl.name, () => {
15+
it('changes url', () => {
16+
const actions: UnknownAction[] = [];
17+
18+
const mockStorage: SuiteSyncStorage = {
19+
accountLabels: {} as any,
20+
addressLabels: {} as any,
21+
outputLabels: {} as any,
22+
walletLabels: {} as any,
23+
dispose: mockNotExpected('dispose'),
24+
updateRelayUrl: jest.fn(),
25+
};
26+
27+
const deps: ChangeRelayUrlDeps = {
28+
getAllDevicesOwners: () => [owner1],
29+
dispatch: (action: any) => actions.push(action),
30+
suiteSyncStorageRepository: {
31+
delete: mockNotExpected('delete'),
32+
get: jest.fn(() => mockStorage),
33+
},
34+
};
35+
36+
const changeRelayUrl = createChangeRelayUrl(deps);
37+
changeRelayUrl({ relayUrl: 'http://localhost:4000' });
38+
39+
expect(actions).toStrictEqual([
40+
{
41+
payload: { url: 'http://localhost:4000' },
42+
type: '@suite/suite-sync/set-local-first-storage-relay-url',
43+
},
44+
]);
45+
46+
expect(deps.suiteSyncStorageRepository.get).toHaveBeenCalledWith(owner1);
47+
expect(mockStorage.updateRelayUrl).toHaveBeenCalledWith('http://localhost:4000');
48+
});
49+
});

0 commit comments

Comments
 (0)