Rentenversicherer/tests/services/apiKeyService.test.ts
Claude 04fe925891
test: Add comprehensive test coverage for untested modules
Add tests for previously untested components and services:
- apiKeyService.ts: localStorage operations (14 tests)
- ApiKeyModal.tsx: form validation, submission (22 tests)
- App.tsx: state transitions, error handling (23 tests)
- latexService.ts: API calls, template detection (35 tests)
- latex_service.py: LaTeX escaping, compilation (59 tests)
- server.py: Flask routes, field mapping (38 tests)

Also fix geminiService tests by adding proper apiKeyService mock.

Total new test coverage: 173 TypeScript tests, 97 Python tests

https://claude.ai/code/session_01D4k6b4nUjwfcHMvectsri2
2026-01-31 10:33:40 +00:00

125 lines
3 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { getApiKey, setApiKey, clearApiKey, hasApiKey } from '../../services/apiKeyService';
describe('apiKeyService', () => {
const STORAGE_KEY = 'gemini_api_key';
beforeEach(() => {
// Clear localStorage before each test
localStorage.clear();
});
afterEach(() => {
localStorage.clear();
});
describe('getApiKey', () => {
it('should return null when no key is stored', () => {
const result = getApiKey();
expect(result).toBeNull();
});
it('should return the stored API key', () => {
localStorage.setItem(STORAGE_KEY, 'AItest123');
const result = getApiKey();
expect(result).toBe('AItest123');
});
it('should return empty string if empty string was stored', () => {
localStorage.setItem(STORAGE_KEY, '');
const result = getApiKey();
expect(result).toBe('');
});
});
describe('setApiKey', () => {
it('should store the API key in localStorage', () => {
setApiKey('AItest456');
expect(localStorage.getItem(STORAGE_KEY)).toBe('AItest456');
});
it('should overwrite existing key', () => {
localStorage.setItem(STORAGE_KEY, 'AIold');
setApiKey('AInew');
expect(localStorage.getItem(STORAGE_KEY)).toBe('AInew');
});
it('should allow storing empty string', () => {
setApiKey('');
expect(localStorage.getItem(STORAGE_KEY)).toBe('');
});
});
describe('clearApiKey', () => {
it('should remove the API key from localStorage', () => {
localStorage.setItem(STORAGE_KEY, 'AItest');
clearApiKey();
expect(localStorage.getItem(STORAGE_KEY)).toBeNull();
});
it('should not throw when no key exists', () => {
expect(() => clearApiKey()).not.toThrow();
});
});
describe('hasApiKey', () => {
it('should return false when no key is stored', () => {
const result = hasApiKey();
expect(result).toBe(false);
});
it('should return false when empty string is stored', () => {
localStorage.setItem(STORAGE_KEY, '');
const result = hasApiKey();
expect(result).toBe(false);
});
it('should return true when a non-empty key is stored', () => {
localStorage.setItem(STORAGE_KEY, 'AItest789');
const result = hasApiKey();
expect(result).toBe(true);
});
it('should return true for single character key', () => {
localStorage.setItem(STORAGE_KEY, 'A');
const result = hasApiKey();
expect(result).toBe(true);
});
});
describe('integration', () => {
it('should work correctly with set and get', () => {
setApiKey('AIintegration');
expect(getApiKey()).toBe('AIintegration');
expect(hasApiKey()).toBe(true);
});
it('should work correctly with set, clear, and get', () => {
setApiKey('AItest');
expect(hasApiKey()).toBe(true);
clearApiKey();
expect(getApiKey()).toBeNull();
expect(hasApiKey()).toBe(false);
});
});
});