-
Notifications
You must be signed in to change notification settings - Fork 0
feat(desktop-main): add electron-store and safeStorage wrapper for secrets #227
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
CoderCoco
merged 9 commits into
main
from
claude/issue-150-feat-desktop-main-electron-store-for-app-state-saf
May 31, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2f9f63b
feat(desktop-main): Apollo — Add electron-store dependency
CoderCoco 8f41d44
feat(desktop-main): Borman — Create SafeStorageService
CoderCoco 50aba82
feat(desktop-main): Cassini — Create ElectronStoreService
CoderCoco 22b1758
feat(desktop-main): Drake — Add SafeStorageService tests
CoderCoco 3ec1ac0
feat(desktop-main): Eddington — Add ElectronStoreService tests
CoderCoco 49ac79f
feat(desktop-main): Faraday — Register SafeStorageService and Electro…
CoderCoco e5ee7b3
fix(desktop-main): Glenn — guard encrypt/decrypt on isAvailable(); fi…
CoderCoco 20e5eaa
fix(desktop-main): Hadfield — make aws.region/profile optional in App…
CoderCoco 7bcde85
fix(desktop-main): extend decrypt() @remarks for keychain-state-flip …
CoderCoco 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
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
233 changes: 233 additions & 0 deletions
233
app/packages/desktop-main/src/services/ElectronStoreService.test.ts
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,233 @@ | ||
| /** | ||
| * Unit tests for ElectronStoreService. | ||
| * | ||
| * `electron-store` is mocked at the module level so no real disk I/O or | ||
| * Electron native modules are ever touched. Protected methods | ||
| * (`readIsElectron`, `createStore`) are stubbed via `vi.spyOn` on the | ||
| * prototype before each Electron-path construction so the constructor takes | ||
| * the right branch. | ||
| */ | ||
| import 'reflect-metadata'; | ||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
| import type Store from 'electron-store'; | ||
|
|
||
| vi.mock('../logger.js', () => ({ | ||
| logger: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() }, | ||
| })); | ||
|
|
||
| vi.mock('electron-store', () => { | ||
| const MockStore = vi.fn().mockImplementation(() => ({ | ||
| get: vi.fn(), | ||
| set: vi.fn(), | ||
| })); | ||
| return { default: MockStore }; | ||
| }); | ||
|
|
||
| import { ElectronStoreService, type AppStoreSchema } from './ElectronStoreService.js'; | ||
| import { SafeStorageService } from './SafeStorageService.js'; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Helpers | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| /** | ||
| * Creates a `SafeStorageService` whose `encrypt` / `decrypt` methods are | ||
| * identity functions by default (outside-Electron degraded path). | ||
| */ | ||
| function makeSafeStorage(): SafeStorageService { | ||
| return new SafeStorageService(); | ||
| } | ||
|
|
||
| /** | ||
| * Builds a minimal mock `Store<AppStoreSchema>` compatible with what | ||
| * `ElectronStoreService` calls on it. | ||
| */ | ||
| function makeMockStore(): Store<AppStoreSchema> { | ||
| return { | ||
| get: vi.fn(), | ||
| set: vi.fn(), | ||
| } as unknown as Store<AppStoreSchema>; | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Non-Electron path (Map fallback) | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| describe('ElectronStoreService — non-Electron path (Map fallback)', () => { | ||
| let service: ElectronStoreService; | ||
| let safeStorage: SafeStorageService; | ||
|
|
||
| beforeEach(() => { | ||
| safeStorage = makeSafeStorage(); | ||
| // process.versions['electron'] is not set in Vitest/Node, so the Map | ||
| // fallback is used automatically — no spy needed. | ||
| service = new ElectronStoreService(safeStorage); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should use Map fallback when not running in Electron', () => { | ||
| expect(service.isElectron()).toBe(false); | ||
| expect(service.get('wizardCompleted')).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should store and retrieve a value in Map fallback', () => { | ||
| service.set('wizardCompleted', true); | ||
|
|
||
| expect(service.get('wizardCompleted')).toBe(true); | ||
| }); | ||
|
|
||
| it('should store and retrieve a nested object in Map fallback', () => { | ||
| const awsValue: AppStoreSchema['aws'] = { region: 'us-east-1', profile: 'default' }; | ||
| service.set('aws', awsValue); | ||
|
|
||
| expect(service.get('aws')).toEqual(awsValue); | ||
| }); | ||
| }); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Electron path (mocked Store) | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| describe('ElectronStoreService — Electron path (mocked Store)', () => { | ||
| let service: ElectronStoreService; | ||
| let safeStorage: SafeStorageService; | ||
| let mockStore: Store<AppStoreSchema>; | ||
|
|
||
| beforeEach(() => { | ||
| safeStorage = makeSafeStorage(); | ||
| mockStore = makeMockStore(); | ||
|
|
||
| // Stub prototype BEFORE construction so the constructor takes the Electron branch. | ||
| vi.spyOn( | ||
| ElectronStoreService.prototype as unknown as { readIsElectron(): boolean }, | ||
| 'readIsElectron', | ||
| ).mockReturnValue(true); | ||
| vi.spyOn( | ||
| ElectronStoreService.prototype as unknown as { createStore(): Store<AppStoreSchema> }, | ||
| 'createStore', | ||
| ).mockReturnValue(mockStore); | ||
|
|
||
| service = new ElectronStoreService(safeStorage); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should call store.get when running in Electron', () => { | ||
| (mockStore.get as ReturnType<typeof vi.fn>).mockReturnValue(true); | ||
|
|
||
| const result = service.get('wizardCompleted'); | ||
|
|
||
| expect(mockStore.get).toHaveBeenCalledWith('wizardCompleted'); | ||
| expect(result).toBe(true); | ||
| }); | ||
|
|
||
| it('should call store.set when running in Electron', () => { | ||
| service.set('wizardCompleted', true); | ||
|
|
||
| expect(mockStore.set).toHaveBeenCalledWith('wizardCompleted', true); | ||
| }); | ||
| }); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Secret field — setSecretAccessKeyId / getSecretAccessKeyId | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| describe('ElectronStoreService — setSecretAccessKeyId / getSecretAccessKeyId', () => { | ||
| let service: ElectronStoreService; | ||
| let safeStorage: SafeStorageService; | ||
|
|
||
| beforeEach(() => { | ||
| safeStorage = makeSafeStorage(); | ||
| service = new ElectronStoreService(safeStorage); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should encrypt accessKeyId before storing', () => { | ||
| vi.spyOn(safeStorage, 'encrypt').mockReturnValue('enc-key-id'); | ||
|
|
||
| service.setSecretAccessKeyId('AKID123'); | ||
|
|
||
| expect(safeStorage.encrypt).toHaveBeenCalledWith('AKID123'); | ||
| const stored = service.get('aws'); | ||
| expect(stored?.accessKeyId).toBe('enc-key-id'); | ||
| }); | ||
|
|
||
| it('should decrypt accessKeyId when reading', () => { | ||
| service.set('aws', { region: 'us-east-1', profile: 'default', accessKeyId: 'enc-key-id' }); | ||
| vi.spyOn(safeStorage, 'decrypt').mockReturnValue('AKID123'); | ||
|
|
||
| const result = service.getSecretAccessKeyId(); | ||
|
|
||
| expect(safeStorage.decrypt).toHaveBeenCalledWith('enc-key-id'); | ||
| expect(result).toBe('AKID123'); | ||
| }); | ||
|
|
||
| it('should return undefined for accessKeyId when not stored', () => { | ||
| expect(service.getSecretAccessKeyId()).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Secret field — setSecretAccessKey / getSecretAccessKey | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| describe('ElectronStoreService — setSecretAccessKey / getSecretAccessKey', () => { | ||
| let service: ElectronStoreService; | ||
| let safeStorage: SafeStorageService; | ||
|
|
||
| beforeEach(() => { | ||
| safeStorage = makeSafeStorage(); | ||
| service = new ElectronStoreService(safeStorage); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should encrypt secretAccessKey before storing', () => { | ||
| vi.spyOn(safeStorage, 'encrypt').mockReturnValue('enc-secret-key'); | ||
|
|
||
| service.setSecretAccessKey('MY_SECRET'); | ||
|
|
||
| expect(safeStorage.encrypt).toHaveBeenCalledWith('MY_SECRET'); | ||
| const stored = service.get('aws'); | ||
| expect(stored?.secretAccessKey).toBe('enc-secret-key'); | ||
| }); | ||
|
|
||
| it('should decrypt secretAccessKey when reading', () => { | ||
| service.set('aws', { region: 'us-east-1', profile: 'default', secretAccessKey: 'enc-secret-key' }); | ||
| vi.spyOn(safeStorage, 'decrypt').mockReturnValue('MY_SECRET'); | ||
|
|
||
| const result = service.getSecretAccessKey(); | ||
|
|
||
| expect(safeStorage.decrypt).toHaveBeenCalledWith('enc-secret-key'); | ||
| expect(result).toBe('MY_SECRET'); | ||
| }); | ||
|
|
||
| it('should return undefined for secretAccessKey when not stored', () => { | ||
| expect(service.getSecretAccessKey()).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Round-trip | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| describe('ElectronStoreService — round-trip', () => { | ||
| let service: ElectronStoreService; | ||
| let safeStorage: SafeStorageService; | ||
|
|
||
| beforeEach(() => { | ||
| safeStorage = makeSafeStorage(); | ||
| service = new ElectronStoreService(safeStorage); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should encrypt and decrypt accessKeyId in a round-trip', () => { | ||
| vi.spyOn(safeStorage, 'encrypt').mockImplementation((plaintext: string) => `enc-${plaintext}`); | ||
| vi.spyOn(safeStorage, 'decrypt').mockImplementation((ciphertext: string) => | ||
| ciphertext.startsWith('enc-') ? ciphertext.slice(4) : ciphertext, | ||
| ); | ||
|
|
||
| service.setSecretAccessKeyId('AKID123'); | ||
| const result = service.getSecretAccessKeyId(); | ||
|
|
||
| expect(result).toBe('AKID123'); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Declined. The repo's
vitest.config.tssetsrestoreMocks: true, which makes Vitest callvi.restoreAllMocks()after every individual test (not just at file boundary). Prototype spies are fully restored after eachit()block, so the Electron-branchbeforeEachalways starts from a clean slate before constructing the next service instance. The cross-describe leakage path the comment describes does not exist under this configuration.