package thintest import ( "strings" "testing" "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/wire" ) // TestContractPlaceCardReachesThePerson (production, 2026-11-27): a merged // website whose task was made in a job thread. Asked in the person's own // conversation, publish_site raised the place card in the task's job lane, // where the conversation never shows it, and the open card then refused // every later ask. A person's request raises its cards, and says why it // cannot, where they asked; the card is in Needs you, and the tool's // answer links to it. A card they cannot see there, or one older than // the current request, is superseded by a fresh one; one this request // raised stands. The publish card that follows the tap, and what became // of the publish, are said there too. func TestContractPlaceCardReachesThePerson(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) r.sitesOn(wire.ProtocolCurrent, false) base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) id, _ := r.websiteReady("j", "Quick website", base, cand, true) r.previewDispatch(cand) r.mergeFromCard("j-m", id, base, cand, strings.Repeat("e", 40)) // The task was made in a job thread. const job = "job:h_abcdefgh" if err := r.inst.DB.G.Exec("UPDATE chat_tasks SET lane = ? WHERE id = ?", job, id).Error; err != nil { t.Fatal(err) } // Asked in the conversation while the runner still cannot publish: // why is said there, not in the job's thread. if err := r.inst.DB.G.Exec("UPDATE chat_messages SET idempotency_key = idempotency_key || ':before' WHERE idempotency_key LIKE 'publish-why:%'").Error; err != nil { t.Fatal(err) } if res := r.call("j0", wire.ToolPublishSite, map[string]string{"task": id}); res.Code != "not_ready" { t.Fatalf("publish_site without carlos %+v", res) } why := "[Quick website](/tasks/" + id + ") is merged, but I can’t publish it yet: the CARLOS command line tool on your runner is missing or older than v0.43.0." r.wait("why, in the conversation", func(c *Conversation) bool { n := 0 for _, m := range c.Replies() { if m.Text == why { n++ } } return n == 2 }) r.next("Publish the site now") r.reachHello("1", []wire.ReachDeploy{}) res := r.call("j1", wire.ToolPublishSite, map[string]string{"task": id}) if res.IsError || res.Code != "proposed" { t.Fatalf("publish_site %+v", res) } place, ok := r.cardKind(wire.CardPlace, id) if !ok || place.State != wire.CardOpen { t.Fatalf("the place card is not in the person's conversation (lanes %d in %s)", r.rows("SELECT count(*) FROM chat_cards WHERE kind = 'place' AND lane = ?", job), job) } if link := "/conversation?card=" + place.ID + "#card-" + place.ID; !strings.Contains(res.Text, link) { t.Fatalf("the answer does not link the card: %q", res.Text) } if page := r.get(r.inst.Cookie(), "/tasks?needs=you", 200); !strings.Contains(page, "#card-"+place.ID) { t.Fatal("Needs you lacks the place card") } // Asked again in the same request: the card stands, linked. cands := placesData(t, r.call("j2", wire.ToolPlaces, map[string]string{"project": "sample"}))["sites"].([]any)[0].(map[string]any)["candidates"].([]any) again := r.call("j2b", wire.ToolProposePlace, map[string]any{"kind": "site", "project": "sample", "candidate": cands[1].(map[string]any)["id"]}) if !again.IsError || again.Code != "already_proposed" || !strings.Contains(again.Text, "/conversation?card="+place.ID+"#card-"+place.ID) { t.Fatalf("asked again %+v", again) } if n := r.rows("SELECT count(*) FROM chat_cards WHERE kind = 'place' AND state = 'open'"); n != 1 { t.Fatalf("%d open place cards", n) } // A card stranded in a job thread (as in production) is superseded, // even in the request that raised it; the fresh card keeps its task. if err := r.inst.DB.G.Exec("UPDATE chat_cards SET lane = ? WHERE id = ?", job, place.ID).Error; err != nil { t.Fatal(err) } if _, ok := r.cardKind(wire.CardPlace, id); ok { t.Fatal("setup: the stranded card still shows") } res = r.call("j3", wire.ToolProposePlace, map[string]any{"kind": "site", "project": "sample", "candidate": cands[0].(map[string]any)["id"]}) fresh, ok := r.cardKind(wire.CardPlace, id) if res.IsError || res.Code != "proposed" || !ok || fresh.ID == place.ID || fresh.State != wire.CardOpen || !strings.Contains(res.Text, "#card-"+fresh.ID) { t.Fatalf("not raised afresh: %+v %+v", res, fresh) } if r.rows("SELECT count(*) FROM chat_cards WHERE id = ? AND state = ?", place.ID, wire.CardResolved) != 1 || r.audits("place.superseded") != 1 { t.Fatal("the stranded card was not superseded, or not audited") } // A later request supersedes the one an earlier request raised, though // it shows: the person asked again. r.next("Publish the site") res = r.call("j4", wire.ToolPublishSite, map[string]string{"task": id}) third, ok := r.cardKind(wire.CardPlace, id) if res.IsError || res.Code != "proposed" || !ok || third.ID == fresh.ID || !strings.Contains(res.Text, "#card-"+third.ID) { t.Fatalf("asked in a later request %+v %+v", res, third) } if n := r.rows("SELECT count(*) FROM chat_cards WHERE kind = 'place' AND state = 'open'"); n != 1 || r.audits("place.superseded") != 2 { t.Fatalf("%d open place cards", n) } if page := r.get(r.inst.Cookie(), "/conversation", 200); !strings.Contains(page, "Nothing to decide any more.") { t.Fatal("a superseded card does not say so") } // The person's tap there confirms it, and the publish card follows in // their conversation, not the job's. out, err := r.client.Answer(r.ctx, third.ID, third.Options[0], third.Revision) if err != nil || out.Card.State != wire.CardAnswered { t.Fatalf("confirm %+v %v", out.Card, err) } pub, ok := r.cardKind(wire.CardPublish, id) if !ok || pub.State != wire.CardOpen { t.Fatal("the publish card is not in the person's conversation") } // Published: said where they tapped Publish. if _, err := r.client.Answer(r.ctx, pub.ID, wire.ChoicePublish, pub.Revision); err != nil { t.Fatal(err) } d := r.publishDispatch() if refused := r.lease(r.runner, "jp-start", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt}); refused != "" { t.Fatalf("start refused %q", refused) } site := "https://" + pub.Site.App + ".bds.oncarlos.com/" r.publishReport("jp-built", d, wire.StepRunning, &wire.PublishResult{Phase: wire.PublishBuilt, Digest: digestA, Files: 2, Bytes: 40}) for i, purpose := range []string{wire.LeasePurposePublish, wire.LeasePurposeMint} { if refused := r.lease(r.runner, "jp-check-"+itoa(i), wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt, Purpose: purpose, Digest: digestA, Destination: d.Publish.Destination}); refused != "" { t.Fatalf("%s refused %q", purpose, refused) } } r.publishReport("jp-done", d, wire.StepDone, &wire.PublishResult{Phase: wire.PublishDone, App: pub.Site.App, URL: site, Version: "s-eeeeeeeeeeee-x-1", Digest: digestA, Outcome: wire.PublishPublished, DeployHeld: true}) r.wait("published, in the conversation", func(c *Conversation) bool { for _, m := range c.Replies() { if strings.Contains(m.Text, site) { return true } } return false }) } // TestContractPlaceCardOtherOwnerUntouched (Astra on 8c5c1fbf): one // owner's request never settles another owner's place card. Bob asking // for the same project's site gets a card of his own; Alice's card stays // open at its revision, in her conversation and her Needs you. func TestContractPlaceCardOtherOwnerUntouched(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) r.reachHello("1", []wire.ReachDeploy{}) if res := r.call("o0", wire.ToolAddProject, map[string]string{"repo": "oficina/sample"}); res.IsError { t.Fatalf("add_project %+v", res) } cands := placesData(t, r.call("o1", wire.ToolPlaces, map[string]string{"project": "sample"}))["sites"].([]any)[0].(map[string]any)["candidates"].([]any) if res := r.call("o2", wire.ToolProposePlace, map[string]any{"kind": "site", "project": "sample", "candidate": cands[0].(map[string]any)["id"]}); res.Code != "proposed" { t.Fatalf("alice %+v", res) } alice, ok := r.cardKind(wire.CardPlace, "") if !ok || alice.State != wire.CardOpen { t.Fatal("no card for alice") } bob := personToolRigAt(t, r, "bob@example.com", equipo.RoleOwner, wire.ProtocolCurrent) bob.reachHello("b1", []wire.ReachDeploy{}) bc := placesData(t, bob.call("b2", wire.ToolPlaces, map[string]string{"project": "sample"}))["sites"].([]any)[0].(map[string]any)["candidates"].([]any) res := bob.call("b3", wire.ToolProposePlace, map[string]any{"kind": "site", "project": "sample", "candidate": bc[0].(map[string]any)["id"]}) if res.IsError || res.Code != "proposed" { t.Fatalf("bob %+v", res) } if n := r.rows("SELECT count(*) FROM chat_cards WHERE id = ? AND state = ? AND revision = ?", alice.ID, wire.CardOpen, alice.Revision); n != 1 || r.audits("place.superseded") != 0 { t.Fatal("bob's request settled alice's card") } if page := r.get(r.inst.Cookie(), "/tasks?needs=you", 200); !strings.Contains(page, "#card-"+alice.ID) { t.Fatal("alice's Needs you lost her card") } }