| 1 | package adopt |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "testing" |
| 6 | |
| 7 | "amadan.net/oficina/alta/provision" |
| 8 | ) |
| 9 | |
| 10 | // baseOps401 is Base's own operations door answering a request with no |
| 11 | // host token, byte for byte as carlos/base 3f8e6d1 writes it |
| 12 | // (internal/operations/operations.go serve and fail): Base serves |
| 13 | // Equipo's Assistant there in agent-access's shapes, before any adoption. |
| 14 | func baseOps401(w http.ResponseWriter) { |
| 15 | w.Header().Set("Cache-Control", "no-store") |
| 16 | w.Header().Set("WWW-Authenticate", "Bearer") |
| 17 | w.Header().Set("Content-Type", "application/json") |
| 18 | w.WriteHeader(http.StatusUnauthorized) |
| 19 | _, _ = w.Write([]byte("{\"error\":{\"code\":\"unauthenticated\",\"message\":\"Base did not accept this token.\"}}\n")) |
| 20 | } |
| 21 | |
| 22 | // Base's door already gives agent-access's challenge, so the strict |
| 23 | // classifier cannot tell it from agent-access. That is why Base's |
| 24 | // baseline is agent-401 and not a 404 or an app 401. |
| 25 | func TestBaseDoorIsAgentShapedBeforeAdoption(t *testing.T) { |
| 26 | if got := probeOf(t, &site{version: "v1", header: "v1", health: 200, ops: baseOps401}).Ops; got != OpsAgent401 { |
| 27 | t.Fatalf("Base's own door classified %q", got) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | // The held-door rule (Paul approved, 2026-11-23, docs/base-adoption.md): |
| 32 | // Base's door answers agent-access's challenge before configure and must |
| 33 | // still answer it, and only it, after the restart. Every other move is |
| 34 | // refused, an unauthenticated 200 included. |
| 35 | func TestBaseAdoptionDelivery(t *testing.T) { |
| 36 | app, ok := provision.AppBySlug("base") |
| 37 | if !ok { |
| 38 | t.Fatal("Base missing") |
| 39 | } |
| 40 | if AgentKeys(app) != ([2]string{"BASE_AGENT_ISSUER", "BASE_AGENT_TEAM"}) { |
| 41 | t.Fatal("wrong Base agent keys") |
| 42 | } |
| 43 | if BaselineOps(app) != OpsAgent401 { |
| 44 | t.Fatal("Base's baseline is its own door's agent-shaped 401") |
| 45 | } |
| 46 | before := Delivery{Version: "v1.0.0", Header: "v1.0.0", Health: 200, Ops: OpsAgent401} |
| 47 | after := before |
| 48 | if err := Delivered(app, before, after); err != nil { |
| 49 | t.Fatalf("held door refused: %v", err) |
| 50 | } |
| 51 | for _, shape := range []OpsShape{OpsApp401, OpsAbsent, OpsAgent200, OpsOther} { |
| 52 | after.Ops = shape |
| 53 | if Delivered(app, before, after) == nil { |
| 54 | t.Fatalf("Base delivered with %s", shape) |
| 55 | } |
| 56 | } |
| 57 | for _, shape := range []OpsShape{OpsApp401, OpsAbsent, OpsAgent200, OpsOther, ""} { |
| 58 | before.Ops = shape |
| 59 | if Delivered(app, before, Delivery{Version: "v1.0.0", Header: "v1.0.0", Health: 200, Ops: OpsAgent401}) == nil { |
| 60 | t.Fatalf("Base delivered from a %q baseline", shape) |
| 61 | } |
| 62 | } |
| 63 | // The rest of the check is not relaxed for Base. |
| 64 | held := Delivery{Version: "v1.0.0", Header: "v1.0.0", Health: 200, Ops: OpsAgent401} |
| 65 | for name, now := range map[string]Delivery{ |
| 66 | "version": {Version: "v1.0.1", Header: "v1.0.0", Health: 200, Ops: OpsAgent401}, |
| 67 | "header": {Version: "v1.0.0", Header: "v1.0.1", Health: 200, Ops: OpsAgent401}, |
| 68 | "health": {Version: "v1.0.0", Header: "v1.0.0", Health: 503, Ops: OpsAgent401}, |
| 69 | "redirect": {Version: "v1.0.0", Header: "v1.0.0", Health: 200, Ops: OpsAgent401, Redirected: true}, |
| 70 | } { |
| 71 | if Delivered(app, held, now) == nil { |
| 72 | t.Fatalf("Base delivered with a changed %s", name) |
| 73 | } |
| 74 | } |
| 75 | // A rollback restart holds the same door, as for every app. |
| 76 | if err := Reverted(held, held); err != nil { |
| 77 | t.Fatalf("Base rollback refused: %v", err) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Preflight admits Base only with its door giving agent-access's |
| 82 | // challenge: a 404 or an app 401 means the canonical host is not serving |
| 83 | // the Base release whose door the held rule describes. |
| 84 | func TestPreflightHoldsBaseToItsDoor(t *testing.T) { |
| 85 | for _, c := range []struct { |
| 86 | ops func(w http.ResponseWriter) |
| 87 | want string |
| 88 | }{ |
| 89 | {baseOps401, ""}, |
| 90 | {agent401, ""}, |
| 91 | {nil, "operations-absent"}, |
| 92 | {app401, "operations-app-401"}, |
| 93 | {agent200(`{"operations":[]}`), "operations-agent-200"}, |
| 94 | } { |
| 95 | x := newCarlosFixture(t, "base") |
| 96 | x.site.ops = c.ops |
| 97 | _, codes := x.preflight(t, nil) |
| 98 | if c.want == "" { |
| 99 | wantCodes(t, codes) |
| 100 | continue |
| 101 | } |
| 102 | wantCodes(t, codes, c.want) |
| 103 | } |
| 104 | // On a resume, with both agent keys the journal's own, the exemption |
| 105 | // for a door that has moved does not apply: Base's door never moves, |
| 106 | // so it must still give the challenge, never a 200 without a token. |
| 107 | keys := AgentKeys(appNamed(t, "base")) |
| 108 | mine := map[string]string{keys[0]: Issuer, keys[1]: fixtureSQID} |
| 109 | for _, c := range []struct { |
| 110 | ops func(w http.ResponseWriter) |
| 111 | want string |
| 112 | }{ |
| 113 | {baseOps401, ""}, |
| 114 | {agent200(`{"operations":[]}`), "operations-agent-200"}, |
| 115 | {nil, "operations-absent"}, |
| 116 | } { |
| 117 | x := newCarlosFixture(t, "base") |
| 118 | for k, v := range mine { |
| 119 | x.f.SetVar("base", "", k, v) |
| 120 | } |
| 121 | x.site.ops = c.ops |
| 122 | _, codes := x.preflight(t, mine) |
| 123 | if c.want == "" { |
| 124 | wantCodes(t, codes) |
| 125 | continue |
| 126 | } |
| 127 | wantCodes(t, codes, c.want) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // The runner's whole delivery check records for Base with its door |
| 132 | // held, and refuses when the door stops answering as it did. |
| 133 | func TestCheckDeliveryRecordsBaseWithItsDoorHeld(t *testing.T) { |
| 134 | w := newCheckFixture(t, "base") |
| 135 | w.site.ops = baseOps401 |
| 136 | if err := w.check(); err != nil { |
| 137 | t.Fatalf("Base check refused: %v", err) |
| 138 | } |
| 139 | if _, ok, err := w.s.DeliveryCheck(t.Context(), "base"); err != nil || !ok { |
| 140 | t.Fatalf("no check recorded (ok %v, err %v)", ok, err) |
| 141 | } |
| 142 | for name, ops := range map[string]func(http.ResponseWriter){ |
| 143 | "absent": nil, |
| 144 | "app-401": app401, |
| 145 | "open": agent200(`{"operations":[]}`), |
| 146 | "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."}}`), |
| 147 | "redirect": respond(http.StatusFound, map[string]string{"Location": "/signin"}, ""), |
| 148 | } { |
| 149 | w := newCheckFixture(t, "base") |
| 150 | w.site.ops = ops |
| 151 | if err := w.check(); err == nil { |
| 152 | t.Fatalf("Base check recorded with its door %s", name) |
| 153 | } |
| 154 | w.noCheck(t) |
| 155 | } |
| 156 | } |
| 157 | |