-
Notifications
You must be signed in to change notification settings - Fork 0
[_]: refactor(mail)/simplify mail account key fetching #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jzunigax2
merged 4 commits into
master
from
fix/make-email-address-optional-param-on-get-keys
May 7, 2026
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c141b5a
refactor(mail): simplify mail account key fetching
jzunigax2 1bd49d4
deps: update @internxt/sdk to version 1.16.2
jzunigax2 00a71a6
test(mail): add unit tests for useMailAccountGuard and useMailKeys hooks
jzunigax2 ffa2a9e
test(mail): refine unit tests for useMailAccountGuard and useMailKeys…
jzunigax2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,58 @@ | ||
| import { renderHook, waitFor } from '@testing-library/react'; | ||
| import { describe, test, expect, vi, beforeEach } from 'vitest'; | ||
| import { Provider } from 'react-redux'; | ||
| import type { PropsWithChildren } from 'react'; | ||
| import { useMailKeys } from './useMailKeys'; | ||
| import { createTestStore } from '@/test-utils/createTestStore'; | ||
| import { MailService } from '@/services/sdk/mail'; | ||
| import { MailKeysService } from '@/services/mail-keys'; | ||
|
|
||
| const createWrapper = (store: ReturnType<typeof createTestStore>) => { | ||
| return ({ children }: PropsWithChildren) => <Provider store={store}>{children}</Provider>; | ||
| }; | ||
|
|
||
| const mockKeys = { | ||
| address: 'jane@inxt.me', | ||
| publicKey: 'pub', | ||
| encryptionPrivateKey: 'enc', | ||
| recoveryPrivateKey: 'rec', | ||
| }; | ||
|
|
||
| describe('useMailKeys', () => { | ||
| beforeEach(() => { | ||
| vi.restoreAllMocks(); | ||
| MailKeysService.instance.clear(); | ||
| }); | ||
|
|
||
| test('When the keys query has no data yet, then it should return null', () => { | ||
| vi.spyOn(MailService.instance, 'getMailAccountKeys').mockReturnValue(new Promise(() => undefined)); | ||
| const store = createTestStore(); | ||
|
|
||
| const { result } = renderHook(() => useMailKeys(), { wrapper: createWrapper(store) }); | ||
|
|
||
| expect(result.current).toBeNull(); | ||
| }); | ||
|
|
||
| test('When the address has decrypted keys cached, then it should return them', async () => { | ||
| vi.spyOn(MailService.instance, 'getMailAccountKeys').mockResolvedValue(mockKeys); | ||
| const decryptedKeys = { publicKey: new Uint8Array([1]), secretKey: new Uint8Array([2]) }; | ||
| MailKeysService.instance.set(mockKeys.address, decryptedKeys); | ||
| const store = createTestStore(); | ||
|
|
||
| const { result } = renderHook(() => useMailKeys(), { wrapper: createWrapper(store) }); | ||
|
|
||
| await waitFor(() => expect(result.current).toBe(decryptedKeys)); | ||
| }); | ||
|
|
||
| test('When the address has no cached keys, then it should return null even after data loads', async () => { | ||
| vi.spyOn(MailService.instance, 'getMailAccountKeys').mockResolvedValue(mockKeys); | ||
| const store = createTestStore(); | ||
|
|
||
| const { result, rerender } = renderHook(() => useMailKeys(), { wrapper: createWrapper(store) }); | ||
|
|
||
| await waitFor(() => { | ||
| rerender(); | ||
| expect(result.current).toBeNull(); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| }); | ||
| }); | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,9 +1,10 @@ | ||
| import type { HybridKeyPair } from 'internxt-crypto'; | ||
| import { useAppSelector } from '@/store/hooks'; | ||
| import { useGetMailAccountKeysQuery } from '@/store/api/mail'; | ||
| import { MailKeysService } from '@/services/mail-keys'; | ||
|
|
||
| export const useMailKeys = (): HybridKeyPair | null => { | ||
| const userEmail = useAppSelector((state) => state.user.user?.email); | ||
| if (!userEmail) return null; | ||
| return MailKeysService.instance.get(userEmail); | ||
| const { data } = useGetMailAccountKeysQuery(); | ||
| const address = data?.address; | ||
| if (!address) return null; | ||
| return MailKeysService.instance.get(address); | ||
| }; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.