Two halves of the same idea, both read out of work Petal already records. Planting: an accepted collocation is a learnable chunk, so it becomes a phrase card. The scheduler didn't need to know — a three-word chunk climbs the ladder exactly like a looked-up word. What needed care was deciding what *isn't* a chunk (single words are word choice; a six-word-plus "collocation" is a rewritten sentence, and sentences make miserable flashcards), and that the example must be the *corrected* sentence — the stored draft still holds the phrasing she just left behind. Re-accepting the same chunk leaves the existing card alone rather than resetting a schedule it has been climbing. The whole thing is best-effort: accepting an edit must never fail because a flashcard couldn't be made. The growth journal: kept this month beside kept the month before, the phrasing that stuck, the patterns that faded. The queries were the easy part; the honesty is the feature. "Stuck" needs the phrase in a *second* document, because one document is just the edit where she left it. "Faded" says nothing at all unless she has been writing lately — otherwise a month away from Petal comes back to her as progress, which is the one way this could lie. And a suggestion had to start recording when she *decided* it, not when the model proposed it, so 0012 adds resolved_at and backfills the old rows to their created_at. It lives as a second tab in the garden, and it feeds the kitten: after an accept she now sometimes hears something true of her alone, once per line, half the time, never waited for. Claude-Session: https://claude.ai/code/session_016y6gyuHkQXPiEuW8RGQyua
153 lines
5.2 KiB
Go
153 lines
5.2 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)
|
|
}
|
|
}
|
|
|
|
// TestResolvedAtBackfill runs migration 0012 against a database that predates
|
|
// it, which is the only shape that matters: on the live box the suggestions
|
|
// table is years of settled edits with no resolved_at to their name. Backfilling
|
|
// to created_at is exactly the approximation the growth journal would otherwise
|
|
// have had to make, and a pending row must stay NULL — nothing has been decided.
|
|
func TestResolvedAtBackfill(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "old.db")
|
|
d, err := Open(path)
|
|
if err != nil {
|
|
t.Fatalf("open: %v", err)
|
|
}
|
|
|
|
// Rewind to the state before 0012: drop the column and forget the migration.
|
|
if _, err := d.Exec(`DROP INDEX idx_suggestions_resolved`); err != nil {
|
|
t.Fatalf("rewind index: %v", err)
|
|
}
|
|
if _, err := d.Exec(`ALTER TABLE suggestions DROP COLUMN resolved_at`); err != nil {
|
|
t.Fatalf("rewind schema: %v", err)
|
|
}
|
|
if _, err := d.Exec(`DELETE FROM schema_migrations WHERE name = '0012_suggestion_resolved_at'`); err != nil {
|
|
t.Fatalf("rewind migration record: %v", err)
|
|
}
|
|
if _, err := d.Exec(`INSERT INTO documents (id, user_id) VALUES ('d1', ?)`, LocalUserID); err != nil {
|
|
t.Fatalf("insert document: %v", err)
|
|
}
|
|
for _, s := range []struct{ id, status string }{
|
|
{"s-old", "accepted"},
|
|
{"s-open", "pending"},
|
|
} {
|
|
if _, err := d.Exec(
|
|
`INSERT INTO suggestions (id, doc_id, from_pos, to_pos, original, replacement, explanation, type, status, created_at)
|
|
VALUES (?, 'd1', 0, 3, 'teh', 'the', 'x', 'grammar', ?, '2026-01-02 03:04:05')`,
|
|
s.id, s.status,
|
|
); err != nil {
|
|
t.Fatalf("seed %s: %v", s.id, err)
|
|
}
|
|
}
|
|
d.Close()
|
|
|
|
d2, err := Open(path)
|
|
if err != nil {
|
|
t.Fatalf("reopen (migrate): %v", err)
|
|
}
|
|
defer d2.Close()
|
|
|
|
// Compared against created_at read back the same way: the driver renders a
|
|
// DATETIME column itself, so the assertion is "the same instant", not a
|
|
// particular text format.
|
|
var settled, created *string
|
|
if err := d2.QueryRow(
|
|
`SELECT resolved_at, created_at FROM suggestions WHERE id = 's-old'`,
|
|
).Scan(&settled, &created); err != nil {
|
|
t.Fatalf("read settled row: %v", err)
|
|
}
|
|
if settled == nil || created == nil || *settled != *created {
|
|
t.Errorf("resolved_at = %v, want it backfilled from created_at (%v)", settled, created)
|
|
}
|
|
|
|
var pending *string
|
|
if err := d2.QueryRow(`SELECT resolved_at FROM suggestions WHERE id = 's-open'`).Scan(&pending); err != nil {
|
|
t.Fatalf("read pending row: %v", err)
|
|
}
|
|
if pending != nil {
|
|
t.Errorf("pending row got resolved_at = %v, want NULL — nothing was decided", *pending)
|
|
}
|
|
}
|