|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +// ── Every schema key must actually be settable ─────────────────────────── |
| 11 | +// |
| 12 | +// The schema is what `slmcode config set`, `config show --all`, the Studio's |
| 13 | +// settings form and the config docs are all generated from. A key that appears |
| 14 | +// there but does not round-trip through Set/Get is a setting the user is |
| 15 | +// offered, types a value into, and which then does nothing — with no error to |
| 16 | +// tell them so. |
| 17 | +// |
| 18 | +// Nothing checked that the two agreed. This walks every patchable key and |
| 19 | +// proves it. |
| 20 | + |
| 21 | +// sampleFor returns a value the schema itself says is valid for a field. |
| 22 | +func sampleFor(f FieldSchema) (any, bool) { |
| 23 | + if len(f.Enum) > 0 { |
| 24 | + for _, v := range f.Enum { |
| 25 | + if strings.TrimSpace(v) != "" { |
| 26 | + return v, true |
| 27 | + } |
| 28 | + } |
| 29 | + return nil, false |
| 30 | + } |
| 31 | + switch f.Type { |
| 32 | + case "string": |
| 33 | + return "round-trip-probe", true |
| 34 | + case "int": |
| 35 | + return 7, true |
| 36 | + case "float": |
| 37 | + return 0.25, true |
| 38 | + case "bool": |
| 39 | + return true, true |
| 40 | + case "duration": |
| 41 | + return "90s", true |
| 42 | + case "string[]": |
| 43 | + return []string{"alpha", "beta"}, true |
| 44 | + default: |
| 45 | + // map / list shapes have no single safe sample; the typed patch tests |
| 46 | + // cover those individually. |
| 47 | + return nil, false |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestEverySettableSchemaKeyRoundTrips(t *testing.T) { |
| 52 | + for _, f := range Schema() { |
| 53 | + if _, patchable := PatchableField(f.Key); !patchable { |
| 54 | + continue |
| 55 | + } |
| 56 | + sample, ok := sampleFor(f) |
| 57 | + if !ok { |
| 58 | + continue |
| 59 | + } |
| 60 | + t.Run(f.Key, func(t *testing.T) { |
| 61 | + c := Default(t.TempDir()) |
| 62 | + if err := c.Set(f.Key, sample); err != nil { |
| 63 | + t.Fatalf("Set(%q, %v) on a %s field: %v", f.Key, sample, f.Type, err) |
| 64 | + } |
| 65 | + got, ok := c.Get(f.Key) |
| 66 | + if !ok { |
| 67 | + t.Fatalf("Get(%q) reported the key does not exist, but Set accepted it", f.Key) |
| 68 | + } |
| 69 | + // Compared as rendered text: Set may normalize (a duration, a |
| 70 | + // lower-cased enum), and the contract is that the value STUCK, not |
| 71 | + // that it survived byte-for-byte. |
| 72 | + if strings.TrimSpace(render(got)) == "" && strings.TrimSpace(render(sample)) != "" { |
| 73 | + t.Errorf("Set(%q, %v) was accepted but Get returned empty — the key does nothing", |
| 74 | + f.Key, sample) |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// A key the schema marks read-only must not be settable through a PATCH. |
| 81 | +// |
| 82 | +// Config.Set itself must keep accepting them — it is the low-level setter that |
| 83 | +// env-var and config-file loading both go through, and a read-only key still |
| 84 | +// has to be loadable from the file that declares it. The flag is a statement |
| 85 | +// about REMOTE editing, so the place it has to hold is the patch path. |
| 86 | +// |
| 87 | +// It did not. `slmcode config set` refused these keys and the Studio's form |
| 88 | +// never offered them, but a PUT /api/config carrying one in the untyped half of |
| 89 | +// the patch set it anyway — three surfaces disagreeing about the same key. |
| 90 | +func TestAPatchCannotSetAReadOnlyKey(t *testing.T) { |
| 91 | + for _, f := range Schema() { |
| 92 | + if _, patchable := PatchableField(f.Key); patchable { |
| 93 | + continue |
| 94 | + } |
| 95 | + sample, ok := sampleFor(f) |
| 96 | + if !ok { |
| 97 | + continue |
| 98 | + } |
| 99 | + t.Run(f.Key, func(t *testing.T) { |
| 100 | + c := Default(t.TempDir()) |
| 101 | + before, _ := c.Get(f.Key) |
| 102 | + |
| 103 | + body, err := json.Marshal(map[string]any{f.Key: sample}) |
| 104 | + if err != nil { |
| 105 | + t.Fatal(err) |
| 106 | + } |
| 107 | + var p Patch |
| 108 | + if err := json.Unmarshal(body, &p); err != nil { |
| 109 | + t.Fatalf("decode patch: %v", err) |
| 110 | + } |
| 111 | + c.ApplyPatch(p) |
| 112 | + |
| 113 | + if after, _ := c.Get(f.Key); render(before) != render(after) { |
| 114 | + t.Errorf("a patch set the read-only key %q (%v → %v)", f.Key, before, after) |
| 115 | + } |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// The most consequential of them, stated on its own so the reason survives. |
| 121 | +// |
| 122 | +// MCP servers are external processes whose tools agents can call, so a request |
| 123 | +// that could register one is tool-execution surface. The Studio panel that |
| 124 | +// lists them says "configured in config.yaml" — file-only is exactly what the |
| 125 | +// read-only flag was declaring. |
| 126 | +func TestAPatchCannotRegisterAnMCPServer(t *testing.T) { |
| 127 | + c := Default(t.TempDir()) |
| 128 | + var p Patch |
| 129 | + body := `{"mcp_servers":[{"name":"attacker","command":"/bin/sh","args":["-c","id"]}]}` |
| 130 | + if err := json.Unmarshal([]byte(body), &p); err != nil { |
| 131 | + t.Fatalf("decode patch: %v", err) |
| 132 | + } |
| 133 | + c.ApplyPatch(p) |
| 134 | + if len(c.MCPServers) != 0 { |
| 135 | + t.Errorf("a patch registered %d MCP server(s): %+v", len(c.MCPServers), c.MCPServers) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +// A patchable key must still go through, or the check would have turned the |
| 140 | +// settings form read-only. |
| 141 | +func TestAPatchStillSetsTheKeysItShould(t *testing.T) { |
| 142 | + c := Default(t.TempDir()) |
| 143 | + var p Patch |
| 144 | + if err := json.Unmarshal([]byte(`{"max_parallel":5}`), &p); err != nil { |
| 145 | + t.Fatal(err) |
| 146 | + } |
| 147 | + c.ApplyPatch(p) |
| 148 | + if c.MaxParallel != 5 { |
| 149 | + t.Errorf("max_parallel = %d, want the patch applied", c.MaxParallel) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +// Every schema key must resolve through Field(), including via any alias — |
| 154 | +// `config set` looks the key up there before it looks anywhere else. |
| 155 | +func TestEverySchemaKeyResolves(t *testing.T) { |
| 156 | + for _, f := range Schema() { |
| 157 | + if _, ok := Field(f.Key); !ok { |
| 158 | + t.Errorf("Schema lists %q but Field() cannot resolve it", f.Key) |
| 159 | + } |
| 160 | + if got := CanonicalKey(f.Key); got != f.Key { |
| 161 | + t.Errorf("CanonicalKey(%q) = %q — the schema lists a non-canonical key", f.Key, got) |
| 162 | + } |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +func render(v any) string { |
| 167 | + if v == nil { |
| 168 | + return "" |
| 169 | + } |
| 170 | + return strings.TrimSpace(fmt.Sprintf("%v", v)) |
| 171 | +} |
0 commit comments