| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "slices" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/equipo" |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | // A go-ahead whose work has gone in asks nothing any more (Paul's |
| 13 | // recipes job, 2026-11-28: merged at 18:52, Base still read "This is |
| 14 | // ready: it could not be fully tested. Go ahead?"). Once the task is |
| 15 | // merged, its outcome card says so in closed words, whether the person's |
| 16 | // own tap merged it or it went in some other way, and no go-ahead is |
| 17 | // raised for work that is already in. |
| 18 | |
| 19 | func mergedOutcome(t *testing.T, c wire.AssistantCard) { |
| 20 | t.Helper() |
| 21 | if c.State == wire.CardOpen { |
| 22 | t.Fatalf("still asking %+v", c) |
| 23 | } |
| 24 | if c.Question != equipo.RunnerCopy("assistant.outcome.resolved") || strings.Contains(c.Question, "?") { |
| 25 | t.Fatalf("question %q, want the closed line", c.Question) |
| 26 | } |
| 27 | if c.Result != equipo.RunnerCopy("assistant.job.detail.done") { |
| 28 | t.Fatalf("result %q, want the finished line", c.Result) |
| 29 | } |
| 30 | if len(c.Links) != 0 { |
| 31 | t.Fatalf("links %+v", c.Links) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func TestContractOutcomeClosesOnceMergedByTap(t *testing.T) { |
| 36 | o := newTeamRigAt(t, wire.ProtocolCurrent) |
| 37 | o.sitesOn(wire.ProtocolCurrent, false) |
| 38 | o.ask("Can you make me an app for sharing recipes?") |
| 39 | _, cmd := o.handoff("mt1", "an app for sharing recipes", "") |
| 40 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 41 | var id string |
| 42 | o.in(cmd.TurnID, func() { id, _ = o.websiteReady("mt", "Recipe sharing app", base, cand, false) }) |
| 43 | card := o.assistantCard("the go-ahead", isMergeOutcome) |
| 44 | if card.State != wire.CardOpen || card.Question != mergeQuestion("environment_reason") { |
| 45 | t.Fatalf("go-ahead %+v", card) |
| 46 | } |
| 47 | ans, err := o.a.Answer(o.ctx, card.ID, wire.ChoiceMerge, card.Revision) |
| 48 | if err != nil || ans.Card.State != wire.CardAnswered { |
| 49 | t.Fatalf("answer %+v %v", ans, err) |
| 50 | } |
| 51 | m := o.dispatched(wire.SpecialistMerge, base, cand, 0) |
| 52 | o.mergedStep("mt-m", m, strings.Repeat("e", 40)) |
| 53 | o.wait("done", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageDone }) |
| 54 | var done wire.AssistantCard |
| 55 | o.until("the go-ahead closed", func() bool { |
| 56 | done = o.assistantCard("the go-ahead", func(c wire.AssistantCard) bool { return c.ID == card.ID }) |
| 57 | return done.Question != card.Question |
| 58 | }) |
| 59 | mergedOutcome(t, done) |
| 60 | if done.State != wire.CardAnswered || done.Revision <= ans.Card.Revision { |
| 61 | t.Fatalf("after the merge %+v (answered %+v)", done, ans.Card) |
| 62 | } |
| 63 | // Tapping the old go-ahead again changes nothing. |
| 64 | if _, err := o.a.Answer(o.ctx, card.ID, wire.ChoiceMerge, done.Revision); err == nil { |
| 65 | t.Fatal("a merged go-ahead took an answer") |
| 66 | } |
| 67 | if n := o.rows("SELECT count(*) FROM chat_steps WHERE task_id = ? AND specialist = ?", id, wire.SpecialistMerge); n != 1 { |
| 68 | t.Fatalf("%d merge steps", n) |
| 69 | } |
| 70 | |
| 71 | // A card left asking by an older Equipo (Paul's, merged before this |
| 72 | // release) is put right by the minute's tick, once. |
| 73 | if _, err := o.inst.DB.Writer().Exec("UPDATE chat_cards SET question = ?, result = ? WHERE id = ?", card.Question, equipo.RunnerCopy("assistant.outcome.option.merge"), card.ID); err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | if err := o.inst.Store.Chat().HousekeepingTick(o.ctx); err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | repaired := o.assistantCard("the go-ahead repaired", func(c wire.AssistantCard) bool { return c.ID == card.ID }) |
| 80 | mergedOutcome(t, repaired) |
| 81 | if repaired.State != wire.CardAnswered || repaired.Revision <= done.Revision { |
| 82 | t.Fatalf("repaired %+v (was %+v)", repaired, done) |
| 83 | } |
| 84 | if err := o.inst.Store.Chat().HousekeepingTick(o.ctx); err != nil { |
| 85 | t.Fatal(err) |
| 86 | } |
| 87 | if again := o.assistantCard("the go-ahead after a second tick", func(c wire.AssistantCard) bool { return c.ID == card.ID }); again.Revision != repaired.Revision { |
| 88 | t.Fatalf("a closed go-ahead moved again: %+v", again) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestContractOutcomeClosesOnceMergedElsewhere(t *testing.T) { |
| 93 | o := newTeamRigAt(t, wire.ProtocolCurrent) |
| 94 | o.sitesOn(wire.ProtocolCurrent, false) |
| 95 | o.ask("Can you make me an app for sharing recipes?") |
| 96 | _, cmd := o.handoff("me1", "an app for sharing recipes", "") |
| 97 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 98 | var id string |
| 99 | o.in(cmd.TurnID, func() { id, _ = o.websiteReady("me", "Recipe sharing app", base, cand, false) }) |
| 100 | card := o.assistantCard("the go-ahead", isMergeOutcome) |
| 101 | if card.State != wire.CardOpen { |
| 102 | t.Fatalf("go-ahead %+v", card) |
| 103 | } |
| 104 | // Merged on the hub by hand: the runner's watch reports it. |
| 105 | if err := o.runner.Send(o.ctx, "me-merged", wire.FrameMerged, wire.MergedReport{TaskID: id, CandidateSHA: cand}); err != nil { |
| 106 | t.Fatal(err) |
| 107 | } |
| 108 | o.wait("done", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageDone }) |
| 109 | var done wire.AssistantCard |
| 110 | o.until("the go-ahead closed", func() bool { |
| 111 | done = o.assistantCard("the go-ahead", func(c wire.AssistantCard) bool { return c.ID == card.ID }) |
| 112 | return done.State != wire.CardOpen |
| 113 | }) |
| 114 | mergedOutcome(t, done) |
| 115 | if !slices.ContainsFunc(o.assistantCards(), func(c wire.AssistantCard) bool { return c.ID == card.ID }) { |
| 116 | t.Fatal("the closed go-ahead left the conversation") |
| 117 | } |
| 118 | if _, err := o.a.Answer(o.ctx, card.ID, wire.ChoiceMerge, done.Revision); err == nil { |
| 119 | t.Fatal("a merged go-ahead took an answer") |
| 120 | } |
| 121 | } |
| 122 | |