Files
ReadMeABook/tests/api/system.routes.test.ts
T
kikootwo 94dbaf073b 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.
2026-01-28 11:41:59 -05:00

73 lines
1.9 KiB
TypeScript

/**
* Component: System API Route Tests
* Documentation: documentation/testing.md
*/
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { createPrismaMock } from '../helpers/prisma';
const prismaMock = createPrismaMock();
const schedulerMock = vi.hoisted(() => ({
start: vi.fn(),
}));
vi.mock('@/lib/db', () => ({
prisma: prismaMock,
}));
vi.mock('@/lib/services/scheduler.service', () => ({
getSchedulerService: () => schedulerMock,
}));
describe('System routes', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('returns healthy status when database is reachable', async () => {
prismaMock.$queryRaw.mockResolvedValueOnce(1);
const { GET } = await import('@/app/api/health/route');
const response = await GET();
const payload = await response.json();
expect(payload.status).toBe('healthy');
expect(payload.database).toBe('connected');
});
it('returns unhealthy status on database error', async () => {
prismaMock.$queryRaw.mockRejectedValueOnce(new Error('db down'));
const { GET } = await import('@/app/api/health/route');
const response = await GET();
const payload = await response.json();
expect(response.status).toBe(503);
expect(payload.status).toBe('unhealthy');
});
it('initializes scheduler on init endpoint', async () => {
const { GET } = await import('@/app/api/init/route');
const response = await GET({} as any);
const payload = await response.json();
expect(payload.success).toBe(true);
expect(schedulerMock.start).toHaveBeenCalled();
});
it('returns version info from environment', async () => {
process.env.APP_VERSION = 'abcdef123456';
process.env.BUILD_DATE = '2025-01-01';
const { GET } = await import('@/app/api/version/route');
const response = await GET();
const payload = await response.json();
expect(payload.shortCommit).toBe('abcdef1');
expect(payload.buildDate).toBe('2025-01-01');
});
});