package ownermode import ( "amadan.net/oficina/equipo/internal/equipo/planning" "amadan.net/oficina/equipo/internal/equipo/team" "amadan.net/oficina/equipo/internal/release" "amadan.net/oficina/equipo/internal/runnerproto" "errors" "gorm.io/gorm" "strings" "testing" "time" ) func TestOwnerEligibilityGuards(t *testing.T) { now := time.Now() for _, fault := range []string{"none", "foreign", "lookup", "stale", "future", "unavailable", "routing", "isolation", "revoked"} { t.Run(fault, func(t *testing.T) { c := planning.ConnectionRecord{Connection: planning.Connection{Person: "owner", RunnerID: "runner", LastSeen: now}} r := runnerproto.ConnectionReport{OwnerMode: true, Available: true, Routing: runnerproto.ConnectionRouting{BindingID: "binding", AccountID: "account", ObservedAccountID: "account", CredentialHomeHash: strings.Repeat("a", 64), AccountPinned: true, NoFailover: true, TimelineRecovery: true, ProcessOwnership: true}} person := "owner" var lookup error switch fault { case "foreign": person = "peer" case "lookup": lookup = errors.New("lookup failed") case "stale": c.LastSeen = now.Add(-90 * time.Second) case "future": c.LastSeen = now.Add(time.Second) case "unavailable": r.Available = false case "routing": r.Routing.NoFailover = false case "revoked": c.RevokedAt = &now } ok, err := Eligible(nil, c, r, now, func(runnerproto.ConnectionReport) bool { return fault != "isolation" }, func(*gorm.DB, string) (string, error) { return person, lookup }, errors.New("forbidden")) if ok != (fault == "none") || fault == "lookup" && err == nil { t.Fatal("eligibility guard failed", fault, ok, err) } }) } } func TestOwnerEvidenceCannotLoseMode(t *testing.T) { need := team.IsolationSnapshot{Mode: "owner mode", Status: "confined", ObservedTuples: []team.IsolationTuple{{Provider: "claude", ProviderVersion: "v", LauncherVersion: "v", BwrapVersion: "v", PolicyVersion: "v", IsolationConfigHash: strings.Repeat("a", 64)}}} have := need if !team.IsolationContains(have, need) { t.Fatal("owner evidence rejected") } have.Mode = "" if team.IsolationContains(have, need) { t.Fatal("owner evidence relabelled as certified") } } // No release certifies a policy: only an owner-mode report, on the // release's own versions, can become a coordinator. A report that is not // owner mode is refused before its eligibility is even looked up. func TestAutoCandidateIsOwnerModeOnly(t *testing.T) { m := release.BuildManifest() report := runnerproto.ConnectionReport{OwnerMode: true, Provider: "claude", ProviderVersion: m.Claude, LauncherVersion: m.LauncherVersion, BwrapVersion: m.BwrapVersion, PolicyVersion: m.PolicyVersion, Kind: "subscription"} looked := 0 eligible := func(*gorm.DB, planning.ConnectionRecord, runnerproto.ConnectionReport) (bool, error) { looked++ return true, nil } if ok, known, err := AutoCandidate(nil, planning.ConnectionRecord{}, report, m, eligible); !ok || !known || err != nil || looked != 1 { t.Fatalf("the owner's report is not a candidate: %v %v %v", ok, known, err) } other := report other.OwnerMode = false if ok, known, err := AutoCandidate(nil, planning.ConnectionRecord{}, other, m, eligible); ok || known || err != nil || looked != 1 { t.Fatalf("a report that is not owner mode is a candidate: %v %v %v, looked up %d times", ok, known, err, looked) } for name, change := range map[string]func(*runnerproto.ConnectionReport){ "provider version": func(r *runnerproto.ConnectionReport) { r.ProviderVersion = "0.0.0" }, "launcher": func(r *runnerproto.ConnectionReport) { r.LauncherVersion = "direct-v2" }, "bwrap": func(r *runnerproto.ConnectionReport) { r.BwrapVersion = "0.10.0" }, "policy": func(r *runnerproto.ConnectionReport) { r.PolicyVersion = "direct-v0" }, "kind": func(r *runnerproto.ConnectionReport) { r.Kind = "api" }, } { stale := report change(&stale) if ok, _, err := AutoCandidate(nil, planning.ConnectionRecord{}, stale, m, eligible); ok || err != nil || looked != 1 { t.Fatal("a report with another", name, "is a candidate") } } }