| 1 | package ownermodetest |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo" |
| 5 | "amadan.net/oficina/equipo/internal/plan3test" |
| 6 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 7 | "context" |
| 8 | "encoding/json" |
| 9 | "net/http/httptest" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | func mustSelfHeal(t *testing.T, err error) { |
| 15 | t.Helper() |
| 16 | if err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | } |
| 20 | func TestSelfHealNoCoordinatorPicker(t *testing.T) { |
| 21 | f, _, _, _ := plan3test.SelfHealFixture(t) |
| 22 | plan3test.SelfHealRouter(t, f, nil) |
| 23 | req := httptest.NewRequest("GET", "/settings/coordinator", nil) |
| 24 | req.AddCookie(f.Cookies["owner@example.com"]) |
| 25 | response := httptest.NewRecorder() |
| 26 | f.Handler.ServeHTTP(response, req) |
| 27 | if response.Code != 200 { |
| 28 | t.Fatalf("settings: %d", response.Code) |
| 29 | } |
| 30 | for _, needle := range []string{`name="connection_id"`, `value="bind"`, `value="reset"`} { |
| 31 | if strings.Contains(response.Body.String(), needle) { |
| 32 | t.Fatalf("manual coordinator control: %s", needle) |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestSelfHealStatusIsScopedAndRevocationApplies(t *testing.T) { |
| 38 | f, x, p, c := plan3test.SelfHealClaim(t) |
| 39 | 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) |
| 40 | out := plan3test.SelfHealTool(t, x, p, c, "status", "equipo_status", map[string]any{}) |
| 41 | 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") { |
| 42 | t.Fatal("status exposed private fields") |
| 43 | } |
| 44 | if !strings.Contains(string(out.Value), "coordinator_limits") || !strings.Contains(string(out.Value), "projects") { |
| 45 | t.Fatal("status incomplete") |
| 46 | } |
| 47 | mustSelfHeal(t, f.DB.G.Exec("UPDATE members SET active=0 WHERE address=?", "owner@example.com").Error) |
| 48 | if _, err := x.CallCoordinatorTool(context.Background(), p, c, runnerproto.CoordinatorToolCall{RequestID: "revoked", Name: "equipo_status", Arguments: json.RawMessage(`{}`)}); err == nil { |
| 49 | t.Fatal("revoked member read status") |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestSelfHealRevokedLoginNotifiedOnce(t *testing.T) { |
| 54 | f, x, p, _ := plan3test.SelfHealFixture(t) |
| 55 | plan3test.SelfHealOwnerReports(t, f) |
| 56 | plan3test.SelfHealQueue(t, f, "hello", "revoked-login") |
| 57 | 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) |
| 58 | for i := 0; i < 3; i++ { |
| 59 | _, err := x.PollCoordinator(context.Background(), p) |
| 60 | mustSelfHeal(t, err) |
| 61 | } |
| 62 | var count int |
| 63 | 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)) |
| 64 | if count != 1 { |
| 65 | t.Fatal("revoked login action missing or repeated") |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func TestSelfHealStatusDoesNotCallStaleReportsAvailable(t *testing.T) { |
| 70 | f, x, p, c := plan3test.SelfHealClaim(t) |
| 71 | mustSelfHeal(t, f.DB.G.Exec("UPDATE connections SET last_seen='2000-01-01' WHERE id='codex'").Error) |
| 72 | out := plan3test.SelfHealTool(t, x, p, c, "stale-status", "equipo_status", map[string]any{}) |
| 73 | var result struct { |
| 74 | Result struct { |
| 75 | Connections []struct { |
| 76 | ID string |
| 77 | Available bool |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | mustSelfHeal(t, json.Unmarshal(out.Value, &result)) |
| 82 | found := false |
| 83 | for _, connection := range result.Result.Connections { |
| 84 | if connection.ID == "codex" { |
| 85 | found = true |
| 86 | if connection.Available { |
| 87 | t.Fatal("stale connection advertised available") |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | if !found { |
| 92 | t.Fatal("missing own unavailable connection") |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | func TestSelfHealRemovedConnectionStillRendersWait(t *testing.T) { |
| 97 | f, x, p, _ := plan3test.SelfHealFixture(t) |
| 98 | plan3test.SelfHealQueue(t, f, "hello", "removed-connection") |
| 99 | mustSelfHeal(t, f.DB.G.Exec("DELETE FROM connections WHERE id='claude'").Error) |
| 100 | _, err := x.PollCoordinator(context.Background(), p) |
| 101 | mustSelfHeal(t, err) |
| 102 | _, err = f.Store.Conversation(context.Background(), equipo.Actor{Address: p.Person}, "", 50) |
| 103 | mustSelfHeal(t, err) |
| 104 | } |
| 105 | |