package ownermodetest import ( "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/plan3test" "amadan.net/oficina/equipo/internal/runnerproto" "context" "encoding/json" "net/http/httptest" "strings" "testing" ) func mustSelfHeal(t *testing.T, err error) { t.Helper() if err != nil { t.Fatal(err) } } func TestSelfHealNoCoordinatorPicker(t *testing.T) { f, _, _, _ := plan3test.SelfHealFixture(t) plan3test.SelfHealRouter(t, f, nil) req := httptest.NewRequest("GET", "/settings/coordinator", nil) req.AddCookie(f.Cookies["owner@example.com"]) response := httptest.NewRecorder() f.Handler.ServeHTTP(response, req) if response.Code != 200 { t.Fatalf("settings: %d", response.Code) } for _, needle := range []string{`name="connection_id"`, `value="bind"`, `value="reset"`} { if strings.Contains(response.Body.String(), needle) { t.Fatalf("manual coordinator control: %s", needle) } } } func TestSelfHealStatusIsScopedAndRevocationApplies(t *testing.T) { f, x, p, c := plan3test.SelfHealClaim(t) mustSelfHeal(t, f.DB.G.Exec("INSERT INTO runners(id,person,instance_id,label,revision,machine_key_hash) VALUES('other-runner','other@example.invalid',?,'foreign-runner-secret',1,'private-machine-key')", f.TeamID).Error) out := plan3test.SelfHealTool(t, x, p, c, "status", "equipo_status", map[string]any{}) if strings.Contains(string(out.Value), "MachineKeyHash") || strings.Contains(string(out.Value), "AccountID") || strings.Contains(string(out.Value), "foreign-runner-secret") || strings.Contains(string(out.Value), "private-machine-key") { t.Fatal("status exposed private fields") } if !strings.Contains(string(out.Value), "coordinator_limits") || !strings.Contains(string(out.Value), "projects") { t.Fatal("status incomplete") } mustSelfHeal(t, f.DB.G.Exec("UPDATE members SET active=0 WHERE address=?", "owner@example.com").Error) if _, err := x.CallCoordinatorTool(context.Background(), p, c, runnerproto.CoordinatorToolCall{RequestID: "revoked", Name: "equipo_status", Arguments: json.RawMessage(`{}`)}); err == nil { t.Fatal("revoked member read status") } } func TestSelfHealRevokedLoginNotifiedOnce(t *testing.T) { f, x, p, _ := plan3test.SelfHealFixture(t) plan3test.SelfHealOwnerReports(t, f) plan3test.SelfHealQueue(t, f, "hello", "revoked-login") mustSelfHeal(t, f.DB.G.Exec(`UPDATE connections SET reports_json=json_set(reports_json,'$[0].Available',json('false'),'$[0].UnavailableReason','account.sign_in_again','$[0].SignInCommand','flock home env CLAUDE_CONFIG_DIR=home claude auth login') WHERE id='claude'`).Error) for i := 0; i < 3; i++ { _, err := x.PollCoordinator(context.Background(), p) mustSelfHeal(t, err) } var count int mustSelfHeal(t, f.DB.Writer().QueryRow(`SELECT count(*) FROM conversation_messages WHERE author_kind='system' AND body LIKE '%flock home env CLAUDE_CONFIG_DIR=home claude auth login%'`).Scan(&count)) if count != 1 { t.Fatal("revoked login action missing or repeated") } } func TestSelfHealStatusDoesNotCallStaleReportsAvailable(t *testing.T) { f, x, p, c := plan3test.SelfHealClaim(t) mustSelfHeal(t, f.DB.G.Exec("UPDATE connections SET last_seen='2000-01-01' WHERE id='codex'").Error) out := plan3test.SelfHealTool(t, x, p, c, "stale-status", "equipo_status", map[string]any{}) var result struct { Result struct { Connections []struct { ID string Available bool } } } mustSelfHeal(t, json.Unmarshal(out.Value, &result)) found := false for _, connection := range result.Result.Connections { if connection.ID == "codex" { found = true if connection.Available { t.Fatal("stale connection advertised available") } } } if !found { t.Fatal("missing own unavailable connection") } } func TestSelfHealRemovedConnectionStillRendersWait(t *testing.T) { f, x, p, _ := plan3test.SelfHealFixture(t) plan3test.SelfHealQueue(t, f, "hello", "removed-connection") mustSelfHeal(t, f.DB.G.Exec("DELETE FROM connections WHERE id='claude'").Error) _, err := x.PollCoordinator(context.Background(), p) mustSelfHeal(t, err) _, err = f.Store.Conversation(context.Background(), equipo.Actor{Address: p.Person}, "", 50) mustSelfHeal(t, err) }