oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package equipotest
2
3import (
4 "amadan.net/oficina/equipo/internal/equipo"
5 "amadan.net/oficina/equipo/internal/equipo/team"
6 "amadan.net/rastrillo/rastrillo/db"
7 "amadan.net/rastrillo/rastrillo/migrate"
8 "context"
9 "fmt"
10 "io"
11 "log/slog"
12 "path/filepath"
13 "strings"
14 "testing"
15 "time"
16)
17
18func migratedExitFixture(t *testing.T, claimed, cancelled bool) (*equipo.Store, *equipo.Execution, *db.DB, string) {
19 t.Helper()
20 ctx := context.Background()
21 path := filepath.Join(t.TempDir(), "old.db")
22 d, err := db.Open(path, slog.New(slog.NewTextHandler(io.Discard, nil)))
23 executionMust(t, err)
24 t.Cleanup(func() { d.Close() })
25 before := &migrate.Set{}
26 for _, m := range equipo.BootSchema.All() {
27 if schemaBefore(m.ID, "0017") {
28 before.Add(m)
29 }
30 }
31 _, err = migrate.Apply(ctx, d, before)
32 executionMust(t, err)
33 s := equipo.NewStore(d)
34 cutoverRow(t, d, "teams", map[string]any{"id": "team", "name": "Existing team", "roster_epoch": 1})
35 cutoverRow(t, d, "members", map[string]any{"address": ownerAddress, "role": "owner", "active": 1, "revision": 1})
36 cutoverRow(t, d, "members", map[string]any{"address": "member@example.com", "role": "member", "active": 1, "revision": 1})
37 options := executionOptions()
38 options.Authority.(*team.FakeAuthority).Issue("old-grant", team.Grant{Account: "team", Operations: map[team.Operation]bool{team.OpJobContinue: true, team.OpAttemptContinue: true}})
39 core, err := team.Open(ctx, path, options)
40 executionMust(t, err)
41 x := &equipo.Execution{Core: core}
42 _, err = x.Core.Resume(ctx)
43 executionMust(t, err)
44 job, err := x.Core.CreateJob(ctx, team.JobSpec{Account: "team", Requester: ownerAddress, GrantRef: "old-grant", Target: team.TargetBinding{Kind: team.TargetRepository, Repository: "example/shop", Ref: "team/old", DefaultRef: "main", BaseRevision: fmt.Sprintf("%064x", 1)}, Evidence: team.EvidenceRequirement{Predicate: team.PredicateSoftwareFix, ExpectedSymptom: team.FixtureSymptom, CheckDefinitionHash: team.CheckDefinitionHash}, Policy: team.DefaultPolicy(5000000, team.Rates{Version: 1}), Steps: team.SoftwareSteps(false)})
45 executionMust(t, err)
46 var instance equipo.Team
47 executionMust(t, d.G.First(&instance).Error)
48 // The job's authority account matches the migrated task's team.
49 _, err = d.Writer().Exec("UPDATE jobs SET account=? WHERE id=?", instance.ID, job.ID)
50 executionMust(t, err)
51 state := "queued"
52 if cancelled {
53 state = "cancel_requested"
54 }
55 cutoverRow(t, d, "tasks", map[string]any{"id": "old", "team_id": instance.ID, "job_id": job.ID, "state": state, "request_id": "old", "requester": ownerAddress, "revision": 1})
56 cutoverRow(t, d, "task_outboxes", map[string]any{"task_id": "old", "state": "done", "engineering": "{}", "review": "{}"})
57 if cancelled {
58 cutoverRow(t, d, "audit_events", map[string]any{"actor": ownerAddress, "kind": "task.cancel", "record_id": "old", "at": time.Now().UTC()})
59 }
60 if claimed {
61 stamp := "2026-11-02T11:59:00Z"
62 cutoverRow(t, d, "attempts", map[string]any{"id": 1, "job_id": job.ID, "step_id": 1, "number": 1, "state": "active", "containment": "pending", "fence": 7, "created_at": stamp, "updated_at": stamp, "lease_deadline": "2026-11-02T12:01:00Z"})
63 _, err = d.Writer().Exec("UPDATE steps SET holder=1,state='running',fence=7 WHERE id=1")
64 executionMust(t, err)
65 cutoverRow(t, d, "time_reservations", map[string]any{"attempt_id": 1, "job_id": job.ID, "state": "open", "reserved_seconds": 120, "started_at": stamp})
66 cutoverRow(t, d, "runner_assignments", map[string]any{"id": "v1", "task_id": "old", "team_id": instance.ID, "state": "claimed", "claim_request": "old-claim", "attempt_id": 1, "job_id": job.ID, "fence": 7})
67 }
68 executionMust(t, core.Close())
69 _, err = migrate.Apply(ctx, d, equipo.BootSchema)
70 executionMust(t, err)
71 x, err = s.AttachExecution(ctx, path, options)
72 executionMust(t, err)
73 t.Cleanup(func() { x.Core.Close() })
74 return s, x, d, "old"
75}
76func TestMigratedTaskWaitExit(t *testing.T) {
77 for _, kind := range []string{"unclaimed", "unclaimed-cancel", "claimed", "claimed-cancel", "member", "removed-owner", "audit-failure"} {
78 t.Run(kind, func(t *testing.T) {
79 claimed := !strings.HasPrefix(kind, "unclaimed")
80 cancelled := strings.HasSuffix(kind, "cancel")
81 s, _, d, id := migratedExitFixture(t, claimed, cancelled)
82 var state, wait string
83 executionMust(t, d.Writer().QueryRow("SELECT state,wait_code FROM tasks WHERE id=?", id).Scan(&state, &wait))
84 if !claimed {
85 if wait == "task.wait.reconcile" {
86 t.Fatal("unclaimed task trapped")
87 }
88 if cancelled && state != "cancelled" {
89 t.Fatal("cancel request lost", state)
90 }
91 return
92 }
93 actor := ownerActor
94 if kind == "member" {
95 actor = equipo.Actor{Address: "member@example.com"}
96 }
97 if kind == "removed-owner" {
98 _, err := d.Writer().Exec("UPDATE members SET active=0 WHERE address=?", ownerAddress)
99 executionMust(t, err)
100 }
101 if kind == "audit-failure" {
102 _, err := d.Writer().Exec("CREATE TRIGGER stop_audit_failure BEFORE INSERT ON audit_events BEGIN SELECT RAISE(ABORT,'audit unavailable'); END")
103 executionMust(t, err)
104 }
105 err := s.OldRunnerStopped(context.Background(), actor, id)
106 if kind == "member" || kind == "removed-owner" || kind == "audit-failure" {
107 if err == nil {
108 t.Fatal("unauthorized or unaudited containment")
109 }
110 if scalar(t, d, "SELECT count(*) FROM attempts WHERE containment='pending'") != 1 {
111 t.Fatal("failed transaction changed containment")
112 }
113 return
114 }
115 executionMust(t, err)
116 executionMust(t, d.Writer().QueryRow("SELECT state,wait_code FROM tasks WHERE id=?", id).Scan(&state, &wait))
117 if wait == "task.wait.reconcile" || cancelled && state != "cancelled" {
118 t.Fatal("task still trapped", state, wait)
119 }
120 if scalar(t, d, "SELECT count(*) FROM runner_assignments WHERE state='contained'") != 1 || scalar(t, d, "SELECT count(*) FROM attempts WHERE containment='contained'") != 1 {
121 t.Fatal("v1 work not contained")
122 }
123 if scalar(t, d, "SELECT count(*) FROM audit_events WHERE kind='task.old_runner_stopped'") != 1 {
124 t.Fatal("missing audit")
125 }
126 if !cancelled {
127 var jobState string
128 executionMust(t, d.Writer().QueryRow("SELECT state FROM jobs LIMIT 1").Scan(&jobState))
129 if jobState != "running" {
130 t.Fatal("job not resumed", jobState)
131 }
132 }
133 })
134 }
135}
136