| 1 | package provision |
| 2 | |
| 3 | import ( |
| 4 | "slices" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "amadan.net/oficina/alta/homeop" |
| 9 | ) |
| 10 | |
| 11 | func TestEquipoAdoptionRegistry(t *testing.T) { |
| 12 | app, ok := AppBySlug("equipo") |
| 13 | if !ok || app.Prefix != "EQUIPO" || app.Name != "Equipo" { |
| 14 | t.Fatalf("Equipo registry: %+v %v", app, ok) |
| 15 | } |
| 16 | if app.AdoptionOnly || !app.Seeded || !app.OwnOnly || app.Roster { |
| 17 | t.Fatalf("Equipo is seeded and own-only (team-bundle spec): %+v", app) |
| 18 | } |
| 19 | if !ReservedLabels["equipo"] { |
| 20 | t.Fatal("Equipo label is not reserved") |
| 21 | } |
| 22 | if homeop.CanonicalOrigin("equipo") != "https://equipo.oficina.works" || !slices.Equal(SortApps([]string{"equipo"}), []string{"equipo"}) { |
| 23 | t.Fatal("Equipo adoption mapping missing") |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | // A completed six-app adoption is extended without reseeding the team or |
| 28 | // touching existing registrations. Both Home confirmations remain required. |
| 29 | func TestEquipoJoinsExistingAdoption(t *testing.T) { |
| 30 | saved := Apps |
| 31 | t.Cleanup(func() { Apps = saved }) |
| 32 | Apps = slices.DeleteFunc(slices.Clone(saved), func(a App) bool { return a.Slug == "equipo" }) |
| 33 | a := newAdoption(t) |
| 34 | e := a.adopter(nil, newObserver()) |
| 35 | a.planA(e) |
| 36 | oldTeam, _ := a.store.Team(a.ctx(), AdoptionLabel) |
| 37 | Apps = saved |
| 38 | a.home.AddEnvConsumer("equipo", "https://equipo.oficina.works") |
| 39 | a.tick(homeop.WindowLifetime * time.Second) |
| 40 | run, err := e.ContinueAdoption(a.ctx()) |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | r, _ := a.store.Run(a.ctx(), run) |
| 45 | if !slices.Equal(r.Apps, []string{"equipo"}) { |
| 46 | t.Fatalf("continuation: %v", r.Apps) |
| 47 | } |
| 48 | for role, p := range a.plans(run) { |
| 49 | if p.Kind != homeop.KindAddApps || p.ExistingTeam != oldTeam.TeamSQID || !slices.Equal(p.Apps, []string{"equipo"}) { |
| 50 | t.Fatalf("%s plan: %+v", role, p) |
| 51 | } |
| 52 | } |
| 53 | a.confirm(homeop.KindAddApps) |
| 54 | if r := a.drive(e, run, 10); r.State != "done" || r.Code != "awaiting-configuration" { |
| 55 | t.Fatalf("registration: %s %s", r.State, r.Code) |
| 56 | } |
| 57 | team, _ := a.store.Team(a.ctx(), AdoptionLabel) |
| 58 | if team.Apps["equipo"] != "active" { |
| 59 | t.Fatal("Equipo missing from existing local team") |
| 60 | } |
| 61 | var environments int |
| 62 | if err := a.store.DB().QueryRow("SELECT count(*) FROM environments").Scan(&environments); err != nil || environments != 0 { |
| 63 | t.Fatalf("adoption reserved environments: %d %v", environments, err) |
| 64 | } |
| 65 | if a.home.InstanceState("equipo") != "pending" { |
| 66 | t.Fatal("Equipo must stay pending before activation") |
| 67 | } |
| 68 | run = a.activate(e, "equipo") |
| 69 | if r := a.drive(e, run, 10); r.State != "done" || a.home.InstanceState("equipo") != "live" { |
| 70 | t.Fatalf("activation: %s %s", r.State, r.Code) |
| 71 | } |
| 72 | if a.home.TeamCount() != 1 || a.home.ConsumerCount() != 0 || a.home.Calls[homeop.DirectorySeedTeam] != 1 || a.home.Calls[homeop.CredentialOwnerSeed] != 1 || a.net.count() != 0 { |
| 73 | t.Fatal("adoption changed existing team, registry, or instances") |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // A fresh Equipo is offered only under profile own (team-bundle spec §5.1 |
| 78 | // rule 3), never under outside, and never by an ordinary run under the |
| 79 | // adoption label. |
| 80 | func TestEquipoFreshInstancesAreOwnOnly(t *testing.T) { |
| 81 | h := newHarness(t) |
| 82 | h.initialized() |
| 83 | e := h.engine(nil) |
| 84 | _, err := e.CreateTeam(h.ctx(), NewTeam{Label: "new-team", Name: "New", Owner: ownerCanary, Apps: []string{"docs", "equipo"}}) |
| 85 | failCode(t, err, "app-own-only") |
| 86 | if err := h.store.CreateTeam(h.ctx(), "existing", "Existing", "existing", false, ownerCanary, []string{"docs"}); err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | _, err = e.AddApps(h.ctx(), "existing", []string{"equipo"}, RunOptions{Profile: ProfileOutside}) |
| 90 | failCode(t, err, "app-own-only") |
| 91 | } |
| 92 | |