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 "net/http"
5 "net/url"
6 "strconv"
7 "strings"
8 "testing"
9
10 "amadan.net/oficina/equipo/internal/wire"
11)
12
13// TestContractShipsButtons (Paul, 2026-11-11): "Does merging to main
14// ship to users?" is two buttons that apply at once, outside the Change
15// form. A tap posts only the answer at the page's revision and leaves
16// the rest of the policy as it was; Undo is the same post with the
17// value before, "unknown" included; each is an audited policy write. A
18// stale revision changes nothing; a member sees no buttons and is
19// refused.
20func TestContractShipsButtons(t *testing.T) {
21 r := newToolRigAt(t, wire.ProtocolInjectionDefences)
22 project := r.project()
23 owner := r.inst.Cookie()
24 path := "/projects/" + project + "/merge-policy"
25 // A policy with something in every field, so a tap that dropped one
26 // would show.
27 p0, _ := r.client.MergePolicy(r.ctx, project)
28 if _, err := r.client.SetMergePolicy(r.ctx, project, wire.SetMergePolicy{Mode: "ask", MaxLines: 40, MaxFiles: 3, MainShips: "unknown", ExtraSensitive: []string{"billing/**"}, Words: "ask me before Woodstar merges", Revision: p0.Revision}); err != nil {
29 t.Fatal(err)
30 }
31 base, _ := r.client.MergePolicy(r.ctx, project)
32 audits := func() int {
33 var n int
34 if err := r.inst.DB.Writer().QueryRow("SELECT COUNT(*) FROM chat_merge_audit WHERE project_id = ? AND kind = 'policy'", project).Scan(&n); err != nil {
35 t.Fatal(err)
36 }
37 return n
38 }
39 tap := func(c *http.Cookie, value string, rev int64) int {
40 t.Helper()
41 status, loc := r.post(c, path, url.Values{"revision": {strconv.FormatInt(rev, 10)}, "only": {"main_ships"}, "main_ships": {value}})
42 if status == http.StatusSeeOther && loc != "/projects/"+project {
43 t.Fatalf("tap %s came back to %q", value, loc)
44 }
45 return status
46 }
47 pressed := func(page, value string) bool {
48 return strings.Contains(page, `aria-pressed="true" data-merge-ships-choice="`+value+`"`)
49 }
50 // Before the first tap: two buttons, neither pressed, no select.
51 page := r.get(owner, "/projects/"+project, 200)
52 for _, v := range []string{"yes", "no"} {
53 if !strings.Contains(page, `aria-pressed="false" data-merge-ships-choice="`+v+`"`) {
54 t.Fatalf("no unpressed %s button: %s", v, page)
55 }
56 }
57 if strings.Contains(page, `<select rst-input name="main_ships"`) || strings.Contains(page, "Not answered") || !strings.Contains(page, `data-merge-policy-form`) {
58 t.Fatalf("the ship question is still on the form: %s", page)
59 }
60 if i, j := strings.Index(page, "data-merge-ships-form"), strings.Index(page, "<details data-merge-policy-edit>"); i < 0 || j < 0 || i > j {
61 t.Fatal("the buttons are inside the Change form")
62 }
63 same := func(p wire.MergePolicy, ships string, rev int64) {
64 t.Helper()
65 if p.MainShips != ships || p.Revision != rev || p.Mode != base.Mode || p.MaxLines != base.MaxLines || p.MaxFiles != base.MaxFiles || strings.Join(p.ExtraSensitive, ",") != "billing/**" || p.Words != base.Words {
66 t.Fatalf("after %s at %d: %+v (was %+v)", ships, rev, p, base)
67 }
68 }
69 n := audits()
70 // Yes, then Undo back to unknown.
71 if s := tap(owner, "yes", base.Revision); s != http.StatusSeeOther {
72 t.Fatalf("tap yes: %d", s)
73 }
74 p, _ := r.client.MergePolicy(r.ctx, project)
75 same(p, "yes", base.Revision+1)
76 if page = r.get(owner, "/projects/"+project, 200); !pressed(page, "yes") || pressed(page, "no") {
77 t.Fatal("yes is not the pressed button")
78 }
79 if s := tap(owner, "unknown", p.Revision); s != http.StatusSeeOther {
80 t.Fatalf("undo to unknown: %d", s)
81 }
82 p, _ = r.client.MergePolicy(r.ctx, project)
83 same(p, "unknown", base.Revision+2)
84 if page = r.get(owner, "/projects/"+project, 200); pressed(page, "yes") || pressed(page, "no") {
85 t.Fatal("a button pressed after Undo to unanswered")
86 }
87 // No, then yes, then Undo back to no.
88 tap(owner, "no", p.Revision)
89 tap(owner, "yes", p.Revision+1)
90 if s := tap(owner, "no", p.Revision+2); s != http.StatusSeeOther {
91 t.Fatalf("undo to no: %d", s)
92 }
93 p, _ = r.client.MergePolicy(r.ctx, project)
94 same(p, "no", base.Revision+5)
95 if got := audits(); got != n+5 {
96 t.Fatalf("audit rows %d, want %d", got, n+5)
97 }
98 // A stale page is refused and changes nothing.
99 if s := tap(owner, "yes", p.Revision-1); s != http.StatusConflict {
100 t.Fatalf("stale tap: %d", s)
101 }
102 if s := tap(owner, "yes", p.Revision+1); s != http.StatusConflict {
103 t.Fatalf("tap ahead of the record: %d", s)
104 }
105 if s := tap(owner, "maybe", p.Revision); s != http.StatusUnprocessableEntity {
106 t.Fatalf("bad value: %d", s)
107 }
108 if status, _ := r.post(owner, path, url.Values{"revision": {strconv.FormatInt(p.Revision, 10)}, "only": {"main_ships"}}); status != http.StatusUnprocessableEntity {
109 t.Fatalf("no value: %d", status)
110 }
111 again, _ := r.client.MergePolicy(r.ctx, project)
112 same(again, "no", p.Revision)
113 // The Change form saves its own fields and keeps the answer.
114 if status, _ := r.post(owner, path, url.Values{"revision": {strconv.FormatInt(p.Revision, 10)}, "mode": {"rules"}, "max_lines": {"40"}, "max_files": {"3"}, "extra_sensitive": {"billing/**"}, "words": {"merge small Woodstar changes"}}); status != http.StatusSeeOther {
115 t.Fatalf("form save: %d", status)
116 }
117 if f, _ := r.client.MergePolicy(r.ctx, project); f.MainShips != "no" || f.Mode != "rules" || f.Words != "merge small Woodstar changes" || f.Revision != p.Revision+1 {
118 t.Fatalf("form save %+v", f)
119 }
120 // A member: no buttons, and a refused post.
121 member := r.memberCookie()
122 if mpage := r.get(member, "/projects/"+project, 200); strings.Contains(mpage, "data-merge-ships") || !strings.Contains(mpage, "How Equipo merges") {
123 t.Fatal("member's project page offers the ship buttons")
124 }
125 if s := tap(member, "yes", p.Revision+1); s != http.StatusForbidden {
126 t.Fatalf("member tap: %d", s)
127 }
128 if f, _ := r.client.MergePolicy(r.ctx, project); f.MainShips != "no" || f.Revision != p.Revision+1 {
129 t.Fatalf("member tap changed it %+v", f)
130 }
131}
132