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 "encoding/json"
5 "net/url"
6 "strings"
7 "testing"
8
9 "amadan.net/oficina/equipo/internal/equipo"
10 "amadan.net/oficina/equipo/internal/wire"
11)
12
13// Earlier conversation contracts compare the original message envelope. Keep
14// those exact assertions while checking the new reference block separately.
15func withoutProjectContext(t *testing.T, text string) string {
16 t.Helper()
17 before, block, found := strings.Cut(text, "\n\n"+equipo.RunnerText("projects.context"))
18 if !found {
19 return text
20 }
21 lines := strings.Split(strings.TrimPrefix(block, "\n"), "\n")
22 if len(lines) > 30 {
23 t.Fatal("too many project context rows")
24 }
25 for _, line := range lines {
26 if line != "" && !json.Valid([]byte(line)) {
27 t.Fatalf("unquoted project context: %q", line)
28 }
29 }
30 return before
31}
32
33func TestProjectsUpfrontPageAndDevice(t *testing.T) {
34 r := newToolRigAt(t, wire.ProtocolCurrent)
35 list, err := r.client.Projects(r.ctx)
36 if err != nil {
37 t.Fatal(err)
38 }
39 if len(list.CanAdd) == 0 || len(list.Connections) != 1 {
40 t.Fatalf("missing reach: %+v", list)
41 }
42 owner := r.inst.Cookie()
43 page := r.get(owner, "/projects", 200)
44 if !strings.Contains(page, "Available to add") || !strings.Contains(page, `action="/projects/add"`) || !strings.Contains(page, `name="q"`) {
45 t.Fatal("missing reachable list/add/filter")
46 }
47 row := list.CanAdd[0]
48 status, _ := r.post(owner, "/projects/add", url.Values{"repo": {row.Repo}})
49 if status != 303 {
50 t.Fatalf("add: %d", status)
51 }
52 list, err = r.client.Projects(r.ctx)
53 if err != nil || len(list.Items) != 1 {
54 t.Fatalf("added: %+v %v", list, err)
55 }
56 p := list.Items[0]
57 var starred wire.Project
58 if err := r.client.do(r.ctx, "PUT", "/api/v1/projects/"+p.ID+"/star", wire.SetProjectStar{Starred: true}, &starred); err != nil || !starred.Starred {
59 t.Fatalf("device star: %+v %v", starred, err)
60 }
61 var edited wire.Project
62 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" {
63 t.Fatalf("details: %+v %v", edited, err)
64 }
65 page = r.get(owner, "/projects/"+p.ID, 200)
66 if !strings.Contains(page, "Where the team starts") || !strings.Contains(page, `name="names"`) {
67 t.Fatal("missing editable metadata")
68 }
69 member := r.memberCookie()
70 memberPage := r.get(member, "/projects/"+p.ID, 200)
71 if strings.Contains(memberPage, `name="names"`) {
72 t.Fatal("member sees owner form")
73 }
74 memberPage = r.get(member, "/projects", 200)
75 if strings.Contains(memberPage, `action="/projects/add"`) {
76 t.Fatal("member sees Add")
77 }
78 var sent wire.Sent
79 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 {
80 t.Fatalf("send: %+v %v", sent, err)
81 }
82 if err := r.runner.Send(r.ctx, "finish-before-project", wire.FrameReply, wire.Reply{TurnID: r.turn, Attempt: 1, Text: "Ready."}); err != nil {
83 t.Fatal(err)
84 }
85 _, body := r.expect(wire.FrameMessage)
86 command := body.(*wire.MessageCommand)
87 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" {
88 t.Fatalf("project not in real turn, or context became person words: %+v", command)
89 }
90 // Editing metadata while a turn is queued is reflected on its dispatch.
91 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 {
92 t.Fatal(err)
93 }
94 follow, err := r.client.Send(r.ctx, "What is next?", "fresh-project-turn")
95 if err != nil {
96 t.Fatal(err)
97 }
98 if err := r.runner.Send(r.ctx, "finish-project-turn", wire.FrameReply, wire.Reply{TurnID: sent.Turn.ID, Attempt: 1, Text: "Done."}); err != nil {
99 t.Fatal(err)
100 }
101 _, body = r.expect(wire.FrameMessage)
102 command = body.(*wire.MessageCommand)
103 if command.TurnID != follow.Turn.ID || !strings.Contains(command.Text, "Fresh home") || strings.Contains(command.Text, "Team home") {
104 t.Fatalf("stale project context: %+v", command)
105 }
106}
107