test: add comprehensive coverage for encryption-utils and auth-utils

Added missing test coverage from code review feedback:

- encryption-utils.test.ts:
  * Added error handling tests for encryptApiKey fallback to plaintext
  * Added error handling tests for decryptApiKey throwing on failure
  * Added tests for isEncryptionAvailable function
  * Achieved 100% coverage on all metrics

- auth-utils.test.ts (new file):
  * Added comprehensive tests for generateApiKey function
  * Added validation tests for validateApiKey function
  * Tests edge cases: empty keys, short keys, null/undefined
  * Achieved 100% coverage on all metrics

All tests pass (569 tests). Overall coverage improved:
- auth-utils.ts: 100% statements, 100% branches, 100% functions
- encryption-utils.ts: 100% statements, 100% branches, 100% functions
This commit is contained in:
2025-10-25 21:35:04 -04:00
parent 9df651cd0c
commit f22404957b
2 changed files with 151 additions and 1 deletions

View File

@@ -1,4 +1,4 @@
import { encryptApiKey, decryptApiKey } from '../src/utils/encryption-utils';
import { encryptApiKey, decryptApiKey, isEncryptionAvailable } from '../src/utils/encryption-utils';
// Mock electron module
jest.mock('electron', () => ({
@@ -70,4 +70,51 @@ describe('Encryption Utils', () => {
expect(encrypted).not.toBe(original);
});
});
describe('error handling', () => {
it('should handle encryption errors and fallback to plaintext', () => {
const { safeStorage } = require('electron');
const originalEncrypt = safeStorage.encryptString;
safeStorage.encryptString = jest.fn(() => {
throw new Error('Encryption failed');
});
const apiKey = 'test-api-key-12345';
const result = encryptApiKey(apiKey);
expect(result).toBe(apiKey); // Should return plaintext on error
safeStorage.encryptString = originalEncrypt; // Restore
});
it('should throw error when decryption fails', () => {
const { safeStorage } = require('electron');
const originalDecrypt = safeStorage.decryptString;
safeStorage.decryptString = jest.fn(() => {
throw new Error('Decryption failed');
});
const encrypted = 'encrypted:aW52YWxpZA=='; // Invalid encrypted data
expect(() => decryptApiKey(encrypted)).toThrow('Failed to decrypt API key');
safeStorage.decryptString = originalDecrypt; // Restore
});
});
describe('isEncryptionAvailable', () => {
it('should return true when encryption is available', () => {
const { isEncryptionAvailable } = require('../src/utils/encryption-utils');
const { safeStorage } = require('electron');
safeStorage.isEncryptionAvailable.mockReturnValueOnce(true);
expect(isEncryptionAvailable()).toBe(true);
});
it('should return false when encryption is not available', () => {
const { isEncryptionAvailable } = require('../src/utils/encryption-utils');
const { safeStorage } = require('electron');
safeStorage.isEncryptionAvailable.mockReturnValueOnce(false);
expect(isEncryptionAvailable()).toBe(false);
});
});
});