package adopt import ( "crypto/sha256" "encoding/base64" "encoding/json" "reflect" "testing" "time" "amadan.net/oficina/alta/carlosop" ) // bindingInput is a complete binding input with every field set. func bindingInput() BindingInput { at := time.Date(2026, 11, 16, 10, 0, 0, 0, time.UTC) id := Identity{Account: "bds", App: "docs", RecordHost: "docs.oficina.works", Slug: "s-docs", Channel: "stable", Region: "eu", CreatedAt: at.Add(-1000 * time.Hour), RecordSeq: 4, ReplicaAssignment: `{"boxes":["b1"]}`} d := func(ops OpsShape, min int) Delivery { return Delivery{Version: "v1.4.0", Header: "v1.4.0", Health: 200, Ops: ops, At: at.Add(time.Duration(min) * time.Minute)} } before := []carlosop.EffectiveVar{ {Key: "DOCS_HOME_ISSUER", Kind: "env", Digest: "vd1:" + hex64("a"), From: "default"}, {Key: "DOCS_INSTANCE_KEY", Kind: "secret", Digest: "vd1:" + hex64("b"), From: "default"}, } after := append(append([]carlosop.EffectiveVar(nil), before...), carlosop.EffectiveVar{Key: "DOCS_AGENT_ISSUER", Kind: "env", Digest: "vd1:" + hex64("c"), From: "default"}, carlosop.EffectiveVar{Key: "DOCS_AGENT_TEAM", Kind: "env", Digest: "vd1:" + hex64("d"), From: "default"}) return BindingInput{ Before: id, After: id, BeforeVars: before, AfterVars: after, BeforeLayers: map[string]string{"default": "cv1:" + hex64("1"), "overlay": "none"}, AfterLayers: map[string]string{"default": "cv1:" + hex64("2"), "overlay": "none"}, TeamSQID: fixtureSQID, Issuer: Issuer, ArtifactSHA256: hex64("e"), Version: "v1.4.0", BeforeDelivery: d(OpsAbsent, 0), AfterDelivery: d(OpsAgent401, 5), NowDelivery: d(OpsAgent401, 60), Check: DeliveryCheck{App: "docs", Identity: id, Delivery: d(OpsAgent401, 10), AfterVersion: "cv1:" + hex64("2"), At: at.Add(10 * time.Minute)}, } } func hex64(c string) string { out := "" for len(out) < 64 { out += c } return out } func TestBindingIncludesDeliveryCheck(t *testing.T) { in := bindingInput() b := EnvironmentBinding(in) raw, err := base64.RawURLEncoding.DecodeString(b) if err != nil || len(raw) != sha256.Size { t.Fatalf("binding is not base64url of 32 bytes: %d bytes, %v", len(raw), err) } if EnvironmentBinding(in) != b { t.Fatal("the binding is not deterministic") } // The first part names the scheme: the binding is the documented // array, not some other encoding of the same values. parts := []any{"oficina/existing-app-observation/v1", in.Before, in.After, [][4]string{{"DOCS_HOME_ISSUER", "env", "vd1:" + hex64("a"), "default"}, {"DOCS_INSTANCE_KEY", "secret", "vd1:" + hex64("b"), "default"}}, [][4]string{{"DOCS_AGENT_ISSUER", "env", "vd1:" + hex64("c"), "default"}, {"DOCS_AGENT_TEAM", "env", "vd1:" + hex64("d"), "default"}, {"DOCS_HOME_ISSUER", "env", "vd1:" + hex64("a"), "default"}, {"DOCS_INSTANCE_KEY", "secret", "vd1:" + hex64("b"), "default"}}, in.BeforeLayers, in.AfterLayers, in.TeamSQID, in.Issuer, in.ArtifactSHA256, in.Version, in.BeforeDelivery, in.AfterDelivery, in.NowDelivery, []any{in.Check.App, in.Check.Identity, in.Check.Delivery, in.Check.AfterVersion, in.Check.At}, } js, err := json.Marshal(parts) if err != nil { t.Fatal(err) } sum := sha256.Sum256(js) if want := base64.RawURLEncoding.EncodeToString(sum[:]); b != want { t.Fatalf("the binding is not base64url(sha256) of the documented array") } // Every part moves it, the delivery check and the three deliveries // above all. edits := map[string]func(*BindingInput){ "check app": func(in *BindingInput) { in.Check.App = "sheets" }, "check identity": func(in *BindingInput) { in.Check.Identity.RecordSeq++ }, "check delivery": func(in *BindingInput) { in.Check.Delivery.Header = "v1.4.1" }, "check delivery ops": func(in *BindingInput) { in.Check.Delivery.Ops = OpsAgent200 }, "check after version": func(in *BindingInput) { in.Check.AfterVersion = "cv1:" + hex64("3") }, "check time": func(in *BindingInput) { in.Check.At = in.Check.At.Add(time.Second) }, "before delivery": func(in *BindingInput) { in.BeforeDelivery.Ops = OpsApp401 }, "after delivery": func(in *BindingInput) { in.AfterDelivery.Version = "v1.4.1" }, "after delivery health": func(in *BindingInput) { in.AfterDelivery.Health = 503 }, "now delivery": func(in *BindingInput) { in.NowDelivery.Redirected = true }, "now delivery time": func(in *BindingInput) { in.NowDelivery.At = in.NowDelivery.At.Add(time.Hour) }, "before identity": func(in *BindingInput) { in.Before.Region = "us" }, "after identity": func(in *BindingInput) { in.After.Seed = `{"s":1}` }, "before var gone": func(in *BindingInput) { in.BeforeVars = []carlosop.EffectiveVar{in.BeforeVars[0]} }, "before var digest": func(in *BindingInput) { in.BeforeVars = withDigest(in.BeforeVars, 1, "vd1:"+hex64("9")) }, "after var source": func(in *BindingInput) { in.AfterVars = withFrom(in.AfterVars, 0, "environment") }, "before layers": func(in *BindingInput) { in.BeforeLayers = map[string]string{"default": "none"} }, "after layers": func(in *BindingInput) { in.AfterLayers = map[string]string{"default": "none"} }, "team": func(in *BindingInput) { in.TeamSQID = "other" }, "issuer": func(in *BindingInput) { in.Issuer = "https://elsewhere.example" }, "artifact": func(in *BindingInput) { in.ArtifactSHA256 = hex64("f") }, "version": func(in *BindingInput) { in.Version = "v1.4.1" }, } for name, edit := range edits { t.Run(name, func(t *testing.T) { e := bindingInput() edit(&e) if EnvironmentBinding(e) == b { t.Fatalf("changing the %s left the binding unchanged", name) } }) } } func withDigest(vars []carlosop.EffectiveVar, i int, d string) []carlosop.EffectiveVar { out := append([]carlosop.EffectiveVar(nil), vars...) out[i].Digest = d return out } func withFrom(vars []carlosop.EffectiveVar, i int, from string) []carlosop.EffectiveVar { out := append([]carlosop.EffectiveVar(nil), vars...) out[i].From = from return out } func TestBindingIsOrderIndependent(t *testing.T) { in := bindingInput() want := EnvironmentBinding(in) shuffled := bindingInput() reverse := func(v []carlosop.EffectiveVar) []carlosop.EffectiveVar { out := make([]carlosop.EffectiveVar, len(v)) for i := range v { out[len(v)-1-i] = v[i] } return out } shuffled.BeforeVars = reverse(shuffled.BeforeVars) shuffled.AfterVars = reverse(shuffled.AfterVars) // Maps built in another order, and shadows (not part of a var's // entry) differing, change nothing. shuffled.BeforeLayers = map[string]string{"overlay": "none"} shuffled.BeforeLayers["default"] = "cv1:" + hex64("1") shuffled.AfterVars[0].Shadows = []string{"default"} keep := append([]carlosop.EffectiveVar(nil), shuffled.AfterVars...) if got := EnvironmentBinding(shuffled); got != want { t.Fatal("the binding depends on the order of vars or layers") } if !reflect.DeepEqual(keep, shuffled.AfterVars) { t.Fatal("EnvironmentBinding reordered its caller's vars") } }