| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "net/url" |
| 5 | "regexp" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | // projectsHello is a runner reaching Woodstar and nine more projects; |
| 13 | // p3's summary is one the catalog clipped mid-word at its limit. |
| 14 | func projectsHello(gen int64) wire.Hello { |
| 15 | h := toolHello(gen) |
| 16 | clipped := strings.Repeat("Reading ", 36) + "proo" // 292 bytes |
| 17 | clipped += strings.Repeat("f", wire.MaxRepoString-len(clipped)) |
| 18 | for i := 0; i < 9; i++ { |
| 19 | repo := "amadan.net/oficina/p" + itoa(i) |
| 20 | h.Repos = append(h.Repos, repo) |
| 21 | info := wire.RepoInfo{Repo: repo, DefaultBranch: "main", Summary: "Project " + itoa(i) + ".", Hints: []string{"go"}, Gate: "make ci"} |
| 22 | if i == 3 { |
| 23 | info.Summary = clipped |
| 24 | } |
| 25 | h.Catalog = append(h.Catalog, info) |
| 26 | } |
| 27 | return h |
| 28 | } |
| 29 | |
| 30 | // stopped records a task in repo and stops its Engineering step: it |
| 31 | // waits on its creator. |
| 32 | func (r *toolRig) stopped(key, repo, title string) string { |
| 33 | r.t.Helper() |
| 34 | id := taskID(r.t, r.call(key+"-rec", wire.ToolRecordTask, map[string]any{"repo": repo, "title": title, "brief": title, "separate": true})) |
| 35 | r.call(key+"-start", wire.ToolStartTask, map[string]string{"task": id, "brief": title}) |
| 36 | _, body := r.expect(wire.FrameDispatch) |
| 37 | d := body.(*wire.DispatchCommand) |
| 38 | r.step(key+"-run", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: strings.Repeat("1", 40)}) |
| 39 | r.step(key+"-fail", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", Termination: true}) |
| 40 | return id |
| 41 | } |
| 42 | |
| 43 | // rowOf is one project's row on the Projects page. |
| 44 | func rowOf(t *testing.T, page, id string) string { |
| 45 | t.Helper() |
| 46 | m := regexp.MustCompile(`(?s)<li equipo-row equipo-prow data-project="` + regexp.QuoteMeta(id) + `"[^>]*>.*?</li>`).FindString(page) |
| 47 | if m == "" { |
| 48 | t.Fatalf("no row for %s", id) |
| 49 | } |
| 50 | return m |
| 51 | } |
| 52 | |
| 53 | // groupOf is the Projects page group a project's row is in. |
| 54 | func groupOf(t *testing.T, page, id string) string { |
| 55 | t.Helper() |
| 56 | at := strings.Index(page, `data-project="`+id+`"`) |
| 57 | if at < 0 { |
| 58 | t.Fatalf("no row for %s", id) |
| 59 | } |
| 60 | groups := regexp.MustCompile(`data-group="(starred|active|quiet)"`).FindAllStringSubmatchIndex(page[:at], -1) |
| 61 | if len(groups) == 0 { |
| 62 | return "" |
| 63 | } |
| 64 | g := groups[len(groups)-1] |
| 65 | return page[g[2]:g[3]] |
| 66 | } |
| 67 | |
| 68 | // newProjectsRig is the owner with Woodstar and nine more projects: two |
| 69 | // of their tasks stopped in Woodstar (waiting on them), one running in |
| 70 | // P1, and P2's repository row missing as for projects added before |
| 71 | // attachments were recorded. ids are the projects by title. |
| 72 | func newProjectsRig(t *testing.T) (*toolRig, map[string]string) { |
| 73 | t.Helper() |
| 74 | r := newToolRig(t) |
| 75 | if err := r.runner.Send(r.ctx, "h-projects", wire.FrameHello, projectsHello(r.runner.Welcome.Generation)); err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | r.wait("catalog", func(c *Conversation) bool { return true }) |
| 79 | ids := map[string]string{} |
| 80 | for i := 0; i < 9; i++ { |
| 81 | res := r.call("add-p"+itoa(i), wire.ToolAddProject, map[string]string{"repo": "oficina/p" + itoa(i)}) |
| 82 | if res.IsError { |
| 83 | t.Fatalf("add p%d: %+v", i, res) |
| 84 | } |
| 85 | } |
| 86 | // Two of the owner's tasks stop in Woodstar: both wait on them. |
| 87 | a := r.stopped("s1", "oficina/sample", "Swipe back does nothing") |
| 88 | r.stopped("s2", "oficina/sample", "Compose sheet clips its bottom inset") |
| 89 | // One in p1, still running: active. |
| 90 | run := taskID(t, r.call("p1-rec", wire.ToolRecordTask, map[string]any{"repo": "oficina/p1", "title": "Timeline jumps when images load", "brief": "Jumps.", "separate": true})) |
| 91 | r.call("p1-start", wire.ToolStartTask, map[string]string{"task": run, "brief": "Keep it still."}) |
| 92 | r.expect(wire.FrameDispatch) |
| 93 | task, _ := r.client.Task(r.ctx, a) |
| 94 | projects, err := r.client.Projects(r.ctx) |
| 95 | if err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | for _, p := range projects.Items { |
| 99 | ids[p.Title] = p.ID |
| 100 | } |
| 101 | woodstar, p1, p2, p3 := ids["Sample"], ids["P1"], ids["P2"], ids["P3"] |
| 102 | if woodstar != task.ProjectID || p1 == "" || p2 == "" || p3 == "" || len(ids) != 10 { |
| 103 | t.Fatalf("projects: %v", ids) |
| 104 | } |
| 105 | // p2's repository row is missing, as for projects added before |
| 106 | // attachments were recorded: it still has its repository. |
| 107 | if _, err := r.inst.DB.Writer().Exec("DELETE FROM chat_project_attachments WHERE project_id = ?", p2); err != nil { |
| 108 | t.Fatal(err) |
| 109 | } |
| 110 | |
| 111 | return r, ids |
| 112 | } |
| 113 | |
| 114 | // TestPagesProjectsOrderAndStars: the Projects page orders itself |
| 115 | // (starred, active, quiet folded), stars on its rules and says so with |
| 116 | // Undo, keeps the person's choice, counts every project's repository, |
| 117 | // cuts descriptions on whole words and filters once there are more than |
| 118 | // eight projects. |
| 119 | func TestPagesProjectsOrderAndStars(t *testing.T) { |
| 120 | r, ids := newProjectsRig(t) |
| 121 | owner := r.inst.Cookie() |
| 122 | woodstar, p1, p2, p3 := ids["Sample"], ids["P1"], ids["P2"], ids["P3"] |
| 123 | page := r.get(owner, "/projects", 200) |
| 124 | // Auto-star: the project with the most tasks waiting on the owner, |
| 125 | // with a receipt and its Undo. |
| 126 | if !strings.Contains(page, "Starred Sample: 2 tasks are waiting on you") || !strings.Contains(page, `<form method="post" action="/projects/`+woodstar+`/star"><input type="hidden" name="starred" value="0"><button equipo-link type="submit" data-star-undo>Undo</button>`) { |
| 127 | t.Fatalf("no receipt with Undo:\n%s", page) |
| 128 | } |
| 129 | if g := groupOf(t, page, woodstar); g != "starred" { |
| 130 | t.Fatalf("Woodstar in %q", g) |
| 131 | } |
| 132 | if row := rowOf(t, page, woodstar); !strings.Contains(row, `aria-pressed="true" data-auto aria-label="Unstar Sample" title="Starred by Equipo: 2 tasks are waiting on you"`) { |
| 133 | t.Fatalf("auto star's hover: %s", row) |
| 134 | } |
| 135 | if g := groupOf(t, page, p1); g != "active" { |
| 136 | t.Fatalf("p1 in %q", g) |
| 137 | } |
| 138 | // The other eight have nothing open and nothing new: folded. |
| 139 | if !strings.Contains(page, `<details equipo-group equipo-fold data-group="quiet"><summary><span>Quiet (8)</span>`) || groupOf(t, page, p2) != "quiet" { |
| 140 | t.Fatalf("quiet group:\n%s", page) |
| 141 | } |
| 142 | if row := rowOf(t, page, p2); !strings.Contains(row, "1 repository") || !strings.Contains(row, "data-no-open>No open tasks<") || strings.Contains(row, "equipo-bar") { |
| 143 | t.Fatalf("p2 row: %s", row) |
| 144 | } |
| 145 | // Every project counts its repository; none says 0. |
| 146 | if strings.Count(page, "1 repository") != 10 || strings.Contains(page, "0 repositories") { |
| 147 | t.Fatalf("repository counts: %d", strings.Count(page, "1 repository")) |
| 148 | } |
| 149 | // Whole words: the clipped summary loses its broken word. |
| 150 | if row := rowOf(t, page, p3); strings.Contains(row, "proo") || !strings.Contains(row, "Reading…</p>") { |
| 151 | t.Fatalf("p3 description: %s", row) |
| 152 | } |
| 153 | // The key names only the states present, in the list's head. |
| 154 | key := regexp.MustCompile(`(?s)<p equipo-key aria-hidden="true">.*?</p>`).FindString(page) |
| 155 | if !strings.Contains(key, "waiting on someone") || !strings.Contains(key, "in progress") || strings.Contains(key, "ready to merge") || strings.Contains(key, ">done") || strings.Contains(page, "equipo-legend") { |
| 156 | t.Fatalf("key: %q", key) |
| 157 | } |
| 158 | // Ten projects: a filter. |
| 159 | if !strings.Contains(page, `<input type="search" name="q" value=""`) { |
| 160 | t.Fatal("no filter above eight projects") |
| 161 | } |
| 162 | found := r.get(owner, "/projects?q=p3", 200) |
| 163 | if !strings.Contains(found, `data-project="`+p3+`"`) || strings.Contains(found, `data-project="`+p2+`"`) || strings.Contains(found, `data-project="`+woodstar+`"`) || !strings.Contains(found, `data-group="quiet" open>`) { |
| 164 | t.Fatalf("filtered:\n%s", found) |
| 165 | } |
| 166 | if none := r.get(owner, "/projects?q=nothing-like-it", 200); !strings.Contains(none, "No project matches “nothing-like-it”.") { |
| 167 | t.Fatal("no match says nothing") |
| 168 | } |
| 169 | |
| 170 | // Undo: the owner's unstar, final while the signal holds. |
| 171 | if status, loc := r.post(owner, "/projects/"+woodstar+"/star", url.Values{"starred": {"0"}}); status != 303 || loc != "/projects" { |
| 172 | t.Fatalf("undo: %d %s", status, loc) |
| 173 | } |
| 174 | for i := 0; i < 2; i++ { |
| 175 | page = r.get(owner, "/projects", 200) |
| 176 | } |
| 177 | if g := groupOf(t, page, woodstar); g != "active" || strings.Contains(page, "Starred Sample") || !strings.Contains(rowOf(t, page, woodstar), `aria-pressed="false" aria-label="Star Sample"`) { |
| 178 | t.Fatalf("after Undo Woodstar is in %q:\n%s", g, page) |
| 179 | } |
| 180 | // The owner stars p1 themselves; the project page says who. |
| 181 | if status, loc := r.post(owner, "/projects/"+p1+"/star", url.Values{"starred": {"1"}, "from": {"project"}}); status != 303 || loc != "/projects/"+p1 { |
| 182 | t.Fatalf("star: %d %s", status, loc) |
| 183 | } |
| 184 | if p := r.get(owner, "/projects/"+p1, 200); !strings.Contains(p, `title="Starred by you"`) { |
| 185 | t.Fatal("project page star") |
| 186 | } |
| 187 | page = r.get(owner, "/projects", 200) |
| 188 | if groupOf(t, page, p1) != "starred" { |
| 189 | t.Fatal("p1 not starred") |
| 190 | } |
| 191 | // Starring is per person: a teammate sees none of it. |
| 192 | if p := r.get(r.memberCookie(), "/projects", 200); strings.Contains(p, "data-starred") { |
| 193 | t.Fatal("a teammate sees the owner's stars") |
| 194 | } |
| 195 | if status, _ := r.post(owner, "/projects/p_nothere/star", url.Values{"starred": {"1"}}); status != 404 { |
| 196 | t.Fatalf("unknown project: %d", status) |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // TestCoordinatorStarsAProject: "Woodstar is a priority" becomes the |
| 201 | // coordinator's star_project, the person's choice made for them: it shows |
| 202 | // who set it, and the rules never undo it. |
| 203 | func TestCoordinatorStarsAProject(t *testing.T) { |
| 204 | r := newToolRig(t) |
| 205 | owner := r.inst.Cookie() |
| 206 | r.call("add", wire.ToolAddProject, map[string]string{"repo": "oficina/sample"}) |
| 207 | // A matching repository title stays untrusted; add an explicit team name. |
| 208 | listed, err := r.client.Projects(r.ctx) |
| 209 | if err != nil { |
| 210 | t.Fatal(err) |
| 211 | } |
| 212 | project := listed.Items[0] |
| 213 | if err := r.client.do(r.ctx, "PUT", "/api/v1/projects/"+project.ID+"/details", wire.EditProject{Revision: project.Revision, Title: "Woodstar", Names: []string{"Woodstar"}}, nil); err != nil { |
| 214 | t.Fatal(err) |
| 215 | } |
| 216 | if res := r.call("bad", wire.ToolStarProject, map[string]any{"project": "Woodstar"}); !res.IsError || res.Code != "invalid" { |
| 217 | t.Fatalf("no starred: %+v", res) |
| 218 | } |
| 219 | if res := r.call("unknown", wire.ToolStarProject, map[string]any{"project": "Nothing", "starred": true}); !res.IsError || res.Code != "unknown_project" { |
| 220 | t.Fatalf("unknown: %+v", res) |
| 221 | } |
| 222 | res := r.call("star", wire.ToolStarProject, map[string]any{"project": "woodstar", "starred": true}) |
| 223 | if res.IsError || res.Code != "starred" || res.Repo != "amadan.net/oficina/sample" { |
| 224 | t.Fatalf("star: %+v", res) |
| 225 | } |
| 226 | projects, _ := r.client.Projects(r.ctx) |
| 227 | id := projects.Items[0].ID |
| 228 | page := r.get(owner, "/projects", 200) |
| 229 | if g := groupOf(t, page, id); g != "starred" || !strings.Contains(rowOf(t, page, id), `title="Starred by your coordinator at your request"`) || strings.Contains(page, "data-star-receipt") { |
| 230 | t.Fatalf("coordinator's star in %q:\n%s", g, page) |
| 231 | } |
| 232 | // By repository name, to unstar: also the person's choice. |
| 233 | if res := r.call("unstar", wire.ToolStarProject, map[string]any{"project": "oficina/sample", "starred": false}); res.Code != "unstarred" { |
| 234 | t.Fatalf("unstar: %+v", res) |
| 235 | } |
| 236 | if page = r.get(owner, "/projects", 200); strings.Contains(page, "data-starred") { |
| 237 | t.Fatal("still starred") |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // TestPagesTaskDecisionsSayWhy: Needs you holds the decision without a |
| 242 | // chip repeating its heading; a change whose checks need a Mac says it |
| 243 | // waits for one and what each answer does; every task offers a quiet |
| 244 | // Ask about this, which opens the conversation with the task named. |
| 245 | func TestPagesTaskDecisionsSayWhy(t *testing.T) { |
| 246 | g := newGallery(t) |
| 247 | a := g.a |
| 248 | owner := a.inst.Cookie() |
| 249 | tasks := a.get(owner, "/tasks", 200) |
| 250 | if strings.Contains(tasks, "Waiting for you") { |
| 251 | t.Fatal("a Needs you row repeats its heading") |
| 252 | } |
| 253 | row := regexp.MustCompile(`(?s)<li equipo-row equipo-trow data-task="` + g.unchecked + `".*?</div>\n</li>`).FindString(tasks) |
| 254 | if !strings.Contains(row, "<span>Waiting for a Mac to run checks</span>") || !strings.Contains(row, `href="/conversation?about=`+g.unchecked+`" data-ask-about>Ask about this</a>`) { |
| 255 | t.Fatalf("unchecked row: %s", row) |
| 256 | } |
| 257 | if n := strings.Count(tasks, "data-ask-about>Ask about this</a>"); n != 7 { |
| 258 | t.Fatalf("Ask about this on %d of 7 tasks", n) |
| 259 | } |
| 260 | needs := a.get(owner, "/tasks?needs=you", 200) |
| 261 | card := regexp.MustCompile(`(?s)<div equipo-decision[^>]*data-decision="` + g.unchecked + `">.*?</div>\n</div>`).FindString(needs) |
| 262 | for _, want := range []string{"Review approved. Checks need a Mac, and no Mac is set up to run them.", "Merge without checks?", |
| 263 | `data-merge-choice="merge">Merge now</button>`, `data-merge-choice="wait_mac">Wait for a Mac</button>`, |
| 264 | "Merge now merges it without checks. Wait for a Mac keeps it ready. Once a Mac is set up, checks run, and if they pass, Equipo merges it by itself.", |
| 265 | `<a equipo-link href="/conversation/settings/machines/add" data-set-up-mac>Set up a Mac</a>`, `href="/conversation?about=` + g.unchecked + `" data-ask-about>Ask about this</a>`} { |
| 266 | if !strings.Contains(card, want) { |
| 267 | t.Fatalf("Mac decision lacks %q:\n%s", want, card) |
| 268 | } |
| 269 | } |
| 270 | stopped := regexp.MustCompile(`(?s)<div equipo-decision[^>]*data-decision="` + g.stopped + `">.*?</div>\n</div>`).FindString(needs) |
| 271 | if !strings.Contains(stopped, "Engineering stopped: the model connection failed, on attempt 1.") || !strings.Contains(stopped, `data-ask-about>Ask about this</a>`) || strings.Contains(stopped, "data-set-up-mac") { |
| 272 | t.Fatalf("stopped decision:\n%s", stopped) |
| 273 | } |
| 274 | // Ask about this: the composer opens with the task named. |
| 275 | conv := a.get(owner, "/conversation?about="+g.unchecked, 200) |
| 276 | if !strings.Contains(conv, `autofocus>About “Swipe back from the edge doesn't work”: </textarea>`) { |
| 277 | t.Fatalf("composer draft:\n%s", regexp.MustCompile(`(?s)<textarea.*?</textarea>`).FindString(conv)) |
| 278 | } |
| 279 | for _, about := range []string{"tknothere", "p_x", "<script>"} { |
| 280 | if c := a.get(owner, "/conversation?about="+url.QueryEscape(about), 200); !strings.Contains(c, `placeholder="`) || strings.Contains(c, "About “") { |
| 281 | t.Fatalf("about=%s drafted something", about) |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |