oficina / alta Public

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

Plain git — no account needed to clone.

Download

Download this file

1package provision
2
3import (
4 "crypto/sha256"
5 "encoding/hex"
6 "encoding/json"
7 "errors"
8 "maps"
9 "slices"
10 "testing"
11 "time"
12
13 "amadan.net/oficina/alta/homeop"
14)
15
16// The ruled list (Paul, 2026-11-12), in one place: ReservedLabels.
17func TestReservedLabelsAreTheRuledList(t *testing.T) {
18 want := []string{
19 "oficina",
20 "docs", "sheets", "slides", "calendar", "calls", "memoria", "equipo", "fichas", "mapa",
21 "api", "auth", "healthz", "static", "www", "id", "admin", "portero", "home", "alta", "base", "forget",
22 }
23 got := slices.Sorted(maps.Keys(ReservedLabels))
24 slices.Sort(want)
25 if !slices.Equal(got, want) {
26 t.Fatalf("reserved labels\n got %v\nwant %v", got, want)
27 }
28 for _, a := range Apps {
29 if !ReservedLabels[a.Slug] {
30 t.Fatalf("registry app %s is not a reserved label", a.Slug)
31 }
32 }
33 for label, code := range map[string]string{
34 "lagged": "",
35 "lag-ged": "",
36 "a-b-c": "",
37 "xn--bcher": "label-double-hyphen",
38 "lag--ged": "label-double-hyphen",
39 "docs": "label-reserved",
40 "oficina": "label-reserved",
41 "forget": "label-reserved",
42 "Docs": "label-shape",
43 "ab": "label-shape",
44 } {
45 if got := LabelRefusal(label); got != code {
46 t.Fatalf("LabelRefusal(%q) = %q, want %q", label, got, code)
47 }
48 }
49}
50
51func TestReservedAndPunycodeLabelsRefusedBeforeAnythingIsRecorded(t *testing.T) {
52 h := newHarness(t)
53 h.initialized()
54 e := h.engine(nil)
55 var fl *Fail
56 for _, label := range append(slices.Sorted(maps.Keys(ReservedLabels)), "xn--lagged", "lag--ged") {
57 want := "label-reserved"
58 switch {
59 case !LabelPattern.MatchString(label):
60 want = "label-shape" // "id": under three characters already
61 case !ReservedLabels[label]:
62 want = "label-double-hyphen"
63 }
64 if _, err := e.CreateTeam(h.ctx(), NewTeam{Label: label, Name: "x", Owner: ownerCanary, Apps: []string{"docs"}}); !errors.As(err, &fl) || fl.Code != want {
65 t.Fatalf("label %q: %v", label, err)
66 }
67 if used, err := h.store.LabelUsed(h.ctx(), label); err != nil || used {
68 t.Fatalf("refused label %q was recorded: %v %v", label, used, err)
69 }
70 }
71}
72
73var goldenInputs = Inputs{Label: "lagged", Owner: "o@x.test", TeamSQID: "Uk", Record: []byte("{}"), Home: HomeConfig{Issuer: "https://id.oficina.works", Directory: "https://oficina.works", KeysBase64: "S0VZUw==", Provisioner: "AQ"}, CallsZone: "Europe/Dublin"}
74
75// outsideGolden is SHA-256 over the JSON of every app's recipe, in
76// registry order, for goldenInputs, as origin/main 2637793 (before
77// profiles existed) computes it. outside is that, byte for byte.
78const outsideGolden = "3836921a89ecd9942eead9b48db0609ed5509586348231bb1c3608112ce38405"
79
80func recipesDigest(in Inputs) string {
81 var all [][]Key
82 for _, a := range ProvisioningApps() {
83 if a.OwnOnly {
84 // Never written under outside (step 0 refuses it), so not
85 // part of today's outside recipes.
86 continue
87 }
88 all = append(all, Recipe(a, in))
89 }
90 b, _ := json.Marshal(all)
91 s := sha256.Sum256(b)
92 return hex.EncodeToString(s[:])
93}
94
95func TestOutsideIsTodaysRecipes(t *testing.T) {
96 for _, opt := range []RunOptions{{}, {Profile: ProfileOutside}} {
97 in := goldenInputs
98 in.RunOptions = opt
99 if got := recipesDigest(in); got != outsideGolden {
100 t.Fatalf("outside recipes (%+v) moved: %s", opt, got)
101 }
102 }
103}
104
105// keyMap is a recipe by name.
106func keyMap(keys []Key) map[string]Key {
107 out := map[string]Key{}
108 for _, k := range keys {
109 out[k.Name] = k
110 }
111 return out
112}
113
114// recipeDiff is what b adds or changes against a, by name; removed names
115// are reported with an empty Key.
116func recipeDiff(a, b []Key) map[string]Key {
117 am, bm := keyMap(a), keyMap(b)
118 out := map[string]Key{}
119 for n, k := range bm {
120 if old, ok := am[n]; !ok || old != k {
121 out[n] = k
122 }
123 }
124 for n := range am {
125 if _, ok := bm[n]; !ok {
126 out[n] = Key{}
127 }
128 }
129 return out
130}
131
132// own differs from outside only in the listed keys, and samples only in
133// Docs' DOCS_SAMPLE_DOCUMENTS, in either profile.
134func TestOwnDiffersOnlyInTheRuledKeys(t *testing.T) {
135 dash := Key{Name: "OFICINA_DASHBOARD_TEAM", Class: Set, Value: "lagged"}
136 wantOwn := map[string]map[string]Key{
137 "docs": {dash.Name: dash},
138 "sheets": {dash.Name: dash},
139 "slides": {dash.Name: dash},
140 "calendar": {dash.Name: dash},
141 "calls": {"CALLS_QUALITY_REPORTS": {Name: "CALLS_QUALITY_REPORTS", Class: Set, Value: "on"}},
142 "memoria": {},
143 }
144 samples := Key{Name: "DOCS_SAMPLE_DOCUMENTS", Class: Set, Value: "1"}
145 for _, app := range ProvisioningApps() {
146 outside, own := goldenInputs, goldenInputs
147 outside.Profile, own.Profile = ProfileOutside, ProfileOwn
148 if d := recipeDiff(Recipe(app, outside), Recipe(app, own)); !maps.Equal(d, wantOwn[app.Slug]) {
149 t.Fatalf("%s: own against outside\n got %v\nwant %v", app.Slug, d, wantOwn[app.Slug])
150 }
151 for _, p := range Profiles {
152 without, with := goldenInputs, goldenInputs
153 without.Profile, with.Profile, with.Samples = p, p, true
154 want := map[string]Key{}
155 if app.Slug == "docs" {
156 want[samples.Name] = samples
157 }
158 if d := recipeDiff(Recipe(app, without), Recipe(app, with)); !maps.Equal(d, want) {
159 t.Fatalf("%s %s: samples\n got %v\nwant %v", app.Slug, p, d, want)
160 }
161 }
162 }
163}
164
165// Alta never holds or writes Memoria's TensorX key, nor turns AI on: in
166// any profile the key is only ever Paul's, admitted by the audit.
167func TestNoProfileWritesMemoriaAI(t *testing.T) {
168 app, _ := AppBySlug("memoria")
169 for _, p := range Profiles {
170 in := goldenInputs
171 in.Profile, in.Samples = p, true
172 for _, k := range Recipe(app, in) {
173 if k.Name == "MEMORIA_AI" || k.Name == "MEMORIA_TENSORX_KEY" {
174 t.Fatalf("%s recipe writes %s", p, k.Name)
175 }
176 }
177 }
178 outside, own := goldenInputs, goldenInputs
179 outside.Profile, own.Profile = ProfileOutside, ProfileOwn
180 if len(OperatorKeys(app, outside)) != 0 {
181 t.Fatal("an outside team admits Memoria AI")
182 }
183 got := OperatorKeys(app, own)
184 if len(got) != 2 || got[0] != (OperatorKey{Name: "MEMORIA_AI", Value: "on"}) || got[1] != (OperatorKey{Name: "MEMORIA_TENSORX_KEY", Secret: true}) {
185 t.Fatalf("own memoria operator keys %v", got)
186 }
187 for _, a := range ProvisioningApps() {
188 if a.Slug != "memoria" && a.Slug != "equipo" && len(OperatorKeys(a, own)) != 0 {
189 t.Fatalf("%s has operator keys", a.Slug)
190 }
191 }
192}
193
194// An own run with samples, end to end: recorded on the run, written into
195// each environment, audited against the profile the environment was
196// written with, and continued with the same choice.
197func TestOwnRunWritesAndAuditsItsProfile(t *testing.T) {
198 h := newHarness(t)
199 h.initialized()
200 e := h.engine(nil)
201 run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "sample", Name: "Sample", Owner: ownerCanary, Apps: allApps, Options: RunOptions{Profile: ProfileOwn, Samples: true}})
202 if err != nil {
203 t.Fatal(err)
204 }
205 if r, _ := h.store.Run(h.ctx(), run); r.Options != (RunOptions{Profile: ProfileOwn, Samples: true}) {
206 t.Fatalf("run options %+v", r.Options)
207 }
208 h.confirm(homeop.KindCreateTeam)
209 if r := h.drive(e, run, 40); r.State != "done" {
210 t.Fatalf("%s %s", r.State, r.Code)
211 }
212 val := func(app, key string) (string, bool) {
213 vars, _, _ := h.con.Env(app, "sample")
214 v, ok := vars[key]
215 return v.Value, ok
216 }
217 for _, app := range []string{"docs", "sheets", "slides", "calendar"} {
218 if v, _ := val(app, "OFICINA_DASHBOARD_TEAM"); v != "sample" {
219 t.Fatalf("%s dashboard team %q", app, v)
220 }
221 }
222 for _, app := range []string{"calls", "memoria"} {
223 if _, ok := val(app, "OFICINA_DASHBOARD_TEAM"); ok {
224 t.Fatalf("%s got a dashboard team it does not read", app)
225 }
226 }
227 if v, _ := val("calls", "CALLS_QUALITY_REPORTS"); v != "on" {
228 t.Fatalf("quality reports %q", v)
229 }
230 if v, _ := val("docs", "DOCS_SAMPLE_DOCUMENTS"); v != "1" {
231 t.Fatalf("samples %q", v)
232 }
233 if _, ok := val("memoria", "MEMORIA_AI"); ok {
234 t.Fatal("Alta turned Memoria AI on")
235 }
236
237 // Paul's post-run step: the AI pair, together, is admitted; either
238 // alone, or a value memoria would refuse, is not.
239 memoria, _ := AppBySlug("memoria")
240 base, owner, _ := h.con.Env("memoria", "sample")
241 with := func(extra map[string]fakeVar) {
242 vars := maps.Clone(base)
243 maps.Copy(vars, extra)
244 h.con.SetEnv("memoria", "sample", owner, vars)
245 }
246 key := fakeVar{Kind: "secret", Envelope: sealFor(t, h, "memoria", "tensorx-canary")}
247 var fl *Fail
248 for _, c := range []struct {
249 extra map[string]fakeVar
250 code string
251 }{
252 {map[string]fakeVar{}, ""},
253 {map[string]fakeVar{"MEMORIA_AI": {Kind: "env", Value: "on"}, "MEMORIA_TENSORX_KEY": key}, ""},
254 {map[string]fakeVar{"MEMORIA_AI": {Kind: "env", Value: "on"}}, "audit-operator-partial"},
255 {map[string]fakeVar{"MEMORIA_TENSORX_KEY": key}, "audit-operator-partial"},
256 {map[string]fakeVar{"MEMORIA_AI": {Kind: "env", Value: "yes"}, "MEMORIA_TENSORX_KEY": key}, "operator-keys-invalid"},
257 {map[string]fakeVar{"MEMORIA_AI": {Kind: "env", Value: "on"}, "MEMORIA_TENSORX_KEY": {Kind: "env", Value: "plain"}}, "operator-keys-invalid"},
258 {map[string]fakeVar{"MEMORIA_AI": {Kind: "env", Value: "on"}, "MEMORIA_TENSORX_KEY": key, "MEMORIA_SIBLINGS": {Kind: "env", Value: "x"}}, "audit-extra-key"},
259 } {
260 with(c.extra)
261 err := e.AuditEnvironment(h.ctx(), "sample", memoria)
262 if c.code == "" && err != nil || c.code != "" && (!errors.As(err, &fl) || fl.Code != c.code) {
263 t.Fatalf("audit with %v: %v, want %q", slices.Sorted(maps.Keys(c.extra)), err, c.code)
264 }
265 }
266 if h.scan("tensorx-canary") {
267 t.Fatal("Paul's TensorX key reached Alta's database")
268 }
269
270 // Continue without a fresh choice keeps own and samples.
271 h.tick(homeop.WindowLifetime * time.Second)
272 next, err := e.AddApps(h.ctx(), "sample", allApps, RunOptions{})
273 if err != nil {
274 t.Fatal(err)
275 }
276 if r, _ := h.store.Run(h.ctx(), next); r.Options != (RunOptions{Profile: ProfileOwn, Samples: true}) {
277 t.Fatalf("continued run options %+v", r.Options)
278 }
279}
280
281// The outside team's audit refuses the AI pair: AI stays off for anyone
282// else's team.
283func TestOutsideAuditRefusesMemoriaAI(t *testing.T) {
284 h := newHarness(t)
285 h.initialized()
286 e := h.engine(nil)
287 run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "beta", Name: "Beta", Owner: ownerCanary, Apps: []string{"memoria"}})
288 if err != nil {
289 t.Fatal(err)
290 }
291 h.confirm(homeop.KindCreateTeam)
292 if r := h.drive(e, run, 20); r.State != "done" {
293 t.Fatalf("%s %s", r.State, r.Code)
294 }
295 vars, owner, _ := h.con.Env("memoria", "beta")
296 vars["MEMORIA_AI"] = fakeVar{Kind: "env", Value: "on"}
297 vars["MEMORIA_TENSORX_KEY"] = fakeVar{Kind: "secret", Envelope: sealFor(t, h, "memoria", "k")}
298 h.con.SetEnv("memoria", "beta", owner, vars)
299 memoria, _ := AppBySlug("memoria")
300 var fl *Fail
301 if err := e.AuditEnvironment(h.ctx(), "beta", memoria); !errors.As(err, &fl) || fl.Code != "audit-extra-key" {
302 t.Fatalf("outside audit admitted Memoria AI: %v", err)
303 }
304}
305
306// An environment written under one run's choice and continued under
307// another's: Alta takes its own profile keys back out (unforked, where any
308// other extra key refuses) and audits the result as the later choice.
309func TestLaterRunTakesItsOwnProfileKeysBackOut(t *testing.T) {
310 h := newHarness(t)
311 h.initialized()
312 e := h.engine(nil)
313 run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "gamma", Name: "Gamma", Owner: ownerCanary, Apps: []string{"docs"}, Options: RunOptions{Profile: ProfileOwn, Samples: true}})
314 if err != nil {
315 t.Fatal(err)
316 }
317 h.confirm(homeop.KindCreateTeam)
318 // No walk yet: the environment is written and parks for its mail.
319 if err := e.Advance(h.ctx(), run); err != nil {
320 t.Fatal(err)
321 }
322 vars, _, _ := h.con.Env("docs", "gamma")
323 if _, ok := vars["OFICINA_DASHBOARD_TEAM"]; !ok {
324 t.Fatalf("own keys not written before the park")
325 }
326 if _, ok, _ := h.store.Done(h.ctx(), "gamma", "docs", StepEnv); ok {
327 t.Fatal("env step done without platform keys")
328 }
329 h.tick(homeop.WindowLifetime * time.Second)
330 next, err := e.AddApps(h.ctx(), "gamma", []string{"docs"}, RunOptions{Profile: ProfileOutside})
331 if err != nil {
332 t.Fatal(err)
333 }
334 h.confirm(homeop.KindAddApps)
335 if r := h.drive(e, next, 30); r.State != "done" {
336 t.Fatalf("%s %s", r.State, r.Code)
337 }
338 vars, _, _ = h.con.Env("docs", "gamma")
339 for _, k := range []string{"OFICINA_DASHBOARD_TEAM", "DOCS_SAMPLE_DOCUMENTS"} {
340 if _, ok := vars[k]; ok {
341 t.Fatalf("%s survived the outside run", k)
342 }
343 }
344 docs, _ := AppBySlug("docs")
345 if err := e.AuditEnvironment(h.ctx(), "gamma", docs); err != nil {
346 t.Fatalf("audit: %v", err)
347 }
348}
349
350// A forked stopgap moved under own: the fork's blanked MEMORIA_AI is not
351// Paul's post-run pair, so it goes like any Track 1 extra.
352func TestOwnForkDropsABlankedMemoriaAI(t *testing.T) {
353 h := newHarness(t)
354 h.initialized()
355 h.con.SetEnv("memoria", "delta-team", "alta", map[string]fakeVar{
356 "MEMORIA_DEK": {Kind: "secret", Envelope: sealFor(t, h, "memoria", "data-key")},
357 "MEMORIA_INSTANCE_KEY": {Kind: "secret", Envelope: sealFor(t, h, "memoria", "instance")},
358 "MEMORIA_AI": {Kind: "env", Value: ""},
359 })
360 h.con.AddInstance("memoria", RouteHost("delta", "memoria", account), "delta-team")
361 h.con.AddClaim("memoria", PublicHost("delta", "memoria"), RouteHost("delta", "memoria", account), "live")
362 e := h.engine(nil)
363 run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "delta", Name: "Delta", Owner: ownerCanary, Apps: []string{"memoria"}, Forked: true, Options: RunOptions{Profile: ProfileOwn}})
364 if err != nil {
365 t.Fatal(err)
366 }
367 h.confirm(homeop.KindCreateTeam)
368 if r := h.drive(e, run, 20); r.State != "done" {
369 t.Fatalf("%s %s", r.State, r.Code)
370 }
371 if vars, _, _ := h.con.Env("memoria", "delta-team"); vars["MEMORIA_AI"] != (fakeVar{}) {
372 t.Fatal("the fork's blanked MEMORIA_AI survived")
373 }
374}
375
376// driveOne creates label with apps under opt and drives it to done.
377func driveOne(t *testing.T, h *harness, e *Engine, label string, apps []string, opt RunOptions) {
378 t.Helper()
379 run, err := e.CreateTeam(h.ctx(), NewTeam{Label: label, Name: "Team", Owner: ownerCanary, Apps: apps, Options: opt})
380 if err != nil {
381 t.Fatal(err)
382 }
383 h.confirm(homeop.KindCreateTeam)
384 if r := h.drive(e, run, 30); r.State != "done" {
385 t.Fatalf("%s %s", r.State, r.Code)
386 }
387 h.tick(homeop.WindowLifetime * time.Second)
388}
389
390// A written environment keeps its choice: a run under another choice
391// would finish having changed nothing, so it is refused, either way.
392func TestProfileChangeOverAWrittenEnvironmentIsRefused(t *testing.T) {
393 h := newHarness(t)
394 h.initialized()
395 e := h.engine(nil)
396 own := RunOptions{Profile: ProfileOwn, Samples: true}
397 driveOne(t, h, e, "omega", []string{"docs", "memoria"}, own)
398 driveOne(t, h, e, "sigma", []string{"docs", "memoria"}, RunOptions{Profile: ProfileOutside})
399 var fl *Fail
400 for _, c := range []struct {
401 label string
402 apps []string
403 opt RunOptions
404 code string
405 }{
406 {"omega", []string{"docs"}, RunOptions{Profile: ProfileOutside}, "profile-differs-docs"},
407 {"omega", []string{"docs"}, RunOptions{Profile: ProfileOwn}, "profile-differs-docs"},
408 {"omega", []string{"memoria"}, RunOptions{Profile: ProfileOutside, Samples: true}, "profile-differs-memoria"},
409 {"sigma", []string{"memoria"}, RunOptions{Profile: ProfileOwn}, "profile-differs-memoria"},
410 {"sigma", []string{"docs"}, RunOptions{Profile: ProfileOutside, Samples: true}, "profile-differs-docs"},
411 } {
412 before, _ := h.store.Runs(h.ctx(), c.label)
413 if _, err := e.AddApps(h.ctx(), c.label, c.apps, c.opt); !errors.As(err, &fl) || fl.Code != c.code {
414 t.Fatalf("%s %v %+v: %v, want %s", c.label, c.apps, c.opt, err, c.code)
415 }
416 if after, _ := h.store.Runs(h.ctx(), c.label); len(after) != len(before) {
417 t.Fatal("a refused profile change recorded a run")
418 }
419 }
420 // The same choice, or Continue, is admitted and keeps what was written.
421 for label, want := range map[string]RunOptions{"omega": own, "sigma": {Profile: ProfileOutside}} {
422 for _, opt := range []RunOptions{{}, want} {
423 next, err := e.AddApps(h.ctx(), label, []string{"docs", "memoria"}, opt)
424 if err != nil {
425 t.Fatalf("%s %+v: %v", label, opt, err)
426 }
427 if r, _ := h.store.Run(h.ctx(), next); r.Options != want {
428 t.Fatalf("%s continued as %+v", label, r.Options)
429 }
430 }
431 }
432}
433
434// "Alta's own keys" is by value too: a foreign OFICINA_DASHBOARD_TEAM or
435// DOCS_SAMPLE_DOCUMENTS in an unforked environment is an extra key.
436func TestForeignProfileValuesAreExtraKeys(t *testing.T) {
437 for _, foreign := range []fakeVar{{Kind: "env", Value: "oficina"}, {Kind: "env", Value: "yes"}, {Kind: "secret", Envelope: "x"}} {
438 for _, name := range []string{"OFICINA_DASHBOARD_TEAM", "DOCS_SAMPLE_DOCUMENTS"} {
439 h := newHarness(t)
440 h.initialized()
441 e := h.engine(nil)
442 run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "kappa", Name: "Kappa", Owner: ownerCanary, Apps: []string{"docs"}})
443 if err != nil {
444 t.Fatal(err)
445 }
446 h.confirm(homeop.KindCreateTeam)
447 if err := e.Advance(h.ctx(), run); err != nil {
448 t.Fatal(err)
449 }
450 vars, owner, _ := h.con.Env("docs", "kappa")
451 vars[name] = foreign
452 h.con.SetEnv("docs", "kappa", owner, vars)
453 if r := h.drive(e, run, 10); r.State != "failed" || r.Code != "extra-key" {
454 t.Fatalf("%s=%+v: %s %s", name, foreign, r.State, r.Code)
455 }
456 if vars, _, _ := h.con.Env("docs", "kappa"); vars[name] != foreign {
457 t.Fatalf("%s was taken out", name)
458 }
459 }
460 }
461}
462
463// Step 7 keeps Paul's AI pair only as memoria reads it.
464func TestInvalidMemoriaAIPairFailsAtStep7(t *testing.T) {
465 h := newHarness(t)
466 h.initialized()
467 e := h.engine(nil)
468 run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "theta", Name: "Theta", Owner: ownerCanary, Apps: []string{"memoria"}, Options: RunOptions{Profile: ProfileOwn}})
469 if err != nil {
470 t.Fatal(err)
471 }
472 h.confirm(homeop.KindCreateTeam)
473 if err := e.Advance(h.ctx(), run); err != nil {
474 t.Fatal(err)
475 }
476 if _, ok, _ := h.store.Done(h.ctx(), "theta", "memoria", StepEnv); ok {
477 t.Fatal("env step done before its platform keys")
478 }
479 vars, owner, _ := h.con.Env("memoria", "theta")
480 vars["MEMORIA_AI"] = fakeVar{Kind: "env", Value: "1"}
481 vars["MEMORIA_TENSORX_KEY"] = fakeVar{Kind: "secret", Envelope: sealFor(t, h, "memoria", "k")}
482 h.con.SetEnv("memoria", "theta", owner, vars)
483 if r := h.drive(e, run, 10); r.State != "failed" || r.Code != "operator-keys-invalid" {
484 t.Fatalf("%s %s", r.State, r.Code)
485 }
486 if st, _, _ := h.store.Step(h.ctx(), "theta", "memoria", StepEnv); st.State != "failed" {
487 t.Fatalf("step 7 %s", st.State)
488 }
489}
490
491// Fable's N1 and N2: Docs written own, Calls then added outside and parked
492// before step 7 recorded. Continue takes only the unfinished app (Calls),
493// under its own run's choice, never Docs' written own; continuing both
494// together is refused, naming the app that disagrees.
495func TestContinueTakesUnfinishedAppsUnderTheirOwnChoice(t *testing.T) {
496 h := newHarness(t)
497 h.initialized()
498 e := h.engine(nil)
499 driveOne(t, h, e, "mixed", []string{"docs"}, RunOptions{Profile: ProfileOwn})
500 add, err := e.AddApps(h.ctx(), "mixed", []string{"calls"}, RunOptions{Profile: ProfileOutside})
501 if err != nil {
502 t.Fatal(err)
503 }
504 h.confirm(homeop.KindAddApps)
505 if err := e.Advance(h.ctx(), add); err != nil {
506 t.Fatal(err)
507 }
508 if st, ok, _ := h.store.Step(h.ctx(), "mixed", "calls", StepEnv); !ok || st.State != "parked" || st.Code != "awaiting-platform-keys" {
509 t.Fatalf("calls step 7: %+v %v", st, ok)
510 }
511 h.tick(homeop.WindowLifetime * time.Second)
512
513 var fl *Fail
514 if _, err := e.AddApps(h.ctx(), "mixed", []string{"docs", "calls"}, RunOptions{}); !errors.As(err, &fl) || fl.Code != "profile-differs-calls" {
515 t.Fatalf("continue over both: %v", err)
516 }
517 apps, _, err := e.ContinueApps(h.ctx(), "mixed")
518 if err != nil || !slices.Equal(apps, []string{"calls"}) {
519 t.Fatalf("continue apps %v %v", apps, err)
520 }
521 next, err := e.AddApps(h.ctx(), "mixed", apps, RunOptions{})
522 if err != nil {
523 t.Fatal(err)
524 }
525 if r, _ := h.store.Run(h.ctx(), next); r.Options != (RunOptions{Profile: ProfileOutside}) || !slices.Equal(r.Apps, []string{"calls"}) {
526 t.Fatalf("continued run %+v %v", r.Options, r.Apps)
527 }
528 h.confirm(homeop.KindAddApps)
529 if r := h.drive(e, next, 30); r.State != "done" {
530 t.Fatalf("%s %s", r.State, r.Code)
531 }
532 vars, _, _ := h.con.Env("calls", "mixed")
533 if v := vars["CALLS_QUALITY_REPORTS"].Value; v != "off" {
534 t.Fatalf("outside Calls written with quality reports %q", v)
535 }
536 if opt, ok, _ := h.store.WrittenOptions(h.ctx(), "mixed", "docs"); !ok || opt.Profile != ProfileOwn {
537 t.Fatalf("docs written %+v", opt)
538 }
539 // Everything finished: Continue covers every active app, whose
540 // written choices disagree, and says so rather than guessing.
541 apps, finished, _ := e.ContinueApps(h.ctx(), "mixed")
542 if !finished {
543 t.Fatal("a finished team has apps to continue")
544 }
545 if _, err := e.ContinuedOptions(h.ctx(), "mixed", apps); !errors.As(err, &fl) || fl.Code != "profile-differs-calls" {
546 t.Fatalf("finished mixed team: %v", err)
547 }
548}
549
550// Fable's L5: a wrong choice on an app not yet written is corrected by
551// continuing under an explicit one, for a seeded team and for a
552// create-team run that never reached Home (whose label is already spent).
553func TestContinueUnderAnExplicitChoiceCorrectsAnUnwrittenApp(t *testing.T) {
554 h := newHarness(t)
555 h.initialized()
556 e := h.engine(nil)
557 outside := RunOptions{Profile: ProfileOutside}
558
559 // Seeded: Calls created own, parked before step 7 recorded.
560 run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "fix", Name: "Fix", Owner: ownerCanary, Apps: []string{"calls"}, Options: RunOptions{Profile: ProfileOwn}})
561 if err != nil {
562 t.Fatal(err)
563 }
564 h.confirm(homeop.KindCreateTeam)
565 if err := e.Advance(h.ctx(), run); err != nil {
566 t.Fatal(err)
567 }
568 if _, ok, _ := h.store.WrittenOptions(h.ctx(), "fix", "calls"); ok {
569 t.Fatal("calls written before its platform keys")
570 }
571 h.tick(homeop.WindowLifetime * time.Second)
572 apps, _, _ := e.ContinueApps(h.ctx(), "fix")
573 next, err := e.AddApps(h.ctx(), "fix", apps, outside)
574 if err != nil {
575 t.Fatal(err)
576 }
577 h.confirm(homeop.KindAddApps)
578 if r := h.drive(e, next, 30); r.State != "done" {
579 t.Fatalf("%s %s", r.State, r.Code)
580 }
581 if vars, _, _ := h.con.Env("calls", "fix"); vars["CALLS_QUALITY_REPORTS"].Value != "off" {
582 t.Fatalf("calls quality reports %q", vars["CALLS_QUALITY_REPORTS"].Value)
583 }
584 if opt, ok, _ := h.store.WrittenOptions(h.ctx(), "fix", "calls"); !ok || opt != outside {
585 t.Fatalf("calls written %+v", opt)
586 }
587 // Now written: the same correction the other way is refused.
588 var fl *Fail
589 if _, err := e.AddApps(h.ctx(), "fix", []string{"calls"}, RunOptions{Profile: ProfileOwn}); !errors.As(err, &fl) || fl.Code != "profile-differs-calls" {
590 t.Fatalf("written calls: %v", err)
591 }
592
593 // Unseeded: a create-team plan never confirmed. Continue under an
594 // explicit choice resubmits the create-team plan under it.
595 if _, err := e.CreateTeam(h.ctx(), NewTeam{Label: "spent", Name: "Spent", Owner: ownerCanary, Apps: []string{"docs"}, Options: RunOptions{Profile: ProfileOwn, Samples: true}}); err != nil {
596 t.Fatal(err)
597 }
598 h.tick(homeop.WindowLifetime * time.Second)
599 apps, _, _ = e.ContinueApps(h.ctx(), "spent")
600 again, err := e.AddApps(h.ctx(), "spent", apps, outside)
601 if err != nil {
602 t.Fatal(err)
603 }
604 r, _ := h.store.Run(h.ctx(), again)
605 if r.Kind != homeop.KindCreateTeam || r.Options != outside || !slices.Equal(r.Apps, []string{"docs"}) {
606 t.Fatalf("resumed create %s %+v %v", r.Kind, r.Options, r.Apps)
607 }
608}
609