package altacompat // The whole existing-team adoption at Home's real operator code, as // alta-adopt runs it: the Home preflight before initialize, initialize, // the preflight before Plan A once initialize's window has lapsed, Plan A, // Plan B for the canary, Plan C for the other five. The counts the // preflight takes are Home's own, from HomeQueries run on its tables. import ( "context" "crypto/sha256" "database/sql" "encoding/base64" "encoding/hex" "slices" "strings" "sync" "testing" "time" "amadan.net/oficina/alta/adopt" "amadan.net/oficina/alta/homeop" "amadan.net/oficina/alta/provision" "amadan.net/oficina/home/internal/operator" ) // served is what each canonical app served in production on 2026-11-17: // the /api/version body and X-Carlos-Version, the same build named two // ways by four of the six. var served = map[string]struct{ body, header string }{ "docs": {"v0.1.0-80-g9de84e4", "v0.1.0-80-g9de84e4"}, "sheets": {"v0.1.0-53-gef9c313", "ef9c313"}, "slides": {"v0.1.0-42-gcb30df5", "cb30df5"}, "calendar": {"v0.1.0-53-g800406a", "800406a"}, "calls": {"ef2335e9df94bf9be1561fcbb1335c87e7c6e4ca", "ef2335e9df94bf9be1561fcbb1335c87e7c6e4ca"}, "memoria": {"v0.1.0-47-g174fb45", "174fb45"}, // Synthetic supported Equipo release; production contract is a separate branch. "equipo": {"v1.0.0", "v1.0.0"}, // Base's live release on 2026-11-23 (carlos/base main). "base": {"3f8e6d1", "3f8e6d1"}, } // observer stands in for the CARLOS half's activation gate: what it // answers is shaped as adopt.Observer answers it (the body as app_version // once adopt.SameRelease admits the pair, Alta's artifact digest of the // header, the binding over the team), so Home's real activation checks // meet the evidence they will meet in production. type observer struct { mu sync.Mutex calls []string } func (o *observer) Observe(_ context.Context, app, team string) (provision.Observation, error) { o.mu.Lock() o.calls = append(o.calls, app) o.mu.Unlock() v, ok := served[app] if !ok || !adopt.SameRelease(v.body, v.header) { return provision.Observation{}, &adopt.Refusal{Codes: []string{"version-header-mismatch"}} } sum := sha256.Sum256([]byte(v.header)) return provision.Observation{ AppVersion: v.body, ArtifactDigest: base64.RawURLEncoding.EncodeToString(sum[:]), EnvironmentBinding: adopt.EnvironmentBinding(adopt.BindingInput{ TeamSQID: team, Issuer: adopt.Issuer, ArtifactSHA256: hex.EncodeToString(sum[:]), Version: v.body, }), }, nil } // homeCounts runs HomeQueries on Home's own tables, as Paul's read-only // carlos db query does, and answers what he would paste. func homeCounts(t *testing.T, h *home) map[string]int64 { t.Helper() counts := map[string]int64{} for _, q := range adopt.HomeQueries() { var n int64 if err := h.dirDB.QueryRow(q).Scan(&n); err != nil { t.Fatalf("%s at Home's directory: %v", q, err) } counts[q[strings.LastIndex(q, " ")+1:]] = n } return counts } func homePreflight(t *testing.T, r *rig, phase string) { t.Helper() _, codes, err := adopt.CheckHome(context.Background(), r.client, r.store, homeCounts(t, r.home), phase) if err != nil || len(codes) > 0 { t.Fatalf("home preflight %s: %v %v", phase, codes, err) } } // manifestDigest is the credential role's union manifest: the env // manifest and the active consumers table, as register answers it. func manifestDigest(t *testing.T, r *rig, env map[string]string) string { t.Helper() table, err := operator.ActiveConsumers(context.Background(), r.home.credDB) if err != nil { t.Fatal(err) } union := map[string]string{"directory": dirOrigin, "alta": "https://alta-x.bds.oncarlos.com"} for id, o := range env { union[id] = o } for id, o := range table { union[id] = o } want, err := operator.ManifestDigest(union) if err != nil { t.Fatal(err) } if got := homeop.ManifestDigest(union); got != want { t.Fatalf("manifest digest: Alta's is not Home's") } return want } func TestAdoptionFlowAgainstRealHome(t *testing.T) { ctx := context.Background() env := map[string]string{} for _, a := range adoptionApps { env[a] = homeop.CanonicalOrigin(a) } r := newRigWith(t, env, nil) obs := &observer{} cfg := r.cfg cfg.Observer = obs r.engine = provision.New(cfg) before := manifestDigest(t, r, env) window := func() { r.home.now = r.home.now.Add(homeop.WindowLifetime * time.Second) } homePreflight(t, r, adopt.PhasePreInitialize) run, err := r.engine.StartInitialize(ctx, provision.InitializeAttestation) if err != nil { t.Fatal(err) } r.home.confirm(t, "directory", "") if got := r.drive(t, run); got.State != "done" { t.Fatalf("initialize: %s %s", got.State, got.Code) } // Before Plan A, initialize's window must have lapsed: the preflight // refuses an open plan. if _, codes, err := adopt.CheckHome(ctx, r.client, r.store, homeCounts(t, r.home), adopt.PhasePrePlanA); err != nil || !slices.Contains(codes, "plan-open") { t.Fatalf("pre-plan-a with initialize's window open: %v %v", codes, err) } window() homePreflight(t, r, adopt.PhasePrePlanA) run, err = r.engine.StartAdoption(ctx, "Oficina", owner) if err != nil { t.Fatal(err) } r.home.confirm(t, "directory", "") r.home.confirm(t, "credential", owner) if got := r.drive(t, run); got.State != "done" || got.Code != "awaiting-configuration" { t.Fatalf("plan A: %s %s", got.State, got.Code) } team, err := r.store.Team(ctx, provision.AdoptionLabel) if err != nil || team.TeamSQID == "" { t.Fatalf("no seeded team: %v", err) } // Plans B and C: the canary first, then the other five. rest := []string{} for _, a := range provision.Slugs() { if a != adopt.CanaryApp { rest = append(rest, a) } } for _, plan := range [][]string{{adopt.CanaryApp}, rest} { window() run, err := r.engine.ActivateAdopted(ctx, plan) if err != nil { t.Fatalf("activate %v: %v", plan, err) } for _, role := range []string{"directory", "credential"} { if v := r.home.confirm(t, role, ""); v.Kind != homeop.KindAddApps || v.ExistingTeam != team.TeamSQID || len(v.Apps) != len(plan) { t.Fatalf("%s plan view: kind %s apps %v", role, v.Kind, v.Apps) } } if got := r.drive(t, run); got.State != "done" { t.Fatalf("activate %v: %s %s", plan, got.State, got.Code) } } if len(obs.calls) != len(adoptionApps) { t.Fatalf("observed %v", obs.calls) } count := func(db *sql.DB, q string, args ...any) int { t.Helper() var n int if err := db.QueryRow(q, args...).Scan(&n); err != nil { t.Fatal(err) } return n } if n := count(r.home.dirDB, "SELECT count(*) FROM teams WHERE label='oficina'"); n != 1 { t.Fatalf("teams %d", n) } // Every canonical binding is live, and there is no other. if n := count(r.home.dirDB, "SELECT count(*) FROM team_instances WHERE state='live' AND consumer_id=app AND origin='https://'||app||'.oficina.works'"); n != len(adoptionApps) { t.Fatalf("live canonical rows %d", n) } if n := count(r.home.dirDB, "SELECT count(*) FROM team_instances"); n != len(adoptionApps) { t.Fatalf("team_instances %d", n) } // Register verified the env manifest and inserted nothing. if n := count(r.home.credDB, "SELECT count(*) FROM consumers"); n != 0 { t.Fatalf("credential consumers %d", n) } if after := manifestDigest(t, r, env); after != before { t.Fatal("the manifest digest changed") } for _, a := range adoptionApps { data, ok, err := r.store.Done(ctx, provision.AdoptionLabel, a, provision.StepRegister) if err != nil || !ok || data["manifest_digest"] != before { t.Fatalf("%s: register's manifest digest is not the unchanged one", a) } // Home activated each app on its production /api/version body. data, ok, err = r.store.Done(ctx, provision.AdoptionLabel, a, provision.StepActivate) if err != nil || !ok || data["app_version"] != served[a].body { t.Fatalf("%s: activated with app_version %v, want %q", a, data["app_version"], served[a].body) } } // Alta's streams are Home's. for _, role := range []string{"directory", "credential"} { seq, head, _, _ := r.store.Stream(ctx, role) db := r.home.dirDB if role == "credential" { db = r.home.credDB } var hs int64 var hh string if err := db.QueryRow("SELECT sequence,head FROM "+role+"_operator_state WHERE singleton=1").Scan(&hs, &hh); err != nil { t.Fatal(err) } if seq != hs || head != hh { t.Fatalf("%s stream: Alta %d, Home %d", role, seq, hs) } } } // Canonical adoption verifies a pre-existing env registration; it cannot // create one or turn a missing registration into a live native route. func TestEquipoAdoptionRequiresRegisteredConsumer(t *testing.T) { ctx := context.Background() env := map[string]string{} for _, a := range apps { env[a] = homeop.CanonicalOrigin(a) } r := newRigWith(t, env, nil) run, err := r.engine.StartInitialize(ctx, provision.InitializeAttestation) if err != nil { t.Fatal(err) } r.home.confirm(t, "directory", "") if got := r.drive(t, run); got.State != "done" { t.Fatalf("initialize: %+v", got) } r.home.now = r.home.now.Add(homeop.WindowLifetime * time.Second) run, err = r.engine.StartAdoption(ctx, "Oficina", owner) if err != nil { t.Fatal(err) } r.home.confirm(t, "directory", "") r.home.confirm(t, "credential", owner) if got := r.drive(t, run); got.State != "failed" || got.Code != "register-refused" { t.Fatalf("unregistered Equipo: %s %s", got.State, got.Code) } var state string if err := r.home.dirDB.QueryRow("SELECT state FROM team_instances WHERE consumer_id='equipo'").Scan(&state); err != nil || state != "pending" { t.Fatalf("unregistered state: %s %v", state, err) } var n int if err := r.home.credDB.QueryRow("SELECT count(*) FROM consumers").Scan(&n); err != nil || n != 0 { t.Fatalf("adoption created consumers: %d %v", n, err) } }