-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: correct source in connection event
- Loading branch information
1 parent
78abd0d
commit 0bfe731
Showing
6 changed files
with
211 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useOriginSource } from '../useOriginSource'; | ||
import { SourceType } from '../useMetrics/useMetrics.types'; | ||
import AppConstants from '../../../core/AppConstants'; | ||
import { RootState } from '../../../reducers'; | ||
|
||
// Mock dependencies | ||
jest.mock('react-redux', () => ({ | ||
useSelector: jest.fn((selector: (state: RootState) => unknown) => | ||
selector({ | ||
sdk: { | ||
wc2Metadata: { | ||
id: '', | ||
}, | ||
}, | ||
} as RootState), | ||
), | ||
})); | ||
|
||
jest.mock('../../../core/SDKConnect/SDKConnect', () => ({ | ||
getInstance: jest.fn(() => ({ | ||
getConnection: jest.fn(({ channelId }) => { | ||
if (channelId === '123e4567-e89b-12d3-a456-426614174000') { | ||
return { | ||
id: channelId, | ||
trigger: 'test-trigger', | ||
otherPublicKey: 'test-public-key', | ||
origin: 'test-origin', | ||
protocolVersion: '1.0', | ||
originatorInfo: { name: 'test-originator' }, | ||
initialConnection: true, | ||
validUntil: Date.now() + 10000, | ||
}; | ||
} | ||
return undefined; | ||
}), | ||
})), | ||
})); | ||
|
||
describe('useOriginSource', () => { | ||
const mockState = { | ||
sdk: { | ||
wc2Metadata: { | ||
id: '', | ||
}, | ||
}, | ||
} as RootState; | ||
|
||
const mockSelector = jest.requireMock('react-redux').useSelector; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
mockSelector.mockImplementation((selector: (state: RootState) => unknown) => | ||
selector(mockState) | ||
); | ||
}); | ||
|
||
it('should return undefined when origin is undefined', () => { | ||
const { result } = renderHook(() => useOriginSource({ origin: undefined })); | ||
expect(result.current).toBeUndefined(); | ||
}); | ||
|
||
it('should return SDK source for valid UUID origin with connection', () => { | ||
const { result } = renderHook(() => | ||
useOriginSource({ origin: '123e4567-e89b-12d3-a456-426614174000' }), | ||
); | ||
expect(result.current).toBe(SourceType.SDK); | ||
}); | ||
|
||
it('should return SDK source for SDK_REMOTE_ORIGIN', () => { | ||
const { result } = renderHook(() => | ||
useOriginSource({ | ||
origin: `${AppConstants.MM_SDK.SDK_REMOTE_ORIGIN}some-path`, | ||
}), | ||
); | ||
expect(result.current).toBe(SourceType.SDK); | ||
}); | ||
|
||
it('should return WALLET_CONNECT source when WC metadata is present', () => { | ||
// Mock WalletConnect metadata | ||
const wcState = { | ||
...mockState, | ||
sdk: { | ||
wc2Metadata: { | ||
id: 'some-wc-id', | ||
}, | ||
}, | ||
} as RootState; | ||
|
||
jest.requireMock('react-redux').useSelector.mockImplementation( | ||
(selector: (state: RootState) => unknown) => selector(wcState), | ||
); | ||
|
||
const { result } = renderHook(() => | ||
useOriginSource({ origin: 'some-non-uuid-origin' }), | ||
); | ||
expect(result.current).toBe(SourceType.WALLET_CONNECT); | ||
}); | ||
|
||
it('should return IN_APP_BROWSER source as default', () => { | ||
mockSelector.mockImplementation((selector: (state: RootState) => unknown) => | ||
selector(mockState) | ||
); | ||
|
||
const { result } = renderHook(() => | ||
useOriginSource({ origin: 'https://example.com' }), | ||
); | ||
expect(result.current).toBe(SourceType.IN_APP_BROWSER); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { useSelector } from 'react-redux'; | ||
import SDKConnect from '../../core/SDKConnect/SDKConnect'; | ||
import { RootState } from '../../reducers'; | ||
import { isUUID } from '../../core/SDKConnect/utils/isUUID'; | ||
import { SourceType } from './useMetrics/useMetrics.types'; | ||
import AppConstants from '../../core/AppConstants'; | ||
|
||
interface UseOriginSourceProps { | ||
origin?: string; | ||
} | ||
|
||
type SourceTypeValue = typeof SourceType[keyof typeof SourceType]; | ||
|
||
export const useOriginSource = ({ origin }: UseOriginSourceProps): SourceTypeValue | undefined => { | ||
const { wc2Metadata } = useSelector((state: RootState) => state.sdk); | ||
|
||
// Return undefined if origin is undefined | ||
if (!origin) { | ||
return undefined; | ||
} | ||
|
||
// Check if origin is a UUID (SDK channel ID format) or starts with SDK_REMOTE_ORIGIN | ||
const isChannelId = isUUID(origin); | ||
const isSDKRemoteOrigin = origin.startsWith(AppConstants.MM_SDK.SDK_REMOTE_ORIGIN); | ||
|
||
const sdkConnection = isChannelId | ||
? SDKConnect.getInstance().getConnection({ channelId: origin }) | ||
: undefined; | ||
|
||
// Check if it's SDK (either by UUID connection or remote origin) | ||
if (sdkConnection || isSDKRemoteOrigin) { | ||
return SourceType.SDK; | ||
} | ||
|
||
// Check if origin matches WalletConnect metadata | ||
const isWalletConnect = wc2Metadata?.id && wc2Metadata.id.length > 0; | ||
if (isWalletConnect) { | ||
return SourceType.WALLET_CONNECT; | ||
} | ||
|
||
return SourceType.IN_APP_BROWSER; | ||
}; | ||
|
||
export default useOriginSource; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { isUUID } from './isUUID'; | ||
|
||
describe('isUUID', () => { | ||
it('should return true for valid UUIDs', () => { | ||
const validUUIDs = [ | ||
'123e4567-e89b-12d3-a456-426614174000', | ||
'c73bcdcc-2669-4bf6-81d3-e4ae73fb11fd', | ||
'507f191e-1f7f-4d1b-9bc8-d8d49c6b1012', | ||
// Uppercase should also work due to case-insensitive flag | ||
'A987FBC9-4BED-3078-CF07-9141BA07C9F3', | ||
]; | ||
|
||
validUUIDs.forEach((uuid) => { | ||
expect(isUUID(uuid)).toBe(true); | ||
}); | ||
}); | ||
|
||
it('should return false for invalid UUIDs', () => { | ||
const invalidUUIDs = [ | ||
'', | ||
'not-a-uuid', | ||
'123e4567-e89b-12d3-a456', // incomplete | ||
'123e4567-e89b-12d3-a456-42661417400z', // invalid character | ||
'123e4567-e89b-12d3-a456-4266141740000', // too long | ||
'123e4567.e89b.12d3.a456.426614174000', // wrong separator | ||
null, | ||
undefined, | ||
]; | ||
|
||
invalidUUIDs.forEach((uuid) => { | ||
// @ts-expect-error Testing invalid inputs | ||
expect(isUUID(uuid)).toBe(false); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const isUUID = (str: string) => { | ||
const uuidRegex = | ||
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; | ||
return uuidRegex.test(str); | ||
}; | ||