cards/packages/cards-domain/tests/schemas.test.ts
Till 45a47e0ffd Phase 3: Domain-Modell + Decks/Cards/Reviews-CRUD
Domain (@cards/domain):
- zod-Schemas SSOT für Deck, Card, Review, StudySession, FsrsSettings,
  Tools (cards.create + cards.search Input/Output)
- CardType-Discriminated-Union: MVP basic+basic-reverse, Future-Set
  (cloze, type-in, image-occlusion, audio, multiple-choice) für
  Schema-stable-Migration vorbereitet
- validateFieldsForType() Pure-Function pro CardType
- FSRS-Adapter über ts-fsrs v5.3.2: newReview, gradeReview,
  subIndexCount, toFsrsCard/fromFsrsCard ISO↔Date-Roundtrip
- Encryption-Hinweis: reviews bleiben PLAINTEXT (Scheduler quert
  täglich `due <= now`, siehe Lessons §3)

Drizzle-Schemas (apps/api/src/db/schema, alles in pgSchema('cards')):
- decks, cards, card_tags, reviews (PK card_id+sub_index), study_sessions,
  tags (deck-skopiert), media_refs (verweist auf mana-media), import_jobs
- _schema.ts-Pattern um Zirkular-Imports zu vermeiden (Lesson aus
  mana-share/-events während F-0)
- Hot-Path-Index reviews_user_due_idx für Scheduler-Queries

Routes (apps/api/src/routes):
- POST/GET/PATCH/DELETE /api/v1/decks (Deck-CRUD)
- POST/GET/PATCH/DELETE /api/v1/cards (Card-CRUD mit Auto-Reviews-Init:
  beim Card-Insert werden N Reviews via subIndexCount(type) angelegt,
  in einer Transaktion)
- GET /api/v1/reviews/due (Hot-Path, optional deck_id-Filter, Limit 500)
- POST /api/v1/reviews/:cardId/:subIndex/grade (FSRS-State-Transition,
  per-Deck FSRS-Settings)

Auth: Stub-Middleware liest X-User-Id-Header (Phase 2 ersetzt durch
@mana/shared-hono authMiddleware mit JWKS-Cache).

Tests (vitest, Hono app.request()):
- @cards/domain: fsrs.test.ts (newReview, gradeReview Roundtrip,
  Rating-Mapping), schemas.test.ts (zod-strict-Variants, Field-Type-
  Validation, hex-Color)
- apps/api: decks.test.ts + cards.test.ts + reviews.test.ts —
  Auth-Gate + Input-Validation. Volle DB-Integrationstests folgen mit
  pg-mem oder testcontainers in späterer Phase.

Cleanup: types.ts entfernt, zod-Schemas sind SSOT (z.infer für Types).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-08 14:21:54 +02:00

154 lines
3.7 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import {
CardCreateSchema,
CardSchema,
CardTypeSchema,
DeckCreateSchema,
DeckSchema,
GradeReviewInputSchema,
validateFieldsForType,
} from '../src/schemas/index.ts';
describe('CardTypeSchema', () => {
it('accepts MVP types', () => {
expect(() => CardTypeSchema.parse('basic')).not.toThrow();
expect(() => CardTypeSchema.parse('basic-reverse')).not.toThrow();
});
it('rejects future types in MVP schema', () => {
expect(() => CardTypeSchema.parse('cloze')).toThrow();
});
});
describe('validateFieldsForType', () => {
it('basic requires front + back', () => {
expect(validateFieldsForType('basic', { front: 'q', back: 'a' })).toEqual({ ok: true });
expect(validateFieldsForType('basic', { front: 'q' })).toEqual({
ok: false,
missing: ['back'],
});
});
it('cloze requires text', () => {
expect(validateFieldsForType('cloze', { text: 'x' })).toEqual({ ok: true });
expect(validateFieldsForType('cloze', {})).toEqual({ ok: false, missing: ['text'] });
});
});
describe('CardCreateSchema', () => {
it('accepts a basic card', () => {
const r = CardCreateSchema.safeParse({
deck_id: 'd-1',
type: 'basic',
fields: { front: 'Q', back: 'A' },
});
expect(r.success).toBe(true);
});
it('rejects basic card without back', () => {
const r = CardCreateSchema.safeParse({
deck_id: 'd-1',
type: 'basic',
fields: { front: 'Q' },
});
expect(r.success).toBe(false);
});
it('rejects unknown type via CardTypeSchema', () => {
const r = CardCreateSchema.safeParse({
deck_id: 'd-1',
type: 'cloze',
fields: { text: 'x' },
});
expect(r.success).toBe(false);
});
it('rejects extra fields (strict)', () => {
const r = CardCreateSchema.safeParse({
deck_id: 'd-1',
type: 'basic',
fields: { front: 'Q', back: 'A' },
malicious: 'inject',
});
expect(r.success).toBe(false);
});
});
describe('DeckCreateSchema', () => {
it('minimal valid deck', () => {
const r = DeckCreateSchema.safeParse({ name: 'My Deck' });
expect(r.success).toBe(true);
});
it('rejects empty name', () => {
const r = DeckCreateSchema.safeParse({ name: '' });
expect(r.success).toBe(false);
});
it('rejects invalid color', () => {
const r = DeckCreateSchema.safeParse({ name: 'X', color: 'red' });
expect(r.success).toBe(false);
});
it('accepts hex color', () => {
const r = DeckCreateSchema.safeParse({ name: 'X', color: '#ff8800' });
expect(r.success).toBe(true);
});
});
describe('GradeReviewInputSchema', () => {
it('accepts a grade input', () => {
const r = GradeReviewInputSchema.safeParse({
card_id: 'c-1',
sub_index: 0,
rating: 'good',
});
expect(r.success).toBe(true);
});
it('rejects unknown rating', () => {
const r = GradeReviewInputSchema.safeParse({
card_id: 'c-1',
sub_index: 0,
rating: 'perfect',
});
expect(r.success).toBe(false);
});
it('defaults sub_index to 0', () => {
const r = GradeReviewInputSchema.parse({ card_id: 'c-1', rating: 'good' });
expect(r.sub_index).toBe(0);
});
});
describe('strict variants reject extras', () => {
it('DeckSchema rejects extra props', () => {
const r = DeckSchema.safeParse({
id: 'd-1',
user_id: 'u-1',
name: 'D',
visibility: 'private',
fsrs_settings: {},
created_at: '2026-05-08T10:00:00.000Z',
updated_at: '2026-05-08T10:00:00.000Z',
leaks: 'no',
});
expect(r.success).toBe(false);
});
it('CardSchema rejects extra props', () => {
const r = CardSchema.safeParse({
id: 'c-1',
deck_id: 'd-1',
user_id: 'u-1',
type: 'basic',
fields: { front: 'Q', back: 'A' },
media_refs: [],
created_at: '2026-05-08T10:00:00.000Z',
updated_at: '2026-05-08T10:00:00.000Z',
leaked: 'yes',
});
expect(r.success).toBe(false);
});
});