oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package thintest
2
3import (
4 "encoding/json"
5 "net/http"
6 "regexp"
7 "slices"
8 "strconv"
9 "strings"
10 "testing"
11
12 "amadan.net/oficina/equipo/internal/equipo"
13 "amadan.net/oficina/equipo/internal/wire"
14)
15
16// reachHello says hello again as a primary at protocol 31 with a current
17// carlos signed in to Carloku (accounts oficina and paulca) and Tito
18// (account tito), holding deploys.
19func (r *toolRig) reachHello(key string, deploys []wire.ReachDeploy) {
20 r.t.Helper()
21 hello := primaryAt(toolHello(r.runner.Welcome.Generation), wire.ProtocolCurrent)
22 hello.Capabilities.Previews, hello.Capabilities.PreviewHost = true, &wire.PreviewHost{Kind: wire.PreviewHostDevbox, Domain: "docs-t1.example.ts.net"}
23 hello.Capabilities.Tools = &wire.ToolHealth{Scanner: wire.ToolHealthy, Carlos: wire.CarlosOK, CarlosVersion: "v0.43.3"}
24 hello.Capabilities.Reach = &wire.Reach{Logins: []wire.ReachLogin{
25 {Console: wire.FlagshipConsole, Accounts: []wire.ReachAccount{{Name: "oficina", Sqid: "bcdfg"}, {Name: "paulca", Sqid: "kmnpq"}}},
26 {Console: wire.TitoConsole, Accounts: []wire.ReachAccount{{Name: "tito", Sqid: "rstuv"}}},
27 }, Deploys: deploys}
28 if err := r.runner.Send(r.ctx, "h-reach-"+key, wire.FrameHello, hello); err != nil {
29 r.t.Fatal(err)
30 }
31 if refused := r.lease(r.runner, "h-reach-ping-"+key, wire.LeaseCheck{}); refused != "" {
32 r.t.Fatalf("ping refused %q", refused)
33 }
34}
35
36// placesData is the quoted data a places tool answered.
37func placesData(t *testing.T, res wire.ToolResult) map[string]any {
38 t.Helper()
39 _, quoted, ok := strings.Cut(res.Text, "): ")
40 raw, err := strconv.Unquote(quoted)
41 var out map[string]any
42 if !ok || err != nil || json.Unmarshal([]byte(raw), &out) != nil {
43 t.Fatalf("places data %q", res.Text)
44 }
45 return out
46}
47
48func (r *toolRig) cardKind(kind, task string) (wire.Card, bool) {
49 r.t.Helper()
50 snap, err := r.client.Snapshot(r.ctx)
51 if err != nil {
52 r.t.Fatal(err)
53 }
54 var out wire.Card
55 found := false
56 for _, c := range snap.Cards {
57 if c.Kind == kind && c.TaskID == task {
58 out, found = c, true
59 }
60 }
61 return out, found
62}
63
64// TestContractPlacesFinishLine is slice 1's finish line (places spec §7):
65// a website merged while the runner could not publish is not offered and
66// says why. Once the runner's carlos can, the coordinator's publish_site
67// raises a place card whose candidates come only from the runner's reach
68// (the tool takes a candidate id, never a console or account); only the
69// owner's own tap confirms one, and confirming publishes nothing. The
70// publish card names the app and the place and says the first publish
71// creates the app and its deploy key with the owner's login; a card whose
72// place moved before the tap is settled and asked again. Publish places a
73// setup step bound to the place, carried in the dispatch; its deploy and
74// mint checks are bound to the destination; the task then shows the
75// confirmed address. The next merged website publishes with the held key,
76// and a binding that moved after the tap withdraws it.
77func TestContractPlacesFinishLine(t *testing.T) {
78 r := newToolRigAt(t, wire.ProtocolCurrent)
79 ctx := r.ctx
80 // Merged while the runner says nothing of its carlos: not offered, and
81 // the task's lane says why.
82 r.sitesOn(wire.ProtocolCurrent, false)
83 base, cand, merged := strings.Repeat("1", 40), strings.Repeat("2", 40), strings.Repeat("e", 40)
84 id, _ := r.websiteReady("f", "Quick website", base, cand, true)
85 r.previewDispatch(cand)
86 r.mergeFromCard("f-m", id, base, cand, merged)
87 want := "[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."
88 r.wait("why it was not offered", func(c *Conversation) bool {
89 for _, m := range c.Replies() {
90 if m.Text == want {
91 return true
92 }
93 }
94 return false
95 })
96 if _, ok := r.cardKind(wire.CardPublish, id); ok {
97 t.Fatal("offered on a runner that cannot publish")
98 }
99 if r.audits("publish.not_offered") != 1 {
100 t.Fatal("not offered, not audited")
101 }
102
103 // The runner's carlos is current and signed in now.
104 r.reachHello("1", []wire.ReachDeploy{})
105 res := r.call("p1", wire.ToolPlaces, map[string]string{"project": "sample"})
106 if res.IsError || res.Code != "places" {
107 t.Fatalf("places %+v", res)
108 }
109 data := placesData(t, res)
110 sites := data["sites"].([]any)
111 line := sites[0].(map[string]any)
112 cands := line["candidates"].([]any)
113 if line["missing"] != "no_place" || len(cands) != 3 {
114 t.Fatalf("places %v", line)
115 }
116 first, paulca := cands[0].(map[string]any), cands[1].(map[string]any)
117 if first["account"] != "oficina" || first["console_label"] != "Carloku" || paulca["account"] != "paulca" || cands[2].(map[string]any)["console_label"] != "Tito" {
118 t.Fatalf("candidates %v", cands)
119 }
120 // A tool can name only a candidate id: not a console, an account or
121 // an app, and not an id the reach does not hold.
122 if res := r.call("p2", wire.ToolProposePlace, map[string]any{"kind": "site", "project": "sample", "candidate": paulca["id"], "console": "console.evil.example"}); !res.IsError || res.Code != "invalid" {
123 t.Fatalf("a console accepted: %+v", res)
124 }
125 if res := r.call("p3", wire.ToolProposePlace, map[string]any{"kind": "site", "project": "sample", "candidate": "pc_000000000000"}); !res.IsError || res.Code != "unknown_candidate" {
126 t.Fatalf("an unknown candidate accepted: %+v", res)
127 }
128 if res := r.call("p4", wire.ToolPublishSite, map[string]string{"task": id, "target": "temporary_site"}); !res.IsError || res.Code != "target_later" {
129 t.Fatalf("a temporary site offered: %+v", res)
130 }
131 res = r.call("p5", wire.ToolPublishSite, map[string]string{"task": id})
132 if res.IsError || res.Code != "proposed" {
133 t.Fatalf("publish_site %+v", res)
134 }
135 place, ok := r.cardKind(wire.CardPlace, id)
136 if !ok || place.State != wire.CardOpen || place.Question != "Publish Sample on Carloku in account oficina?" || place.Place == nil || len(place.Place.Candidates) != 3 || !slices.Equal(place.Options, []string{first["id"].(string), paulca["id"].(string), cands[2].(map[string]any)["id"].(string), wire.ChoiceNotNow}) {
137 t.Fatalf("place card %+v %+v", place, place.Place)
138 }
139 if r.heldKinds(wire.FrameDispatch) != 0 {
140 t.Fatal("proposing dispatched something")
141 }
142 if page := r.get(r.inst.Cookie(), "/conversation", 200); !strings.Contains(page, `equipo-card="place"`) || !strings.Contains(page, `data-place-choice="`+paulca["id"].(string)+`">Carloku, account paulca</button>`) || !strings.Contains(page, `>Tito, account tito</button>`) {
143 t.Fatal("the conversation page lacks the place card's buttons")
144 }
145 // Only the card's owner answers it, at its revision, with one of its
146 // candidates.
147 mate := personToolRig(t, r, "mate@example.com", equipo.RoleOwner)
148 if _, err := mate.client.Answer(ctx, place.ID, paulca["id"].(string), place.Revision); Status(err) != http.StatusNotFound {
149 t.Fatalf("teammate answered: %v", err)
150 }
151 if _, err := r.client.Answer(ctx, place.ID, paulca["id"].(string), place.Revision+1); Status(err) != http.StatusConflict {
152 t.Fatalf("stale answer: %v", err)
153 }
154 if _, err := r.client.Answer(ctx, place.ID, "pc_000000000000", place.Revision); Status(err) != http.StatusUnprocessableEntity {
155 t.Fatalf("another candidate: %v", err)
156 }
157 // An owner no more: refused, and nothing is confirmed.
158 if _, err := r.inst.DB.Writer().Exec("UPDATE members SET role = 'member' WHERE address = ?", Owner); err != nil {
159 t.Fatal(err)
160 }
161 if _, err := r.client.Answer(ctx, place.ID, paulca["id"].(string), place.Revision); Status(err) != http.StatusForbidden || r.rows("SELECT count(*) FROM chat_places") != 0 {
162 t.Fatalf("a member confirmed a place: %v", err)
163 }
164 if _, err := r.inst.DB.Writer().Exec("UPDATE members SET role = 'owner' WHERE address = ?", Owner); err != nil {
165 t.Fatal(err)
166 }
167 out, err := r.client.Answer(ctx, place.ID, paulca["id"].(string), place.Revision)
168 if err != nil || out.Card.State != wire.CardAnswered {
169 t.Fatalf("confirm %+v %v", out.Card, err)
170 }
171 if r.audits("place.confirmed") != 1 || r.heldKinds(wire.FrameDispatch) != 0 {
172 t.Fatal("confirming was not audited, or published")
173 }
174 pub, ok := r.cardKind(wire.CardPublish, id)
175 app := ""
176 if pub.Site != nil {
177 app = pub.Site.App
178 }
179 if !ok || !regexp.MustCompile(`^site-sample-[a-z2-7]{6}$`).MatchString(app) || pub.Question != "Publish Quick website to "+app+" on Carloku in account paulca? This first publish creates the app and a deploy key for it using your CARLOS login on your runner." {
180 t.Fatalf("publish card %+v %+v", pub, pub.Site)
181 }
182
183 // The place moves before the tap: that card is settled and asked again.
184 if err := r.inst.DB.G.Exec("UPDATE chat_places SET revision = revision + 1").Error; err != nil {
185 t.Fatal(err)
186 }
187 if out, err := r.client.Answer(ctx, pub.ID, wire.ChoicePublish, pub.Revision); err != nil || out.Card.State != wire.CardResolved {
188 t.Fatalf("a moved place published: %+v %v", out.Card, err)
189 }
190 if r.heldKinds(wire.FrameDispatch) != 0 {
191 t.Fatal("a stale card dispatched")
192 }
193 again, _ := r.cardKind(wire.CardPublish, id)
194 if again.ID == pub.ID || again.State != wire.CardOpen {
195 t.Fatalf("not asked again: %+v", again)
196 }
197 if _, err := r.client.Answer(ctx, again.ID, wire.ChoicePublish, again.Revision); err != nil {
198 t.Fatal(err)
199 }
200 d := r.publishDispatch()
201 dest := wire.SiteDestination(wire.FlagshipConsole, "kmnpq", "paulca", app)
202 if d.Publish == nil || d.Publish.Console != wire.FlagshipConsole || d.Publish.Account != "kmnpq" || d.Publish.AccountName != "paulca" || d.Publish.App != app || d.Publish.Mode != wire.SiteSetup || d.Publish.Destination != dest || d.Publish.PlaceRevision != 2 || d.CandidateSHA != merged {
203 t.Fatalf("dispatch %+v", d.Publish)
204 }
205 if refused := r.lease(r.runner, "f-start", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt}); refused != "" {
206 t.Fatalf("start refused %q", refused)
207 }
208 r.publishReport("f-built", d, wire.StepRunning, &wire.PublishResult{Phase: wire.PublishBuilt, Digest: digestA, Files: 2, Bytes: 40})
209 if refused := r.lease(r.runner, "f-tito", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt, Purpose: wire.LeasePurposePublish, Digest: digestA, Destination: wire.SiteDestination(wire.TitoConsole, "rstuv", "tito", app)}); refused == "" {
210 t.Fatal("a deploy to another place allowed")
211 }
212 for i, purpose := range []string{wire.LeasePurposePublish, wire.LeasePurposeMint} {
213 if refused := r.lease(r.runner, "f-check-"+itoa(i), wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt, Purpose: purpose, Digest: digestA, Destination: dest}); refused != "" {
214 t.Fatalf("%s refused %q", purpose, refused)
215 }
216 }
217 site := "https://" + app + ".bds.oncarlos.com/"
218 r.publishReport("f-published", d, wire.StepDone, &wire.PublishResult{Phase: wire.PublishDone, App: app, URL: site, Version: "s-eeeeeeeeeeee-x-1", Digest: digestA, Outcome: wire.PublishPublished, DeployHeld: true})
219 k, _ := r.client.Task(ctx, id)
220 if k.Site == nil || k.Site.State != wire.SitePublished || k.Site.URL != site {
221 last := k.Steps[len(k.Steps)-1]
222 t.Fatalf("the task shows %+v; step %s %s %+v", k.Site, last.State, last.Reason, last.Publish)
223 }
224 if r.audits("site.create_allowed") != 1 || r.audits("site.deploy_mint_allowed") != 1 {
225 t.Fatal("the setup's authority uses were not audited")
226 }
227 // site_status says what Equipo recorded, from the runner's reach.
228 r.reachHello("2", []wire.ReachDeploy{{Console: wire.FlagshipConsole, Account: "kmnpq", App: app, Name: d.Publish.DeployName, Fingerprint: "0123456789abcdef"}})
229 status := placesData(t, r.call("p6", wire.ToolSiteStatus, map[string]string{"project": "sample"}))
230 if status["app"] != app || status["account"] != "paulca" || status["console"] != "Carloku" || status["can"] != wire.SitePublish || status["deploy_key"] != "held" || status["last_publish"].(map[string]any)["url"] != site {
231 t.Fatalf("site_status %v", status)
232 }
233
234 // The Places page says it, state first, read-only.
235 page := r.get(r.inst.Cookie(), "/conversation/settings/places", 200)
236 for _, want := range []string{"Sample: " + app + " on Carloku in account paulca.", "Live at " + site + ", published ", "Its deploy key is " + d.Publish.DeployName + "."} {
237 if !strings.Contains(page, want) {
238 t.Fatalf("the Places page lacks %q", want)
239 }
240 }
241 if strings.Contains(page, `action="/conversation/settings/places`) {
242 t.Fatal("the Places page writes")
243 }
244
245 // The next merged website publishes with the held key, no note.
246 cand2, merged2 := strings.Repeat("3", 40), strings.Repeat("f", 40)
247 id2, _ := r.websiteReady("g", "Opening hours", base, cand2, true)
248 r.previewDispatch(cand2)
249 r.mergeFromCard("g-m", id2, base, cand2, merged2)
250 pub2, ok := r.cardKind(wire.CardPublish, id2)
251 if !ok || pub2.Question != "Publish Opening hours to "+app+" on Carloku in account paulca?" {
252 t.Fatalf("second publish card %+v", pub2)
253 }
254 if _, err := r.client.Answer(ctx, pub2.ID, wire.ChoicePublish, pub2.Revision); err != nil {
255 t.Fatal(err)
256 }
257 d2 := r.publishDispatch()
258 if d2.Publish.Mode != wire.SitePublish || d2.Publish.DeployName != "" || d2.Publish.Destination != dest {
259 t.Fatalf("second dispatch %+v", d2.Publish)
260 }
261 // The binding moves after the tap: the step is withdrawn at its start.
262 if err := r.inst.DB.G.Exec("UPDATE chat_project_places SET revision = revision + 1").Error; err != nil {
263 t.Fatal(err)
264 }
265 if refused := r.lease(r.runner, "g-start", wire.LeaseCheck{StepID: d2.StepID, Attempt: d2.Attempt}); refused != "withdrawn" {
266 t.Fatalf("a moved binding started: %q", refused)
267 }
268}
269
270// TestContractPlaceNotNowAndRefusals: Not now confirms nothing; a member's
271// coordinator cannot propose a place or offer a publish; a task that is
272// not a merged website is not offered.
273func TestContractPlaceNotNowAndRefusals(t *testing.T) {
274 r := newToolRigAt(t, wire.ProtocolCurrent)
275 r.sitesOn(wire.ProtocolCurrent, false)
276 base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40)
277 id, _ := r.websiteReady("n", "Quick website", base, cand, true)
278 if res := r.call("n0", wire.ToolPublishSite, map[string]string{"task": id}); !res.IsError || res.Code != "not_website" {
279 t.Fatalf("offered before the merge: %+v", res)
280 }
281 r.previewDispatch(cand)
282 r.mergeFromCard("n-m", id, base, cand, strings.Repeat("e", 40))
283 r.reachHello("1", []wire.ReachDeploy{})
284 if res := r.call("n1", wire.ToolPublishSite, map[string]string{"task": id}); res.Code != "proposed" {
285 t.Fatalf("publish_site %+v", res)
286 }
287 place, _ := r.cardKind(wire.CardPlace, id)
288 if out, err := r.client.Answer(r.ctx, place.ID, wire.ChoiceNotNow, place.Revision); err != nil || out.Card.State != wire.CardAnswered {
289 t.Fatalf("not now %+v %v", out, err)
290 }
291 if n := r.rows("SELECT count(*) FROM chat_places"); n != 0 || r.audits("place.declined") != 1 {
292 t.Fatal("Not now confirmed a place, or was not audited")
293 }
294 if _, ok := r.cardKind(wire.CardPublish, id); ok {
295 t.Fatal("Not now offered a publish")
296 }
297 if page := r.get(r.inst.Cookie(), "/conversation/settings/places", 200); !strings.Contains(page, "No places yet.") {
298 t.Fatal("the empty Places page")
299 }
300}
301
302// TestContractPlaceMintRecovery: a setup that published but holds no key
303// is followed by a mint card for the same merged commit (the recovery
304// route); its tap places a mint step whose mint check records it as the
305// key's step and whose deploy check then holds even when a hello with the
306// new key arrives between them. A publish check before the mint is
307// refused; a setup is never offered again; a mint that found no app at
308// all makes the next card a setup.
309func TestContractPlaceMintRecovery(t *testing.T) {
310 r := newToolRigAt(t, wire.ProtocolCurrent)
311 ctx := r.ctx
312 r.reachHello("1", []wire.ReachDeploy{})
313 base, cand, merged := strings.Repeat("1", 40), strings.Repeat("2", 40), strings.Repeat("e", 40)
314 id, _ := r.websiteReady("m", "Quick website", base, cand, true)
315 r.previewDispatch(cand)
316 r.placeOnMerge = true
317 r.mergeFromCard("m-m", id, base, cand, merged)
318 pub, _ := r.cardKind(wire.CardPublish, id)
319 if _, err := r.client.Answer(ctx, pub.ID, wire.ChoicePublish, pub.Revision); err != nil {
320 t.Fatal(err)
321 }
322 d := r.publishDispatch()
323 dest := d.Publish.Destination
324 r.lease(r.runner, "m-start", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt})
325 r.publishReport("m-built", d, wire.StepRunning, &wire.PublishResult{Phase: wire.PublishBuilt, Digest: digestA, Files: 2, Bytes: 40})
326 if refused := r.lease(r.runner, "m-deploy", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt, Purpose: wire.LeasePurposePublish, Digest: digestA, Destination: dest}); refused != "" {
327 t.Fatalf("deploy refused %q", refused)
328 }
329 // The setup's own mint is allowed, then lost: its record must not
330 // block the recovery's new tap.
331 if refused := r.lease(r.runner, "m-mint", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt, Purpose: wire.LeasePurposeMint, Digest: digestA, Destination: dest}); refused != "" {
332 t.Fatalf("setup mint refused %q", refused)
333 }
334 site := "https://" + d.Publish.App + ".bds.oncarlos.com/"
335 // Published, but the mint was lost: no key held.
336 r.publishReport("m-published", d, wire.StepDone, &wire.PublishResult{Phase: wire.PublishDone, App: d.Publish.App, URL: site, Digest: digestA, Outcome: wire.PublishPublished})
337 r.reachHello("2", []wire.ReachDeploy{{Console: wire.FlagshipConsole, Account: "bcdfg", App: d.Publish.App, Name: d.Publish.DeployName, Lost: true}})
338 mint, ok := r.cardKind(wire.CardPublish, id)
339 if !ok || mint.ID == pub.ID || mint.State != wire.CardOpen || !strings.HasSuffix(mint.Question, "This makes a new deploy key for the site with your CARLOS login on your runner, then publishes with it.") {
340 t.Fatalf("no mint card: %+v", mint)
341 }
342 if k, _ := r.client.Task(ctx, id); k.Site == nil || k.Site.URL != site {
343 t.Fatalf("the published address went: %+v", k.Site)
344 }
345 if _, err := r.client.Answer(ctx, mint.ID, wire.ChoicePublish, mint.Revision); err != nil {
346 t.Fatal(err)
347 }
348 m := r.publishDispatch()
349 if m.Publish.Mode != wire.SiteMint || m.Publish.DeployName == "" || m.Publish.DeployName == d.Publish.DeployName || m.Publish.Destination != dest {
350 t.Fatalf("mint dispatch %+v", m.Publish)
351 }
352 if refused := r.lease(r.runner, "mm-start", wire.LeaseCheck{StepID: m.StepID, Attempt: m.Attempt}); refused != "" {
353 t.Fatalf("mint start refused %q", refused)
354 }
355 r.publishReport("mm-built", m, wire.StepRunning, &wire.PublishResult{Phase: wire.PublishBuilt, Digest: digestA, Files: 2, Bytes: 40})
356 if refused := r.lease(r.runner, "mm-early", wire.LeaseCheck{StepID: m.StepID, Attempt: m.Attempt, Purpose: wire.LeasePurposePublish, Digest: digestA, Destination: dest}); refused == "" {
357 t.Fatal("a mint step deployed before its mint check")
358 }
359 if refused := r.lease(r.runner, "mm-mint", wire.LeaseCheck{StepID: m.StepID, Attempt: m.Attempt, Purpose: wire.LeasePurposeMint, Digest: digestA, Destination: dest}); refused != "" {
360 t.Fatalf("mint refused %q", refused)
361 }
362 // The new key reaches a hello before the deploy check.
363 r.reachHello("3", []wire.ReachDeploy{{Console: wire.FlagshipConsole, Account: "bcdfg", App: m.Publish.App, Name: m.Publish.DeployName, Fingerprint: "0123456789abcdef"}})
364 if refused := r.lease(r.runner, "mm-deploy", wire.LeaseCheck{StepID: m.StepID, Attempt: m.Attempt, Purpose: wire.LeasePurposePublish, Digest: digestA, Destination: dest}); refused != "" {
365 t.Fatalf("deploy after the mint refused %q", refused)
366 }
367 if n := r.rows("SELECT count(*) FROM chat_project_places WHERE deploy_step = ? AND deploy_name = ? AND deploy_minted_by = ?", m.StepID, m.Publish.DeployName, Owner); n != 1 {
368 t.Fatal("the mint is not recorded on the binding")
369 }
370 r.publishReport("mm-published", m, wire.StepDone, &wire.PublishResult{Phase: wire.PublishDone, App: m.Publish.App, URL: site, Digest: digestA, Outcome: wire.PublishPublished, DeployHeld: true})
371 if c, _ := r.cardKind(wire.CardPublish, id); c.State == wire.CardOpen {
372 t.Fatalf("asked again with the key held: %+v", c)
373 }
374}
375
376// TestContractPlaceLegacyPublishCard: a publish card raised before places
377// names no place. Answering it, either way, settles it and grants
378// nothing; publish_site settles it too and asks where the site goes.
379func TestContractPlaceLegacyPublishCard(t *testing.T) {
380 r := newToolRigAt(t, wire.ProtocolCurrent)
381 r.sitesOn(wire.ProtocolCurrent, false)
382 base, cand, merged := strings.Repeat("1", 40), strings.Repeat("2", 40), strings.Repeat("e", 40)
383 id, _ := r.websiteReady("l", "Quick website", base, cand, true)
384 r.previewDispatch(cand)
385 r.mergeFromCard("l-m", id, base, cand, merged)
386 r.reachHello("1", []wire.ReachDeploy{})
387 legacy := func(cid string) {
388 t.Helper()
389 if _, err := r.inst.DB.Writer().Exec("INSERT INTO chat_cards (id, person, kind, state, task_id, project_id, name, question, options, revision, created_at, updated_at, lane) SELECT ?, ?, 'publish', 'open', id, project_id, title, 'Publish Quick website on the web as site-sample-abcdef?', '[\"publish\",\"not_now\"]', 1, datetime('now'), datetime('now'), lane FROM chat_tasks WHERE id = ?", cid, Owner, id); err != nil {
390 t.Fatal(err)
391 }
392 if _, err := r.inst.DB.Writer().Exec("UPDATE chat_tasks SET publish_asked = merged_sha WHERE id = ?", id); err != nil {
393 t.Fatal(err)
394 }
395 }
396 legacy("c_legacy1")
397 if res := r.call("l1", wire.ToolPublishSite, map[string]string{"task": id}); res.Code != "proposed" {
398 t.Fatalf("publish_site past a legacy card: %+v", res)
399 }
400 if n := r.rows("SELECT count(*) FROM chat_cards WHERE id = 'c_legacy1' AND state = ?", wire.CardResolved); n != 1 {
401 t.Fatal("the legacy card is still open")
402 }
403 legacy("c_legacy2")
404 out, err := r.client.Answer(r.ctx, "c_legacy2", wire.ChoicePublish, 1)
405 if err != nil || out.Card.State != wire.CardResolved || r.heldKinds(wire.FrameDispatch) != 0 {
406 t.Fatalf("a legacy Publish: %+v %v", out.Card, err)
407 }
408 if n := r.rows("SELECT count(*) FROM chat_steps WHERE task_id = ? AND specialist = ?", id, wire.SpecialistPublish); n != 0 {
409 t.Fatal("a legacy card placed a publish step")
410 }
411}
412
413// TestContractPlaceSetupAgainAfterMissing: a setup whose deploy failed
414// leaves a creation attempt, so the next card is a mint; the mint finds
415// no app at all (publish_app_missing), which clears the attempt, and the
416// next card is a setup again for the same merged commit.
417func TestContractPlaceSetupAgainAfterMissing(t *testing.T) {
418 r := newToolRigAt(t, wire.ProtocolCurrent)
419 ctx := r.ctx
420 r.reachHello("1", []wire.ReachDeploy{})
421 base, cand, merged := strings.Repeat("1", 40), strings.Repeat("2", 40), strings.Repeat("e", 40)
422 id, _ := r.websiteReady("x", "Quick website", base, cand, true)
423 r.previewDispatch(cand)
424 r.placeOnMerge = true
425 r.mergeFromCard("x-m", id, base, cand, merged)
426 run := func(key string) *wire.DispatchCommand {
427 t.Helper()
428 c, ok := r.cardKind(wire.CardPublish, id)
429 if !ok || c.State != wire.CardOpen {
430 t.Fatalf("%s: no open publish card %+v", key, c)
431 }
432 if _, err := r.client.Answer(ctx, c.ID, wire.ChoicePublish, c.Revision); err != nil {
433 t.Fatal(err)
434 }
435 d := r.publishDispatch()
436 r.lease(r.runner, key+"-start", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt})
437 r.publishReport(key+"-built", d, wire.StepRunning, &wire.PublishResult{Phase: wire.PublishBuilt, Digest: digestA, Files: 2, Bytes: 40})
438 return d
439 }
440 d := run("x1")
441 if d.Publish.Mode != wire.SiteSetup {
442 t.Fatalf("first %+v", d.Publish)
443 }
444 r.lease(r.runner, "x1-deploy", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt, Purpose: wire.LeasePurposePublish, Digest: digestA, Destination: d.Publish.Destination})
445 // The deploy failed: done, unconfirmed, no key.
446 r.publishReport("x1-failed", d, wire.StepDone, &wire.PublishResult{Phase: wire.PublishDone, App: d.Publish.App, Digest: digestA, Outcome: wire.PublishUnconfirmed})
447 m := run("x2")
448 if m.Publish.Mode != wire.SiteMint {
449 t.Fatalf("after a failed setup %+v", m.Publish)
450 }
451 r.step("x2-missing", wire.StepReport{TaskID: m.TaskID, StepID: m.StepID, Attempt: m.Attempt, State: wire.StepFailed, Reason: "publish_app_missing", CandidateSHA: m.CandidateSHA, Termination: true, Findings: []wire.Finding{}})
452 if n := r.rows("SELECT count(*) FROM chat_project_places WHERE app_created_at IS NULL AND app_created_step = ''"); n != 1 {
453 t.Fatal("the creation attempt was not cleared")
454 }
455 again := run("x3")
456 if again.Publish.Mode != wire.SiteSetup || again.CandidateSHA != merged {
457 t.Fatalf("not a setup again %+v", again.Publish)
458 }
459}
460
461// TestContractPlaceRecoveryAfterStoredKey: a setup whose deploy failed,
462// then a mint that stored its key but stopped before its deploy: the key
463// is held, so the next card is a plain publish with it for the same
464// merged commit (a commit that went live is never offered again).
465func TestContractPlaceRecoveryAfterStoredKey(t *testing.T) {
466 r := newToolRigAt(t, wire.ProtocolCurrent)
467 ctx := r.ctx
468 r.reachHello("1", []wire.ReachDeploy{})
469 base, cand, merged := strings.Repeat("1", 40), strings.Repeat("2", 40), strings.Repeat("e", 40)
470 id, _ := r.websiteReady("k", "Quick website", base, cand, true)
471 r.previewDispatch(cand)
472 r.placeOnMerge = true
473 r.mergeFromCard("k-m", id, base, cand, merged)
474 tap := func(key string) *wire.DispatchCommand {
475 t.Helper()
476 c, ok := r.cardKind(wire.CardPublish, id)
477 if !ok || c.State != wire.CardOpen {
478 t.Fatalf("%s: no open publish card %+v", key, c)
479 }
480 if _, err := r.client.Answer(ctx, c.ID, wire.ChoicePublish, c.Revision); err != nil {
481 t.Fatal(err)
482 }
483 d := r.publishDispatch()
484 r.lease(r.runner, key+"-start", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt})
485 r.publishReport(key+"-built", d, wire.StepRunning, &wire.PublishResult{Phase: wire.PublishBuilt, Digest: digestA, Files: 2, Bytes: 40})
486 return d
487 }
488 d := tap("k1")
489 r.lease(r.runner, "k1-deploy", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt, Purpose: wire.LeasePurposePublish, Digest: digestA, Destination: d.Publish.Destination})
490 r.publishReport("k1-failed", d, wire.StepDone, &wire.PublishResult{Phase: wire.PublishDone, App: d.Publish.App, Digest: digestA, Outcome: wire.PublishUnconfirmed})
491 m := tap("k2")
492 if refused := r.lease(r.runner, "k2-mint", wire.LeaseCheck{StepID: m.StepID, Attempt: m.Attempt, Purpose: wire.LeasePurposeMint, Digest: digestA, Destination: m.Publish.Destination}); refused != "" {
493 t.Fatalf("mint refused %q", refused)
494 }
495 // The key is stored; the runner stops before the deploy.
496 r.reachHello("2", []wire.ReachDeploy{{Console: wire.FlagshipConsole, Account: "bcdfg", App: m.Publish.App, Name: m.Publish.DeployName, Fingerprint: "0123456789abcdef"}})
497 r.step("k2-restarted", wire.StepReport{TaskID: m.TaskID, StepID: m.StepID, Attempt: m.Attempt, State: wire.StepFailed, Reason: "publish_mint_failed", CandidateSHA: m.CandidateSHA, Termination: true, Findings: []wire.Finding{}})
498 p := tap("k3")
499 if p.Publish.Mode != wire.SitePublish || p.CandidateSHA != merged {
500 t.Fatalf("not a publish with the held key %+v", p.Publish)
501 }
502 r.lease(r.runner, "k3-deploy", wire.LeaseCheck{StepID: p.StepID, Attempt: p.Attempt, Purpose: wire.LeasePurposePublish, Digest: digestA, Destination: p.Publish.Destination})
503 site := "https://" + p.Publish.App + ".bds.oncarlos.com/"
504 r.publishReport("k3-published", p, wire.StepDone, &wire.PublishResult{Phase: wire.PublishDone, App: p.Publish.App, URL: site, Digest: digestA, Outcome: wire.PublishPublished, DeployHeld: true})
505 if res := r.call("k4", wire.ToolPublishSite, map[string]string{"task": id}); res.Code != "nothing_due" {
506 t.Fatalf("a live commit offered again: %+v", res)
507 }
508}
509