oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package equipotest
2
3import (
4 "bytes"
5 "log/slog"
6 "net/http"
7 "net/url"
8 "strings"
9 "sync"
10 "testing"
11 "time"
12
13 "amadan.net/oficina/equipo/internal/equipo"
14 "amadan.net/oficina/equipo/internal/equipo/github"
15 "amadan.net/oficina/equipo/internal/runner/forge/forgetest"
16 "amadan.net/rastrillo/rastrillo"
17)
18
19// Review fixes (Fable, 2026-11-23): page-level lifecycle rendering.
20
21const githubAppRemains = "still exists on GitHub"
22
23func githubStart(t *testing.T, f githubAuthFixture, client *http.Client, login, kind string) string {
24 t.Helper()
25 response := clientPost(t, client, f.server.URL+"/github/connect/start", testOrigin, url.Values{"account": {login}, "account_type": {kind}})
26 if response.StatusCode != 200 {
27 t.Fatalf("start %d", response.StatusCode)
28 }
29 return githubBody(t, response)
30}
31func githubPage(t *testing.T, f githubAuthFixture, client *http.Client, query string) string {
32 t.Helper()
33 response := clientGet(t, client, f.server.URL+"/conversation/settings/github"+query)
34 if response.StatusCode != 200 {
35 t.Fatalf("page %d", response.StatusCode)
36 }
37 return githubBody(t, response)
38}
39func githubConvert(t *testing.T, f githubAuthFixture, g *forgetest.GitHub, client *http.Client, body string) {
40 t.Helper()
41 _, _, state := githubManifestForm(t, body)
42 response := clientGet(t, client, f.server.URL+"/github/connect/manifest?"+url.Values{"state": {state}, "code": {g.Code}}.Encode())
43 githubBody(t, response)
44 if location := response.Header.Get("Location"); !strings.Contains(location, "install=1") {
45 t.Fatalf("conversion failed: %s", location)
46 }
47}
48
49func TestGitHubAbandonedStartsPage(t *testing.T) {
50 f, g := githubFixture(t, true)
51 client := signinClient(t, f.server.URL)
52 signinEmail(t, client, f.server.URL, testOrigin, f.mail, ownerAddress)
53 githubStart(t, f, client, g.Login, g.AccountType)
54 if body := githubStart(t, f, client, g.Login, g.AccountType); strings.Contains(body, githubAppRemains) {
55 t.Fatal("start-back-start claimed an App remains")
56 }
57 body := githubStart(t, f, client, g.Login, g.AccountType)
58 if page := githubPage(t, f, client, ""); strings.Contains(page, githubAppRemains) || strings.Count(page, "data-forge-connection=") != 1 {
59 t.Fatal("three abandoned starts left dead rows on the page")
60 }
61 githubConvert(t, f, g, client, body)
62 if body = githubStart(t, f, client, "another-org", "Organization"); strings.Count(body, githubAppRemains) != 1 {
63 t.Fatal("converted-then-superseded App not shown exactly once on Start")
64 }
65 githubStart(t, f, client, "third-org", "Organization")
66 if page := githubPage(t, f, client, ""); strings.Count(page, githubAppRemains) != 1 {
67 t.Fatal("converted-then-superseded App not shown exactly once on the page")
68 }
69}
70
71func TestGitHubBusyCheckNotice(t *testing.T) {
72 f, g := githubFixture(t, true)
73 client := signinClient(t, f.server.URL)
74 signinEmail(t, client, f.server.URL, testOrigin, f.mail, ownerAddress)
75 githubConvert(t, f, g, client, githubStart(t, f, client, g.Login, g.AccountType))
76 var c github.Connection
77 if err := f.db.G.First(&c).Error; err != nil {
78 t.Fatal(err)
79 }
80 for _, path := range []string{"check", "confirm"} {
81 response := clientPost(t, client, f.server.URL+"/github/connect/"+path, testOrigin, url.Values{"id": {c.ID}})
82 githubBody(t, response)
83 if path == "confirm" && response.Header.Get("Location") != "/conversation/settings/github" {
84 t.Fatal("Connect failed")
85 }
86 }
87 // A second tap while the first Check still holds its claim.
88 if err := f.db.G.Model(&github.Connection{}).Where("id = ?", c.ID).Updates(map[string]any{"claim": "held", "claim_until": time.Now().Add(time.Minute)}).Error; err != nil {
89 t.Fatal(err)
90 }
91 response := clientPost(t, client, f.server.URL+"/github/connect/check", testOrigin, url.Values{"id": {c.ID}})
92 githubBody(t, response)
93 location := response.Header.Get("Location")
94 if location != "/conversation/settings/github?failed=check" {
95 t.Fatalf("busy Check redirected to %s", location)
96 }
97 page := githubPage(t, f, client, strings.TrimPrefix(location, "/conversation/settings/github"))
98 if !strings.Contains(page, "could not check GitHub right now") || strings.Contains(page, "Start again to create a new GitHub App") || !strings.Contains(page, "GitHub is connected") {
99 t.Fatal("busy Check showed the wrong notice")
100 }
101}
102
103func TestGitHubSettingsNav(t *testing.T) {
104 for _, tc := range []struct {
105 name string
106 enabled, show bool
107 role equipo.Role
108 }{{"owner", true, true, equipo.RoleOwner}, {"member", true, false, equipo.RoleMember}, {"flag-off", false, false, equipo.RoleOwner}} {
109 t.Run(tc.name, func(t *testing.T) {
110 f, _ := githubFixture(t, tc.enabled)
111 client := signinClient(t, f.server.URL)
112 signinEmail(t, client, f.server.URL, testOrigin, f.mail, ownerAddress)
113 if err := f.db.G.Model(&equipo.Member{}).Where("address = ?", ownerAddress).Update("role", tc.role).Error; err != nil {
114 t.Fatal(err)
115 }
116 response := clientGet(t, client, f.server.URL+"/conversation/settings")
117 if response.StatusCode != 200 {
118 t.Fatalf("settings %d", response.StatusCode)
119 }
120 body := githubBody(t, response)
121 if !strings.Contains(body, `data-settings-link="thinking"`) {
122 t.Fatal("settings nav absent")
123 }
124 if got := strings.Contains(body, `data-settings-link="github"`); got != tc.show {
125 t.Fatalf("GitHub in nav: %v, want %v", got, tc.show)
126 }
127 })
128 }
129}
130
131type githubSyncBuffer struct {
132 mu sync.Mutex
133 b bytes.Buffer
134}
135
136func (s *githubSyncBuffer) Write(p []byte) (int, error) {
137 s.mu.Lock()
138 defer s.mu.Unlock()
139 return s.b.Write(p)
140}
141func (s *githubSyncBuffer) String() string { s.mu.Lock(); defer s.mu.Unlock(); return s.b.String() }
142
143// Re-review P3-3: the serving minute loop logs a failed GitHub sweep with the
144// diagnostic constant and its type, never the UI string or the error text.
145func TestGitHubSweepFailureLogged(t *testing.T) {
146 d, _ := foundationDB(t)
147 logs := &githubSyncBuffer{}
148 logger := slog.New(slog.NewTextHandler(logs, nil))
149 cfg := testConfig(testOrigin)
150 router, err := equipo.Router(d, cfg, &capturingMailer{}, logger)
151 if err != nil {
152 t.Fatal(err)
153 }
154 // The sweep's own query fails on the loop's first pass.
155 if err = d.G.Exec("DROP TABLE forge_connections").Error; err != nil {
156 t.Fatal(err)
157 }
158 var opts rastrillo.Options
159 opts.Logger = logger
160 equipo.Configure(&opts, equipo.Mux(router), time.Now())
161 t.Cleanup(opts.Background.Stop)
162 deadline := time.Now().Add(10 * time.Second)
163 for !strings.Contains(logs.String(), "Could not clear expired GitHub setups.") {
164 if time.Now().After(deadline) {
165 t.Fatalf("sweep failure not logged: %s", logs.String())
166 }
167 time.Sleep(20 * time.Millisecond)
168 }
169 for _, line := range strings.Split(logs.String(), "\n") {
170 if strings.Contains(line, "GitHub setups") && (!strings.Contains(line, "kind=other") || !strings.Contains(line, "err_type=") || strings.Contains(line, "forge_connections") || strings.Contains(line, "could not check GitHub")) {
171 t.Fatalf("sweep log line: %s", line)
172 }
173 }
174}
175