| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/wire" |
| 9 | ) |
| 10 | |
| 11 | // stepFrame sends one step report and waits for the instance's ack. |
| 12 | func (r *toolRig) step(id string, rep wire.StepReport) { |
| 13 | r.t.Helper() |
| 14 | if err := r.runner.Send(r.ctx, id, wire.FrameStep, rep); err != nil { |
| 15 | r.t.Fatal(err) |
| 16 | } |
| 17 | for { |
| 18 | f, body, err := r.runner.Next(r.ctx) |
| 19 | if err != nil { |
| 20 | r.t.Fatal(err) |
| 21 | } |
| 22 | if a, ok := body.(*wire.Ack); ok && f.Type == wire.FrameAck && a.ID == id { |
| 23 | return |
| 24 | } |
| 25 | if f.Type != wire.FrameAck { |
| 26 | // A command the report caused (the next step's dispatch). |
| 27 | r.held = append(r.held, heldFrame{f, body, r.runner.Received[len(r.runner.Received)-1]}) |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // expectDispatch reads frames until a dispatch arrives, acking it. |
| 33 | func (r *toolRig) expect(kind string) (wire.Frame, any) { |
| 34 | r.t.Helper() |
| 35 | for i, h := range r.held { |
| 36 | if h.f.Type == kind { |
| 37 | r.held = append(r.held[:i:i], r.held[i+1:]...) |
| 38 | r.runner.Received = append(r.runner.Received, h.raw) |
| 39 | if err := r.runner.Send(r.ctx, "a-"+h.f.ID, wire.FrameAck, wire.Ack{ID: h.f.ID}); err != nil { |
| 40 | r.t.Fatal(err) |
| 41 | } |
| 42 | return h.f, h.body |
| 43 | } |
| 44 | } |
| 45 | for { |
| 46 | f, body, err := r.runner.Next(r.ctx) |
| 47 | if err != nil { |
| 48 | r.t.Fatal(err) |
| 49 | } |
| 50 | if f.Type == kind { |
| 51 | if f.Durable() { |
| 52 | if err := r.runner.Send(r.ctx, "a-"+f.ID, wire.FrameAck, wire.Ack{ID: f.ID}); err != nil { |
| 53 | r.t.Fatal(err) |
| 54 | } |
| 55 | } |
| 56 | return f, body |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func taskID(t *testing.T, res wire.ToolResult) string { |
| 62 | t.Helper() |
| 63 | var v struct { |
| 64 | Task string `json:"task"` |
| 65 | } |
| 66 | if res.IsError || json.Unmarshal([]byte(res.Text), &v) != nil || v.Task == "" { |
| 67 | t.Fatalf("no task in %+v", res) |
| 68 | } |
| 69 | return v.Task |
| 70 | } |
| 71 | |
| 72 | // TestContractTasks records a task the moment the person reports it, |
| 73 | // attaches a finding, dispatches its Engineering step and follows the |
| 74 | // runner's reports to a pushed branch, with Retry refused until the |
| 75 | // runner has confirmed termination of the current attempt. |
| 76 | func TestContractTasks(t *testing.T) { |
| 77 | r := newToolRig(t) |
| 78 | ctx := r.ctx |
| 79 | res := r.call("k1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Swipe back from the edge does nothing", "brief": "On iOS, swiping from the left edge does not go back."}) |
| 80 | if res.Code != "recorded" { |
| 81 | t.Fatalf("record: %+v", res) |
| 82 | } |
| 83 | id := taskID(t, res) |
| 84 | // The coordinator links the task by its title in its reply. |
| 85 | if !strings.Contains(res.Text, `"link":"/tasks/`+id+`"`) || !strings.Contains(res.Text, `"title":"Swipe back from the edge does nothing"`) || strings.Contains(res.Text, "task card") { |
| 86 | t.Fatalf("record_task result lacks the link: %s", res.Text) |
| 87 | } |
| 88 | r.wait("noted task, its card and project", func(c *Conversation) bool { |
| 89 | k, ok := c.Tasks[id] |
| 90 | cards := 0 |
| 91 | for _, card := range c.Cards { |
| 92 | if card.Kind == wire.CardTask && card.TaskID == id && card.TurnID == r.turn { |
| 93 | cards++ |
| 94 | } |
| 95 | } |
| 96 | return ok && k.Stage == wire.StageNoted && cards == 1 && len(c.Projects) == 1 |
| 97 | }) |
| 98 | task, err := r.client.Task(ctx, id) |
| 99 | if err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | if task.Branch != wire.TaskBranch(id, task.Title) || task.Project != "Sample" || task.Repo != "amadan.net/oficina/sample" || task.Creator != Owner || len(task.Steps) != 0 { |
| 103 | t.Fatalf("task %+v", task) |
| 104 | } |
| 105 | var project wire.Project |
| 106 | r.conv.Locked(func(c *Conversation) { |
| 107 | for _, p := range c.Projects { |
| 108 | project = p |
| 109 | } |
| 110 | }) |
| 111 | // Projects are products: named from the catalog, with the repository attached. |
| 112 | if project.Title != "Sample" || len(project.Attachments) != 1 || project.Attachments[0].Ref != "amadan.net/oficina/sample" || task.ProjectID != project.ID { |
| 113 | t.Fatalf("project %+v", project) |
| 114 | } |
| 115 | |
| 116 | // Findings: only for the team's task, a repository in the catalog and a clean path. |
| 117 | for _, bad := range []map[string]any{ |
| 118 | {"task": "tk-nope", "text": "x", "repo": "oficina/sample", "path": "a.go", "line": 1}, |
| 119 | {"task": id, "text": "x", "repo": "oficina/secret", "path": "a.go", "line": 1}, |
| 120 | {"task": id, "text": "x", "repo": "oficina/sample", "path": "../etc/passwd", "line": 1}, |
| 121 | {"task": id, "text": "", "repo": "oficina/sample", "path": "a.go", "line": 1}, |
| 122 | } { |
| 123 | if res := r.call("bad-"+itoa(len(r.runner.Sent)), wire.ToolAddFinding, bad); !res.IsError { |
| 124 | t.Fatalf("finding accepted: %v -> %+v", bad, res) |
| 125 | } |
| 126 | } |
| 127 | res = r.call("k2", wire.ToolAddFinding, map[string]any{"task": id, "text": "EdgeSwipe returns false, so back never fires.", "repo": "oficina/sample", "path": "panes/swipe.go", "line": 4}) |
| 128 | if res.IsError || res.Code != "finding" { |
| 129 | t.Fatalf("finding: %+v", res) |
| 130 | } |
| 131 | r.wait("investigated", func(c *Conversation) bool { |
| 132 | k := c.Tasks[id] |
| 133 | return k.Stage == wire.StageInvestigated && len(k.Findings) == 1 && k.Findings[0].Line == 4 |
| 134 | }) |
| 135 | |
| 136 | // Retry before any step: nothing to retry. |
| 137 | if _, err := r.client.RetryTask(ctx, id); Status(err) != 409 { |
| 138 | t.Fatalf("retry before a step: %v", err) |
| 139 | } |
| 140 | res = r.call("k3", wire.ToolStartTask, map[string]string{"task": id, "brief": "Make EdgeSwipe go back."}) |
| 141 | if res.IsError || res.Code != "started" || !strings.Contains(res.Text, `"link":"/tasks/`+id+`"`) { |
| 142 | t.Fatalf("start: %+v", res) |
| 143 | } |
| 144 | if again := r.call("k3b", wire.ToolStartTask, map[string]string{"task": id, "brief": "again"}); !again.IsError { |
| 145 | t.Fatalf("second start accepted: %+v", again) |
| 146 | } |
| 147 | f, body := r.expect(wire.FrameDispatch) |
| 148 | shape(t, "frames/dispatch.json", r.runner.Received[len(r.runner.Received)-1]) |
| 149 | d := body.(*wire.DispatchCommand) |
| 150 | if d.Person != Owner || d.TaskID != id || d.Attempt != 1 || d.Repo != "amadan.net/oficina/sample" || d.Branch != task.Branch || d.BaseSHA != "" || d.Specialist != wire.SpecialistEngineering || len(d.Findings) != 1 || !strings.Contains(d.Brief, "Make EdgeSwipe go back.") { |
| 151 | t.Fatalf("dispatch %s %+v", f.ID, d) |
| 152 | } |
| 153 | r.wait("in progress", func(c *Conversation) bool { |
| 154 | k := c.Tasks[id] |
| 155 | return k.Stage == wire.StageInProgress && len(k.Steps) == 1 && k.Steps[0].State == wire.StepQueued |
| 156 | }) |
| 157 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 158 | r.step("r-s1", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base, Findings: []wire.Finding{}}) |
| 159 | shape(t, "frames/step.json", r.runner.Sent[len(r.runner.Sent)-1]) |
| 160 | r.wait("running with base", func(c *Conversation) bool { |
| 161 | k := c.Tasks[id] |
| 162 | return k.BaseSHA == base && len(k.Steps) == 1 && k.Steps[0].State == wire.StepRunning |
| 163 | }) |
| 164 | if _, err := r.client.RetryTask(ctx, id); Status(err) != 409 { |
| 165 | t.Fatalf("retry while running: %v", err) |
| 166 | } |
| 167 | // A failure without termination proof does not allow Retry. |
| 168 | r.step("r-s2", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed"}) |
| 169 | if _, err := r.client.RetryTask(ctx, id); Status(err) != 409 { |
| 170 | t.Fatalf("retry without termination: %v", err) |
| 171 | } |
| 172 | r.step("r-s3", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", Termination: true}) |
| 173 | r.wait("failed, terminated", func(c *Conversation) bool { |
| 174 | s := c.Steps[d.StepID] |
| 175 | return s.State == wire.StepFailed && s.Terminated |
| 176 | }) |
| 177 | acc, err := r.client.RetryTask(ctx, id) |
| 178 | if err != nil || acc.Attempt != 2 { |
| 179 | t.Fatalf("retry: %+v %v", acc, err) |
| 180 | } |
| 181 | shape(t, "api/task-accepted.json", acc) |
| 182 | if _, err := r.client.RetryTask(ctx, id); Status(err) != 409 { |
| 183 | t.Fatalf("second retry: %v", err) |
| 184 | } |
| 185 | _, body = r.expect(wire.FrameDispatch) |
| 186 | d2 := body.(*wire.DispatchCommand) |
| 187 | if d2.Attempt != 2 || d2.StepID != d.StepID || d2.BaseSHA != base { |
| 188 | t.Fatalf("retry dispatch %+v", d2) |
| 189 | } |
| 190 | // Reports for the old attempt are ignored once a new one exists. |
| 191 | r.step("r-s4", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepDone, CandidateSHA: strings.Repeat("9", 40), Pushed: true, Termination: true}) |
| 192 | r.step("r-s5", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 2, State: wire.StepRunning, BaseSHA: base}) |
| 193 | r.step("r-s6", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 2, State: wire.StepRunning, BaseSHA: base, CandidateSHA: cand}) |
| 194 | // The candidate of an attempt never changes, and a step never goes back. |
| 195 | r.step("r-s7", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 2, State: wire.StepRunning, BaseSHA: base, CandidateSHA: strings.Repeat("3", 40)}) |
| 196 | r.step("r-s7b", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 2, State: wire.StepDone, BaseSHA: base, CandidateSHA: strings.Repeat("3", 40), Pushed: true, Termination: true}) |
| 197 | if k, _ := r.client.Task(ctx, id); k.Steps[0].CandidateSHA != cand || k.Steps[0].State != wire.StepRunning { |
| 198 | t.Fatalf("candidate changed or done with another candidate: %+v", k.Steps[0]) |
| 199 | } |
| 200 | r.step("r-s8", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 2, State: wire.StepDone, BaseSHA: base, CandidateSHA: cand, Pushed: true, Termination: true, Summary: "EdgeSwipe goes back now.", LogTail: "Edit panes/swipe.go"}) |
| 201 | r.step("r-s9", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 2, State: wire.StepRunning, BaseSHA: base, CandidateSHA: cand}) |
| 202 | r.wait("pushed", func(c *Conversation) bool { |
| 203 | s := c.Steps[d.StepID] |
| 204 | k := c.Tasks[id] |
| 205 | return s.State == wire.StepDone && s.Pushed && s.CandidateSHA == cand && s.Attempt == 2 && strings.HasPrefix(k.URL, "https://amadan.net/oficina/sample?branch=equipo/") |
| 206 | }) |
| 207 | task, err = r.client.Task(ctx, id) |
| 208 | // Pushed, the candidate goes to review: its checks are dispatched (slice 4). |
| 209 | if err != nil || task.Stage != wire.StageInReview || len(task.Steps) != 2 || task.Steps[1].Specialist != wire.SpecialistChecks || task.Steps[0].Summary != "EdgeSwipe goes back now." { |
| 210 | t.Fatalf("task after push %+v %v", task, err) |
| 211 | } |
| 212 | shape(t, "api/task.json", task) |
| 213 | r.dispatched(wire.SpecialistChecks, base, cand, 0) |
| 214 | if _, err := r.client.RetryTask(ctx, id); Status(err) != 409 { |
| 215 | t.Fatalf("retry after done: %v", err) |
| 216 | } |
| 217 | log, err := r.client.StepLog(ctx, d.StepID) |
| 218 | if err != nil || log.Tail != "Edit panes/swipe.go" || log.Attempt != 2 { |
| 219 | t.Fatalf("log %+v %v", log, err) |
| 220 | } |
| 221 | shape(t, "api/step-log.json", log) |
| 222 | entry, err := r.client.PostThread(ctx, id, "It happens on the iPhone only.", "k-th") |
| 223 | if err != nil || entry.Author != Owner { |
| 224 | t.Fatalf("thread %+v %v", entry, err) |
| 225 | } |
| 226 | if again, err := r.client.PostThread(ctx, id, "It happens on the iPhone only.", "k-th"); err != nil || again.ID != entry.ID { |
| 227 | t.Fatalf("thread repeat %+v %v", again, err) |
| 228 | } |
| 229 | list, err := r.client.Tasks(ctx) |
| 230 | if err != nil || len(list.Items) != 1 || len(list.Items[0].Thread) != 1 { |
| 231 | t.Fatalf("tasks %+v %v", list, err) |
| 232 | } |
| 233 | shape(t, "api/tasks.json", list) |
| 234 | read := r.call("k4", wire.ToolReadTask, map[string]string{"task": id}) |
| 235 | for _, want := range []string{task.Branch, cand, "It happens on the iPhone only.", "EdgeSwipe returns false", `"link":"/tasks/` + id + `"`} { |
| 236 | if !strings.Contains(read.Text, want) { |
| 237 | t.Fatalf("read_task lacks %q: %s", want, read.Text) |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // A second task: cancelled while noted ends at once. |
| 242 | res = r.call("k5", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Typo in the README", "brief": "teh"}) |
| 243 | other := taskID(t, res) |
| 244 | if acc, err := r.client.CancelTask(ctx, other); err != nil || acc.Attempt != 0 { |
| 245 | t.Fatalf("cancel noted: %+v %v", acc, err) |
| 246 | } |
| 247 | r.wait("cancelled", func(c *Conversation) bool { return c.Tasks[other].Stage == wire.StageCancelled }) |
| 248 | if _, err := r.client.CancelTask(ctx, other); Status(err) != 409 { |
| 249 | t.Fatalf("cancel twice: %v", err) |
| 250 | } |
| 251 | if res := r.call("k6", wire.ToolStartTask, map[string]string{"task": other, "brief": "x"}); !res.IsError { |
| 252 | t.Fatalf("started a cancelled task: %+v", res) |
| 253 | } |
| 254 | |
| 255 | // A third: cancelled while its step runs ends only on the runner's report. |
| 256 | third := taskID(t, r.call("k7", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Another", "brief": "b"})) |
| 257 | r.call("k8", wire.ToolStartTask, map[string]string{"task": third, "brief": "go"}) |
| 258 | _, body = r.expect(wire.FrameDispatch) |
| 259 | d3 := body.(*wire.DispatchCommand) |
| 260 | r.step("r-t1", wire.StepReport{TaskID: third, StepID: d3.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: base}) |
| 261 | acc, err = r.client.CancelTask(ctx, third) |
| 262 | if err != nil || acc.Attempt != 1 { |
| 263 | t.Fatalf("cancel running: %+v %v", acc, err) |
| 264 | } |
| 265 | _, body = r.expect(wire.FrameCancelStep) |
| 266 | shape(t, "frames/cancel_step.json", r.runner.Received[len(r.runner.Received)-1]) |
| 267 | if c := body.(*wire.CancelStepCommand); c.StepID != d3.StepID || c.Attempt != 1 || c.Person != Owner { |
| 268 | t.Fatalf("cancel_step %+v", c) |
| 269 | } |
| 270 | snap, _ := r.client.Task(ctx, third) |
| 271 | if snap.Stage == wire.StageCancelled { |
| 272 | t.Fatal("cancelled before the runner reported termination") |
| 273 | } |
| 274 | r.step("r-t2", wire.StepReport{TaskID: third, StepID: d3.StepID, Attempt: 1, State: wire.StepCancelled, Reason: "cancelled", Termination: true}) |
| 275 | r.wait("third cancelled", func(c *Conversation) bool { return c.Tasks[third].Stage == wire.StageCancelled }) |
| 276 | if _, err := r.client.RetryTask(ctx, third); Status(err) != 409 { |
| 277 | t.Fatalf("retry of a cancelled task: %v", err) |
| 278 | } |
| 279 | // Events carry the task and step resources. |
| 280 | var sawTask, sawStep bool |
| 281 | r.conv.Locked(func(c *Conversation) { sawTask, sawStep = len(c.Tasks) == 3, len(c.Steps) == 3 }) |
| 282 | if !sawTask || !sawStep { |
| 283 | t.Fatal("task and step events missing") |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | // TestContractStepSync settles a step the runner no longer holds: after |
| 288 | // a reconnect its sync proves nothing runs, so the step failed with |
| 289 | // termination and Retry is allowed. |
| 290 | func TestContractStepSync(t *testing.T) { |
| 291 | r := newToolRig(t) |
| 292 | id := taskID(t, r.call("s1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Sync me", "brief": "b"})) |
| 293 | r.call("s2", wire.ToolStartTask, map[string]string{"task": id, "brief": "go"}) |
| 294 | _, body := r.expect(wire.FrameDispatch) |
| 295 | d := body.(*wire.DispatchCommand) |
| 296 | r.step("r-s1", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: strings.Repeat("1", 40)}) |
| 297 | // A sync that still holds the step changes nothing. |
| 298 | if err := r.runner.Send(r.ctx, "r-sync-a", wire.FrameSync, wire.Sync{Sessions: []wire.SessionState{}, Runs: []wire.Run{{TurnID: r.turn, Attempt: 1, State: "running"}}, Steps: []wire.StepRun{{TaskID: id, StepID: d.StepID, Attempt: 1, State: "running"}}}); err != nil { |
| 299 | t.Fatal(err) |
| 300 | } |
| 301 | // A durable no-op after it: once acked, the sync was applied. |
| 302 | r.step("r-s1b", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepRunning}) |
| 303 | if k, _ := r.client.Task(r.ctx, id); k.Steps[0].State != wire.StepRunning { |
| 304 | t.Fatalf("a held step was settled: %+v", k.Steps[0]) |
| 305 | } |
| 306 | // A second task whose dispatch the runner has not acked yet. |
| 307 | other := taskID(t, r.call("s3", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Not yet", "brief": "b"})) |
| 308 | r.call("s4", wire.ToolStartTask, map[string]string{"task": other, "brief": "go"}) |
| 309 | if err := r.runner.Send(r.ctx, "r-sync-b", wire.FrameSync, wire.Sync{Sessions: []wire.SessionState{}, Runs: []wire.Run{{TurnID: r.turn, Attempt: 1, State: "running"}}, Steps: []wire.StepRun{}}); err != nil { |
| 310 | t.Fatal(err) |
| 311 | } |
| 312 | // The runner no longer holds the step: interrupted, a passing stop, |
| 313 | // so Equipo retries it once on its own (needs-you triage). |
| 314 | r.wait("retried after the sync", func(c *Conversation) bool { |
| 315 | s := c.Steps[d.StepID] |
| 316 | return s.Attempt == 2 && s.State == wire.StepQueued |
| 317 | }) |
| 318 | if acc, err := r.client.RetryTask(r.ctx, id); Status(err) != 409 { |
| 319 | t.Fatalf("a person's retry on top of Equipo's: %+v %v", acc, err) |
| 320 | } |
| 321 | if k, _ := r.client.Task(r.ctx, other); k.Steps[0].State != wire.StepQueued { |
| 322 | t.Fatalf("a step whose dispatch is unacked was settled: %+v", k.Steps[0]) |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | // TestContractTaskToolsNeedAPersonTurn: a move-in turn cannot record or |
| 327 | // start work, and a task of another team member's runner is not reported. |
| 328 | func TestContractTaskToolsScope(t *testing.T) { |
| 329 | r := newToolRig(t) |
| 330 | if res := r.call("x1", wire.ToolStartTask, map[string]string{"task": "tknope", "brief": "go"}); !res.IsError || res.Code != "not_found" { |
| 331 | t.Fatalf("unknown task: %+v", res) |
| 332 | } |
| 333 | if res := r.call("x2", wire.ToolRecordTask, map[string]string{"repo": "oficina/secret", "title": "x", "brief": "y"}); !res.IsError || res.Code != "not_allowed" { |
| 334 | t.Fatalf("repo outside the allowlist: %+v", res) |
| 335 | } |
| 336 | if res := r.call("x3", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "", "brief": "y"}); !res.IsError { |
| 337 | t.Fatalf("empty title: %+v", res) |
| 338 | } |
| 339 | // A step report for a task the runner was never sent is refused. |
| 340 | if err := r.runner.Send(r.ctx, "r-ghost", wire.FrameStep, wire.StepReport{TaskID: "tkghost", StepID: "s_ghost", Attempt: 1, State: wire.StepDone, Termination: true}); err != nil { |
| 341 | t.Fatal(err) |
| 342 | } |
| 343 | if _, _, err := r.runner.Next(r.ctx); err == nil { |
| 344 | // The instance closes the stream on a frame it cannot apply, or |
| 345 | // acks nothing; either way no task appears. |
| 346 | r.conv.Locked(func(c *Conversation) { |
| 347 | if len(c.Tasks) != 0 { |
| 348 | t.Fatalf("ghost task: %+v", c.Tasks) |
| 349 | } |
| 350 | }) |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | // TestContractGrantCard: nothing reachable puts an access card with the |
| 355 | // namespace the name says; acting on it asks the runner to check now, |
| 356 | // and the card resolves itself once a hello's catalog reaches the name. |
| 357 | func TestContractGrantCard(t *testing.T) { |
| 358 | r := newToolRig(t) |
| 359 | res := r.call("g1", wire.ToolFind, map[string]string{"query": "moonbeam/web"}) |
| 360 | if res.Code != "none" || strings.Contains(res.Text, "end with") { |
| 361 | t.Fatalf("find: %+v", res) |
| 362 | } |
| 363 | var card wire.Card |
| 364 | r.wait("access card", func(c *Conversation) bool { |
| 365 | for _, k := range c.Cards { |
| 366 | if k.Kind == wire.CardAccess { |
| 367 | card = k |
| 368 | } |
| 369 | } |
| 370 | return card.ID != "" |
| 371 | }) |
| 372 | if card.Name != "moonbeam/web" || card.Choice != "equipo-test" || card.State != wire.CardOpen { |
| 373 | t.Fatalf("card %+v", card) |
| 374 | } |
| 375 | if _, err := r.client.Answer(r.ctx, card.ID, "something", card.Revision); Status(err) != 422 { |
| 376 | t.Fatalf("other answer: %v", err) |
| 377 | } |
| 378 | if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceCheck, card.Revision); err != nil { |
| 379 | t.Fatal(err) |
| 380 | } |
| 381 | r.expect(wire.FrameRecheck) |
| 382 | shape(t, "frames/recheck.json", r.runner.Received[len(r.runner.Received)-1]) |
| 383 | // A hello that still does not reach it changes nothing. |
| 384 | hello := wire.Hello{Generation: r.runner.Welcome.Generation, Repos: []string{"amadan.net/oficina/sample"}, CreateIn: []string{}, Agent: "equipo-test", Catalog: []wire.RepoInfo{{Repo: "amadan.net/oficina/sample", Names: []string{"sample"}, Hints: []string{}}}, Connections: []wire.HeldConnection{}, Versions: map[string]string{}} |
| 385 | if err := r.runner.Send(r.ctx, "h-g1", wire.FrameHello, hello); err != nil { |
| 386 | t.Fatal(err) |
| 387 | } |
| 388 | // An unrelated repository that merely matches a word of the query |
| 389 | // (app) does not resolve it. |
| 390 | hello.Catalog = append(hello.Catalog, wire.RepoInfo{Repo: "amadan.net/other/app", Summary: "An app.", Names: []string{"app", "Moonbeams"}, Hints: []string{}}) |
| 391 | if err := r.runner.Send(r.ctx, "h-g1b", wire.FrameHello, hello); err != nil { |
| 392 | t.Fatal(err) |
| 393 | } |
| 394 | // A durable no-op round trip, so the hello was applied. |
| 395 | r.call("g-sync", wire.ToolStatus, map[string]string{}) |
| 396 | if c, _ := r.client.Snapshot(r.ctx); func() string { |
| 397 | for _, k := range c.Cards { |
| 398 | if k.ID == card.ID { |
| 399 | return k.State |
| 400 | } |
| 401 | } |
| 402 | return "" |
| 403 | }() != wire.CardOpen { |
| 404 | t.Fatal("resolved by an unrelated repository") |
| 405 | } |
| 406 | hello.Catalog = append(hello.Catalog, wire.RepoInfo{Repo: "amadan.net/moonbeam/web", Names: []string{"web", "Moonbeam"}, Hints: []string{}}) |
| 407 | if err := r.runner.Send(r.ctx, "h-g2", wire.FrameHello, hello); err != nil { |
| 408 | t.Fatal(err) |
| 409 | } |
| 410 | r.wait("resolved", func(c *Conversation) bool { return c.Cards[card.ID].State == wire.CardResolved }) |
| 411 | if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceCheck, card.Revision); Status(err) != 409 { |
| 412 | t.Fatalf("check on a resolved card: %v", err) |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | // TestContractSyncBeforeReplay: the runner reported a failure without |
| 417 | // termination proof and lost the connection before the ack; on |
| 418 | // reconnect its sync (attempt present, termination unverified) arrives |
| 419 | // before the outbox replay. The instance must not infer termination |
| 420 | // from that sync, and Retry stays refused after the replayed report. |
| 421 | func TestContractSyncBeforeReplay(t *testing.T) { |
| 422 | r := newToolRig(t) |
| 423 | id := taskID(t, r.call("q1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Replay order", "brief": "b"})) |
| 424 | r.call("q2", wire.ToolStartTask, map[string]string{"task": id, "brief": "go"}) |
| 425 | _, body := r.expect(wire.FrameDispatch) |
| 426 | d := body.(*wire.DispatchCommand) |
| 427 | r.step("r-q1", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: strings.Repeat("1", 40)}) |
| 428 | // Reconnect order: sync first, holding the attempt as pending. |
| 429 | if err := r.runner.Send(r.ctx, "r-sync-q", wire.FrameSync, wire.Sync{Sessions: []wire.SessionState{}, Runs: []wire.Run{{TurnID: r.turn, Attempt: 1, State: "running"}}, Steps: []wire.StepRun{{TaskID: id, StepID: d.StepID, Attempt: 1, State: "pending"}}}); err != nil { |
| 430 | t.Fatal(err) |
| 431 | } |
| 432 | // Then the replayed report, still without termination. |
| 433 | r.step("r-q2", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed"}) |
| 434 | k, err := r.client.Task(r.ctx, id) |
| 435 | if err != nil || k.Steps[0].State != wire.StepFailed || k.Steps[0].Terminated { |
| 436 | t.Fatalf("termination inferred: %+v %v", k.Steps, err) |
| 437 | } |
| 438 | if _, err := r.client.RetryTask(r.ctx, id); Status(err) != 409 { |
| 439 | t.Fatalf("retry without verified termination: %v", err) |
| 440 | } |
| 441 | // The proof, when it comes, allows Retry. |
| 442 | r.step("r-q3", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", Termination: true}) |
| 443 | if acc, err := r.client.RetryTask(r.ctx, id); err != nil || acc.Attempt != 2 { |
| 444 | t.Fatalf("retry after the proof: %+v %v", acc, err) |
| 445 | } |
| 446 | } |
| 447 | |