import { describe, it, expect } from 'vitest'; import { Hono } from 'hono'; import { reviewsRouter } from '../src/routes/reviews.ts'; import type { CardsDb } from '../src/db/connection.ts'; function buildApp() { const stub = { select: () => ({ from: () => ({ innerJoin: () => ({ innerJoin: () => ({ where: () => ({ limit: () => [] }), }), where: () => ({ orderBy: () => ({ limit: () => [] }), }), }), where: () => ({ orderBy: () => ({ limit: () => [] }), }), }), }), }; const app = new Hono(); app.route('/api/v1/reviews', reviewsRouter({ db: stub as unknown as CardsDb })); return { app }; } describe('reviewsRouter — auth-gate', () => { it('GET /due ohne X-User-Id ist 401', async () => { const { app } = buildApp(); const res = await app.request('/api/v1/reviews/due'); expect(res.status).toBe(401); }); it('POST /grade ohne X-User-Id ist 401', async () => { const { app } = buildApp(); const res = await app.request('/api/v1/reviews/c-1/0/grade', { method: 'POST', body: JSON.stringify({ rating: 'good' }), }); expect(res.status).toBe(401); }); }); describe('reviewsRouter — Input-Validation', () => { it('POST mit invalid sub_index ist 422', async () => { const { app } = buildApp(); const res = await app.request('/api/v1/reviews/c-1/-1/grade', { method: 'POST', headers: { 'X-User-Id': 'u-1', 'Content-Type': 'application/json' }, body: JSON.stringify({ rating: 'good' }), }); expect(res.status).toBe(422); }); it('POST ohne rating ist 422', async () => { const { app } = buildApp(); const res = await app.request('/api/v1/reviews/c-1/0/grade', { method: 'POST', headers: { 'X-User-Id': 'u-1', 'Content-Type': 'application/json' }, body: '{}', }); expect(res.status).toBe(422); }); it('POST mit unknown rating ist 422', async () => { const { app } = buildApp(); const res = await app.request('/api/v1/reviews/c-1/0/grade', { method: 'POST', headers: { 'X-User-Id': 'u-1', 'Content-Type': 'application/json' }, body: JSON.stringify({ rating: 'maybe' }), }); expect(res.status).toBe(422); }); it('POST mit gültigem rating erreicht Lookup (404 bei stub)', async () => { const { app } = buildApp(); const res = await app.request('/api/v1/reviews/c-1/0/grade', { method: 'POST', headers: { 'X-User-Id': 'u-1', 'Content-Type': 'application/json' }, body: JSON.stringify({ rating: 'good' }), }); expect(res.status).toBe(404); const body = (await res.json()) as { error: string }; expect(body.error).toBe('not_found'); }); });