package thintest import ( "encoding/json" "net/url" "strings" "testing" "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/wire" ) // Earlier conversation contracts compare the original message envelope. Keep // those exact assertions while checking the new reference block separately. func withoutProjectContext(t *testing.T, text string) string { t.Helper() before, block, found := strings.Cut(text, "\n\n"+equipo.RunnerText("projects.context")) if !found { return text } lines := strings.Split(strings.TrimPrefix(block, "\n"), "\n") if len(lines) > 30 { t.Fatal("too many project context rows") } for _, line := range lines { if line != "" && !json.Valid([]byte(line)) { t.Fatalf("unquoted project context: %q", line) } } return before } func TestProjectsUpfrontPageAndDevice(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) list, err := r.client.Projects(r.ctx) if err != nil { t.Fatal(err) } if len(list.CanAdd) == 0 || len(list.Connections) != 1 { t.Fatalf("missing reach: %+v", list) } owner := r.inst.Cookie() page := r.get(owner, "/projects", 200) if !strings.Contains(page, "Available to add") || !strings.Contains(page, `action="/projects/add"`) || !strings.Contains(page, `name="q"`) { t.Fatal("missing reachable list/add/filter") } row := list.CanAdd[0] status, _ := r.post(owner, "/projects/add", url.Values{"repo": {row.Repo}}) if status != 303 { t.Fatalf("add: %d", status) } list, err = r.client.Projects(r.ctx) if err != nil || len(list.Items) != 1 { t.Fatalf("added: %+v %v", list, err) } p := list.Items[0] var starred wire.Project if err := r.client.do(r.ctx, "PUT", "/api/v1/projects/"+p.ID+"/star", wire.SetProjectStar{Starred: true}, &starred); err != nil || !starred.Starred { t.Fatalf("device star: %+v %v", starred, err) } var edited wire.Project if err := r.client.do(r.ctx, "PUT", "/api/v1/projects/"+p.ID+"/details", wire.EditProject{Revision: p.Revision, Title: "Team home", Description: "Where the team starts", Names: []string{"Portal"}}, &edited); err != nil || edited.TitleSource != "owner" { t.Fatalf("details: %+v %v", edited, err) } page = r.get(owner, "/projects/"+p.ID, 200) if !strings.Contains(page, "Where the team starts") || !strings.Contains(page, `name="names"`) { t.Fatal("missing editable metadata") } member := r.memberCookie() memberPage := r.get(member, "/projects/"+p.ID, 200) if strings.Contains(memberPage, `name="names"`) { t.Fatal("member sees owner form") } memberPage = r.get(member, "/projects", 200) if strings.Contains(memberPage, `action="/projects/add"`) { t.Fatal("member sees Add") } var sent wire.Sent if err := r.client.do(r.ctx, "POST", "/api/v1/messages", wire.SendMessage{Text: "Fix the navigation", ProjectID: p.ID, IdempotencyKey: "project-request"}, &sent); err != nil || sent.Message.ProjectID != p.ID { t.Fatalf("send: %+v %v", sent, err) } if err := r.runner.Send(r.ctx, "finish-before-project", wire.FrameReply, wire.Reply{TurnID: r.turn, Attempt: 1, Text: "Ready."}); err != nil { t.Fatal(err) } _, body := r.expect(wire.FrameMessage) command := body.(*wire.MessageCommand) if command.TurnID != sent.Turn.ID || !strings.Contains(command.Text, "trusted_names") || !strings.Contains(command.Text, "Team home") || !strings.Contains(command.Text, "The person selected project "+p.Repo) || len(command.PersonText) != 1 || command.PersonText[0] != "Fix the navigation" { t.Fatalf("project not in real turn, or context became person words: %+v", command) } // Editing metadata while a turn is queued is reflected on its dispatch. if err := r.client.do(r.ctx, "PUT", "/api/v1/projects/"+p.ID+"/details", wire.EditProject{Revision: edited.Revision, Title: "Fresh home", Description: "Fresh details"}, nil); err != nil { t.Fatal(err) } follow, err := r.client.Send(r.ctx, "What is next?", "fresh-project-turn") if err != nil { t.Fatal(err) } if err := r.runner.Send(r.ctx, "finish-project-turn", wire.FrameReply, wire.Reply{TurnID: sent.Turn.ID, Attempt: 1, Text: "Done."}); err != nil { t.Fatal(err) } _, body = r.expect(wire.FrameMessage) command = body.(*wire.MessageCommand) if command.TurnID != follow.Turn.ID || !strings.Contains(command.Text, "Fresh home") || strings.Contains(command.Text, "Team home") { t.Fatalf("stale project context: %+v", command) } }