package adopt import ( "net/http" "testing" "amadan.net/oficina/alta/provision" ) // baseOps401 is Base's own operations door answering a request with no // host token, byte for byte as carlos/base 3f8e6d1 writes it // (internal/operations/operations.go serve and fail): Base serves // Equipo's Assistant there in agent-access's shapes, before any adoption. func baseOps401(w http.ResponseWriter) { w.Header().Set("Cache-Control", "no-store") w.Header().Set("WWW-Authenticate", "Bearer") w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusUnauthorized) _, _ = w.Write([]byte("{\"error\":{\"code\":\"unauthenticated\",\"message\":\"Base did not accept this token.\"}}\n")) } // Base's door already gives agent-access's challenge, so the strict // classifier cannot tell it from agent-access. That is why Base's // baseline is agent-401 and not a 404 or an app 401. func TestBaseDoorIsAgentShapedBeforeAdoption(t *testing.T) { if got := probeOf(t, &site{version: "v1", header: "v1", health: 200, ops: baseOps401}).Ops; got != OpsAgent401 { t.Fatalf("Base's own door classified %q", got) } } // The held-door rule (Paul approved, 2026-11-23, docs/base-adoption.md): // Base's door answers agent-access's challenge before configure and must // still answer it, and only it, after the restart. Every other move is // refused, an unauthenticated 200 included. func TestBaseAdoptionDelivery(t *testing.T) { app, ok := provision.AppBySlug("base") if !ok { t.Fatal("Base missing") } if AgentKeys(app) != ([2]string{"BASE_AGENT_ISSUER", "BASE_AGENT_TEAM"}) { t.Fatal("wrong Base agent keys") } if BaselineOps(app) != OpsAgent401 { t.Fatal("Base's baseline is its own door's agent-shaped 401") } before := Delivery{Version: "v1.0.0", Header: "v1.0.0", Health: 200, Ops: OpsAgent401} after := before if err := Delivered(app, before, after); err != nil { t.Fatalf("held door refused: %v", err) } for _, shape := range []OpsShape{OpsApp401, OpsAbsent, OpsAgent200, OpsOther} { after.Ops = shape if Delivered(app, before, after) == nil { t.Fatalf("Base delivered with %s", shape) } } for _, shape := range []OpsShape{OpsApp401, OpsAbsent, OpsAgent200, OpsOther, ""} { before.Ops = shape if Delivered(app, before, Delivery{Version: "v1.0.0", Header: "v1.0.0", Health: 200, Ops: OpsAgent401}) == nil { t.Fatalf("Base delivered from a %q baseline", shape) } } // The rest of the check is not relaxed for Base. held := Delivery{Version: "v1.0.0", Header: "v1.0.0", Health: 200, Ops: OpsAgent401} for name, now := range map[string]Delivery{ "version": {Version: "v1.0.1", Header: "v1.0.0", Health: 200, Ops: OpsAgent401}, "header": {Version: "v1.0.0", Header: "v1.0.1", Health: 200, Ops: OpsAgent401}, "health": {Version: "v1.0.0", Header: "v1.0.0", Health: 503, Ops: OpsAgent401}, "redirect": {Version: "v1.0.0", Header: "v1.0.0", Health: 200, Ops: OpsAgent401, Redirected: true}, } { if Delivered(app, held, now) == nil { t.Fatalf("Base delivered with a changed %s", name) } } // A rollback restart holds the same door, as for every app. if err := Reverted(held, held); err != nil { t.Fatalf("Base rollback refused: %v", err) } } // Preflight admits Base only with its door giving agent-access's // challenge: a 404 or an app 401 means the canonical host is not serving // the Base release whose door the held rule describes. func TestPreflightHoldsBaseToItsDoor(t *testing.T) { for _, c := range []struct { ops func(w http.ResponseWriter) want string }{ {baseOps401, ""}, {agent401, ""}, {nil, "operations-absent"}, {app401, "operations-app-401"}, {agent200(`{"operations":[]}`), "operations-agent-200"}, } { x := newCarlosFixture(t, "base") x.site.ops = c.ops _, codes := x.preflight(t, nil) if c.want == "" { wantCodes(t, codes) continue } wantCodes(t, codes, c.want) } // On a resume, with both agent keys the journal's own, the exemption // for a door that has moved does not apply: Base's door never moves, // so it must still give the challenge, never a 200 without a token. keys := AgentKeys(appNamed(t, "base")) mine := map[string]string{keys[0]: Issuer, keys[1]: fixtureSQID} for _, c := range []struct { ops func(w http.ResponseWriter) want string }{ {baseOps401, ""}, {agent200(`{"operations":[]}`), "operations-agent-200"}, {nil, "operations-absent"}, } { x := newCarlosFixture(t, "base") for k, v := range mine { x.f.SetVar("base", "", k, v) } x.site.ops = c.ops _, codes := x.preflight(t, mine) if c.want == "" { wantCodes(t, codes) continue } wantCodes(t, codes, c.want) } } // The runner's whole delivery check records for Base with its door // held, and refuses when the door stops answering as it did. func TestCheckDeliveryRecordsBaseWithItsDoorHeld(t *testing.T) { w := newCheckFixture(t, "base") w.site.ops = baseOps401 if err := w.check(); err != nil { t.Fatalf("Base check refused: %v", err) } if _, ok, err := w.s.DeliveryCheck(t.Context(), "base"); err != nil || !ok { t.Fatalf("no check recorded (ok %v, err %v)", ok, err) } for name, ops := range map[string]func(http.ResponseWriter){ "absent": nil, "app-401": app401, "open": agent200(`{"operations":[]}`), "503": respond(http.StatusServiceUnavailable, map[string]string{"Cache-Control": "no-store", "Content-Type": "application/json"}, `{"error":{"code":"unavailable","message":"Base could not answer. Try again."}}`), "redirect": respond(http.StatusFound, map[string]string{"Location": "/signin"}, ""), } { w := newCheckFixture(t, "base") w.site.ops = ops if err := w.check(); err == nil { t.Fatalf("Base check recorded with its door %s", name) } w.noCheck(t) } }