package thintest import ( "net/url" "regexp" "strings" "testing" "amadan.net/oficina/equipo/internal/wire" ) // projectsHello is a runner reaching Woodstar and nine more projects; // p3's summary is one the catalog clipped mid-word at its limit. func projectsHello(gen int64) wire.Hello { h := toolHello(gen) clipped := strings.Repeat("Reading ", 36) + "proo" // 292 bytes clipped += strings.Repeat("f", wire.MaxRepoString-len(clipped)) for i := 0; i < 9; i++ { repo := "amadan.net/oficina/p" + itoa(i) h.Repos = append(h.Repos, repo) info := wire.RepoInfo{Repo: repo, DefaultBranch: "main", Summary: "Project " + itoa(i) + ".", Hints: []string{"go"}, Gate: "make ci"} if i == 3 { info.Summary = clipped } h.Catalog = append(h.Catalog, info) } return h } // stopped records a task in repo and stops its Engineering step: it // waits on its creator. func (r *toolRig) stopped(key, repo, title string) string { r.t.Helper() id := taskID(r.t, r.call(key+"-rec", wire.ToolRecordTask, map[string]any{"repo": repo, "title": title, "brief": title, "separate": true})) r.call(key+"-start", wire.ToolStartTask, map[string]string{"task": id, "brief": title}) _, body := r.expect(wire.FrameDispatch) d := body.(*wire.DispatchCommand) r.step(key+"-run", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: strings.Repeat("1", 40)}) r.step(key+"-fail", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", Termination: true}) return id } // rowOf is one project's row on the Projects page. func rowOf(t *testing.T, page, id string) string { t.Helper() m := regexp.MustCompile(`(?s)
  • ]*>.*?
  • `).FindString(page) if m == "" { t.Fatalf("no row for %s", id) } return m } // groupOf is the Projects page group a project's row is in. func groupOf(t *testing.T, page, id string) string { t.Helper() at := strings.Index(page, `data-project="`+id+`"`) if at < 0 { t.Fatalf("no row for %s", id) } groups := regexp.MustCompile(`data-group="(starred|active|quiet)"`).FindAllStringSubmatchIndex(page[:at], -1) if len(groups) == 0 { return "" } g := groups[len(groups)-1] return page[g[2]:g[3]] } // newProjectsRig is the owner with Woodstar and nine more projects: two // of their tasks stopped in Woodstar (waiting on them), one running in // P1, and P2's repository row missing as for projects added before // attachments were recorded. ids are the projects by title. func newProjectsRig(t *testing.T) (*toolRig, map[string]string) { t.Helper() r := newToolRig(t) if err := r.runner.Send(r.ctx, "h-projects", wire.FrameHello, projectsHello(r.runner.Welcome.Generation)); err != nil { t.Fatal(err) } r.wait("catalog", func(c *Conversation) bool { return true }) ids := map[string]string{} for i := 0; i < 9; i++ { res := r.call("add-p"+itoa(i), wire.ToolAddProject, map[string]string{"repo": "oficina/p" + itoa(i)}) if res.IsError { t.Fatalf("add p%d: %+v", i, res) } } // Two of the owner's tasks stop in Woodstar: both wait on them. a := r.stopped("s1", "oficina/sample", "Swipe back does nothing") r.stopped("s2", "oficina/sample", "Compose sheet clips its bottom inset") // One in p1, still running: active. 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})) r.call("p1-start", wire.ToolStartTask, map[string]string{"task": run, "brief": "Keep it still."}) r.expect(wire.FrameDispatch) task, _ := r.client.Task(r.ctx, a) projects, err := r.client.Projects(r.ctx) if err != nil { t.Fatal(err) } for _, p := range projects.Items { ids[p.Title] = p.ID } woodstar, p1, p2, p3 := ids["Sample"], ids["P1"], ids["P2"], ids["P3"] if woodstar != task.ProjectID || p1 == "" || p2 == "" || p3 == "" || len(ids) != 10 { t.Fatalf("projects: %v", ids) } // p2's repository row is missing, as for projects added before // attachments were recorded: it still has its repository. if _, err := r.inst.DB.Writer().Exec("DELETE FROM chat_project_attachments WHERE project_id = ?", p2); err != nil { t.Fatal(err) } return r, ids } // TestPagesProjectsOrderAndStars: the Projects page orders itself // (starred, active, quiet folded), stars on its rules and says so with // Undo, keeps the person's choice, counts every project's repository, // cuts descriptions on whole words and filters once there are more than // eight projects. func TestPagesProjectsOrderAndStars(t *testing.T) { r, ids := newProjectsRig(t) owner := r.inst.Cookie() woodstar, p1, p2, p3 := ids["Sample"], ids["P1"], ids["P2"], ids["P3"] page := r.get(owner, "/projects", 200) // Auto-star: the project with the most tasks waiting on the owner, // with a receipt and its Undo. if !strings.Contains(page, "Starred Sample: 2 tasks are waiting on you") || !strings.Contains(page, `
    `) { t.Fatalf("no receipt with Undo:\n%s", page) } if g := groupOf(t, page, woodstar); g != "starred" { t.Fatalf("Woodstar in %q", g) } 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"`) { t.Fatalf("auto star's hover: %s", row) } if g := groupOf(t, page, p1); g != "active" { t.Fatalf("p1 in %q", g) } // The other eight have nothing open and nothing new: folded. if !strings.Contains(page, `
    Quiet (8)`) || groupOf(t, page, p2) != "quiet" { t.Fatalf("quiet group:\n%s", page) } 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") { t.Fatalf("p2 row: %s", row) } // Every project counts its repository; none says 0. if strings.Count(page, "1 repository") != 10 || strings.Contains(page, "0 repositories") { t.Fatalf("repository counts: %d", strings.Count(page, "1 repository")) } // Whole words: the clipped summary loses its broken word. if row := rowOf(t, page, p3); strings.Contains(row, "proo") || !strings.Contains(row, "Reading…

    ") { t.Fatalf("p3 description: %s", row) } // The key names only the states present, in the list's head. key := regexp.MustCompile(`(?s)`).FindString(page) 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") { t.Fatalf("key: %q", key) } // Ten projects: a filter. if !strings.Contains(page, ``) { t.Fatalf("filtered:\n%s", found) } if none := r.get(owner, "/projects?q=nothing-like-it", 200); !strings.Contains(none, "No project matches “nothing-like-it”.") { t.Fatal("no match says nothing") } // Undo: the owner's unstar, final while the signal holds. if status, loc := r.post(owner, "/projects/"+woodstar+"/star", url.Values{"starred": {"0"}}); status != 303 || loc != "/projects" { t.Fatalf("undo: %d %s", status, loc) } for i := 0; i < 2; i++ { page = r.get(owner, "/projects", 200) } 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"`) { t.Fatalf("after Undo Woodstar is in %q:\n%s", g, page) } // The owner stars p1 themselves; the project page says who. if status, loc := r.post(owner, "/projects/"+p1+"/star", url.Values{"starred": {"1"}, "from": {"project"}}); status != 303 || loc != "/projects/"+p1 { t.Fatalf("star: %d %s", status, loc) } if p := r.get(owner, "/projects/"+p1, 200); !strings.Contains(p, `title="Starred by you"`) { t.Fatal("project page star") } page = r.get(owner, "/projects", 200) if groupOf(t, page, p1) != "starred" { t.Fatal("p1 not starred") } // Starring is per person: a teammate sees none of it. if p := r.get(r.memberCookie(), "/projects", 200); strings.Contains(p, "data-starred") { t.Fatal("a teammate sees the owner's stars") } if status, _ := r.post(owner, "/projects/p_nothere/star", url.Values{"starred": {"1"}}); status != 404 { t.Fatalf("unknown project: %d", status) } } // TestCoordinatorStarsAProject: "Woodstar is a priority" becomes the // coordinator's star_project, the person's choice made for them: it shows // who set it, and the rules never undo it. func TestCoordinatorStarsAProject(t *testing.T) { r := newToolRig(t) owner := r.inst.Cookie() r.call("add", wire.ToolAddProject, map[string]string{"repo": "oficina/sample"}) // A matching repository title stays untrusted; add an explicit team name. listed, err := r.client.Projects(r.ctx) if err != nil { t.Fatal(err) } project := listed.Items[0] 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 { t.Fatal(err) } if res := r.call("bad", wire.ToolStarProject, map[string]any{"project": "Woodstar"}); !res.IsError || res.Code != "invalid" { t.Fatalf("no starred: %+v", res) } if res := r.call("unknown", wire.ToolStarProject, map[string]any{"project": "Nothing", "starred": true}); !res.IsError || res.Code != "unknown_project" { t.Fatalf("unknown: %+v", res) } res := r.call("star", wire.ToolStarProject, map[string]any{"project": "woodstar", "starred": true}) if res.IsError || res.Code != "starred" || res.Repo != "amadan.net/oficina/sample" { t.Fatalf("star: %+v", res) } projects, _ := r.client.Projects(r.ctx) id := projects.Items[0].ID page := r.get(owner, "/projects", 200) 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") { t.Fatalf("coordinator's star in %q:\n%s", g, page) } // By repository name, to unstar: also the person's choice. if res := r.call("unstar", wire.ToolStarProject, map[string]any{"project": "oficina/sample", "starred": false}); res.Code != "unstarred" { t.Fatalf("unstar: %+v", res) } if page = r.get(owner, "/projects", 200); strings.Contains(page, "data-starred") { t.Fatal("still starred") } } // TestPagesTaskDecisionsSayWhy: Needs you holds the decision without a // chip repeating its heading; a change whose checks need a Mac says it // waits for one and what each answer does; every task offers a quiet // Ask about this, which opens the conversation with the task named. func TestPagesTaskDecisionsSayWhy(t *testing.T) { g := newGallery(t) a := g.a owner := a.inst.Cookie() tasks := a.get(owner, "/tasks", 200) if strings.Contains(tasks, "Waiting for you") { t.Fatal("a Needs you row repeats its heading") } row := regexp.MustCompile(`(?s)
  • \n
  • `).FindString(tasks) if !strings.Contains(row, "Waiting for a Mac to run checks") || !strings.Contains(row, `href="/conversation?about=`+g.unchecked+`" data-ask-about>Ask about this`) { t.Fatalf("unchecked row: %s", row) } if n := strings.Count(tasks, "data-ask-about>Ask about this"); n != 7 { t.Fatalf("Ask about this on %d of 7 tasks", n) } needs := a.get(owner, "/tasks?needs=you", 200) card := regexp.MustCompile(`(?s)
    ]*data-decision="` + g.unchecked + `">.*?
    \n`).FindString(needs) for _, want := range []string{"Review approved. Checks need a Mac, and no Mac is set up to run them.", "Merge without checks?", `data-merge-choice="merge">Merge now`, `data-merge-choice="wait_mac">Wait for a Mac`, "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.", `Set up a Mac`, `href="/conversation?about=` + g.unchecked + `" data-ask-about>Ask about this`} { if !strings.Contains(card, want) { t.Fatalf("Mac decision lacks %q:\n%s", want, card) } } stopped := regexp.MustCompile(`(?s)
    ]*data-decision="` + g.stopped + `">.*?
    \n`).FindString(needs) if !strings.Contains(stopped, "Engineering stopped: the model connection failed, on attempt 1.") || !strings.Contains(stopped, `data-ask-about>Ask about this`) || strings.Contains(stopped, "data-set-up-mac") { t.Fatalf("stopped decision:\n%s", stopped) } // Ask about this: the composer opens with the task named. conv := a.get(owner, "/conversation?about="+g.unchecked, 200) if !strings.Contains(conv, `autofocus>About “Swipe back from the edge doesn't work”: `) { t.Fatalf("composer draft:\n%s", regexp.MustCompile(`(?s)`).FindString(conv)) } for _, about := range []string{"tknothere", "p_x", "