package thintest import ( "slices" "strings" "testing" "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/wire" ) // A go-ahead whose work has gone in asks nothing any more (Paul's // recipes job, 2026-11-28: merged at 18:52, Base still read "This is // ready: it could not be fully tested. Go ahead?"). Once the task is // merged, its outcome card says so in closed words, whether the person's // own tap merged it or it went in some other way, and no go-ahead is // raised for work that is already in. func mergedOutcome(t *testing.T, c wire.AssistantCard) { t.Helper() if c.State == wire.CardOpen { t.Fatalf("still asking %+v", c) } if c.Question != equipo.RunnerCopy("assistant.outcome.resolved") || strings.Contains(c.Question, "?") { t.Fatalf("question %q, want the closed line", c.Question) } if c.Result != equipo.RunnerCopy("assistant.job.detail.done") { t.Fatalf("result %q, want the finished line", c.Result) } if len(c.Links) != 0 { t.Fatalf("links %+v", c.Links) } } func TestContractOutcomeClosesOnceMergedByTap(t *testing.T) { o := newTeamRigAt(t, wire.ProtocolCurrent) o.sitesOn(wire.ProtocolCurrent, false) o.ask("Can you make me an app for sharing recipes?") _, cmd := o.handoff("mt1", "an app for sharing recipes", "") base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) var id string o.in(cmd.TurnID, func() { id, _ = o.websiteReady("mt", "Recipe sharing app", base, cand, false) }) card := o.assistantCard("the go-ahead", isMergeOutcome) if card.State != wire.CardOpen || card.Question != mergeQuestion("environment_reason") { t.Fatalf("go-ahead %+v", card) } ans, err := o.a.Answer(o.ctx, card.ID, wire.ChoiceMerge, card.Revision) if err != nil || ans.Card.State != wire.CardAnswered { t.Fatalf("answer %+v %v", ans, err) } m := o.dispatched(wire.SpecialistMerge, base, cand, 0) o.mergedStep("mt-m", m, strings.Repeat("e", 40)) o.wait("done", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageDone }) var done wire.AssistantCard o.until("the go-ahead closed", func() bool { done = o.assistantCard("the go-ahead", func(c wire.AssistantCard) bool { return c.ID == card.ID }) return done.Question != card.Question }) mergedOutcome(t, done) if done.State != wire.CardAnswered || done.Revision <= ans.Card.Revision { t.Fatalf("after the merge %+v (answered %+v)", done, ans.Card) } // Tapping the old go-ahead again changes nothing. if _, err := o.a.Answer(o.ctx, card.ID, wire.ChoiceMerge, done.Revision); err == nil { t.Fatal("a merged go-ahead took an answer") } if n := o.rows("SELECT count(*) FROM chat_steps WHERE task_id = ? AND specialist = ?", id, wire.SpecialistMerge); n != 1 { t.Fatalf("%d merge steps", n) } // A card left asking by an older Equipo (Paul's, merged before this // release) is put right by the minute's tick, once. 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 { t.Fatal(err) } if err := o.inst.Store.Chat().HousekeepingTick(o.ctx); err != nil { t.Fatal(err) } repaired := o.assistantCard("the go-ahead repaired", func(c wire.AssistantCard) bool { return c.ID == card.ID }) mergedOutcome(t, repaired) if repaired.State != wire.CardAnswered || repaired.Revision <= done.Revision { t.Fatalf("repaired %+v (was %+v)", repaired, done) } if err := o.inst.Store.Chat().HousekeepingTick(o.ctx); err != nil { t.Fatal(err) } 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 { t.Fatalf("a closed go-ahead moved again: %+v", again) } } func TestContractOutcomeClosesOnceMergedElsewhere(t *testing.T) { o := newTeamRigAt(t, wire.ProtocolCurrent) o.sitesOn(wire.ProtocolCurrent, false) o.ask("Can you make me an app for sharing recipes?") _, cmd := o.handoff("me1", "an app for sharing recipes", "") base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) var id string o.in(cmd.TurnID, func() { id, _ = o.websiteReady("me", "Recipe sharing app", base, cand, false) }) card := o.assistantCard("the go-ahead", isMergeOutcome) if card.State != wire.CardOpen { t.Fatalf("go-ahead %+v", card) } // Merged on the hub by hand: the runner's watch reports it. if err := o.runner.Send(o.ctx, "me-merged", wire.FrameMerged, wire.MergedReport{TaskID: id, CandidateSHA: cand}); err != nil { t.Fatal(err) } o.wait("done", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageDone }) var done wire.AssistantCard o.until("the go-ahead closed", func() bool { done = o.assistantCard("the go-ahead", func(c wire.AssistantCard) bool { return c.ID == card.ID }) return done.State != wire.CardOpen }) mergedOutcome(t, done) if !slices.ContainsFunc(o.assistantCards(), func(c wire.AssistantCard) bool { return c.ID == card.ID }) { t.Fatal("the closed go-ahead left the conversation") } if _, err := o.a.Answer(o.ctx, card.ID, wire.ChoiceMerge, done.Revision); err == nil { t.Fatal("a merged go-ahead took an answer") } }