oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package equipo
2
3import (
4 "context"
5 "database/sql"
6 "io"
7 "log/slog"
8 "path/filepath"
9 "strings"
10 "testing"
11
12 "amadan.net/rastrillo/rastrillo/db"
13 "amadan.net/rastrillo/rastrillo/migrate"
14)
15
16// beforeMacEngineering is every migration before slice F's 0120: the
17// database a protocol 35 instance leaves behind.
18func beforeMacEngineering(t *testing.T) *migrate.Set {
19 t.Helper()
20 s := &migrate.Set{}
21 found := false
22 for _, m := range BootSchema.All() {
23 if strings.HasPrefix(m.ID, "equipo/0120_") {
24 found = true
25 break
26 }
27 s.Add(m)
28 }
29 if !found {
30 t.Fatal("no equipo/0120_ migration")
31 }
32 return s
33}
34
35// throughMacEngineering is every migration up to and including 0120,
36// so what it adds is measured apart from later migrations.
37func throughMacEngineering(t *testing.T) *migrate.Set {
38 t.Helper()
39 s := &migrate.Set{}
40 for _, m := range BootSchema.All() {
41 s.Add(m)
42 if strings.HasPrefix(m.ID, "equipo/0120_") {
43 return s
44 }
45 }
46 t.Fatal("no equipo/0120_ migration")
47 return nil
48}
49
50// Migration 0120 over a database holding an open checks transfer (mid
51// relay, chunks received) and kept questions (answered, and one still
52// running) only adds columns: every row reads as it did, a transfer's
53// kind and old base are empty (a checks transfer), a step has no Xcode
54// access, outcome or kept session, and a question has no frozen
55// Engineering pin, so later code treats each as before slice F.
56func TestMacEngineeringMigrationKeepsOpenTransfersAndQuestions(t *testing.T) {
57 ctx := context.Background()
58 d, err := db.Open(filepath.Join(t.TempDir(), "protocol35.db"), slog.New(slog.NewTextHandler(io.Discard, nil)))
59 if err != nil {
60 t.Fatal(err)
61 }
62 defer d.Close()
63 w := d.Writer()
64 if _, err := migrate.Apply(ctx, d, beforeMacEngineering(t)); err != nil {
65 t.Fatal(err)
66 }
67 exec := func(q string, args ...any) {
68 t.Helper()
69 if _, err := w.Exec(q, args...); err != nil {
70 t.Fatalf("%s: %v", q, err)
71 }
72 }
73 a, b := strings.Repeat("a", 40), strings.Repeat("b", 40)
74 exec(`INSERT INTO runner_transfers(id, person, from_runner, to_runner, repo, task_id, step_id, attempt, base_sha, candidate_sha, direction, state, sha256, total, received, bytes, expires_at, created_at) VALUES
75 ('tr_open','owner@example.com','R1','R2','amadan.net/oficina/sample','tk1','s_checks',1,?,?,'inbound','open',?,3,1,196608,'2026-11-29 10:00:00','2026-11-28 10:00:00'),
76 ('tr_out','owner@example.com','R2','R1','amadan.net/oficina/sample','tk2','s_eng',2,?,?,'outbound','complete',?,1,1,1024,'2026-11-29 10:00:00','2026-11-28 10:00:00')`,
77 a, b, strings.Repeat("c", 64), a, b, strings.Repeat("d", 64))
78 step := `INSERT INTO chat_steps(id, task_id, idx, specialist, person, state, reason, attempt, base_sha, candidate_sha, askable, runner_id, revision, created_at, updated_at) VALUES (?,?,?,?,?,?,'',?,?,?,?,?,1,'2026-11-28 10:00:00','2026-11-28 10:00:00')`
79 exec(step, "s_checks", "tk1", 2, "checks", "owner@example.com", "running", 1, a, b, 0, "R2")
80 exec(step, "s_eng", "tk2", 1, "engineering", "owner@example.com", "done", 2, a, b, 1, "R1")
81 question := `INSERT INTO chat_questions(id, task_id, asker, idempotency_key, step_id, specialist, chosen, text, state, reason, attempt, dispatched, answer, timing, revision, created_at, updated_at) VALUES (?, 'tk2', 'owner@example.com', ?, 's_eng', 'engineering', '', ?, ?, '', 1, 1, ?, '', 1, '2026-11-28 10:00:00', '2026-11-28 10:00:00')`
82 exec(question, "q_answered", "k1", "What did you change?", "answered", "The edge swipe.")
83 exec(question, "q_running", "k2", "And why?", "running", "")
84
85 if _, err := migrate.Apply(ctx, d, throughMacEngineering(t)); err != nil {
86 t.Fatal(err)
87 }
88 str := func(q string, args ...any) string {
89 t.Helper()
90 var v sql.NullString
91 if err := w.QueryRow(q, args...).Scan(&v); err != nil {
92 t.Fatalf("%s: %v", q, err)
93 }
94 return v.String
95 }
96 for _, id := range []string{"tr_open", "tr_out"} {
97 if got := str(`SELECT coalesce(kind,'') || '|' || coalesce(old_base_sha,'') FROM runner_transfers WHERE id=?`, id); got != "|" {
98 t.Fatalf("transfer %s kind|old base %q", id, got)
99 }
100 }
101 if got := str(`SELECT state || '/' || received || '/' || total || '/' || base_sha || '/' || candidate_sha FROM runner_transfers WHERE id='tr_open'`); got != "open/1/3/"+a+"/"+b {
102 t.Fatalf("open transfer changed: %s", got)
103 }
104 for _, id := range []string{"s_checks", "s_eng"} {
105 if got := str(`SELECT coalesce(xcode_access,'') || '|' || coalesce(outcome_phase,'') || '|' || coalesce(session_id,'') FROM chat_steps WHERE id=?`, id); got != "||" {
106 t.Fatalf("step %s new columns %q", id, got)
107 }
108 }
109 if got := str(`SELECT state || '/' || askable || '/' || runner_id FROM chat_steps WHERE id='s_eng'`); got != "done/1/R1" {
110 t.Fatalf("kept step changed: %s", got)
111 }
112 for _, id := range []string{"q_answered", "q_running"} {
113 if got := str(`SELECT coalesce(eng_attempt,0) || '|' || coalesce(eng_account,'') || '|' || coalesce(eng_session,'') || '|' || coalesce(runner_id,'') || '|' || coalesce(base_sha,'') || '|' || coalesce(candidate_sha,'') FROM chat_questions WHERE id=?`, id); got != "0|||||" {
114 t.Fatalf("question %s new columns %q", id, got)
115 }
116 }
117 if got := str(`SELECT state || '/' || answer || '/' || attempt FROM chat_questions WHERE id='q_answered'`); got != "answered/The edge swipe./1" {
118 t.Fatalf("kept question changed: %s", got)
119 }
120 // Additive only: every column the protocol 35 tables had is still there.
121 before, err := migrate.Replay(ctx, beforeMacEngineering(t).All())
122 if err != nil {
123 t.Fatal(err)
124 }
125 defer before.Close()
126 columns := func(q interface {
127 QueryContext(context.Context, string, ...any) (*sql.Rows, error)
128 }, table string) map[string]bool {
129 rows, err := q.QueryContext(ctx, "SELECT name FROM pragma_table_info(?)", table)
130 if err != nil {
131 t.Fatal(err)
132 }
133 defer rows.Close()
134 out := map[string]bool{}
135 for rows.Next() {
136 var n string
137 if err := rows.Scan(&n); err != nil {
138 t.Fatal(err)
139 }
140 out[n] = true
141 }
142 return out
143 }
144 added := map[string][]string{
145 "runner_transfers": {"kind", "old_base_sha"},
146 "chat_steps": {"xcode_access", "outcome_phase", "session_id", "publication_runner", "on_worker"},
147 "chat_questions": {"eng_attempt", "eng_account", "eng_session", "runner_id", "base_sha", "candidate_sha"},
148 "chat_tasks": {"mac_needs"},
149 }
150 for table, cols := range added {
151 was, now := columns(before, table), columns(w, table)
152 for c := range was {
153 if !now[c] {
154 t.Errorf("%s lost %s", table, c)
155 }
156 }
157 for _, c := range cols {
158 if was[c] || !now[c] {
159 t.Errorf("%s.%s: before %v, after %v", table, c, was[c], now[c])
160 }
161 }
162 if len(now) != len(was)+len(cols) {
163 t.Errorf("%s gained %d columns, want %d", table, len(now)-len(was), len(cols))
164 }
165 }
166}
167