oficina / alta Public

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

Plain git — no account needed to clone.

Download

Download this file

1package adopt
2
3import (
4 "encoding/json"
5 "reflect"
6 "testing"
7 "time"
8
9 "amadan.net/oficina/alta/carlosop"
10 "amadan.net/oficina/alta/provision"
11)
12
13func TestAgentKeysAndCanonicalHost(t *testing.T) {
14 app, ok := provision.AppBySlug("docs")
15 if !ok {
16 t.Fatal("no docs app")
17 }
18 k := AgentKeys(app)
19 if k[0] != app.Prefix+"_AGENT_ISSUER" || k[1] != app.Prefix+"_AGENT_TEAM" {
20 t.Fatalf("keys = %v", k)
21 }
22 if CanonicalHost(app) != "docs.oficina.works" {
23 t.Fatalf("host = %q", CanonicalHost(app))
24 }
25 if Issuer != "https://connect.oficina.works" {
26 t.Fatal("issuer")
27 }
28}
29
30func inst() (carlosop.Instance, carlosop.Effective) {
31 return carlosop.Instance{
32 Host: "rec.example", Account: "bds", App: "docs", Slug: "s1",
33 Environment: "env1", Channel: "stable", Region: "eu", RecordSeq: 7,
34 CreatedAt: time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC),
35 ReplicaAssignment: json.RawMessage(`{"b": 2, "a": 1}`), Seed: json.RawMessage(`{"x":[1, 2]}`),
36 },
37 carlosop.Effective{Host: "rec.example", Environment: "env1", Owned: false}
38}
39
40// dom is the canonical domain's claim, routed to inst's record.
41func dom() carlosop.Domain {
42 return carlosop.Domain{Host: "docs.oficina.works", Route: "rec.example", State: "live"}
43}
44
45func TestIdentityOf(t *testing.T) {
46 in, eff := inst()
47 id := IdentityOf(dom(), in, eff)
48 if id.Account != "bds" || id.App != "docs" || id.RecordHost != "rec.example" || id.Domain != "docs.oficina.works" || id.Route != "rec.example" ||
49 id.Slug != "s1" || id.Environment != "env1" || id.Channel != "stable" || id.Region != "eu" || id.RecordSeq != 7 {
50 t.Fatalf("identity = %+v", id)
51 }
52 if id.Moving || id.Owned {
53 t.Fatal("moving/owned set")
54 }
55 if id.ReplicaAssignment != `{"a":1,"b":2}` || id.Seed != `{"x":[1,2]}` {
56 t.Fatalf("not canonical: %q %q", id.ReplicaAssignment, id.Seed)
57 }
58 in.Move = json.RawMessage(`{"to":"us"}`)
59 eff.Owned = true
60 id = IdentityOf(dom(), in, eff)
61 if !id.Moving || !id.Owned {
62 t.Fatal("moving/owned not carried")
63 }
64 in.Move = json.RawMessage(`null`)
65 if IdentityOf(dom(), in, eff).Moving {
66 t.Fatal("a JSON null move counts as moving")
67 }
68 in.ReplicaAssignment, in.Seed = nil, nil
69 id = IdentityOf(dom(), in, eff)
70 if id.ReplicaAssignment != "" || id.Seed != "" {
71 t.Fatal("absent should be empty")
72 }
73}
74
75func TestSameIdentityFlagsEachField(t *testing.T) {
76 in, eff := inst()
77 base := IdentityOf(dom(), in, eff)
78 if got := SameIdentity(base, base); len(got) != 0 {
79 t.Fatalf("equal identities differ: %v", got)
80 }
81 cases := []struct {
82 name, code string
83 mut func(*Identity)
84 }{
85 {"slug", "storage", func(i *Identity) { i.Slug = "s2" }},
86 {"created_at", "storage", func(i *Identity) { i.CreatedAt = i.CreatedAt.Add(time.Second) }},
87 {"replica", "storage", func(i *Identity) { i.ReplicaAssignment = `{"a":9}` }},
88 {"seed", "storage", func(i *Identity) { i.Seed = `{"x":[]}` }},
89 {"record-host", "instance", func(i *Identity) { i.RecordHost = "other.example" }},
90 {"domain", "alias", func(i *Identity) { i.Domain = "other.oficina.works" }},
91 {"route", "alias", func(i *Identity) { i.Route = "other.example" }},
92 {"environment", "environment", func(i *Identity) { i.Environment = "env2" }},
93 {"owned", "owned", func(i *Identity) { i.Owned = !i.Owned }},
94 {"channel", "channel", func(i *Identity) { i.Channel = "beta" }},
95 {"region", "region", func(i *Identity) { i.Region = "us" }},
96 {"moving", "moving", func(i *Identity) { i.Moving = !i.Moving }},
97 {"record-seq", "record", func(i *Identity) { i.RecordSeq++ }},
98 }
99 for _, c := range cases {
100 t.Run(c.name, func(t *testing.T) {
101 b := base
102 c.mut(&b)
103 got := SameIdentity(base, b)
104 if !reflect.DeepEqual(got, []string{c.code}) {
105 t.Fatalf("codes = %v, want [%s]", got, c.code)
106 }
107 })
108 }
109 t.Run("several-sorted-unique", func(t *testing.T) {
110 b := base
111 b.Slug, b.Seed, b.Region = "z", "{}", "us"
112 if got := SameIdentity(base, b); !reflect.DeepEqual(got, []string{"region", "storage"}) {
113 t.Fatalf("codes = %v", got)
114 }
115 })
116}
117