| 1 | //go:build testbuild |
| 2 | |
| 3 | package headless |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "database/sql" |
| 8 | "encoding/json" |
| 9 | "fmt" |
| 10 | "os" |
| 11 | "os/exec" |
| 12 | "path/filepath" |
| 13 | "slices" |
| 14 | "strings" |
| 15 | "sync" |
| 16 | "testing" |
| 17 | "time" |
| 18 | |
| 19 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 20 | "amadan.net/oficina/equipo/internal/thintest" |
| 21 | "amadan.net/oficina/equipo/internal/wire" |
| 22 | ) |
| 23 | |
| 24 | // branchPoint is main where Mac runners slice F branched: protocol 35, |
| 25 | // Mac work waiting for a Mac, no Engineering on one. M9g runs its |
| 26 | // runner, fake Mac worker (the test build) and instance against this |
| 27 | // branch's. |
| 28 | const branchPoint = "8bd40ed15aae17b594ce2f0fe07e02978b80222f" |
| 29 | |
| 30 | // oldMacInstanceTest is added to the branch point's tree when its test |
| 31 | // binary is built: an instance of that release, in its own process, with |
| 32 | // a primary and a worker enrolled, until stdin closes. |
| 33 | const oldMacInstanceTest = `package headless |
| 34 | |
| 35 | import ( |
| 36 | "encoding/json" |
| 37 | "io" |
| 38 | "os" |
| 39 | "testing" |
| 40 | |
| 41 | "amadan.net/oficina/equipo/internal/thintest" |
| 42 | ) |
| 43 | |
| 44 | func TestM9OldInstance(t *testing.T) { |
| 45 | out := os.Getenv("EQUIPO_M9_OUT") |
| 46 | if out == "" { |
| 47 | t.Skip("M9g only") |
| 48 | } |
| 49 | // EQUIPO_M9_DB: the instance's database lives there, outside this |
| 50 | // test's own directory, so a later process (or a newer release) can |
| 51 | // open it; with EQUIPO_M9_EXISTING=1 it is opened as it is. |
| 52 | path, existing := os.Getenv("EQUIPO_M9_DB"), os.Getenv("EQUIPO_M9_EXISTING") == "1" |
| 53 | inst := thintest.StartInstanceWith(t, func(i *thintest.Instance) { |
| 54 | if path == "" { |
| 55 | return |
| 56 | } |
| 57 | if !existing { |
| 58 | raw, err := os.ReadFile(i.Path) |
| 59 | if err != nil || os.WriteFile(path, raw, 0600) != nil { |
| 60 | t.Fatal("the database could not be placed") |
| 61 | } |
| 62 | } |
| 63 | i.Path = path |
| 64 | }) |
| 65 | token := inst.DeviceToken(t) |
| 66 | out2 := map[string]any{"url": inst.URL, "team": inst.TeamID, "token": token, "db": inst.Path} |
| 67 | if !existing { |
| 68 | out2["primary"], out2["worker"] = inst.EnrolRunner(t), inst.EnrolRunner(t) |
| 69 | } |
| 70 | raw, _ := json.Marshal(out2) |
| 71 | if err := os.WriteFile(out+".tmp", raw, 0600); err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | if err := os.Rename(out+".tmp", out); err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | _, _ = io.Copy(io.Discard, os.Stdin) |
| 78 | } |
| 79 | ` |
| 80 | |
| 81 | // branchBinary is the branch point's headless test binary, with the |
| 82 | // test build (its fake Mac worker) and the old instance above, built |
| 83 | // once and cached as M8's is. |
| 84 | func branchBinary(t *testing.T) string { |
| 85 | return oldBinaryAt(t, branchPoint, "testbuild", map[string]string{"m9_old_instance_test.go": oldMacInstanceTest}) |
| 86 | } |
| 87 | |
| 88 | // workerOnline waits for the worker labelled Tito Studio. |
| 89 | func (s *session) workerOnline(t *testing.T) { |
| 90 | t.Helper() |
| 91 | s.wait(t, "the worker online", 30*time.Second, func(c *thintest.Conversation) bool { |
| 92 | for _, r := range c.Runners { |
| 93 | if r.Role == wire.RoleWorker && r.Online && r.Label == "Tito Studio" { |
| 94 | return true |
| 95 | } |
| 96 | } |
| 97 | return false |
| 98 | }) |
| 99 | } |
| 100 | |
| 101 | // waitsForUpdate sends a Panes change (Mac work) and waits for its |
| 102 | // Engineering to wait for the update of who, never dispatched. |
| 103 | func (s *session) waitsForUpdate(t *testing.T, inst *thintest.Instance, worker, who string) { |
| 104 | t.Helper() |
| 105 | sent := s.send(t, "tool:record_task:woodstar/panes|Pane titles, mixed|Say what each pane is for.\ntool:start_task:$task|edit:Woodstar/Swipe.swift|edgeSwipe = false|edgeSwipe = true") |
| 106 | id := s.taskOf(t, sent.Turn.ID) |
| 107 | s.wait(t, "Engineering waiting for the update", 2*time.Minute, func(c *thintest.Conversation) bool { |
| 108 | return stepOf(c.Tasks[id], wire.SpecialistEngineering, 0).Placement == wire.PlacementMacUpdate |
| 109 | }) |
| 110 | k, err := s.env.Client.Task(s.ctx, id) |
| 111 | if err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | eng := stepOf(k, wire.SpecialistEngineering, 0) |
| 115 | var waits string |
| 116 | if err := inst.DB.Writer().QueryRow("SELECT coalesce(sandbox_runner, '') FROM chat_steps WHERE id = ?", eng.ID).Scan(&waits); err != nil { |
| 117 | t.Fatal(err) |
| 118 | } |
| 119 | if eng.State != wire.StepQueued || eng.RunnerID != "" || waits != who { |
| 120 | t.Fatalf("waiting for %q to update: %+v (waits for %q)", who, eng, waits) |
| 121 | } |
| 122 | // A moment more: nothing is dispatched anywhere meanwhile. |
| 123 | time.Sleep(2 * time.Second) |
| 124 | var dispatched int |
| 125 | if err := inst.DB.Writer().QueryRow("SELECT count(*) FROM runner_commands WHERE kind = ? AND instr(body, ?) > 0", wire.FrameDispatch, eng.ID).Scan(&dispatched); err != nil || dispatched != 0 { |
| 126 | t.Fatalf("%d dispatches of the waiting Engineering step (%v)", dispatched, err) |
| 127 | } |
| 128 | if _, err := s.env.Client.CancelTask(s.ctx, id); err != nil { |
| 129 | t.Fatal(err) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | func sessionProtocol(t *testing.T, inst *thintest.Instance, runner string) int { |
| 134 | t.Helper() |
| 135 | var p int |
| 136 | if err := inst.DB.Writer().QueryRow("SELECT protocol FROM runner_sessions WHERE runner_id = ?", runner).Scan(&p); err != nil { |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | return p |
| 140 | } |
| 141 | |
| 142 | // M9g: mixed versions with main at the branch point (protocol 35). Mac |
| 143 | // Engineering needs 37 on both the primary and the worker: either one |
| 144 | // older, and it waits for that one's update (waiting_mac_update), |
| 145 | // dispatched nowhere. A 37 worker against an instance rolled back to |
| 146 | // the branch point speaks 35 in its hello, queues nothing only 37 |
| 147 | // carries, and loses nothing: its Claude connection stays, and it still |
| 148 | // takes checks. |
| 149 | func TestMacMixedVersions(t *testing.T) { |
| 150 | if testing.Short() { |
| 151 | t.Skip("builds main at the branch point") |
| 152 | } |
| 153 | old := branchBinary(t) |
| 154 | |
| 155 | t.Run("old worker, new primary", func(t *testing.T) { |
| 156 | env, inst, primary := localEnv(t) |
| 157 | addWoodstar(t, primary.root, primary.opts.Forge.(*localForge)) |
| 158 | woodstarCredentials(t, primary) |
| 159 | primary.start(t) |
| 160 | inst.Store.Chat().SetMacEngineering(true) |
| 161 | worker, _ := macWorker(t, inst, primary) |
| 162 | worker.binary = old |
| 163 | worker.start(t) |
| 164 | s := open(t, env) |
| 165 | s.keep() |
| 166 | s.workerOnline(t) |
| 167 | if p := sessionProtocol(t, inst, worker.runnerID); p != wire.Protocol35 { |
| 168 | t.Fatalf("the branch point's worker speaks %d", p) |
| 169 | } |
| 170 | s.waitsForUpdate(t, inst, worker.runnerID, worker.runnerID) |
| 171 | }) |
| 172 | |
| 173 | t.Run("new worker, old primary", func(t *testing.T) { |
| 174 | env, inst, primary := localEnv(t) |
| 175 | addWoodstar(t, primary.root, primary.opts.Forge.(*localForge)) |
| 176 | woodstarCredentials(t, primary) |
| 177 | primary.subprocess, primary.binary = true, old |
| 178 | primary.start(t) |
| 179 | var primaryID string |
| 180 | if err := inst.DB.Writer().QueryRow("SELECT runner_id FROM runner_sessions WHERE connected = 1").Scan(&primaryID); err != nil { |
| 181 | t.Fatal(err) |
| 182 | } |
| 183 | inst.Store.Chat().SetMacEngineering(true) |
| 184 | worker, _ := macWorker(t, inst, primary) |
| 185 | worker.start(t) |
| 186 | s := open(t, env) |
| 187 | s.keep() |
| 188 | s.workerOnline(t) |
| 189 | if p, w := sessionProtocol(t, inst, primaryID), sessionProtocol(t, inst, worker.runnerID); p != wire.Protocol35 || w != wire.ProtocolCurrent { |
| 190 | t.Fatalf("protocols: primary %d, worker %d", p, w) |
| 191 | } |
| 192 | s.waitsForUpdate(t, inst, worker.runnerID, primaryID) |
| 193 | }) |
| 194 | |
| 195 | // The same worker, rolled back and forward: at 37 it makes a change |
| 196 | // (a candidate it retains, a session it keeps). The instance is rolled |
| 197 | // back to the branch point on the database snapshot taken before the |
| 198 | // upgrade; the worker speaks 35, its first connection check there is |
| 199 | // a frame only 37 carries, which it holds. Forward again on the |
| 200 | // database as 37 left it: the held frames go (the same rows, acked) |
| 201 | // and the retained work resumes (Engineering answers from the session |
| 202 | // it kept before the rollback). |
| 203 | t.Run("same worker, 37 to 35 to 37", func(t *testing.T) { |
| 204 | dir := t.TempDir() |
| 205 | base := filepath.Join(dir, "base.db") |
| 206 | first := startBranchInstance(t, old, base, false) |
| 207 | first.stop() |
| 208 | cur := filepath.Join(dir, "current.db") |
| 209 | copyFile(t, base, cur) |
| 210 | inst := thintest.StartInstanceWith(t, func(i *thintest.Instance) { i.Path, i.MacEngineering = cur, true }) |
| 211 | primary, forge := runnerSetup(t, inst.URL, inst.TeamID, first.Primary) |
| 212 | t.Cleanup(func() { primary.stop(t) }) |
| 213 | addWoodstar(t, primary.root, forge) |
| 214 | woodstarCredentials(t, primary) |
| 215 | primary.start(t) |
| 216 | worker, _ := macWorkerAs(t, first.Worker, primary) |
| 217 | worker.inst = inst |
| 218 | worker.start(t) |
| 219 | turns := 0 |
| 220 | env := &Env{Client: thintest.NewClient(inst.URL, inst.DeviceToken(t)), turns: &turns, ForgePath: forge.path} |
| 221 | s := open(t, env) |
| 222 | s.keep() |
| 223 | s.workerOnline(t) |
| 224 | if p := sessionProtocol(t, inst, worker.runnerID); p != wire.ProtocolCurrent { |
| 225 | t.Fatalf("at 37 or later the worker speaks %d", p) |
| 226 | } |
| 227 | // 37: Engineering on the worker. Its CLI holds after its edit while |
| 228 | // the instance goes away (the rollback starts); then the attempt |
| 229 | // finishes with nobody to tell: it commits, retains the candidate, |
| 230 | // keeps the session, and queues what it would say. |
| 231 | hold := filepath.Join(t.TempDir(), "hold") |
| 232 | if err := os.WriteFile(hold, nil, 0600); err != nil { |
| 233 | t.Fatal(err) |
| 234 | } |
| 235 | sent := s.send(t, "tool:record_task:woodstar/panes|Swipe, across the rollback|Swipe back from the edge feels late.\ntool:start_task:$task|edit:Woodstar/Swipe.swift|edgeSwipe = false|edgeSwipe = true afterwait:"+hold) |
| 236 | task := s.taskOf(t, sent.Turn.ID) |
| 237 | s.wait(t, "Engineering running on the worker", 3*time.Minute, func(c *thintest.Conversation) bool { |
| 238 | e := stepOf(c.Tasks[task], wire.SpecialistEngineering, 0) |
| 239 | return e.RunnerID == worker.runnerID && e.State == wire.StepRunning && e.StartedAt != nil |
| 240 | }) |
| 241 | for deadline := time.Now().Add(time.Minute); len(engineeringProcs(worker)) == 0; time.Sleep(50 * time.Millisecond) { |
| 242 | if time.Now().After(deadline) { |
| 243 | t.Fatal("the CLI never started on the worker") |
| 244 | } |
| 245 | } |
| 246 | k, err := env.Client.Task(s.ctx, task) |
| 247 | if err != nil { |
| 248 | t.Fatal(err) |
| 249 | } |
| 250 | eng := stepOf(k, wire.SpecialistEngineering, 0) |
| 251 | s.close() |
| 252 | before := workerOutbox(t, worker) |
| 253 | inst.Stop() |
| 254 | if err := os.Remove(hold); err != nil { |
| 255 | t.Fatal(err) |
| 256 | } |
| 257 | var queued []string |
| 258 | for deadline := time.Now().Add(2 * time.Minute); ; time.Sleep(250 * time.Millisecond) { |
| 259 | queued = queued[:0] |
| 260 | steps := 0 |
| 261 | for id, row := range workerOutbox(t, worker) { |
| 262 | if _, seen := before[id]; !seen && !row.acked { |
| 263 | queued = append(queued, id) |
| 264 | if row.kind == wire.FrameStep { |
| 265 | steps++ |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | // Committed, then done: the worker's two reports of the end. |
| 270 | if steps >= 2 && len(workerKept(t, worker, eng.ID)) == 1 && len(engineeringProcs(worker)) == 0 { |
| 271 | break |
| 272 | } |
| 273 | if time.Now().After(deadline) { |
| 274 | out := workerOutbox(t, worker) |
| 275 | var ks []string |
| 276 | for _, id := range queued { |
| 277 | ks = append(ks, out[id].kind) |
| 278 | } |
| 279 | t.Fatalf("the attempt did not finish offline: queued %v %v, kept %v, procs %v", queued, ks, workerKept(t, worker, eng.ID), engineeringProcs(worker)) |
| 280 | } |
| 281 | } |
| 282 | slices.Sort(queued) |
| 283 | kinds := workerOutbox(t, worker) |
| 284 | kept, refs := workerKept(t, worker, eng.ID), workerRefs(t, worker) |
| 285 | if !slices.ContainsFunc(refs, func(r string) bool { return strings.HasPrefix(r, "refs/equipo/"+task+"/"+eng.ID+"/1 ") }) { |
| 286 | t.Fatalf("the candidate is not retained: %v", refs) |
| 287 | } |
| 288 | t.Logf("queued while the instance was away: %d frames", len(queued)) |
| 289 | |
| 290 | // Rolled back: the branch point on the snapshot from before the |
| 291 | // upgrade, at its own address; the runners follow it there. |
| 292 | worker.stop(t) |
| 293 | primary.stop(t) |
| 294 | back := filepath.Join(dir, "rollback.db") |
| 295 | copyFile(t, base, back) |
| 296 | old35 := startBranchInstance(t, old, back, true) |
| 297 | primary.cfg.Origin = old35.URL |
| 298 | primary.start(t) |
| 299 | worker.cfg.Origin, worker.inst, worker.instanceDB = old35.URL, nil, back |
| 300 | worker.start(t) |
| 301 | d, err := sql.Open("sqlite", "file:"+back+"?mode=ro&_pragma=busy_timeout(5000)") |
| 302 | if err != nil { |
| 303 | t.Fatal(err) |
| 304 | } |
| 305 | var protocol int |
| 306 | var hello string |
| 307 | err = d.QueryRow("SELECT protocol, hello FROM runner_sessions WHERE runner_id = ?", worker.runnerID).Scan(&protocol, &hello) |
| 308 | d.Close() |
| 309 | if err != nil { |
| 310 | t.Fatal(err) |
| 311 | } |
| 312 | var h wire.Hello |
| 313 | if err := json.Unmarshal([]byte(hello), &h); err != nil || h.Capabilities == nil { |
| 314 | t.Fatalf("hello %s: %v", hello, err) |
| 315 | } |
| 316 | if protocol != wire.Protocol35 || h.Capabilities.Engineering != nil || h.SealKey != "" || h.Inventory || len(h.Connections) != 0 || slices.ContainsFunc(h.Capabilities.Probes, func(p wire.Probe) bool { return strings.HasPrefix(p.ID, "F") }) { |
| 317 | t.Fatalf("the worker's hello at %d: %s", protocol, hello) |
| 318 | } |
| 319 | // Nothing only 37 carries is sent to it; nothing is lost; what the |
| 320 | // worker retained stays. |
| 321 | time.Sleep(3 * time.Second) |
| 322 | during := workerOutbox(t, worker) |
| 323 | for _, id := range queued { |
| 324 | row, ok := during[id] |
| 325 | if !ok { |
| 326 | t.Fatalf("frame %s (%s) was lost in the rollback", id, kinds[id].kind) |
| 327 | } |
| 328 | if row.acked { |
| 329 | // A report of Engineering, which only 37 places: an older |
| 330 | // instance would drop it. |
| 331 | t.Fatalf("frame %s (%s), which only 37 carries, was sent at 35", id, row.kind) |
| 332 | } |
| 333 | } |
| 334 | if k, r := workerKept(t, worker, eng.ID), workerRefs(t, worker); !slices.Equal(k, kept) || !slices.Equal(r, refs) { |
| 335 | t.Fatalf("retained work changed at 35: sessions %v (were %v), refs %v (were %v)", k, kept, r, refs) |
| 336 | } |
| 337 | var at35 []string |
| 338 | for _, id := range queued { |
| 339 | at35 = append(at35, fmt.Sprintf("%s:%s:acked=%v", id, during[id].kind, during[id].acked)) |
| 340 | } |
| 341 | t.Logf("at 35: %v", at35) |
| 342 | |
| 343 | // Forward again: 37 on its own database, at its address. |
| 344 | worker.stop(t) |
| 345 | primary.stop(t) |
| 346 | old35.stop() |
| 347 | inst.Restart(t) |
| 348 | primary.cfg.Origin = inst.URL |
| 349 | primary.start(t) |
| 350 | worker.cfg.Origin, worker.inst, worker.instanceDB = inst.URL, inst, "" |
| 351 | worker.start(t) |
| 352 | if p := sessionProtocol(t, inst, worker.runnerID); p != wire.ProtocolCurrent { |
| 353 | t.Fatalf("forward again, the worker speaks %d", p) |
| 354 | } |
| 355 | for deadline := time.Now().Add(time.Minute); ; time.Sleep(250 * time.Millisecond) { |
| 356 | now, sent := workerOutbox(t, worker), 0 |
| 357 | for _, id := range queued { |
| 358 | if row, ok := now[id]; !ok { |
| 359 | t.Fatalf("queued frame %s is gone", id) |
| 360 | } else if row.acked { |
| 361 | sent++ |
| 362 | } |
| 363 | } |
| 364 | if sent == len(queued) { |
| 365 | break |
| 366 | } |
| 367 | if time.Now().After(deadline) { |
| 368 | t.Fatalf("%d of %d queued frames sent at 37", sent, len(queued)) |
| 369 | } |
| 370 | } |
| 371 | s = open(t, env) |
| 372 | s.keep() |
| 373 | s.workerOnline(t) |
| 374 | // The work resumes: the attempt the instance saw start is done and |
| 375 | // published, judged, and Engineering answers from its session. |
| 376 | k = s.settled(t, task, 3*time.Minute) |
| 377 | if e := stepOf(k, wire.SpecialistEngineering, 0); e.ID != eng.ID || e.Attempt != 1 || e.State != wire.StepDone || !e.Pushed || k.Stage != wire.StageReadyToMerge || !slices.ContainsFunc(refs, func(r string) bool { return r == "refs/equipo/"+task+"/"+eng.ID+"/1 "+e.CandidateSHA }) { |
| 378 | t.Fatalf("after the round trip: %s %s", k.Stage, stepKinds(k)) |
| 379 | } |
| 380 | q, _ := s.asked(t, task, "What did you change? ask:git", wire.SpecialistEngineering, time.Minute) |
| 381 | if q.State != wire.QuestionAnswered || q.StepID != eng.ID || !strings.HasPrefix(q.Answer, "From my session:") || !strings.Contains(q.Answer, "The change: +") { |
| 382 | t.Fatalf("the retained work after the rollback: %+v", q) |
| 383 | } |
| 384 | if attempt, session, keptNow := macAskPin(t, inst, worker, q.ID); attempt != 1 || session != kept[0] || keptNow != kept[0] { |
| 385 | t.Fatalf("answered from attempt %d session %q (kept %q), want the session kept before the rollback, %q", attempt, session, keptNow, kept[0]) |
| 386 | } |
| 387 | }) |
| 388 | } |
| 389 | |
| 390 | // branchInstance is the branch point's instance in its own process. |
| 391 | type branchInstance struct { |
| 392 | URL, Team, Token, DB string |
| 393 | Primary, Worker runnerproto.RunnerCredential |
| 394 | stop func() |
| 395 | } |
| 396 | |
| 397 | // startBranchInstance starts the branch point's instance on the database |
| 398 | // at db: made there afresh (a primary and a worker enrolled), or opened |
| 399 | // as it is. |
| 400 | func startBranchInstance(t *testing.T, old, db string, existing bool) *branchInstance { |
| 401 | t.Helper() |
| 402 | out := filepath.Join(t.TempDir(), "instance.json") |
| 403 | cmd := exec.Command(old, "-test.run", "^TestM9OldInstance$", "-test.timeout", "20m") |
| 404 | cmd.Env = append(os.Environ(), "EQUIPO_M9_OUT="+out, "EQUIPO_M9_DB="+db) |
| 405 | if existing { |
| 406 | cmd.Env = append(cmd.Env, "EQUIPO_M9_EXISTING=1") |
| 407 | } |
| 408 | stdin, _ := cmd.StdinPipe() |
| 409 | var logs bytes.Buffer |
| 410 | cmd.Stdout, cmd.Stderr = &logs, &logs |
| 411 | if err := cmd.Start(); err != nil { |
| 412 | t.Fatal(err) |
| 413 | } |
| 414 | var once sync.Once |
| 415 | b := &branchInstance{} |
| 416 | b.stop = func() { |
| 417 | once.Do(func() { |
| 418 | stdin.Close() |
| 419 | done := make(chan error, 1) |
| 420 | go func() { done <- cmd.Wait() }() |
| 421 | select { |
| 422 | case <-done: |
| 423 | case <-time.After(30 * time.Second): |
| 424 | cmd.Process.Kill() |
| 425 | <-done |
| 426 | } |
| 427 | }) |
| 428 | } |
| 429 | t.Cleanup(b.stop) |
| 430 | for deadline := time.Now().Add(2 * time.Minute); ; time.Sleep(100 * time.Millisecond) { |
| 431 | raw, err := os.ReadFile(out) |
| 432 | if err == nil && json.Unmarshal(raw, b) == nil { |
| 433 | return b |
| 434 | } |
| 435 | if time.Now().After(deadline) { |
| 436 | t.Fatalf("the branch point's instance did not start: %s", logs.String()) |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | func copyFile(t *testing.T, from, to string) { |
| 442 | t.Helper() |
| 443 | raw, err := os.ReadFile(from) |
| 444 | if err != nil { |
| 445 | t.Fatal(err) |
| 446 | } |
| 447 | if _, err := os.Stat(from + "-wal"); err == nil { |
| 448 | t.Fatalf("%s was not closed cleanly (its WAL is there)", from) |
| 449 | } |
| 450 | if err := os.WriteFile(to, raw, 0600); err != nil { |
| 451 | t.Fatal(err) |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | type outboxRow struct { |
| 456 | kind string |
| 457 | acked bool |
| 458 | } |
| 459 | |
| 460 | // workerOutbox is every frame of the worker's outbox, by id. |
| 461 | func workerOutbox(t *testing.T, worker *localRunner) map[string]outboxRow { |
| 462 | t.Helper() |
| 463 | d, err := sql.Open("sqlite", "file:"+filepath.Join(worker.cfg.StateDir, "thin.db")+"?mode=ro&_pragma=busy_timeout(5000)") |
| 464 | if err != nil { |
| 465 | t.Fatal(err) |
| 466 | } |
| 467 | defer d.Close() |
| 468 | rows, err := d.Query("SELECT id, kind, acked FROM outbox") |
| 469 | if err != nil { |
| 470 | t.Fatal(err) |
| 471 | } |
| 472 | defer rows.Close() |
| 473 | out := map[string]outboxRow{} |
| 474 | for rows.Next() { |
| 475 | var id string |
| 476 | var r outboxRow |
| 477 | if err := rows.Scan(&id, &r.kind, &r.acked); err != nil { |
| 478 | t.Fatal(err) |
| 479 | } |
| 480 | out[id] = r |
| 481 | } |
| 482 | return out |
| 483 | } |
| 484 | |
| 485 | // workerRefs is every candidate ref of the worker's stores, as |
| 486 | // "<ref> <sha>", sorted. |
| 487 | func workerRefs(t *testing.T, worker *localRunner) []string { |
| 488 | t.Helper() |
| 489 | stores, _ := filepath.Glob(filepath.Join(worker.cfg.StateDir, "worker", "objects", "*.git")) |
| 490 | var out []string |
| 491 | for _, store := range stores { |
| 492 | raw, err := exec.Command("git", "--git-dir="+store, "for-each-ref", "--format=%(refname) %(objectname)", "refs/equipo").Output() |
| 493 | if err != nil { |
| 494 | t.Fatal(err) |
| 495 | } |
| 496 | out = append(out, strings.Fields(strings.ReplaceAll(string(raw), " ", "\x00"))...) |
| 497 | } |
| 498 | for i := range out { |
| 499 | out[i] = strings.ReplaceAll(out[i], "\x00", " ") |
| 500 | } |
| 501 | slices.Sort(out) |
| 502 | return out |
| 503 | } |
| 504 | |