| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/equipo" |
| 8 | "amadan.net/oficina/equipo/internal/wire" |
| 9 | ) |
| 10 | |
| 11 | // TestContractPlaceCardReachesThePerson (production, 2026-11-27): a merged |
| 12 | // website whose task was made in a job thread. Asked in the person's own |
| 13 | // conversation, publish_site raised the place card in the task's job lane, |
| 14 | // where the conversation never shows it, and the open card then refused |
| 15 | // every later ask. A person's request raises its cards, and says why it |
| 16 | // cannot, where they asked; the card is in Needs you, and the tool's |
| 17 | // answer links to it. A card they cannot see there, or one older than |
| 18 | // the current request, is superseded by a fresh one; one this request |
| 19 | // raised stands. The publish card that follows the tap, and what became |
| 20 | // of the publish, are said there too. |
| 21 | func TestContractPlaceCardReachesThePerson(t *testing.T) { |
| 22 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 23 | r.sitesOn(wire.ProtocolCurrent, false) |
| 24 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 25 | id, _ := r.websiteReady("j", "Quick website", base, cand, true) |
| 26 | r.previewDispatch(cand) |
| 27 | r.mergeFromCard("j-m", id, base, cand, strings.Repeat("e", 40)) |
| 28 | // The task was made in a job thread. |
| 29 | const job = "job:h_abcdefgh" |
| 30 | if err := r.inst.DB.G.Exec("UPDATE chat_tasks SET lane = ? WHERE id = ?", job, id).Error; err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | // Asked in the conversation while the runner still cannot publish: |
| 34 | // why is said there, not in the job's thread. |
| 35 | 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 { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | if res := r.call("j0", wire.ToolPublishSite, map[string]string{"task": id}); res.Code != "not_ready" { |
| 39 | t.Fatalf("publish_site without carlos %+v", res) |
| 40 | } |
| 41 | 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." |
| 42 | r.wait("why, in the conversation", func(c *Conversation) bool { |
| 43 | n := 0 |
| 44 | for _, m := range c.Replies() { |
| 45 | if m.Text == why { |
| 46 | n++ |
| 47 | } |
| 48 | } |
| 49 | return n == 2 |
| 50 | }) |
| 51 | r.next("Publish the site now") |
| 52 | r.reachHello("1", []wire.ReachDeploy{}) |
| 53 | res := r.call("j1", wire.ToolPublishSite, map[string]string{"task": id}) |
| 54 | if res.IsError || res.Code != "proposed" { |
| 55 | t.Fatalf("publish_site %+v", res) |
| 56 | } |
| 57 | place, ok := r.cardKind(wire.CardPlace, id) |
| 58 | if !ok || place.State != wire.CardOpen { |
| 59 | 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) |
| 60 | } |
| 61 | if link := "/conversation?card=" + place.ID + "#card-" + place.ID; !strings.Contains(res.Text, link) { |
| 62 | t.Fatalf("the answer does not link the card: %q", res.Text) |
| 63 | } |
| 64 | if page := r.get(r.inst.Cookie(), "/tasks?needs=you", 200); !strings.Contains(page, "#card-"+place.ID) { |
| 65 | t.Fatal("Needs you lacks the place card") |
| 66 | } |
| 67 | |
| 68 | // Asked again in the same request: the card stands, linked. |
| 69 | cands := placesData(t, r.call("j2", wire.ToolPlaces, map[string]string{"project": "sample"}))["sites"].([]any)[0].(map[string]any)["candidates"].([]any) |
| 70 | again := r.call("j2b", wire.ToolProposePlace, map[string]any{"kind": "site", "project": "sample", "candidate": cands[1].(map[string]any)["id"]}) |
| 71 | if !again.IsError || again.Code != "already_proposed" || !strings.Contains(again.Text, "/conversation?card="+place.ID+"#card-"+place.ID) { |
| 72 | t.Fatalf("asked again %+v", again) |
| 73 | } |
| 74 | if n := r.rows("SELECT count(*) FROM chat_cards WHERE kind = 'place' AND state = 'open'"); n != 1 { |
| 75 | t.Fatalf("%d open place cards", n) |
| 76 | } |
| 77 | |
| 78 | // A card stranded in a job thread (as in production) is superseded, |
| 79 | // even in the request that raised it; the fresh card keeps its task. |
| 80 | if err := r.inst.DB.G.Exec("UPDATE chat_cards SET lane = ? WHERE id = ?", job, place.ID).Error; err != nil { |
| 81 | t.Fatal(err) |
| 82 | } |
| 83 | if _, ok := r.cardKind(wire.CardPlace, id); ok { |
| 84 | t.Fatal("setup: the stranded card still shows") |
| 85 | } |
| 86 | res = r.call("j3", wire.ToolProposePlace, map[string]any{"kind": "site", "project": "sample", "candidate": cands[0].(map[string]any)["id"]}) |
| 87 | fresh, ok := r.cardKind(wire.CardPlace, id) |
| 88 | if res.IsError || res.Code != "proposed" || !ok || fresh.ID == place.ID || fresh.State != wire.CardOpen || !strings.Contains(res.Text, "#card-"+fresh.ID) { |
| 89 | t.Fatalf("not raised afresh: %+v %+v", res, fresh) |
| 90 | } |
| 91 | if r.rows("SELECT count(*) FROM chat_cards WHERE id = ? AND state = ?", place.ID, wire.CardResolved) != 1 || r.audits("place.superseded") != 1 { |
| 92 | t.Fatal("the stranded card was not superseded, or not audited") |
| 93 | } |
| 94 | |
| 95 | // A later request supersedes the one an earlier request raised, though |
| 96 | // it shows: the person asked again. |
| 97 | r.next("Publish the site") |
| 98 | res = r.call("j4", wire.ToolPublishSite, map[string]string{"task": id}) |
| 99 | third, ok := r.cardKind(wire.CardPlace, id) |
| 100 | if res.IsError || res.Code != "proposed" || !ok || third.ID == fresh.ID || !strings.Contains(res.Text, "#card-"+third.ID) { |
| 101 | t.Fatalf("asked in a later request %+v %+v", res, third) |
| 102 | } |
| 103 | if n := r.rows("SELECT count(*) FROM chat_cards WHERE kind = 'place' AND state = 'open'"); n != 1 || r.audits("place.superseded") != 2 { |
| 104 | t.Fatalf("%d open place cards", n) |
| 105 | } |
| 106 | if page := r.get(r.inst.Cookie(), "/conversation", 200); !strings.Contains(page, "Nothing to decide any more.") { |
| 107 | t.Fatal("a superseded card does not say so") |
| 108 | } |
| 109 | |
| 110 | // The person's tap there confirms it, and the publish card follows in |
| 111 | // their conversation, not the job's. |
| 112 | out, err := r.client.Answer(r.ctx, third.ID, third.Options[0], third.Revision) |
| 113 | if err != nil || out.Card.State != wire.CardAnswered { |
| 114 | t.Fatalf("confirm %+v %v", out.Card, err) |
| 115 | } |
| 116 | pub, ok := r.cardKind(wire.CardPublish, id) |
| 117 | if !ok || pub.State != wire.CardOpen { |
| 118 | t.Fatal("the publish card is not in the person's conversation") |
| 119 | } |
| 120 | // Published: said where they tapped Publish. |
| 121 | if _, err := r.client.Answer(r.ctx, pub.ID, wire.ChoicePublish, pub.Revision); err != nil { |
| 122 | t.Fatal(err) |
| 123 | } |
| 124 | d := r.publishDispatch() |
| 125 | if refused := r.lease(r.runner, "jp-start", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt}); refused != "" { |
| 126 | t.Fatalf("start refused %q", refused) |
| 127 | } |
| 128 | site := "https://" + pub.Site.App + ".bds.oncarlos.com/" |
| 129 | r.publishReport("jp-built", d, wire.StepRunning, &wire.PublishResult{Phase: wire.PublishBuilt, Digest: digestA, Files: 2, Bytes: 40}) |
| 130 | for i, purpose := range []string{wire.LeasePurposePublish, wire.LeasePurposeMint} { |
| 131 | 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 != "" { |
| 132 | t.Fatalf("%s refused %q", purpose, refused) |
| 133 | } |
| 134 | } |
| 135 | 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}) |
| 136 | r.wait("published, in the conversation", func(c *Conversation) bool { |
| 137 | for _, m := range c.Replies() { |
| 138 | if strings.Contains(m.Text, site) { |
| 139 | return true |
| 140 | } |
| 141 | } |
| 142 | return false |
| 143 | }) |
| 144 | } |
| 145 | |
| 146 | // TestContractPlaceCardOtherOwnerUntouched (Astra on 8c5c1fbf): one |
| 147 | // owner's request never settles another owner's place card. Bob asking |
| 148 | // for the same project's site gets a card of his own; Alice's card stays |
| 149 | // open at its revision, in her conversation and her Needs you. |
| 150 | func TestContractPlaceCardOtherOwnerUntouched(t *testing.T) { |
| 151 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 152 | r.reachHello("1", []wire.ReachDeploy{}) |
| 153 | if res := r.call("o0", wire.ToolAddProject, map[string]string{"repo": "oficina/sample"}); res.IsError { |
| 154 | t.Fatalf("add_project %+v", res) |
| 155 | } |
| 156 | cands := placesData(t, r.call("o1", wire.ToolPlaces, map[string]string{"project": "sample"}))["sites"].([]any)[0].(map[string]any)["candidates"].([]any) |
| 157 | if res := r.call("o2", wire.ToolProposePlace, map[string]any{"kind": "site", "project": "sample", "candidate": cands[0].(map[string]any)["id"]}); res.Code != "proposed" { |
| 158 | t.Fatalf("alice %+v", res) |
| 159 | } |
| 160 | alice, ok := r.cardKind(wire.CardPlace, "") |
| 161 | if !ok || alice.State != wire.CardOpen { |
| 162 | t.Fatal("no card for alice") |
| 163 | } |
| 164 | bob := personToolRigAt(t, r, "bob@example.com", equipo.RoleOwner, wire.ProtocolCurrent) |
| 165 | bob.reachHello("b1", []wire.ReachDeploy{}) |
| 166 | bc := placesData(t, bob.call("b2", wire.ToolPlaces, map[string]string{"project": "sample"}))["sites"].([]any)[0].(map[string]any)["candidates"].([]any) |
| 167 | res := bob.call("b3", wire.ToolProposePlace, map[string]any{"kind": "site", "project": "sample", "candidate": bc[0].(map[string]any)["id"]}) |
| 168 | if res.IsError || res.Code != "proposed" { |
| 169 | t.Fatalf("bob %+v", res) |
| 170 | } |
| 171 | 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 { |
| 172 | t.Fatal("bob's request settled alice's card") |
| 173 | } |
| 174 | if page := r.get(r.inst.Cookie(), "/tasks?needs=you", 200); !strings.Contains(page, "#card-"+alice.ID) { |
| 175 | t.Fatal("alice's Needs you lost her card") |
| 176 | } |
| 177 | } |
| 178 | |