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 "net/http"
5 "regexp"
6 "slices"
7 "strings"
8 "testing"
9 "time"
10
11 "amadan.net/oficina/equipo/internal/equipo"
12 "amadan.net/oficina/equipo/internal/wire"
13)
14
15// sitesOn says hello again as a primary at protocol that is paired with
16// a CARLOS devbox for previews and, when sites, a current carlos signed in
17// to Carloku owning account oficina (protocol 31: places). With sites, a
18// merge then confirms that place on its place card (placeOnMerge), so a
19// test about the publish card meets one.
20func (r *toolRig) sitesOn(protocol int, sites bool) {
21 r.t.Helper()
22 hello := primaryAt(toolHello(r.runner.Welcome.Generation), protocol)
23 hello.Capabilities.Previews, hello.Capabilities.PreviewHost = true, &wire.PreviewHost{Kind: wire.PreviewHostDevbox, Domain: "docs-t1.example.ts.net"}
24 if sites {
25 hello.Capabilities.Tools = &wire.ToolHealth{Scanner: wire.ToolHealthy, Carlos: wire.CarlosOK, CarlosVersion: "v0.43.3"}
26 hello.Capabilities.Reach = &wire.Reach{Logins: []wire.ReachLogin{{Console: wire.FlagshipConsole, Accounts: []wire.ReachAccount{{Name: "oficina", Sqid: "bcdfg"}, {Name: "paulca", Sqid: "kmnpq"}}}}, Deploys: []wire.ReachDeploy{}}
27 r.placeOnMerge = protocol >= wire.ProtocolPlaces
28 }
29 if err := r.runner.Send(r.ctx, "h-sites-"+itoa(int(time.Now().UnixNano()%1e9)), wire.FrameHello, hello); err != nil {
30 r.t.Fatal(err)
31 }
32 if refused := r.lease(r.runner, "h-sites-ping", wire.LeaseCheck{}); refused != "" {
33 r.t.Fatalf("ping refused %q", refused)
34 }
35}
36
37// websiteReady takes a website task to Review's approval on cand with no
38// checks found (so its merge is asked), Review saying the candidate has a
39// static site config when has; it returns the task and its merge card.
40func (r *toolRig) websiteReady(key, title, base, cand string, has bool) (string, wire.Card) {
41 r.t.Helper()
42 id := r.websiteJudged(key, title, base, cand, has)
43 var card wire.Card
44 r.wait("the merge card", func(c *Conversation) bool {
45 for _, k := range c.Cards {
46 if k.Kind == wire.CardMerge && k.TaskID == id {
47 card = k
48 return true
49 }
50 }
51 return false
52 })
53 return id, card
54}
55
56// websiteJudged is websiteReady up to Review's approval: the task.
57func (r *toolRig) websiteJudged(key, title, base, cand string, has bool) string {
58 r.t.Helper()
59 id, _ := r.pushed(key, title, base, cand)
60 r.expect(wire.FrameWatchMerge)
61 c := r.dispatched(wire.SpecialistChecks, base, cand, 0)
62 r.checks(key+"-c", c, wire.ChecksNone, nil, "", "")
63 rv := r.dispatched(wire.SpecialistReview, base, cand, 0)
64 r.step(key+"-r", wire.StepReport{TaskID: rv.TaskID, StepID: rv.StepID, Attempt: rv.Attempt, State: wire.StepDone, BaseSHA: base, CandidateSHA: cand, Termination: true, Family: "codex", Account: "codex-a", Verdict: wire.VerdictApprove, ReviewLabel: wire.ReviewOtherFamily, Findings: []wire.Finding{}, Summary: "Read it.", HasPreview: has})
65 return id
66}
67
68// mergeFromCard answers the merge card Merge and reports the hub's merge
69// of cand as sha.
70func (r *toolRig) mergeFromCard(key, id, base, cand, sha string) {
71 r.t.Helper()
72 card := r.cardOf(id)
73 if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceMerge, card.Revision); err != nil {
74 r.t.Fatalf("merge %v", err)
75 }
76 m := r.dispatched(wire.SpecialistMerge, base, cand, 0)
77 r.mergedStep(key, m, sha)
78 r.wait("done", func(c *Conversation) bool { return c.Tasks[id].Stage == wire.StageDone })
79}
80
81// publishCard is the task's newest publish card, if any.
82func (r *toolRig) publishCard(id string) (wire.Card, bool) {
83 r.t.Helper()
84 snap, err := r.client.Snapshot(r.ctx)
85 if err != nil {
86 r.t.Fatal(err)
87 }
88 var out wire.Card
89 found := false
90 for _, c := range snap.Cards {
91 if c.Kind == wire.CardPublish && c.TaskID == id {
92 out, found = c, true
93 }
94 }
95 return out, found
96}
97
98func (r *toolRig) publishDispatch() *wire.DispatchCommand {
99 r.t.Helper()
100 for {
101 _, body := r.expect(wire.FrameDispatch)
102 if d := body.(*wire.DispatchCommand); d.Specialist == wire.SpecialistPublish {
103 return d
104 }
105 }
106}
107
108func (r *toolRig) publishReport(key string, d *wire.DispatchCommand, state string, p *wire.PublishResult) {
109 r.t.Helper()
110 r.step(key, wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: d.Attempt, State: state, CandidateSHA: d.CandidateSHA, Termination: state != wire.StepRunning, Findings: []wire.Finding{}, Publish: p})
111}
112
113// TestContractPublishSite: the website's preview reaches its merge card
114// once it is up (the card raised first gets a new revision with it).
115// After Equipo merges it, a separate publish card asks its creator, and
116// only they answer it; Publish places one publish step on their primary
117// for exactly the merged commit and the app derived from the project.
118// Its start and its deploy check are decided for that commit and the
119// export reported built; only a report for this app, this export and a
120// URL on the app's own host counts; the task then carries the public URL
121// and the coordinator says it in one line. Every step is audited.
122func TestContractPublishSite(t *testing.T) {
123 r := newToolRigAt(t, wire.ProtocolCurrent)
124 r.sitesOn(wire.ProtocolCurrent, true)
125 ctx := r.ctx
126 base, cand, merged := strings.Repeat("1", 40), strings.Repeat("2", 40), strings.Repeat("e", 40)
127 id, card := r.websiteReady("w", "Quick website", base, cand, true)
128 if card.PreviewURL != "" {
129 t.Fatalf("a preview link before the preview: %+v", card)
130 }
131 pv := r.previewDispatch(cand)
132 if pv == nil {
133 t.Fatal("no preview")
134 }
135 r.built("w-pv-built", pv, digestA)
136 link := "https://eq-" + id + ".docs-t1.example.ts.net/"
137 r.previewDone("w-pv-done", pv, wire.PreviewResult{Phase: wire.PreviewDone, App: "eq-" + id, URL: link, Digest: digestA, Outcome: wire.PreviewDeployed})
138 after := r.cardOf(id)
139 if after.PreviewURL != link || after.Revision <= card.Revision || after.State != wire.CardOpen {
140 t.Fatalf("merge card after the preview %+v", after)
141 }
142 if k, _ := r.client.Task(ctx, id); k.PreviewURL != r.inst.URL+"/tasks/"+id+"/preview" || k.Site != nil {
143 t.Fatalf("task preview %q site %+v", k.PreviewURL, k.Site)
144 }
145 r.mergeFromCard("w-m", id, base, cand, merged)
146
147 var pub wire.Card
148 r.wait("the publish card", func(c *Conversation) bool {
149 for _, k := range c.Cards {
150 if k.Kind == wire.CardPublish && k.TaskID == id {
151 pub = k
152 return true
153 }
154 }
155 return false
156 })
157 shape(t, "api/card-publish.json", pub)
158 // The app is minted for the project and named on the card.
159 if pub.Site == nil || !regexp.MustCompile(`^site-sample-[a-z2-7]{6}$`).MatchString(pub.Site.App) {
160 t.Fatalf("minted app %+v", pub.Site)
161 }
162 app := pub.Site.App
163 if pub.State != wire.CardOpen || pub.Name != "Quick website" || !slices.Equal(pub.Options, []string{wire.ChoicePublish, wire.ChoiceNotNow}) || pub.Site == nil || *pub.Site != (wire.CardSite{App: app, SHA: merged, State: wire.SiteAsked}) || pub.Question != "Publish Quick website to "+app+" on Carloku in account oficina? This first publish creates the app and a deploy key for it using your CARLOS login on your runner." {
164 t.Fatalf("publish card %+v %+v", pub, pub.Site)
165 }
166 k, _ := r.client.Task(ctx, id)
167 if k.PreviewURL != "" || k.Site == nil || k.Site.State != wire.SiteAsked || k.Site.App != app || k.Site.SHA != merged {
168 t.Fatalf("merged task preview %q site %+v", k.PreviewURL, k.Site)
169 }
170 if r.heldKinds(wire.FrameDispatch) != 0 {
171 t.Fatal("published before anyone answered")
172 }
173 // Only the creator answers; a stale revision or another choice is refused.
174 mate := personToolRig(t, r, "mate@example.com", equipo.RoleOwner)
175 if _, err := mate.client.Answer(ctx, pub.ID, wire.ChoicePublish, pub.Revision); Status(err) != http.StatusNotFound {
176 t.Fatalf("teammate answered: %v", err)
177 }
178 if _, err := r.client.Answer(ctx, pub.ID, wire.ChoicePublish, pub.Revision+1); Status(err) != http.StatusConflict {
179 t.Fatalf("stale answer: %v", err)
180 }
181 if _, err := r.client.Answer(ctx, pub.ID, "deploy", pub.Revision); Status(err) != http.StatusUnprocessableEntity {
182 t.Fatalf("other choice: %v", err)
183 }
184 out, err := r.client.Answer(ctx, pub.ID, wire.ChoicePublish, pub.Revision)
185 if err != nil || out.Card.State != wire.CardAnswered || out.Card.Choice != wire.ChoicePublish || out.Card.Site == nil || out.Card.Site.State != wire.SitePublishing {
186 t.Fatalf("publish %+v %v", out.Card, err)
187 }
188 d := r.publishDispatch()
189 dest := wire.SiteDestination(wire.FlagshipConsole, "bcdfg", "oficina", app)
190 if d.CandidateSHA != merged || d.Person != Owner || d.Repo != "amadan.net/oficina/sample" || d.Publish == nil || d.Attempt != 1 || !strings.HasPrefix(d.Publish.Place, "pl_") || !regexp.MustCompile(`^eq-sample-[a-z2-7]{6}-[a-z2-7]{4}$`).MatchString(d.Publish.DeployName) {
191 t.Fatalf("publish dispatch %+v %+v", d, d.Publish)
192 }
193 if want := (wire.PublishDispatch{App: app, Into: "main", Console: wire.FlagshipConsole, Account: "bcdfg", AccountName: "oficina", Place: d.Publish.Place, PlaceRevision: 1, BindingRevision: 1, Destination: dest, Mode: wire.SiteSetup, DeployName: d.Publish.DeployName}); *d.Publish != want {
194 t.Fatalf("publish dispatch %+v\nwant %+v", *d.Publish, want)
195 }
196 if _, err := r.client.Answer(ctx, pub.ID, wire.ChoicePublish, out.Card.Revision); Status(err) != http.StatusConflict {
197 t.Fatalf("answered twice: %v", err)
198 }
199 if refused := r.lease(r.runner, "w-start", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt}); refused != "" {
200 t.Fatalf("start refused %q", refused)
201 }
202 if refused := r.lease(r.runner, "w-early", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt, Purpose: wire.LeasePurposePublish, Digest: digestA, Destination: dest}); refused != wire.RefusedPublishNotBuilt {
203 t.Fatalf("deploy before built: %q", refused)
204 }
205 r.publishReport("w-built", d, wire.StepRunning, &wire.PublishResult{Phase: wire.PublishBuilt, Digest: digestA, Files: 2, Bytes: 40})
206 if refused := r.lease(r.runner, "w-other", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt, Purpose: wire.LeasePurposePublish, Digest: digestB, Destination: dest}); refused == "" {
207 t.Fatal("another export's deploy allowed")
208 }
209 if refused := r.lease(r.runner, "w-elsewhere", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt, Purpose: wire.LeasePurposePublish, Digest: digestA, Destination: wire.SiteDestination(wire.FlagshipConsole, "kmnpq", "paulca", app)}); refused == "" {
210 t.Fatal("a deploy to another destination allowed")
211 }
212 if refused := r.lease(r.runner, "w-mint-early", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt, Purpose: wire.LeasePurposeMint, Digest: digestA, Destination: dest}); refused == "" {
213 t.Fatal("a setup's mint allowed before its deploy check")
214 }
215 if refused := r.lease(r.runner, "w-preview", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt, Purpose: wire.LeasePurposePreview, Digest: digestA}); refused == "" {
216 t.Fatal("a preview check allowed on a publish step")
217 }
218 if refused := r.lease(r.runner, "w-deploy", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt, Purpose: wire.LeasePurposePublish, Digest: digestA, Destination: dest}); refused != "" {
219 t.Fatalf("deploy refused %q", refused)
220 }
221 if refused := r.lease(r.runner, "w-mint", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt, Purpose: wire.LeasePurposeMint, Digest: digestA, Destination: dest}); refused != "" {
222 t.Fatalf("mint refused %q", refused)
223 }
224 site := "https://" + app + ".bds.oncarlos.com/"
225 for name, p := range map[string]wire.PublishResult{
226 "another app": {Phase: wire.PublishDone, App: "site-other", URL: "https://site-other.bds.oncarlos.com/", Digest: digestA, Outcome: wire.PublishPublished},
227 "another export": {Phase: wire.PublishDone, App: app, URL: site, Digest: digestB, Outcome: wire.PublishPublished},
228 "a foreign URL": {Phase: wire.PublishDone, App: app, URL: "https://evil.example/", Digest: digestA, Outcome: wire.PublishPublished},
229 "no outcome": {Phase: wire.PublishDone, App: app, URL: site, Digest: digestA},
230 } {
231 r.publishReport("w-"+strings.ReplaceAll(name, " ", "-"), d, wire.StepDone, &p)
232 if k, _ := r.client.Task(ctx, id); k.Steps[len(k.Steps)-1].State == wire.StepDone || k.Site.State != wire.SitePublishing {
233 t.Fatalf("%s counted: %+v", name, k.Site)
234 }
235 }
236 r.publishReport("w-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})
237 k, _ = r.client.Task(ctx, id)
238 shape(t, "api/task-published.json", k)
239 last := k.Steps[len(k.Steps)-1]
240 if k.Stage != wire.StageDone || k.Site == nil || k.Site.State != wire.SitePublished || k.Site.URL != site || k.Site.At == nil || last.Specialist != wire.SpecialistPublish || last.Publish == nil || last.Publish.URL != site || last.Publish.Files != 2 {
241 t.Fatalf("published task %+v step %+v", k.Site, last.Publish)
242 }
243 want := "Published [Quick website](/tasks/" + id + ") at " + site + "."
244 r.wait("the coordinator's line", func(c *Conversation) bool {
245 rs := c.Replies()
246 return len(rs) > 0 && rs[len(rs)-1].Text == want
247 })
248 if c, _ := r.publishCard(id); c.Site == nil || c.Site.State != wire.SitePublished || c.Site.URL != site {
249 t.Fatalf("card after publishing %+v", c.Site)
250 }
251 for kind, n := range map[string]int{"publish.requested": 1, "publish.approved": 1, "publish.built": 1, "publish.published": 1} {
252 if got := r.audits(kind); got != n {
253 t.Fatalf("%s audited %d times", kind, got)
254 }
255 }
256 if r.audits("place.proposed") != 1 || r.audits("place.confirmed") != 1 || r.audits("site.create_allowed") != 1 || r.audits("site.deploy_mint_allowed") != 1 {
257 t.Fatal("the place and the site's setup were not audited")
258 }
259 if r.audits("publish.authorize") != 7 {
260 t.Fatalf("authorize audited %d times", r.audits("publish.authorize"))
261 }
262 if page := r.get(r.inst.Cookie(), "/conversation", 200); !strings.Contains(page, `equipo-card="publish"`) || !strings.Contains(page, "Publishing.") {
263 t.Fatalf("conversation page lacks the publish card")
264 }
265}
266
267// TestContractPublishNotOffered: nothing is offered for a merged task
268// whose Review found no static site config, nor on a primary that does
269// not say it can publish sites (paired with none, or below protocol 16).
270func TestContractPublishNotOffered(t *testing.T) {
271 r := newToolRigAt(t, wire.ProtocolCurrent)
272 r.sitesOn(wire.ProtocolCurrent, true)
273 base := strings.Repeat("1", 40)
274 id, _ := r.websiteReady("n", "Fix the README", base, strings.Repeat("2", 40), false)
275 r.mergeFromCard("n-m", id, base, strings.Repeat("2", 40), strings.Repeat("e", 40))
276 r.syncNow("n-sync")
277 if c, ok := r.publishCard(id); ok {
278 t.Fatalf("offered without a site: %+v", c)
279 }
280 off := newToolRigAt(t, wire.ProtocolCurrent)
281 off.sitesOn(wire.ProtocolCurrent, false)
282 id2, _ := off.websiteReady("o", "Quick website", base, strings.Repeat("3", 40), true)
283 off.previewDispatch(strings.Repeat("3", 40))
284 off.mergeFromCard("o-m", id2, base, strings.Repeat("3", 40), strings.Repeat("f", 40))
285 off.syncNow("o-sync")
286 if c, ok := off.publishCard(id2); ok {
287 t.Fatalf("offered on a primary with no site host: %+v", c)
288 }
289 old := newToolRigAt(t, wire.ProtocolReadings)
290 old.sitesOn(wire.ProtocolReadings, false)
291 id3, _ := old.websiteReady("p", "Quick website", base, strings.Repeat("4", 40), true)
292 old.previewDispatch(strings.Repeat("4", 40))
293 old.mergeFromCard("p-m", id3, base, strings.Repeat("4", 40), strings.Repeat("d", 40))
294 old.syncNow("p-sync")
295 if c, ok := old.publishCard(id3); ok {
296 t.Fatalf("offered on a protocol 15 primary: %+v", c)
297 }
298}
299
300// TestContractPublishNotNow: Not now publishes nothing and says so on the
301// task; the creator's publish is then refused by the card.
302func TestContractPublishNotNow(t *testing.T) {
303 r := newToolRigAt(t, wire.ProtocolCurrent)
304 r.sitesOn(wire.ProtocolCurrent, true)
305 base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40)
306 id, _ := r.websiteReady("x", "Quick website", base, cand, true)
307 r.previewDispatch(cand)
308 r.mergeFromCard("x-m", id, base, cand, strings.Repeat("e", 40))
309 var pub wire.Card
310 r.wait("the publish card", func(c *Conversation) bool {
311 for _, k := range c.Cards {
312 if k.Kind == wire.CardPublish && k.TaskID == id {
313 pub = k
314 return true
315 }
316 }
317 return false
318 })
319 out, err := r.client.Answer(r.ctx, pub.ID, wire.ChoiceNotNow, pub.Revision)
320 if err != nil || out.Card.State != wire.CardAnswered || out.Card.Site.State != wire.SiteDeclined {
321 t.Fatalf("not now %+v %v", out.Card, err)
322 }
323 if k, _ := r.client.Task(r.ctx, id); k.Site == nil || k.Site.State != wire.SiteDeclined {
324 t.Fatalf("declined site %+v", k.Site)
325 }
326 if r.heldKinds(wire.FrameDispatch) != 0 || r.audits("publish.declined") != 1 {
327 t.Fatal("not now published, or was not audited")
328 }
329}
330
331// TestContractPublishNameTaken: a publish the runner refused because the
332// account already holds the app's name is a failure the card shows; the
333// creator is told, and not asked again.
334func TestContractPublishNameTaken(t *testing.T) {
335 r := newToolRigAt(t, wire.ProtocolCurrent)
336 r.sitesOn(wire.ProtocolCurrent, true)
337 base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40)
338 id, _ := r.websiteReady("y", "Quick website", base, cand, true)
339 r.previewDispatch(cand)
340 r.mergeFromCard("y-m", id, base, cand, strings.Repeat("e", 40))
341 var pub wire.Card
342 r.wait("the publish card", func(c *Conversation) bool {
343 for _, k := range c.Cards {
344 if k.Kind == wire.CardPublish && k.TaskID == id {
345 pub = k
346 return true
347 }
348 }
349 return false
350 })
351 if _, err := r.client.Answer(r.ctx, pub.ID, wire.ChoicePublish, pub.Revision); err != nil {
352 t.Fatal(err)
353 }
354 d := r.publishDispatch()
355 r.publishReport("y-built", d, wire.StepRunning, &wire.PublishResult{Phase: wire.PublishBuilt, Digest: digestA, Files: 2, Bytes: 40})
356 r.step("y-taken", wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepFailed, Reason: "publish_app_taken", CandidateSHA: d.CandidateSHA, Termination: true, Findings: []wire.Finding{}})
357 c, _ := r.publishCard(id)
358 if c.ID != pub.ID || c.Site == nil || c.Site.State != wire.SiteFailed {
359 t.Fatalf("card after a taken name %+v %+v", c, c.Site)
360 }
361 if k, _ := r.client.Task(r.ctx, id); k.Site == nil || k.Site.State != wire.SiteFailed {
362 t.Fatalf("task site %+v", k.Site)
363 }
364 want := "Could not publish [Quick website](/tasks/" + id + "): Carloku already has an app called " + d.Publish.App + ", so I left it alone."
365 r.wait("the coordinator's line", func(c *Conversation) bool {
366 rs := c.Replies()
367 return len(rs) > 0 && rs[len(rs)-1].Text == want
368 })
369}
370