fix: isolate cleanup from key retrieval, add deleteSetting test
This commit is contained in:
@@ -62,11 +62,15 @@ async function getOpenCodeManager(): Promise<OpenCodeManager> {
|
||||
// Load API key from encrypted storage
|
||||
const keyStore = getSecureKeyStore();
|
||||
openCodeManagerInitPromise = (async () => {
|
||||
try {
|
||||
// Clean up old plain-text key from settings (pre-keychain storage)
|
||||
try {
|
||||
await keyStore.cleanupPlainTextKey('opencode_api_key');
|
||||
} catch {
|
||||
// Best-effort cleanup; not critical
|
||||
}
|
||||
|
||||
// Load API key from encrypted storage
|
||||
try {
|
||||
const key = await keyStore.retrieve('opencode_api_key');
|
||||
if (key) {
|
||||
openCodeManager!.setApiKey(key);
|
||||
|
||||
@@ -776,6 +776,23 @@ describe('ChatEngine', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('deleteSetting', () => {
|
||||
it('should delete a setting by key', async () => {
|
||||
let capturedPredicate: any;
|
||||
vi.mocked(mockLocalDb.delete).mockImplementation(() => ({
|
||||
where: vi.fn((predicate) => {
|
||||
capturedPredicate = predicate;
|
||||
return Promise.resolve();
|
||||
}),
|
||||
} as any));
|
||||
|
||||
await chatEngine.deleteSetting('opencode_api_key');
|
||||
|
||||
expect(mockLocalDb.delete).toHaveBeenCalled();
|
||||
expect(capturedPredicate).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('setSelectedModel', () => {
|
||||
it('should save selected model', async () => {
|
||||
let capturedValues: any;
|
||||
|
||||
@@ -240,7 +240,7 @@ describe('chatHandlers keychain integration', () => {
|
||||
expect(manager.setApiKey).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('still initializes when cleanupPlainTextKey() throws on init', async () => {
|
||||
it('still initializes and loads key when cleanupPlainTextKey() throws on init', async () => {
|
||||
secureKeyStoreCleanupError = new Error('delete failed');
|
||||
|
||||
const mod = await import('../../src/main/ipc/chatHandlers');
|
||||
@@ -252,6 +252,10 @@ describe('chatHandlers keychain integration', () => {
|
||||
const result = await handler!(undefined);
|
||||
// Init should complete even if cleanup fails
|
||||
expect(result.ready).toBe(true);
|
||||
|
||||
// The encrypted key should still be loaded despite cleanup failure
|
||||
const manager = openCodeManagerInstances[0];
|
||||
expect(manager.setApiKey).toHaveBeenCalledWith('encrypted-stored-key');
|
||||
});
|
||||
|
||||
it('returns error when store() throws on chat:setApiKey', async () => {
|
||||
|
||||
Reference in New Issue
Block a user