| 1 | package codex |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 9 | ) |
| 10 | |
| 11 | func liveConfig(t *testing.T) (map[string]any, provider.Policy) { |
| 12 | t.Helper() |
| 13 | raw, err := os.ReadFile("testdata/live-config-0.165.0.json") |
| 14 | if err != nil { |
| 15 | t.Fatal(err) |
| 16 | } |
| 17 | var frame struct{ Result map[string]any } |
| 18 | if err = json.Unmarshal(raw, &frame); err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | settings := marshal(frame.Result["layers"].([]any)[0].(map[string]any)["config"]) |
| 22 | manifest := marshal(map[string]any{"Action": "coordinate", "ProviderVersion": Version}) |
| 23 | return frame.Result, provider.Policy{Settings: settings, Manifest: manifest, ManifestHash: provider.ManifestDigest(manifest)} |
| 24 | } |
| 25 | func TestCodexLiveConfigReadback(t *testing.T) { |
| 26 | v, p := liveConfig(t) |
| 27 | if err := validateConfig(marshal(v), p); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | } |
| 31 | func TestCodexLiveConfigStrict(t *testing.T) { |
| 32 | for _, mode := range []string{"enabled-tool", "unknown-tool", "missing-feature", "changed-approval", "changed-layer", "extra-server", "other-action", "other-version"} { |
| 33 | t.Run(mode, func(t *testing.T) { |
| 34 | v, p := liveConfig(t) |
| 35 | cfg := v["config"].(map[string]any) |
| 36 | switch mode { |
| 37 | case "enabled-tool": |
| 38 | cfg["tools"].(map[string]any)["update_plan"] = map[string]any{"enabled": true} |
| 39 | case "unknown-tool": |
| 40 | cfg["tools"].(map[string]any)["unknown"] = map[string]any{"enabled": false} |
| 41 | case "missing-feature": |
| 42 | delete(cfg["features"].(map[string]any), "shell_tool") |
| 43 | case "changed-approval": |
| 44 | cfg["approvals_reviewer"] = "auto_review" |
| 45 | case "changed-layer": |
| 46 | v["layers"].([]any)[0].(map[string]any)["config"].(map[string]any)["tools"].(map[string]any)["update_plan"] = map[string]any{"enabled": true} |
| 47 | case "extra-server": |
| 48 | cfg["mcp_servers"].(map[string]any)["other"] = map[string]any{} |
| 49 | case "other-action", "other-version": |
| 50 | m := map[string]any{"Action": "coordinate", "ProviderVersion": Version} |
| 51 | if mode == "other-action" { |
| 52 | m["Action"] = "implement" |
| 53 | } else { |
| 54 | m["ProviderVersion"] = "other" |
| 55 | } |
| 56 | p.Manifest = marshal(m) |
| 57 | p.ManifestHash = provider.ManifestDigest(p.Manifest) |
| 58 | } |
| 59 | if validateConfig(marshal(v), p) == nil { |
| 60 | t.Fatal("unsafe readback accepted") |
| 61 | } |
| 62 | }) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func TestSpecialistQuestionReadback(t *testing.T) { |
| 67 | for _, mode := range []string{"valid", "enabled-layer", "missing-layer", "enabled-effective", "default-mode", "other-version"} { |
| 68 | t.Run(mode, func(t *testing.T) { |
| 69 | v, p := liveConfig(t) |
| 70 | layer := v["layers"].([]any)[0].(map[string]any)["config"].(map[string]any) |
| 71 | tools := layer["tools"].(map[string]any) |
| 72 | delete(tools, "update_plan") |
| 73 | p.Settings = marshal(layer) |
| 74 | p.Manifest = marshal(map[string]any{"Action": "implement", "ProviderVersion": Version}) |
| 75 | cfg := v["config"].(map[string]any) |
| 76 | switch mode { |
| 77 | case "enabled-layer": |
| 78 | tools["experimental_request_user_input"] = map[string]any{"enabled": true} |
| 79 | case "missing-layer": |
| 80 | delete(tools, "experimental_request_user_input") |
| 81 | case "enabled-effective": |
| 82 | cfg["tools"].(map[string]any)["experimental_request_user_input"] = map[string]any{"enabled": true} |
| 83 | case "default-mode": |
| 84 | cfg["features"].(map[string]any)["default_mode_request_user_input"] = true |
| 85 | case "other-version": |
| 86 | p.Manifest = marshal(map[string]any{"Action": "implement", "ProviderVersion": "other"}) |
| 87 | } |
| 88 | p.ManifestHash = provider.ManifestDigest(p.Manifest) |
| 89 | if got := validateConfig(marshal(v), p); (got == nil) != (mode == "valid") { |
| 90 | t.Fatalf("readback mode %s: %v", mode, got) |
| 91 | } |
| 92 | }) |
| 93 | } |
| 94 | } |
| 95 | |