Files
ReadMeABook/tests/api/cache.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

54 lines
1.7 KiB
TypeScript

/**
* Component: Cache API Route Tests
* Documentation: documentation/testing.md
*/
import { beforeEach, describe, expect, it, vi } from 'vitest';
const fsMock = vi.hoisted(() => ({
access: vi.fn(),
readFile: vi.fn(),
}));
vi.mock('fs/promises', () => ({ default: fsMock, ...fsMock, constants: { R_OK: 4 } }));
describe('Thumbnail cache route', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('rejects invalid filenames', async () => {
const { GET } = await import('@/app/api/cache/thumbnails/[filename]/route');
const response = await GET({} as any, { params: Promise.resolve({ filename: '../bad' }) });
const payload = await response.json();
expect(response.status).toBe(400);
expect(payload.error).toBe('Invalid filename');
});
it('returns 404 when file is missing', async () => {
fsMock.access.mockRejectedValueOnce(new Error('missing'));
const { GET } = await import('@/app/api/cache/thumbnails/[filename]/route');
const response = await GET({} as any, { params: Promise.resolve({ filename: 'file.jpg' }) });
const payload = await response.json();
expect(response.status).toBe(404);
expect(payload.error).toBe('File not found');
});
it('serves cached image with content type', async () => {
fsMock.access.mockResolvedValueOnce(undefined);
fsMock.readFile.mockResolvedValueOnce(Buffer.from('data'));
const { GET } = await import('@/app/api/cache/thumbnails/[filename]/route');
const response = await GET({} as any, { params: Promise.resolve({ filename: 'file.png' }) });
expect(response.status).toBe(200);
expect(response.headers.get('content-type')).toBe('image/png');
});
});