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