package equipotest import ( "bytes" "log/slog" "net/http" "net/url" "strings" "sync" "testing" "time" "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/equipo/github" "amadan.net/oficina/equipo/internal/runner/forge/forgetest" "amadan.net/rastrillo/rastrillo" ) // Review fixes (Fable, 2026-11-23): page-level lifecycle rendering. const githubAppRemains = "still exists on GitHub" func githubStart(t *testing.T, f githubAuthFixture, client *http.Client, login, kind string) string { t.Helper() response := clientPost(t, client, f.server.URL+"/github/connect/start", testOrigin, url.Values{"account": {login}, "account_type": {kind}}) if response.StatusCode != 200 { t.Fatalf("start %d", response.StatusCode) } return githubBody(t, response) } func githubPage(t *testing.T, f githubAuthFixture, client *http.Client, query string) string { t.Helper() response := clientGet(t, client, f.server.URL+"/conversation/settings/github"+query) if response.StatusCode != 200 { t.Fatalf("page %d", response.StatusCode) } return githubBody(t, response) } func githubConvert(t *testing.T, f githubAuthFixture, g *forgetest.GitHub, client *http.Client, body string) { t.Helper() _, _, state := githubManifestForm(t, body) response := clientGet(t, client, f.server.URL+"/github/connect/manifest?"+url.Values{"state": {state}, "code": {g.Code}}.Encode()) githubBody(t, response) if location := response.Header.Get("Location"); !strings.Contains(location, "install=1") { t.Fatalf("conversion failed: %s", location) } } func TestGitHubAbandonedStartsPage(t *testing.T) { f, g := githubFixture(t, true) client := signinClient(t, f.server.URL) signinEmail(t, client, f.server.URL, testOrigin, f.mail, ownerAddress) githubStart(t, f, client, g.Login, g.AccountType) if body := githubStart(t, f, client, g.Login, g.AccountType); strings.Contains(body, githubAppRemains) { t.Fatal("start-back-start claimed an App remains") } body := githubStart(t, f, client, g.Login, g.AccountType) if page := githubPage(t, f, client, ""); strings.Contains(page, githubAppRemains) || strings.Count(page, "data-forge-connection=") != 1 { t.Fatal("three abandoned starts left dead rows on the page") } githubConvert(t, f, g, client, body) if body = githubStart(t, f, client, "another-org", "Organization"); strings.Count(body, githubAppRemains) != 1 { t.Fatal("converted-then-superseded App not shown exactly once on Start") } githubStart(t, f, client, "third-org", "Organization") if page := githubPage(t, f, client, ""); strings.Count(page, githubAppRemains) != 1 { t.Fatal("converted-then-superseded App not shown exactly once on the page") } } func TestGitHubBusyCheckNotice(t *testing.T) { f, g := githubFixture(t, true) client := signinClient(t, f.server.URL) signinEmail(t, client, f.server.URL, testOrigin, f.mail, ownerAddress) githubConvert(t, f, g, client, githubStart(t, f, client, g.Login, g.AccountType)) var c github.Connection if err := f.db.G.First(&c).Error; err != nil { t.Fatal(err) } for _, path := range []string{"check", "confirm"} { response := clientPost(t, client, f.server.URL+"/github/connect/"+path, testOrigin, url.Values{"id": {c.ID}}) githubBody(t, response) if path == "confirm" && response.Header.Get("Location") != "/conversation/settings/github" { t.Fatal("Connect failed") } } // A second tap while the first Check still holds its claim. 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 { t.Fatal(err) } response := clientPost(t, client, f.server.URL+"/github/connect/check", testOrigin, url.Values{"id": {c.ID}}) githubBody(t, response) location := response.Header.Get("Location") if location != "/conversation/settings/github?failed=check" { t.Fatalf("busy Check redirected to %s", location) } page := githubPage(t, f, client, strings.TrimPrefix(location, "/conversation/settings/github")) 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") { t.Fatal("busy Check showed the wrong notice") } } func TestGitHubSettingsNav(t *testing.T) { for _, tc := range []struct { name string enabled, show bool role equipo.Role }{{"owner", true, true, equipo.RoleOwner}, {"member", true, false, equipo.RoleMember}, {"flag-off", false, false, equipo.RoleOwner}} { t.Run(tc.name, func(t *testing.T) { f, _ := githubFixture(t, tc.enabled) client := signinClient(t, f.server.URL) signinEmail(t, client, f.server.URL, testOrigin, f.mail, ownerAddress) if err := f.db.G.Model(&equipo.Member{}).Where("address = ?", ownerAddress).Update("role", tc.role).Error; err != nil { t.Fatal(err) } response := clientGet(t, client, f.server.URL+"/conversation/settings") if response.StatusCode != 200 { t.Fatalf("settings %d", response.StatusCode) } body := githubBody(t, response) if !strings.Contains(body, `data-settings-link="thinking"`) { t.Fatal("settings nav absent") } if got := strings.Contains(body, `data-settings-link="github"`); got != tc.show { t.Fatalf("GitHub in nav: %v, want %v", got, tc.show) } }) } } type githubSyncBuffer struct { mu sync.Mutex b bytes.Buffer } func (s *githubSyncBuffer) Write(p []byte) (int, error) { s.mu.Lock() defer s.mu.Unlock() return s.b.Write(p) } func (s *githubSyncBuffer) String() string { s.mu.Lock(); defer s.mu.Unlock(); return s.b.String() } // Re-review P3-3: the serving minute loop logs a failed GitHub sweep with the // diagnostic constant and its type, never the UI string or the error text. func TestGitHubSweepFailureLogged(t *testing.T) { d, _ := foundationDB(t) logs := &githubSyncBuffer{} logger := slog.New(slog.NewTextHandler(logs, nil)) cfg := testConfig(testOrigin) router, err := equipo.Router(d, cfg, &capturingMailer{}, logger) if err != nil { t.Fatal(err) } // The sweep's own query fails on the loop's first pass. if err = d.G.Exec("DROP TABLE forge_connections").Error; err != nil { t.Fatal(err) } var opts rastrillo.Options opts.Logger = logger equipo.Configure(&opts, equipo.Mux(router), time.Now()) t.Cleanup(opts.Background.Stop) deadline := time.Now().Add(10 * time.Second) for !strings.Contains(logs.String(), "Could not clear expired GitHub setups.") { if time.Now().After(deadline) { t.Fatalf("sweep failure not logged: %s", logs.String()) } time.Sleep(20 * time.Millisecond) } for _, line := range strings.Split(logs.String(), "\n") { 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")) { t.Fatalf("sweep log line: %s", line) } } }