package plan3test import ( "amadan.net/oficina/equipo/internal/equipo" "amadan.net/rastrillo/rastrillo/db" "amadan.net/rastrillo/rastrillo/migrate" "io" "log/slog" "path/filepath" "testing" ) func TestNotificationMigrationUpgrade(t *testing.T) { path := filepath.Join(t.TempDir(), "notifications.db") logger := slog.New(slog.NewTextHandler(io.Discard, nil)) d, err := db.Open(path, logger) must(t, err) old := &migrate.Set{} for _, m := range equipo.BootSchema.All() { if m.ID != "equipo/0031_notifications" && m.ID != "equipo/0033_plan_notifications" { old.Add(m) } } _, err = migrate.Apply(ctx, d, old) must(t, err) s := equipo.NewStore(d) must(t, s.Bootstrap(ctx, []string{owner.Address})) msg, err := s.SendMessage(ctx, owner, "", "historical", equipo.Mutation{RequestID: "old"}) must(t, err) var team string must(t, d.Writer().QueryRow("SELECT id FROM teams").Scan(&team)) _, err = d.Writer().Exec("INSERT INTO tasks(id,requester,request_id,team_id,revision,state,plan_revision,wait_code) VALUES('old-task',?,'old-task',?,1,'proposed',1,'plan.awaiting_approval')", owner.Address, team) must(t, err) applied, err := migrate.Apply(ctx, d, equipo.BootSchema) must(t, err) if len(applied.Applied) != 2 || applied.Applied[0] != "equipo/0031_notifications" { t.Fatal(applied) } var n int must(t, d.Writer().QueryRow("SELECT count(*) FROM notification_outbox WHERE resource_id='old-task'").Scan(&n)) if n != 1 { t.Fatal("upgrade lost existing actionable task", n) } fresh := newFixture(t) upgraded, err := migrate.Read(ctx, d.Writer()) must(t, err) freshSchema, err := migrate.Read(ctx, fresh.DB.Writer()) must(t, err) if !upgraded.Equal(freshSchema) { t.Fatal(upgraded.Diff(freshSchema)) } must(t, d.Close()) d, err = db.Open(path, logger) must(t, err) t.Cleanup(func() { must(t, d.Close()) }) applied, err = migrate.Apply(ctx, d, equipo.BootSchema) must(t, err) if len(applied.Applied) != 0 { t.Fatal("notification migration reapplied") } page, err := equipo.NewStore(d).Conversation(ctx, owner, "", 50) must(t, err) if len(page.Messages) != 2 || page.Messages[0].ID != msg.ID { t.Fatal("upgrade lost historical conversation") } }