Files
ReadMeABook/tests/api/admin-job-status.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

64 lines
1.8 KiB
TypeScript

/**
* Component: Admin Job Status API Route Tests
* Documentation: documentation/testing.md
*/
import { beforeEach, describe, expect, it, vi } from 'vitest';
const verifyAccessTokenMock = vi.hoisted(() => vi.fn());
const jobQueueMock = vi.hoisted(() => ({ getJob: vi.fn() }));
vi.mock('@/lib/utils/jwt', () => ({
verifyAccessToken: verifyAccessTokenMock,
}));
vi.mock('@/lib/services/job-queue.service', () => ({
getJobQueueService: () => jobQueueMock,
}));
const makeRequest = (token?: string) => ({
headers: {
get: (key: string) => (key.toLowerCase() === 'authorization' ? token : null),
},
});
describe('Admin job status route', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('rejects missing authorization', async () => {
const { GET } = await import('@/app/api/admin/job-status/[id]/route');
const response = await GET(makeRequest() as any, { params: Promise.resolve({ id: '1' }) });
const payload = await response.json();
expect(response.status).toBe(401);
expect(payload.error).toBe('Unauthorized');
});
it('returns job status for admin token', async () => {
verifyAccessTokenMock.mockReturnValue({ role: 'admin' });
jobQueueMock.getJob.mockResolvedValue({
id: '1',
type: 'search',
status: 'completed',
createdAt: new Date(),
startedAt: null,
completedAt: null,
result: null,
errorMessage: null,
attempts: 1,
maxAttempts: 3,
});
const { GET } = await import('@/app/api/admin/job-status/[id]/route');
const response = await GET(makeRequest('Bearer token') as any, { params: Promise.resolve({ id: '1' }) });
const payload = await response.json();
expect(payload.success).toBe(true);
expect(payload.job.status).toBe('completed');
});
});