mirror of
https://github.com/kikootwo/ReadMeABook.git
synced 2026-06-03 04:40:09 +00:00
Add backend unit test framework and modularize settings UI
Introduced a Vitest-based backend unit testing framework with supporting scripts, helpers, and GitHub Actions integration. Refactored the admin settings page to a modular architecture, splitting monolithic logic into feature-specific tabs and hooks for improved maintainability and testability. Updated documentation to reflect the new testing setup and settings architecture, and added new dependencies for testing utilities.
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* Component: Encryption Service Tests
|
||||
* Documentation: documentation/backend/services/config.md
|
||||
*/
|
||||
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const ORIGINAL_KEY = process.env.CONFIG_ENCRYPTION_KEY;
|
||||
|
||||
afterEach(() => {
|
||||
process.env.CONFIG_ENCRYPTION_KEY = ORIGINAL_KEY;
|
||||
vi.resetModules();
|
||||
});
|
||||
|
||||
describe('EncryptionService', () => {
|
||||
it('throws when encryption key is missing', async () => {
|
||||
delete process.env.CONFIG_ENCRYPTION_KEY;
|
||||
vi.resetModules();
|
||||
|
||||
const { EncryptionService } = await import('@/lib/services/encryption.service');
|
||||
expect(() => new EncryptionService()).toThrow(/CONFIG_ENCRYPTION_KEY/);
|
||||
});
|
||||
|
||||
it('encrypts and decrypts values', async () => {
|
||||
process.env.CONFIG_ENCRYPTION_KEY = 'a'.repeat(32);
|
||||
vi.resetModules();
|
||||
|
||||
const { EncryptionService } = await import('@/lib/services/encryption.service');
|
||||
const service = new EncryptionService();
|
||||
|
||||
const encrypted = service.encrypt('secret');
|
||||
const decrypted = service.decrypt(encrypted);
|
||||
|
||||
expect(decrypted).toBe('secret');
|
||||
});
|
||||
|
||||
it('rejects invalid encrypted data formats', async () => {
|
||||
process.env.CONFIG_ENCRYPTION_KEY = 'b'.repeat(32);
|
||||
vi.resetModules();
|
||||
|
||||
const { EncryptionService } = await import('@/lib/services/encryption.service');
|
||||
const service = new EncryptionService();
|
||||
|
||||
expect(() => service.decrypt('invalid')).toThrow(/Decryption failed/);
|
||||
});
|
||||
|
||||
it('generates a random key', async () => {
|
||||
const { EncryptionService } = await import('@/lib/services/encryption.service');
|
||||
const key = EncryptionService.generateKey();
|
||||
|
||||
expect(typeof key).toBe('string');
|
||||
expect(key.length).toBeGreaterThan(40);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user