| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "log/slog" |
| 6 | "path/filepath" |
| 7 | "reflect" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/equipo" |
| 11 | "amadan.net/rastrillo/rastrillo/db" |
| 12 | "amadan.net/rastrillo/rastrillo/migrate" |
| 13 | ) |
| 14 | |
| 15 | func TestConversationMigrationUpgrade(t *testing.T) { |
| 16 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 17 | d, e := db.Open(filepath.Join(t.TempDir(), "upgrade.db"), logger) |
| 18 | must(t, e) |
| 19 | t.Cleanup(func() { must(t, d.Close()) }) |
| 20 | old := &migrate.Set{} |
| 21 | for _, m := range equipo.BootSchema.All() { |
| 22 | if m.ID != "equipo/0019_conversation" && m.ID != "equipo/0021_coordinator_sources" && m.ID != "equipo/0031_notifications" && m.ID != "equipo/0033_plan_notifications" { |
| 23 | old.Add(m) |
| 24 | } |
| 25 | } |
| 26 | _, e = migrate.Apply(ctx, d, old) |
| 27 | must(t, e) |
| 28 | f := &fixture{DB: d, Store: equipo.NewStore(d)} |
| 29 | must(t, f.Store.Bootstrap(ctx, []string{owner.Address})) |
| 30 | must(t, d.Writer().QueryRow("SELECT id FROM teams").Scan(&f.TeamID)) |
| 31 | task := f.task(t, owner, "old-task") |
| 32 | f.exec(t, "INSERT INTO sessions(token_hash,subject,created_at,expires_at) VALUES('preserved',?,'2026-11-01','2026-12-01')", owner.Address) |
| 33 | before := f.count(t, "SELECT count(*) FROM audit_events") |
| 34 | result, e := migrate.Apply(ctx, d, equipo.BootSchema) |
| 35 | must(t, e) |
| 36 | if !reflect.DeepEqual(result.Applied, []string{"equipo/0019_conversation", "equipo/0021_coordinator_sources", "equipo/0031_notifications", "equipo/0033_plan_notifications"}) { |
| 37 | t.Fatal("wrong upgrade", result.Applied) |
| 38 | } |
| 39 | var saved equipo.Task |
| 40 | must(t, d.G.First(&saved, "id=?", task.ID).Error) |
| 41 | if saved != task || f.count(t, "SELECT count(*) FROM sessions WHERE token_hash='preserved'") != 1 || f.count(t, "SELECT count(*) FROM audit_events") != before { |
| 42 | t.Fatal("upgrade changed historical records") |
| 43 | } |
| 44 | for range 2 { |
| 45 | _, e = f.Store.TaskThread(ctx, owner, task.ID, "", 50) |
| 46 | must(t, e) |
| 47 | } |
| 48 | if f.count(t, "SELECT count(*) FROM conversation_threads WHERE task_id=?", task.ID) != 1 { |
| 49 | t.Fatal("old task thread not unique") |
| 50 | } |
| 51 | result, e = migrate.Apply(ctx, d, equipo.BootSchema) |
| 52 | must(t, e) |
| 53 | if len(result.Applied) != 0 { |
| 54 | t.Fatal("migration reapplied") |
| 55 | } |
| 56 | fresh := newFixture(t) |
| 57 | upgraded, e := migrate.Read(ctx, d.Writer()) |
| 58 | must(t, e) |
| 59 | baseline, e := migrate.Read(ctx, fresh.DB.Writer()) |
| 60 | must(t, e) |
| 61 | if !reflect.DeepEqual(upgraded, baseline) { |
| 62 | t.Fatal("fresh and upgraded schemas differ") |
| 63 | } |
| 64 | } |
| 65 | |