package plan3test import ( "io" "log/slog" "path/filepath" "testing" "amadan.net/oficina/equipo/internal/equipo" "amadan.net/rastrillo/rastrillo/db" "amadan.net/rastrillo/rastrillo/migrate" ) func TestPersonAPIMigrationUpgrade(t *testing.T) { path := filepath.Join(t.TempDir(), "person-api.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/0023_person_api_requests" { 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 message", equipo.Mutation{RequestID: "historical"}) must(t, err) result, err := migrate.Apply(ctx, d, equipo.BootSchema) must(t, err) if len(result.Applied) != 1 || result.Applied[0] != "equipo/0023_person_api_requests" { t.Fatal(result.Applied) } f := newFixture(t) upgraded, err := migrate.Read(ctx, d.Writer()) must(t, err) fresh, err := migrate.Read(ctx, f.DB.Writer()) must(t, err) if !upgraded.Equal(fresh) { t.Fatal(upgraded.Diff(fresh)) } must(t, d.Close()) d, err = db.Open(path, logger) must(t, err) t.Cleanup(func() { must(t, d.Close()) }) result, err = migrate.Apply(ctx, d, equipo.BootSchema) must(t, err) if len(result.Applied) != 0 { t.Fatal("reapplied", result.Applied) } 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("historical message lost") } } func TestPersonAPIRequestMetadataConstraintsAndExpiry(t *testing.T) { f := newFixture(t) if f.count(t, "SELECT count(*) FROM pragma_table_info('person_api_requests') WHERE \"notnull\"=0") != 0 { t.Fatal("nullable required metadata") } if f.count(t, "SELECT count(*) FROM pragma_index_info('person_api_requests_expiry') WHERE name='expires_at'") != 1 { t.Fatal("missing expiry index") } expectHTTP(t, apiRequest(t, f, owner, "POST", "/api/v1/conversation/messages", `{"body":"old"}`, "old", ""), 200) f.exec(t, "UPDATE person_api_requests SET expires_at='2000-01-01T00:00:00Z' WHERE request_id='old'") expectHTTP(t, apiRequest(t, f, owner, "POST", "/api/v1/conversation/messages", `{"body":"new"}`, "new", ""), 200) if f.count(t, "SELECT count(*) FROM person_api_requests WHERE request_id='old'") != 0 { t.Fatal("expired replay metadata retained") } } func TestPersonAPIDeviceMigrationUpgrade(t *testing.T) { path := filepath.Join(t.TempDir(), "person-devices.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/0098_native_device" && m.ID != "equipo_identity/0002_native_lifecycle" && m.ID != "equipo/0030_person_devices" && m.ID != "equipo/0030_person_devices_usage" && 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 message", equipo.Mutation{RequestID: "historical"}) must(t, err) result, err := migrate.Apply(ctx, d, equipo.BootSchema) must(t, err) if len(result.Applied) != 6 || result.Applied[0] != "equipo/0030_person_devices" || result.Applied[1] != "equipo/0030_person_devices_usage" || result.Applied[2] != "equipo/0031_notifications" { t.Fatal(result.Applied) } f := newFixture(t) upgraded, err := migrate.Read(ctx, d.Writer()) must(t, err) fresh, err := migrate.Read(ctx, f.DB.Writer()) must(t, err) if !upgraded.Equal(fresh) { t.Fatal(upgraded.Diff(fresh)) } must(t, d.Close()) d, err = db.Open(path, logger) must(t, err) t.Cleanup(func() { must(t, d.Close()) }) result, err = migrate.Apply(ctx, d, equipo.BootSchema) must(t, err) if len(result.Applied) != 0 { t.Fatal("reapplied", result.Applied) } 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("historical message lost") } } func TestPersonAPIDeviceUsageMigration(t *testing.T) { path := filepath.Join(t.TempDir(), "device-usage.db") d, err := db.Open(path, slog.New(slog.NewTextHandler(io.Discard, nil))) must(t, err) t.Cleanup(func() { must(t, d.Close()) }) old := &migrate.Set{} for _, m := range equipo.BootSchema.All() { if m.ID != "equipo/0030_person_devices_usage" { old.Add(m) } } _, err = migrate.Apply(ctx, d, old) must(t, err) _, err = d.Writer().Exec(`INSERT INTO person_devices(id,request_hash,person,token_hash,revision) VALUES('history','request','person','hash',3)`) must(t, err) result, err := migrate.Apply(ctx, d, equipo.BootSchema) must(t, err) if len(result.Applied) != 1 || result.Applied[0] != "equipo/0030_person_devices_usage" { t.Fatal(result.Applied) } var count int must(t, d.Writer().QueryRow(`SELECT count(*) FROM person_devices WHERE id='history' AND token_hash='hash' AND revision=3 AND last_used_at IS NULL`).Scan(&count)) if count != 1 { t.Fatal("usage migration lost device history") } }