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, `