oficina / alta Public

Clone
git clone https://amadan.net/oficina/alta

Plain git — no account needed to clone.

Download

Download this file

1package altacompat
2
3// The whole existing-team adoption at Home's real operator code, as
4// alta-adopt runs it: the Home preflight before initialize, initialize,
5// the preflight before Plan A once initialize's window has lapsed, Plan A,
6// Plan B for the canary, Plan C for the other five. The counts the
7// preflight takes are Home's own, from HomeQueries run on its tables.
8
9import (
10 "context"
11 "crypto/sha256"
12 "database/sql"
13 "encoding/base64"
14 "encoding/hex"
15 "slices"
16 "strings"
17 "sync"
18 "testing"
19 "time"
20
21 "amadan.net/oficina/alta/adopt"
22 "amadan.net/oficina/alta/homeop"
23 "amadan.net/oficina/alta/provision"
24 "amadan.net/oficina/home/internal/operator"
25)
26
27// served is what each canonical app served in production on 2026-11-17:
28// the /api/version body and X-Carlos-Version, the same build named two
29// ways by four of the six.
30var served = map[string]struct{ body, header string }{
31 "docs": {"v0.1.0-80-g9de84e4", "v0.1.0-80-g9de84e4"},
32 "sheets": {"v0.1.0-53-gef9c313", "ef9c313"},
33 "slides": {"v0.1.0-42-gcb30df5", "cb30df5"},
34 "calendar": {"v0.1.0-53-g800406a", "800406a"},
35 "calls": {"ef2335e9df94bf9be1561fcbb1335c87e7c6e4ca", "ef2335e9df94bf9be1561fcbb1335c87e7c6e4ca"},
36 "memoria": {"v0.1.0-47-g174fb45", "174fb45"},
37 // Synthetic supported Equipo release; production contract is a separate branch.
38 "equipo": {"v1.0.0", "v1.0.0"},
39 // Base's live release on 2026-11-23 (carlos/base main).
40 "base": {"3f8e6d1", "3f8e6d1"},
41}
42
43// observer stands in for the CARLOS half's activation gate: what it
44// answers is shaped as adopt.Observer answers it (the body as app_version
45// once adopt.SameRelease admits the pair, Alta's artifact digest of the
46// header, the binding over the team), so Home's real activation checks
47// meet the evidence they will meet in production.
48type observer struct {
49 mu sync.Mutex
50 calls []string
51}
52
53func (o *observer) Observe(_ context.Context, app, team string) (provision.Observation, error) {
54 o.mu.Lock()
55 o.calls = append(o.calls, app)
56 o.mu.Unlock()
57 v, ok := served[app]
58 if !ok || !adopt.SameRelease(v.body, v.header) {
59 return provision.Observation{}, &adopt.Refusal{Codes: []string{"version-header-mismatch"}}
60 }
61 sum := sha256.Sum256([]byte(v.header))
62 return provision.Observation{
63 AppVersion: v.body,
64 ArtifactDigest: base64.RawURLEncoding.EncodeToString(sum[:]),
65 EnvironmentBinding: adopt.EnvironmentBinding(adopt.BindingInput{
66 TeamSQID: team, Issuer: adopt.Issuer, ArtifactSHA256: hex.EncodeToString(sum[:]), Version: v.body,
67 }),
68 }, nil
69}
70
71// homeCounts runs HomeQueries on Home's own tables, as Paul's read-only
72// carlos db query does, and answers what he would paste.
73func homeCounts(t *testing.T, h *home) map[string]int64 {
74 t.Helper()
75 counts := map[string]int64{}
76 for _, q := range adopt.HomeQueries() {
77 var n int64
78 if err := h.dirDB.QueryRow(q).Scan(&n); err != nil {
79 t.Fatalf("%s at Home's directory: %v", q, err)
80 }
81 counts[q[strings.LastIndex(q, " ")+1:]] = n
82 }
83 return counts
84}
85
86func homePreflight(t *testing.T, r *rig, phase string) {
87 t.Helper()
88 _, codes, err := adopt.CheckHome(context.Background(), r.client, r.store, homeCounts(t, r.home), phase)
89 if err != nil || len(codes) > 0 {
90 t.Fatalf("home preflight %s: %v %v", phase, codes, err)
91 }
92}
93
94// manifestDigest is the credential role's union manifest: the env
95// manifest and the active consumers table, as register answers it.
96func manifestDigest(t *testing.T, r *rig, env map[string]string) string {
97 t.Helper()
98 table, err := operator.ActiveConsumers(context.Background(), r.home.credDB)
99 if err != nil {
100 t.Fatal(err)
101 }
102 union := map[string]string{"directory": dirOrigin, "alta": "https://alta-x.bds.oncarlos.com"}
103 for id, o := range env {
104 union[id] = o
105 }
106 for id, o := range table {
107 union[id] = o
108 }
109 want, err := operator.ManifestDigest(union)
110 if err != nil {
111 t.Fatal(err)
112 }
113 if got := homeop.ManifestDigest(union); got != want {
114 t.Fatalf("manifest digest: Alta's is not Home's")
115 }
116 return want
117}
118
119func TestAdoptionFlowAgainstRealHome(t *testing.T) {
120 ctx := context.Background()
121 env := map[string]string{}
122 for _, a := range adoptionApps {
123 env[a] = homeop.CanonicalOrigin(a)
124 }
125 r := newRigWith(t, env, nil)
126 obs := &observer{}
127 cfg := r.cfg
128 cfg.Observer = obs
129 r.engine = provision.New(cfg)
130 before := manifestDigest(t, r, env)
131 window := func() { r.home.now = r.home.now.Add(homeop.WindowLifetime * time.Second) }
132
133 homePreflight(t, r, adopt.PhasePreInitialize)
134 run, err := r.engine.StartInitialize(ctx, provision.InitializeAttestation)
135 if err != nil {
136 t.Fatal(err)
137 }
138 r.home.confirm(t, "directory", "")
139 if got := r.drive(t, run); got.State != "done" {
140 t.Fatalf("initialize: %s %s", got.State, got.Code)
141 }
142 // Before Plan A, initialize's window must have lapsed: the preflight
143 // refuses an open plan.
144 if _, codes, err := adopt.CheckHome(ctx, r.client, r.store, homeCounts(t, r.home), adopt.PhasePrePlanA); err != nil || !slices.Contains(codes, "plan-open") {
145 t.Fatalf("pre-plan-a with initialize's window open: %v %v", codes, err)
146 }
147 window()
148 homePreflight(t, r, adopt.PhasePrePlanA)
149
150 run, err = r.engine.StartAdoption(ctx, "Oficina", owner)
151 if err != nil {
152 t.Fatal(err)
153 }
154 r.home.confirm(t, "directory", "")
155 r.home.confirm(t, "credential", owner)
156 if got := r.drive(t, run); got.State != "done" || got.Code != "awaiting-configuration" {
157 t.Fatalf("plan A: %s %s", got.State, got.Code)
158 }
159 team, err := r.store.Team(ctx, provision.AdoptionLabel)
160 if err != nil || team.TeamSQID == "" {
161 t.Fatalf("no seeded team: %v", err)
162 }
163
164 // Plans B and C: the canary first, then the other five.
165 rest := []string{}
166 for _, a := range provision.Slugs() {
167 if a != adopt.CanaryApp {
168 rest = append(rest, a)
169 }
170 }
171 for _, plan := range [][]string{{adopt.CanaryApp}, rest} {
172 window()
173 run, err := r.engine.ActivateAdopted(ctx, plan)
174 if err != nil {
175 t.Fatalf("activate %v: %v", plan, err)
176 }
177 for _, role := range []string{"directory", "credential"} {
178 if v := r.home.confirm(t, role, ""); v.Kind != homeop.KindAddApps || v.ExistingTeam != team.TeamSQID || len(v.Apps) != len(plan) {
179 t.Fatalf("%s plan view: kind %s apps %v", role, v.Kind, v.Apps)
180 }
181 }
182 if got := r.drive(t, run); got.State != "done" {
183 t.Fatalf("activate %v: %s %s", plan, got.State, got.Code)
184 }
185 }
186 if len(obs.calls) != len(adoptionApps) {
187 t.Fatalf("observed %v", obs.calls)
188 }
189
190 count := func(db *sql.DB, q string, args ...any) int {
191 t.Helper()
192 var n int
193 if err := db.QueryRow(q, args...).Scan(&n); err != nil {
194 t.Fatal(err)
195 }
196 return n
197 }
198 if n := count(r.home.dirDB, "SELECT count(*) FROM teams WHERE label='oficina'"); n != 1 {
199 t.Fatalf("teams %d", n)
200 }
201 // Every canonical binding is live, and there is no other.
202 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) {
203 t.Fatalf("live canonical rows %d", n)
204 }
205 if n := count(r.home.dirDB, "SELECT count(*) FROM team_instances"); n != len(adoptionApps) {
206 t.Fatalf("team_instances %d", n)
207 }
208 // Register verified the env manifest and inserted nothing.
209 if n := count(r.home.credDB, "SELECT count(*) FROM consumers"); n != 0 {
210 t.Fatalf("credential consumers %d", n)
211 }
212 if after := manifestDigest(t, r, env); after != before {
213 t.Fatal("the manifest digest changed")
214 }
215 for _, a := range adoptionApps {
216 data, ok, err := r.store.Done(ctx, provision.AdoptionLabel, a, provision.StepRegister)
217 if err != nil || !ok || data["manifest_digest"] != before {
218 t.Fatalf("%s: register's manifest digest is not the unchanged one", a)
219 }
220 // Home activated each app on its production /api/version body.
221 data, ok, err = r.store.Done(ctx, provision.AdoptionLabel, a, provision.StepActivate)
222 if err != nil || !ok || data["app_version"] != served[a].body {
223 t.Fatalf("%s: activated with app_version %v, want %q", a, data["app_version"], served[a].body)
224 }
225 }
226 // Alta's streams are Home's.
227 for _, role := range []string{"directory", "credential"} {
228 seq, head, _, _ := r.store.Stream(ctx, role)
229 db := r.home.dirDB
230 if role == "credential" {
231 db = r.home.credDB
232 }
233 var hs int64
234 var hh string
235 if err := db.QueryRow("SELECT sequence,head FROM "+role+"_operator_state WHERE singleton=1").Scan(&hs, &hh); err != nil {
236 t.Fatal(err)
237 }
238 if seq != hs || head != hh {
239 t.Fatalf("%s stream: Alta %d, Home %d", role, seq, hs)
240 }
241 }
242}
243
244// Canonical adoption verifies a pre-existing env registration; it cannot
245// create one or turn a missing registration into a live native route.
246func TestEquipoAdoptionRequiresRegisteredConsumer(t *testing.T) {
247 ctx := context.Background()
248 env := map[string]string{}
249 for _, a := range apps {
250 env[a] = homeop.CanonicalOrigin(a)
251 }
252 r := newRigWith(t, env, nil)
253 run, err := r.engine.StartInitialize(ctx, provision.InitializeAttestation)
254 if err != nil {
255 t.Fatal(err)
256 }
257 r.home.confirm(t, "directory", "")
258 if got := r.drive(t, run); got.State != "done" {
259 t.Fatalf("initialize: %+v", got)
260 }
261 r.home.now = r.home.now.Add(homeop.WindowLifetime * time.Second)
262 run, err = r.engine.StartAdoption(ctx, "Oficina", owner)
263 if err != nil {
264 t.Fatal(err)
265 }
266 r.home.confirm(t, "directory", "")
267 r.home.confirm(t, "credential", owner)
268 if got := r.drive(t, run); got.State != "failed" || got.Code != "register-refused" {
269 t.Fatalf("unregistered Equipo: %s %s", got.State, got.Code)
270 }
271 var state string
272 if err := r.home.dirDB.QueryRow("SELECT state FROM team_instances WHERE consumer_id='equipo'").Scan(&state); err != nil || state != "pending" {
273 t.Fatalf("unregistered state: %s %v", state, err)
274 }
275 var n int
276 if err := r.home.credDB.QueryRow("SELECT count(*) FROM consumers").Scan(&n); err != nil || n != 0 {
277 t.Fatalf("adoption created consumers: %d %v", n, err)
278 }
279}
280