Files
petal/internal/db/db_test.go
prosolis 8aa437ec82 Phase 12 + 13: collocation coach + vocabulary garden
Phase 12 — collocation coach: a third suggestion family for gentle
"natives usually say…" hints on non-native word pairings, reusing the
existing runPass/pendingScope/rail machinery.
- llm/collocation.go (RunCollocation, 25s floor, reuses ParseCheckpoint)
  + collocationSystemPrompt/CollocationMessages (warm, Mandarin gloss,
  defers grammar/spelling to the grammar family)
- migration 0005 rebuilds the suggestions table to extend the type CHECK
  (SQLite can't ALTER a CHECK)
- collocationScope + CollocationLimit + POST /{id}/collocation
- fix: grammarScope was `type != 'voice'` and would wipe the new
  collocation flags; now `type NOT IN ('voice','collocation')`
- frontend: --color-blossom, "Make it sound natural 🌸" pill,
  collocating/runCollocation in useCheckpoint, StatusBar dot

Phase 13 — vocabulary garden: capture looked-up words and surface them
for gentle spaced repetition.
- new internal/vocab package: migration 0006 (vocab_words, SM-2-lite
  columns, doc_id ON DELETE SET NULL, UNIQUE(user_id,word)),
  scheduler.go (Leitner ladder 1/3/7/16/35 then geometric; gentle
  "again", no streak-shaming), handlers (capture-upsert/list/due/
  review/delete, owner-scoped, SQLite-side datetime math)
- auto-capture on word lookup (dictionary-known words only, captures
  the surrounding sentence + doc_id) + 🤍/💚 toggle on WordCard
- GardenPanel: blossom grid (bloom by reps), flashcard review (sentence
  blanked, flip, again/good/easy, recognition↔production), sleepy-kitten
  footer; opened from a global 🌷 header button

Tests: TestCollocationPassCoexists, vocab scheduler + handlers, db CHECK
extended. go build/vet/test + tsc + vite + vitest (51/51) clean;
migration verified against a copy of the live DB; live backend smoke
walked the full vocab lifecycle + the warm-502 collocation path.

Claude-Session: https://claude.ai/code/session_016Yr6jELuRc7hyzYLccQKZd
2026-06-26 15:50:25 -07:00

86 lines
2.7 KiB
Go

package db
import (
"path/filepath"
"testing"
)
func TestOpenMigratesAndSeeds(t *testing.T) {
path := filepath.Join(t.TempDir(), "test.db")
d, err := Open(path)
if err != nil {
t.Fatalf("first open: %v", err)
}
// Local user is seeded.
var email string
if err := d.QueryRow(`SELECT email FROM users WHERE id = ?`, LocalUserID).Scan(&email); err != nil {
t.Fatalf("local user not seeded: %v", err)
}
// All expected tables exist.
for _, table := range []string{"users", "documents", "suggestions", "plagiarism_reports", "schema_migrations"} {
var name string
err := d.QueryRow(`SELECT name FROM sqlite_master WHERE type='table' AND name=?`, table).Scan(&name)
if err != nil {
t.Errorf("table %q missing: %v", table, err)
}
}
// The voice suggestion type is permitted by the CHECK constraint.
if _, err := d.Exec(`INSERT INTO documents (id, user_id) VALUES ('d1', ?)`, LocalUserID); err != nil {
t.Fatalf("insert document: %v", err)
}
if _, err := d.Exec(
`INSERT INTO suggestions (doc_id, from_pos, to_pos, original, replacement, explanation, type)
VALUES ('d1', 0, 4, 'teh', '', 'voice flag', 'voice')`,
); err != nil {
t.Fatalf("insert voice suggestion: %v", err)
}
// The collocation type (added by migration 0005's table rebuild) is permitted.
if _, err := d.Exec(
`INSERT INTO suggestions (doc_id, from_pos, to_pos, original, replacement, explanation, type)
VALUES ('d1', 0, 9, 'do a decision', 'make a decision', 'natives usually say…', 'collocation')`,
); err != nil {
t.Fatalf("insert collocation suggestion: %v", err)
}
// An invalid type is rejected.
if _, err := d.Exec(
`INSERT INTO suggestions (doc_id, from_pos, to_pos, original, replacement, explanation, type)
VALUES ('d1', 0, 4, 'teh', 'the', 'x', 'nonsense')`,
); err == nil {
t.Error("expected CHECK constraint to reject invalid suggestion type")
}
// Cascade delete removes child suggestions (foreign keys enabled).
if _, err := d.Exec(`DELETE FROM documents WHERE id = 'd1'`); err != nil {
t.Fatalf("delete document: %v", err)
}
var n int
if err := d.QueryRow(`SELECT COUNT(*) FROM suggestions WHERE doc_id = 'd1'`).Scan(&n); err != nil {
t.Fatal(err)
}
if n != 0 {
t.Errorf("expected cascade delete, got %d orphan suggestions", n)
}
d.Close()
// Re-opening is idempotent: migrations and seed don't double-apply or error.
d2, err := Open(path)
if err != nil {
t.Fatalf("second open: %v", err)
}
defer d2.Close()
var users int
if err := d2.QueryRow(`SELECT COUNT(*) FROM users WHERE id = ?`, LocalUserID).Scan(&users); err != nil {
t.Fatal(err)
}
if users != 1 {
t.Errorf("expected exactly 1 local user after reopen, got %d", users)
}
}