package adopt import ( "encoding/json" "reflect" "testing" "time" "amadan.net/oficina/alta/carlosop" "amadan.net/oficina/alta/provision" ) func TestAgentKeysAndCanonicalHost(t *testing.T) { app, ok := provision.AppBySlug("docs") if !ok { t.Fatal("no docs app") } k := AgentKeys(app) if k[0] != app.Prefix+"_AGENT_ISSUER" || k[1] != app.Prefix+"_AGENT_TEAM" { t.Fatalf("keys = %v", k) } if CanonicalHost(app) != "docs.oficina.works" { t.Fatalf("host = %q", CanonicalHost(app)) } if Issuer != "https://connect.oficina.works" { t.Fatal("issuer") } } func inst() (carlosop.Instance, carlosop.Effective) { return carlosop.Instance{ Host: "rec.example", Account: "bds", App: "docs", Slug: "s1", Environment: "env1", Channel: "stable", Region: "eu", RecordSeq: 7, CreatedAt: time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC), ReplicaAssignment: json.RawMessage(`{"b": 2, "a": 1}`), Seed: json.RawMessage(`{"x":[1, 2]}`), }, carlosop.Effective{Host: "rec.example", Environment: "env1", Owned: false} } // dom is the canonical domain's claim, routed to inst's record. func dom() carlosop.Domain { return carlosop.Domain{Host: "docs.oficina.works", Route: "rec.example", State: "live"} } func TestIdentityOf(t *testing.T) { in, eff := inst() id := IdentityOf(dom(), in, eff) if id.Account != "bds" || id.App != "docs" || id.RecordHost != "rec.example" || id.Domain != "docs.oficina.works" || id.Route != "rec.example" || id.Slug != "s1" || id.Environment != "env1" || id.Channel != "stable" || id.Region != "eu" || id.RecordSeq != 7 { t.Fatalf("identity = %+v", id) } if id.Moving || id.Owned { t.Fatal("moving/owned set") } if id.ReplicaAssignment != `{"a":1,"b":2}` || id.Seed != `{"x":[1,2]}` { t.Fatalf("not canonical: %q %q", id.ReplicaAssignment, id.Seed) } in.Move = json.RawMessage(`{"to":"us"}`) eff.Owned = true id = IdentityOf(dom(), in, eff) if !id.Moving || !id.Owned { t.Fatal("moving/owned not carried") } in.Move = json.RawMessage(`null`) if IdentityOf(dom(), in, eff).Moving { t.Fatal("a JSON null move counts as moving") } in.ReplicaAssignment, in.Seed = nil, nil id = IdentityOf(dom(), in, eff) if id.ReplicaAssignment != "" || id.Seed != "" { t.Fatal("absent should be empty") } } func TestSameIdentityFlagsEachField(t *testing.T) { in, eff := inst() base := IdentityOf(dom(), in, eff) if got := SameIdentity(base, base); len(got) != 0 { t.Fatalf("equal identities differ: %v", got) } cases := []struct { name, code string mut func(*Identity) }{ {"slug", "storage", func(i *Identity) { i.Slug = "s2" }}, {"created_at", "storage", func(i *Identity) { i.CreatedAt = i.CreatedAt.Add(time.Second) }}, {"replica", "storage", func(i *Identity) { i.ReplicaAssignment = `{"a":9}` }}, {"seed", "storage", func(i *Identity) { i.Seed = `{"x":[]}` }}, {"record-host", "instance", func(i *Identity) { i.RecordHost = "other.example" }}, {"domain", "alias", func(i *Identity) { i.Domain = "other.oficina.works" }}, {"route", "alias", func(i *Identity) { i.Route = "other.example" }}, {"environment", "environment", func(i *Identity) { i.Environment = "env2" }}, {"owned", "owned", func(i *Identity) { i.Owned = !i.Owned }}, {"channel", "channel", func(i *Identity) { i.Channel = "beta" }}, {"region", "region", func(i *Identity) { i.Region = "us" }}, {"moving", "moving", func(i *Identity) { i.Moving = !i.Moving }}, {"record-seq", "record", func(i *Identity) { i.RecordSeq++ }}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { b := base c.mut(&b) got := SameIdentity(base, b) if !reflect.DeepEqual(got, []string{c.code}) { t.Fatalf("codes = %v, want [%s]", got, c.code) } }) } t.Run("several-sorted-unique", func(t *testing.T) { b := base b.Slug, b.Seed, b.Region = "z", "{}", "us" if got := SameIdentity(base, b); !reflect.DeepEqual(got, []string{"region", "storage"}) { t.Fatalf("codes = %v", got) } }) }