| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "io" |
| 7 | "log/slog" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/equipo/chat" |
| 13 | "amadan.net/rastrillo/rastrillo/db" |
| 14 | "amadan.net/rastrillo/rastrillo/migrate" |
| 15 | ) |
| 16 | |
| 17 | // slice4Schema is every migration before the runners migration: the |
| 18 | // database a slice 4 instance leaves behind. |
| 19 | func slice4Schema() *migrate.Set { |
| 20 | s := &migrate.Set{} |
| 21 | for _, m := range BootSchema.All() { |
| 22 | if strings.HasPrefix(m.ID, "equipo/0054_") { |
| 23 | break |
| 24 | } |
| 25 | s.Add(m) |
| 26 | } |
| 27 | return s |
| 28 | } |
| 29 | |
| 30 | // Migration 0054 over a slice 4 database binds every command and every |
| 31 | // step, in every state, to the person's primary at migration time (the |
| 32 | // primaryRunner query), marks every runner primary, and leaves no step |
| 33 | // without an owner. |
| 34 | func TestRunnersMigrationBindsSlice4Rows(t *testing.T) { |
| 35 | ctx := context.Background() |
| 36 | d, err := db.Open(filepath.Join(t.TempDir(), "slice4.db"), slog.New(slog.NewTextHandler(io.Discard, nil))) |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | defer d.Close() |
| 41 | w := d.Writer() |
| 42 | if _, err := migrate.Apply(ctx, d, slice4Schema()); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | exec := func(q string, args ...any) { |
| 46 | t.Helper() |
| 47 | if _, err := w.Exec(q, args...); err != nil { |
| 48 | t.Fatalf("%s: %v", q, err) |
| 49 | } |
| 50 | } |
| 51 | // The owner had a revoked runner, then an older one, then the newest |
| 52 | // (the slice 4 primary). A teammate (not an owner) has a runner; a |
| 53 | // third person never had one. |
| 54 | exec(`INSERT INTO members(address, role, revision, active, session_epoch) VALUES ('owner@example.com','owner',1,1,1), ('mate@example.com','member',1,1,1), ('new@example.com','owner',1,1,1)`) |
| 55 | exec(`INSERT INTO runners(id, person, machine_key_hash, label, revision, revoked_at, instance_id) VALUES |
| 56 | ('R0','owner@example.com','k0','old',2,'2026-11-01 00:00:00','i'), |
| 57 | ('R1','owner@example.com','k1','older',1,NULL,'i'), |
| 58 | ('R2','owner@example.com','k2','docs-t1',1,NULL,'i'), |
| 59 | ('M1','mate@example.com','k3','mate',1,NULL,'i')`) |
| 60 | exec(`INSERT INTO runner_commands(seq, person, kind, body, created_at, acked_at) VALUES |
| 61 | (1,'owner@example.com','message','{}','2026-11-07 10:00:00',NULL), |
| 62 | (2,'owner@example.com','dispatch','{}','2026-11-07 10:00:01',NULL), |
| 63 | (3,'owner@example.com','watch_merge','{}','2026-11-07 10:00:02','2026-11-07 10:00:03'), |
| 64 | (4,'mate@example.com','message','{}','2026-11-07 10:00:04',NULL)`) |
| 65 | step := `INSERT INTO chat_steps(id, task_id, idx, specialist, person, state, reason, attempt, terminated, revision, created_at, updated_at) VALUES (?,?,?,?,?,?,?,?,?,1,'2026-11-07 10:00:00','2026-11-07 10:00:00')` |
| 66 | exec(step, "s_running", "tk1", 1, "engineering", "owner@example.com", "running", "", 1, 0) |
| 67 | exec(step, "s_pending", "tk2", 2, "checks", "owner@example.com", "failed", "provider_failed", 1, 0) |
| 68 | exec(step, "s_queued", "tk3", 1, "engineering", "owner@example.com", "queued", "", 2, 0) |
| 69 | exec(step, "s_done", "tk4", 1, "review", "owner@example.com", "done", "", 1, 1) |
| 70 | exec(step, "s_mate", "tk5", 1, "engineering", "mate@example.com", "running", "", 1, 0) |
| 71 | exec(step, "s_nobody", "tk6", 1, "engineering", "new@example.com", "queued", "", 1, 0) |
| 72 | exec(`INSERT INTO chat_tasks(id, stage, revision) VALUES ('tk1','in_progress',1), ('tk3','in_progress',1), ('tk6','in_progress',1)`) |
| 73 | |
| 74 | if _, err := migrate.Apply(ctx, d, BootSchema); err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | str := func(q string, args ...any) string { |
| 78 | t.Helper() |
| 79 | var v sql.NullString |
| 80 | if err := w.QueryRow(q, args...).Scan(&v); err != nil { |
| 81 | t.Fatalf("%s: %v", q, err) |
| 82 | } |
| 83 | return v.String |
| 84 | } |
| 85 | for _, id := range []string{"R0", "R1", "R2", "M1"} { |
| 86 | if role := str("SELECT role FROM runners WHERE id=?", id); role != "primary" { |
| 87 | t.Fatalf("runner %s role %q", id, role) |
| 88 | } |
| 89 | } |
| 90 | for seq, want := range map[int]string{1: "R2", 2: "R2", 3: "R2", 4: "M1"} { |
| 91 | if got := str("SELECT runner_id FROM runner_commands WHERE seq=?", seq); got != want { |
| 92 | t.Fatalf("command %d bound to %q, want %q", seq, got, want) |
| 93 | } |
| 94 | } |
| 95 | for id, want := range map[string]string{"s_running": "R2", "s_pending": "R2", "s_queued": "R2", "s_done": "R2", "s_mate": "M1", "s_nobody": ""} { |
| 96 | if got := str("SELECT runner_id FROM chat_steps WHERE id=?", id); got != want { |
| 97 | t.Fatalf("step %s bound to %q, want %q", id, got, want) |
| 98 | } |
| 99 | } |
| 100 | if p := str("SELECT placement FROM chat_steps WHERE id='s_nobody'"); p != "waiting_runner" { |
| 101 | t.Fatalf("runnerless step placement %q", p) |
| 102 | } |
| 103 | if p := str("SELECT coalesce(placement,'') FROM chat_steps WHERE id='s_running'"); p != "" { |
| 104 | t.Fatalf("bound step placement %q", p) |
| 105 | } |
| 106 | if r := str("SELECT engineering_runner FROM chat_tasks WHERE id='tk1'"); r != "R2" { |
| 107 | t.Fatalf("engineering runner %q", r) |
| 108 | } |
| 109 | // Nothing the migration touched changed state. |
| 110 | if st := str("SELECT state || '/' || terminated FROM chat_steps WHERE id='s_pending'"); st != "failed/0" { |
| 111 | t.Fatalf("pending termination became %s", st) |
| 112 | } |
| 113 | n, err := chat.UnboundSteps(d.G) |
| 114 | if err != nil || n != 0 { |
| 115 | t.Fatalf("unbound steps after migration: %d %v", n, err) |
| 116 | } |
| 117 | // The check does catch a step with no owner. |
| 118 | exec(`UPDATE chat_steps SET runner_id = '' WHERE id = 's_running'`) |
| 119 | if n, err := chat.UnboundSteps(d.G); err != nil || n != 1 { |
| 120 | t.Fatalf("unbound step not caught: %d %v", n, err) |
| 121 | } |
| 122 | } |
| 123 | |