package thintest import ( "encoding/json" "net/http" "regexp" "slices" "strconv" "strings" "testing" "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/wire" ) // reachHello says hello again as a primary at protocol 31 with a current // carlos signed in to Carloku (accounts oficina and paulca) and Tito // (account tito), holding deploys. func (r *toolRig) reachHello(key string, deploys []wire.ReachDeploy) { r.t.Helper() hello := primaryAt(toolHello(r.runner.Welcome.Generation), wire.ProtocolCurrent) hello.Capabilities.Previews, hello.Capabilities.PreviewHost = true, &wire.PreviewHost{Kind: wire.PreviewHostDevbox, Domain: "docs-t1.example.ts.net"} hello.Capabilities.Tools = &wire.ToolHealth{Scanner: wire.ToolHealthy, Carlos: wire.CarlosOK, CarlosVersion: "v0.43.3"} hello.Capabilities.Reach = &wire.Reach{Logins: []wire.ReachLogin{ {Console: wire.FlagshipConsole, Accounts: []wire.ReachAccount{{Name: "oficina", Sqid: "bcdfg"}, {Name: "paulca", Sqid: "kmnpq"}}}, {Console: wire.TitoConsole, Accounts: []wire.ReachAccount{{Name: "tito", Sqid: "rstuv"}}}, }, Deploys: deploys} if err := r.runner.Send(r.ctx, "h-reach-"+key, wire.FrameHello, hello); err != nil { r.t.Fatal(err) } if refused := r.lease(r.runner, "h-reach-ping-"+key, wire.LeaseCheck{}); refused != "" { r.t.Fatalf("ping refused %q", refused) } } // placesData is the quoted data a places tool answered. func placesData(t *testing.T, res wire.ToolResult) map[string]any { t.Helper() _, quoted, ok := strings.Cut(res.Text, "): ") raw, err := strconv.Unquote(quoted) var out map[string]any if !ok || err != nil || json.Unmarshal([]byte(raw), &out) != nil { t.Fatalf("places data %q", res.Text) } return out } func (r *toolRig) cardKind(kind, task string) (wire.Card, bool) { r.t.Helper() snap, err := r.client.Snapshot(r.ctx) if err != nil { r.t.Fatal(err) } var out wire.Card found := false for _, c := range snap.Cards { if c.Kind == kind && c.TaskID == task { out, found = c, true } } return out, found } // TestContractPlacesFinishLine is slice 1's finish line (places spec §7): // a website merged while the runner could not publish is not offered and // says why. Once the runner's carlos can, the coordinator's publish_site // raises a place card whose candidates come only from the runner's reach // (the tool takes a candidate id, never a console or account); only the // owner's own tap confirms one, and confirming publishes nothing. The // publish card names the app and the place and says the first publish // creates the app and its deploy key with the owner's login; a card whose // place moved before the tap is settled and asked again. Publish places a // setup step bound to the place, carried in the dispatch; its deploy and // mint checks are bound to the destination; the task then shows the // confirmed address. The next merged website publishes with the held key, // and a binding that moved after the tap withdraws it. func TestContractPlacesFinishLine(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) ctx := r.ctx // Merged while the runner says nothing of its carlos: not offered, and // the task's lane says why. r.sitesOn(wire.ProtocolCurrent, false) base, cand, merged := strings.Repeat("1", 40), strings.Repeat("2", 40), strings.Repeat("e", 40) id, _ := r.websiteReady("f", "Quick website", base, cand, true) r.previewDispatch(cand) r.mergeFromCard("f-m", id, base, cand, merged) 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." r.wait("why it was not offered", func(c *Conversation) bool { for _, m := range c.Replies() { if m.Text == want { return true } } return false }) if _, ok := r.cardKind(wire.CardPublish, id); ok { t.Fatal("offered on a runner that cannot publish") } if r.audits("publish.not_offered") != 1 { t.Fatal("not offered, not audited") } // The runner's carlos is current and signed in now. r.reachHello("1", []wire.ReachDeploy{}) res := r.call("p1", wire.ToolPlaces, map[string]string{"project": "sample"}) if res.IsError || res.Code != "places" { t.Fatalf("places %+v", res) } data := placesData(t, res) sites := data["sites"].([]any) line := sites[0].(map[string]any) cands := line["candidates"].([]any) if line["missing"] != "no_place" || len(cands) != 3 { t.Fatalf("places %v", line) } first, paulca := cands[0].(map[string]any), cands[1].(map[string]any) if first["account"] != "oficina" || first["console_label"] != "Carloku" || paulca["account"] != "paulca" || cands[2].(map[string]any)["console_label"] != "Tito" { t.Fatalf("candidates %v", cands) } // A tool can name only a candidate id: not a console, an account or // an app, and not an id the reach does not hold. 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" { t.Fatalf("a console accepted: %+v", res) } if res := r.call("p3", wire.ToolProposePlace, map[string]any{"kind": "site", "project": "sample", "candidate": "pc_000000000000"}); !res.IsError || res.Code != "unknown_candidate" { t.Fatalf("an unknown candidate accepted: %+v", res) } if res := r.call("p4", wire.ToolPublishSite, map[string]string{"task": id, "target": "temporary_site"}); !res.IsError || res.Code != "target_later" { t.Fatalf("a temporary site offered: %+v", res) } res = r.call("p5", 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 || 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}) { t.Fatalf("place card %+v %+v", place, place.Place) } if r.heldKinds(wire.FrameDispatch) != 0 { t.Fatal("proposing dispatched something") } 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`) || !strings.Contains(page, `>Tito, account tito`) { t.Fatal("the conversation page lacks the place card's buttons") } // Only the card's owner answers it, at its revision, with one of its // candidates. mate := personToolRig(t, r, "mate@example.com", equipo.RoleOwner) if _, err := mate.client.Answer(ctx, place.ID, paulca["id"].(string), place.Revision); Status(err) != http.StatusNotFound { t.Fatalf("teammate answered: %v", err) } if _, err := r.client.Answer(ctx, place.ID, paulca["id"].(string), place.Revision+1); Status(err) != http.StatusConflict { t.Fatalf("stale answer: %v", err) } if _, err := r.client.Answer(ctx, place.ID, "pc_000000000000", place.Revision); Status(err) != http.StatusUnprocessableEntity { t.Fatalf("another candidate: %v", err) } // An owner no more: refused, and nothing is confirmed. if _, err := r.inst.DB.Writer().Exec("UPDATE members SET role = 'member' WHERE address = ?", Owner); err != nil { t.Fatal(err) } 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 { t.Fatalf("a member confirmed a place: %v", err) } if _, err := r.inst.DB.Writer().Exec("UPDATE members SET role = 'owner' WHERE address = ?", Owner); err != nil { t.Fatal(err) } out, err := r.client.Answer(ctx, place.ID, paulca["id"].(string), place.Revision) if err != nil || out.Card.State != wire.CardAnswered { t.Fatalf("confirm %+v %v", out.Card, err) } if r.audits("place.confirmed") != 1 || r.heldKinds(wire.FrameDispatch) != 0 { t.Fatal("confirming was not audited, or published") } pub, ok := r.cardKind(wire.CardPublish, id) app := "" if pub.Site != nil { app = pub.Site.App } 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." { t.Fatalf("publish card %+v %+v", pub, pub.Site) } // The place moves before the tap: that card is settled and asked again. if err := r.inst.DB.G.Exec("UPDATE chat_places SET revision = revision + 1").Error; err != nil { t.Fatal(err) } if out, err := r.client.Answer(ctx, pub.ID, wire.ChoicePublish, pub.Revision); err != nil || out.Card.State != wire.CardResolved { t.Fatalf("a moved place published: %+v %v", out.Card, err) } if r.heldKinds(wire.FrameDispatch) != 0 { t.Fatal("a stale card dispatched") } again, _ := r.cardKind(wire.CardPublish, id) if again.ID == pub.ID || again.State != wire.CardOpen { t.Fatalf("not asked again: %+v", again) } if _, err := r.client.Answer(ctx, again.ID, wire.ChoicePublish, again.Revision); err != nil { t.Fatal(err) } d := r.publishDispatch() dest := wire.SiteDestination(wire.FlagshipConsole, "kmnpq", "paulca", app) 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 { t.Fatalf("dispatch %+v", d.Publish) } if refused := r.lease(r.runner, "f-start", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt}); refused != "" { t.Fatalf("start refused %q", refused) } r.publishReport("f-built", d, wire.StepRunning, &wire.PublishResult{Phase: wire.PublishBuilt, Digest: digestA, Files: 2, Bytes: 40}) 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 == "" { t.Fatal("a deploy to another place allowed") } for i, purpose := range []string{wire.LeasePurposePublish, wire.LeasePurposeMint} { 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 != "" { t.Fatalf("%s refused %q", purpose, refused) } } site := "https://" + app + ".bds.oncarlos.com/" 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}) k, _ := r.client.Task(ctx, id) if k.Site == nil || k.Site.State != wire.SitePublished || k.Site.URL != site { last := k.Steps[len(k.Steps)-1] t.Fatalf("the task shows %+v; step %s %s %+v", k.Site, last.State, last.Reason, last.Publish) } if r.audits("site.create_allowed") != 1 || r.audits("site.deploy_mint_allowed") != 1 { t.Fatal("the setup's authority uses were not audited") } // site_status says what Equipo recorded, from the runner's reach. r.reachHello("2", []wire.ReachDeploy{{Console: wire.FlagshipConsole, Account: "kmnpq", App: app, Name: d.Publish.DeployName, Fingerprint: "0123456789abcdef"}}) status := placesData(t, r.call("p6", wire.ToolSiteStatus, map[string]string{"project": "sample"})) 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 { t.Fatalf("site_status %v", status) } // The Places page says it, state first, read-only. page := r.get(r.inst.Cookie(), "/conversation/settings/places", 200) for _, want := range []string{"Sample: " + app + " on Carloku in account paulca.", "Live at " + site + ", published ", "Its deploy key is " + d.Publish.DeployName + "."} { if !strings.Contains(page, want) { t.Fatalf("the Places page lacks %q", want) } } if strings.Contains(page, `action="/conversation/settings/places`) { t.Fatal("the Places page writes") } // The next merged website publishes with the held key, no note. cand2, merged2 := strings.Repeat("3", 40), strings.Repeat("f", 40) id2, _ := r.websiteReady("g", "Opening hours", base, cand2, true) r.previewDispatch(cand2) r.mergeFromCard("g-m", id2, base, cand2, merged2) pub2, ok := r.cardKind(wire.CardPublish, id2) if !ok || pub2.Question != "Publish Opening hours to "+app+" on Carloku in account paulca?" { t.Fatalf("second publish card %+v", pub2) } if _, err := r.client.Answer(ctx, pub2.ID, wire.ChoicePublish, pub2.Revision); err != nil { t.Fatal(err) } d2 := r.publishDispatch() if d2.Publish.Mode != wire.SitePublish || d2.Publish.DeployName != "" || d2.Publish.Destination != dest { t.Fatalf("second dispatch %+v", d2.Publish) } // The binding moves after the tap: the step is withdrawn at its start. if err := r.inst.DB.G.Exec("UPDATE chat_project_places SET revision = revision + 1").Error; err != nil { t.Fatal(err) } if refused := r.lease(r.runner, "g-start", wire.LeaseCheck{StepID: d2.StepID, Attempt: d2.Attempt}); refused != "withdrawn" { t.Fatalf("a moved binding started: %q", refused) } } // TestContractPlaceNotNowAndRefusals: Not now confirms nothing; a member's // coordinator cannot propose a place or offer a publish; a task that is // not a merged website is not offered. func TestContractPlaceNotNowAndRefusals(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("n", "Quick website", base, cand, true) if res := r.call("n0", wire.ToolPublishSite, map[string]string{"task": id}); !res.IsError || res.Code != "not_website" { t.Fatalf("offered before the merge: %+v", res) } r.previewDispatch(cand) r.mergeFromCard("n-m", id, base, cand, strings.Repeat("e", 40)) r.reachHello("1", []wire.ReachDeploy{}) if res := r.call("n1", wire.ToolPublishSite, map[string]string{"task": id}); res.Code != "proposed" { t.Fatalf("publish_site %+v", res) } place, _ := r.cardKind(wire.CardPlace, id) if out, err := r.client.Answer(r.ctx, place.ID, wire.ChoiceNotNow, place.Revision); err != nil || out.Card.State != wire.CardAnswered { t.Fatalf("not now %+v %v", out, err) } if n := r.rows("SELECT count(*) FROM chat_places"); n != 0 || r.audits("place.declined") != 1 { t.Fatal("Not now confirmed a place, or was not audited") } if _, ok := r.cardKind(wire.CardPublish, id); ok { t.Fatal("Not now offered a publish") } if page := r.get(r.inst.Cookie(), "/conversation/settings/places", 200); !strings.Contains(page, "No places yet.") { t.Fatal("the empty Places page") } } // TestContractPlaceMintRecovery: a setup that published but holds no key // is followed by a mint card for the same merged commit (the recovery // route); its tap places a mint step whose mint check records it as the // key's step and whose deploy check then holds even when a hello with the // new key arrives between them. A publish check before the mint is // refused; a setup is never offered again; a mint that found no app at // all makes the next card a setup. func TestContractPlaceMintRecovery(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) ctx := r.ctx r.reachHello("1", []wire.ReachDeploy{}) base, cand, merged := strings.Repeat("1", 40), strings.Repeat("2", 40), strings.Repeat("e", 40) id, _ := r.websiteReady("m", "Quick website", base, cand, true) r.previewDispatch(cand) r.placeOnMerge = true r.mergeFromCard("m-m", id, base, cand, merged) pub, _ := r.cardKind(wire.CardPublish, id) if _, err := r.client.Answer(ctx, pub.ID, wire.ChoicePublish, pub.Revision); err != nil { t.Fatal(err) } d := r.publishDispatch() dest := d.Publish.Destination r.lease(r.runner, "m-start", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt}) r.publishReport("m-built", d, wire.StepRunning, &wire.PublishResult{Phase: wire.PublishBuilt, Digest: digestA, Files: 2, Bytes: 40}) if refused := r.lease(r.runner, "m-deploy", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt, Purpose: wire.LeasePurposePublish, Digest: digestA, Destination: dest}); refused != "" { t.Fatalf("deploy refused %q", refused) } // The setup's own mint is allowed, then lost: its record must not // block the recovery's new tap. if refused := r.lease(r.runner, "m-mint", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt, Purpose: wire.LeasePurposeMint, Digest: digestA, Destination: dest}); refused != "" { t.Fatalf("setup mint refused %q", refused) } site := "https://" + d.Publish.App + ".bds.oncarlos.com/" // Published, but the mint was lost: no key held. r.publishReport("m-published", d, wire.StepDone, &wire.PublishResult{Phase: wire.PublishDone, App: d.Publish.App, URL: site, Digest: digestA, Outcome: wire.PublishPublished}) r.reachHello("2", []wire.ReachDeploy{{Console: wire.FlagshipConsole, Account: "bcdfg", App: d.Publish.App, Name: d.Publish.DeployName, Lost: true}}) mint, ok := r.cardKind(wire.CardPublish, id) 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.") { t.Fatalf("no mint card: %+v", mint) } if k, _ := r.client.Task(ctx, id); k.Site == nil || k.Site.URL != site { t.Fatalf("the published address went: %+v", k.Site) } if _, err := r.client.Answer(ctx, mint.ID, wire.ChoicePublish, mint.Revision); err != nil { t.Fatal(err) } m := r.publishDispatch() if m.Publish.Mode != wire.SiteMint || m.Publish.DeployName == "" || m.Publish.DeployName == d.Publish.DeployName || m.Publish.Destination != dest { t.Fatalf("mint dispatch %+v", m.Publish) } if refused := r.lease(r.runner, "mm-start", wire.LeaseCheck{StepID: m.StepID, Attempt: m.Attempt}); refused != "" { t.Fatalf("mint start refused %q", refused) } r.publishReport("mm-built", m, wire.StepRunning, &wire.PublishResult{Phase: wire.PublishBuilt, Digest: digestA, Files: 2, Bytes: 40}) if refused := r.lease(r.runner, "mm-early", wire.LeaseCheck{StepID: m.StepID, Attempt: m.Attempt, Purpose: wire.LeasePurposePublish, Digest: digestA, Destination: dest}); refused == "" { t.Fatal("a mint step deployed before its mint check") } if refused := r.lease(r.runner, "mm-mint", wire.LeaseCheck{StepID: m.StepID, Attempt: m.Attempt, Purpose: wire.LeasePurposeMint, Digest: digestA, Destination: dest}); refused != "" { t.Fatalf("mint refused %q", refused) } // The new key reaches a hello before the deploy check. r.reachHello("3", []wire.ReachDeploy{{Console: wire.FlagshipConsole, Account: "bcdfg", App: m.Publish.App, Name: m.Publish.DeployName, Fingerprint: "0123456789abcdef"}}) if refused := r.lease(r.runner, "mm-deploy", wire.LeaseCheck{StepID: m.StepID, Attempt: m.Attempt, Purpose: wire.LeasePurposePublish, Digest: digestA, Destination: dest}); refused != "" { t.Fatalf("deploy after the mint refused %q", refused) } 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 { t.Fatal("the mint is not recorded on the binding") } 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}) if c, _ := r.cardKind(wire.CardPublish, id); c.State == wire.CardOpen { t.Fatalf("asked again with the key held: %+v", c) } } // TestContractPlaceLegacyPublishCard: a publish card raised before places // names no place. Answering it, either way, settles it and grants // nothing; publish_site settles it too and asks where the site goes. func TestContractPlaceLegacyPublishCard(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) r.sitesOn(wire.ProtocolCurrent, false) base, cand, merged := strings.Repeat("1", 40), strings.Repeat("2", 40), strings.Repeat("e", 40) id, _ := r.websiteReady("l", "Quick website", base, cand, true) r.previewDispatch(cand) r.mergeFromCard("l-m", id, base, cand, merged) r.reachHello("1", []wire.ReachDeploy{}) legacy := func(cid string) { t.Helper() 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 { t.Fatal(err) } if _, err := r.inst.DB.Writer().Exec("UPDATE chat_tasks SET publish_asked = merged_sha WHERE id = ?", id); err != nil { t.Fatal(err) } } legacy("c_legacy1") if res := r.call("l1", wire.ToolPublishSite, map[string]string{"task": id}); res.Code != "proposed" { t.Fatalf("publish_site past a legacy card: %+v", res) } if n := r.rows("SELECT count(*) FROM chat_cards WHERE id = 'c_legacy1' AND state = ?", wire.CardResolved); n != 1 { t.Fatal("the legacy card is still open") } legacy("c_legacy2") out, err := r.client.Answer(r.ctx, "c_legacy2", wire.ChoicePublish, 1) if err != nil || out.Card.State != wire.CardResolved || r.heldKinds(wire.FrameDispatch) != 0 { t.Fatalf("a legacy Publish: %+v %v", out.Card, err) } if n := r.rows("SELECT count(*) FROM chat_steps WHERE task_id = ? AND specialist = ?", id, wire.SpecialistPublish); n != 0 { t.Fatal("a legacy card placed a publish step") } } // TestContractPlaceSetupAgainAfterMissing: a setup whose deploy failed // leaves a creation attempt, so the next card is a mint; the mint finds // no app at all (publish_app_missing), which clears the attempt, and the // next card is a setup again for the same merged commit. func TestContractPlaceSetupAgainAfterMissing(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) ctx := r.ctx r.reachHello("1", []wire.ReachDeploy{}) base, cand, merged := strings.Repeat("1", 40), strings.Repeat("2", 40), strings.Repeat("e", 40) id, _ := r.websiteReady("x", "Quick website", base, cand, true) r.previewDispatch(cand) r.placeOnMerge = true r.mergeFromCard("x-m", id, base, cand, merged) run := func(key string) *wire.DispatchCommand { t.Helper() c, ok := r.cardKind(wire.CardPublish, id) if !ok || c.State != wire.CardOpen { t.Fatalf("%s: no open publish card %+v", key, c) } if _, err := r.client.Answer(ctx, c.ID, wire.ChoicePublish, c.Revision); err != nil { t.Fatal(err) } d := r.publishDispatch() r.lease(r.runner, key+"-start", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt}) r.publishReport(key+"-built", d, wire.StepRunning, &wire.PublishResult{Phase: wire.PublishBuilt, Digest: digestA, Files: 2, Bytes: 40}) return d } d := run("x1") if d.Publish.Mode != wire.SiteSetup { t.Fatalf("first %+v", d.Publish) } r.lease(r.runner, "x1-deploy", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt, Purpose: wire.LeasePurposePublish, Digest: digestA, Destination: d.Publish.Destination}) // The deploy failed: done, unconfirmed, no key. r.publishReport("x1-failed", d, wire.StepDone, &wire.PublishResult{Phase: wire.PublishDone, App: d.Publish.App, Digest: digestA, Outcome: wire.PublishUnconfirmed}) m := run("x2") if m.Publish.Mode != wire.SiteMint { t.Fatalf("after a failed setup %+v", m.Publish) } 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{}}) if n := r.rows("SELECT count(*) FROM chat_project_places WHERE app_created_at IS NULL AND app_created_step = ''"); n != 1 { t.Fatal("the creation attempt was not cleared") } again := run("x3") if again.Publish.Mode != wire.SiteSetup || again.CandidateSHA != merged { t.Fatalf("not a setup again %+v", again.Publish) } } // TestContractPlaceRecoveryAfterStoredKey: a setup whose deploy failed, // then a mint that stored its key but stopped before its deploy: the key // is held, so the next card is a plain publish with it for the same // merged commit (a commit that went live is never offered again). func TestContractPlaceRecoveryAfterStoredKey(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) ctx := r.ctx r.reachHello("1", []wire.ReachDeploy{}) base, cand, merged := strings.Repeat("1", 40), strings.Repeat("2", 40), strings.Repeat("e", 40) id, _ := r.websiteReady("k", "Quick website", base, cand, true) r.previewDispatch(cand) r.placeOnMerge = true r.mergeFromCard("k-m", id, base, cand, merged) tap := func(key string) *wire.DispatchCommand { t.Helper() c, ok := r.cardKind(wire.CardPublish, id) if !ok || c.State != wire.CardOpen { t.Fatalf("%s: no open publish card %+v", key, c) } if _, err := r.client.Answer(ctx, c.ID, wire.ChoicePublish, c.Revision); err != nil { t.Fatal(err) } d := r.publishDispatch() r.lease(r.runner, key+"-start", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt}) r.publishReport(key+"-built", d, wire.StepRunning, &wire.PublishResult{Phase: wire.PublishBuilt, Digest: digestA, Files: 2, Bytes: 40}) return d } d := tap("k1") r.lease(r.runner, "k1-deploy", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt, Purpose: wire.LeasePurposePublish, Digest: digestA, Destination: d.Publish.Destination}) r.publishReport("k1-failed", d, wire.StepDone, &wire.PublishResult{Phase: wire.PublishDone, App: d.Publish.App, Digest: digestA, Outcome: wire.PublishUnconfirmed}) m := tap("k2") 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 != "" { t.Fatalf("mint refused %q", refused) } // The key is stored; the runner stops before the deploy. r.reachHello("2", []wire.ReachDeploy{{Console: wire.FlagshipConsole, Account: "bcdfg", App: m.Publish.App, Name: m.Publish.DeployName, Fingerprint: "0123456789abcdef"}}) 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{}}) p := tap("k3") if p.Publish.Mode != wire.SitePublish || p.CandidateSHA != merged { t.Fatalf("not a publish with the held key %+v", p.Publish) } r.lease(r.runner, "k3-deploy", wire.LeaseCheck{StepID: p.StepID, Attempt: p.Attempt, Purpose: wire.LeasePurposePublish, Digest: digestA, Destination: p.Publish.Destination}) site := "https://" + p.Publish.App + ".bds.oncarlos.com/" 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}) if res := r.call("k4", wire.ToolPublishSite, map[string]string{"task": id}); res.Code != "nothing_due" { t.Fatalf("a live commit offered again: %+v", res) } }