oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package codex
2
3import (
4 "amadan.net/oficina/equipo/internal/runner/provider"
5 "encoding/json"
6 "os"
7 "testing"
8)
9
10const tFixturePath = "testdata/unauthenticated-0.165.0.json"
11
12func recordedFrames() []map[string]any {
13 raw, e := os.ReadFile(tFixturePath)
14 if e != nil {
15 panic(e)
16 }
17 var v struct{ Frames []map[string]any }
18 if e = json.Unmarshal(raw, &v); e != nil {
19 panic(e)
20 }
21 return v.Frames
22}
23func recordedReply(_ string, id float64) any {
24 for _, f := range recordedFrames() {
25 if f["id"] == id {
26 return f["result"]
27 }
28 }
29 panic("missing reply")
30}
31func recordedNotification() any {
32 for _, f := range recordedFrames() {
33 if f["method"] != nil {
34 return f
35 }
36 }
37 panic("missing notification")
38}
39func recordedConfig(settings map[string]any) any {
40 r := recordedReply(tFixturePath, 2).(map[string]any)
41 var old map[string]any
42 raw, _ := os.ReadFile(tFixturePath)
43 var fixture struct{ Settings map[string]any }
44 json.Unmarshal(raw, &fixture)
45 old = fixture.Settings
46 // Remove only recorded settings, keeping the binary's expanded defaults.
47 var remove func(map[string]any, map[string]any)
48 remove = func(dst, src map[string]any) {
49 for k, v := range src {
50 if m, ok := v.(map[string]any); ok {
51 remove(dst[k].(map[string]any), m)
52 } else {
53 delete(dst, k)
54 }
55 }
56 }
57 remove(r["config"].(map[string]any), old)
58 var merge func(map[string]any, map[string]any)
59 merge = func(dst, src map[string]any) {
60 for k, v := range src {
61 if m, ok := v.(map[string]any); ok {
62 d, ok := dst[k].(map[string]any)
63 if !ok {
64 d = map[string]any{}
65 dst[k] = d
66 }
67 merge(d, m)
68 } else {
69 dst[k] = v
70 }
71 }
72 }
73 merge(r["config"].(map[string]any), settings)
74 r["layers"].([]any)[0].(map[string]any)["config"] = settings
75 origins := map[string]any{}
76 var walk func(string, any)
77 walk = func(key string, v any) {
78 if m, ok := v.(map[string]any); ok {
79 for k, w := range m {
80 prefix := k
81 if key != "" {
82 prefix = key + "." + k
83 }
84 walk(prefix, w)
85 }
86 } else {
87 origins[key] = map[string]any{"name": map[string]any{"type": "sessionFlags"}, "version": "fixture"}
88 }
89 }
90 walk("", settings)
91 r["origins"] = origins
92 return r
93}
94func TestCodexRecordedReadback(t *testing.T) {
95 f := fixtureFor(t)
96 s, _ := openFixture(t, f)
97 if s.ID == "" {
98 t.Fatal("missing session")
99 }
100}
101func TestCodexRecordedConfigLayers(t *testing.T) {
102 for _, kind := range []string{"user-config", "system-config", "unknown-layer", "flags-missing", "flags-changed", "origin-unknown", "origin-prefix", "origin-version", "effective-changed"} {
103 t.Run(kind, func(t *testing.T) {
104 raw, _ := os.ReadFile(tFixturePath)
105 var fixture struct{ Settings json.RawMessage }
106 json.Unmarshal(raw, &fixture)
107 v := recordedReply(tFixturePath, 2).(map[string]any)
108 layers := v["layers"].([]any)
109 switch kind {
110 case "user-config":
111 layers[1].(map[string]any)["config"] = map[string]any{"hooks": true}
112 case "system-config":
113 layers[2].(map[string]any)["config"] = map[string]any{"hooks": true}
114 case "unknown-layer":
115 layers[1].(map[string]any)["name"] = map[string]any{"type": "project"}
116 case "flags-missing":
117 v["layers"] = layers[1:]
118 case "flags-changed":
119 layers[0].(map[string]any)["config"].(map[string]any)["model"] = "other"
120 case "origin-unknown":
121 v["origins"].(map[string]any)["hooks.enabled"] = map[string]any{"name": map[string]any{"type": "sessionFlags"}, "version": "x"}
122 case "origin-prefix":
123 v["origins"].(map[string]any)["model_extra"] = map[string]any{"name": map[string]any{"type": "sessionFlags"}, "version": "x"}
124 case "origin-version":
125 v["origins"].(map[string]any)["model"].(map[string]any)["version"] = ""
126 case "effective-changed":
127 v["config"].(map[string]any)["model"] = "other"
128 }
129 if validateConfig(marshal(v), provider.Policy{Settings: fixture.Settings}) == nil {
130 t.Fatal("unsafe readback accepted", kind)
131 }
132 })
133 }
134}
135
136func TestCodexPreflightNotification(t *testing.T) {
137 for _, raw := range []string{`{"method":"remoteControl/status/changed","params":{"status":"enabled"}}`, `{"method":"account/updated","params":{"status":"disabled"}}`, `{"method":"config/updated","params":{"status":"disabled"}}`, `{"id":1,"method":"remoteControl/status/changed","params":{"status":"disabled"}}`} {
138 v, e := parse([]byte(raw))
139 if e != nil {
140 t.Fatal(e)
141 }
142 if disabledRemoteControl(v) || provider.CodexStartupNotice(v.ID, v.Method, v.Params) {
143 t.Fatal("unsafe notification ignored", raw)
144 }
145 }
146}
147