77} from "../lib/config/config-draft-model.ts" ;
88import { createInitialConfigState } from "../lib/config/config-state-model.ts" ;
99import { ConfigFormCollectionDraft } from "./config-form-collection-draft.ts" ;
10+ import type { JsonSchema } from "./config-form.shared.ts" ;
1011import { analyzeConfigSchema , renderConfigForm } from "./config-form.ts" ;
1112
1213function expectElement < T extends Element > ( element : T | null | undefined , label : string ) : T {
@@ -32,13 +33,35 @@ describe("config form map integrity", () => {
3233 } ) ;
3334 const config = ( codeMode ?: boolean ) => ( {
3435 agents : wrapScope ( {
35- models : { "example/model" : modelSettings ( codeMode ) , "example/other" : { alias : "other" } } ,
36+ models : {
37+ "example/model.v1" : modelSettings ( codeMode ) ,
38+ "example/other" : { alias : "other" } ,
39+ } ,
40+ ...( scope === "agent"
41+ ? { tools : { codeMode : { enabled : "auto" , maxOutputBytes : 4096 } } }
42+ : { } ) ,
3643 } ) ,
3744 tools : { codeMode : { enabled : "auto" , maxOutputBytes : 4096 } } ,
3845 } ) ;
3946 const modelSchema = {
4047 type : "object" ,
4148 properties : {
49+ ...( scope === "agent"
50+ ? {
51+ tools : {
52+ type : "object" ,
53+ properties : {
54+ codeMode : {
55+ anyOf : [
56+ { type : "boolean" } ,
57+ { const : "auto" } ,
58+ { type : "object" , additionalProperties : true } ,
59+ ] ,
60+ } ,
61+ } ,
62+ } ,
63+ }
64+ : { } ) ,
4265 models : {
4366 type : "object" ,
4467 additionalProperties : {
@@ -62,7 +85,13 @@ describe("config form map integrity", () => {
6285 properties :
6386 scope === "defaults"
6487 ? { defaults : modelSchema }
65- : { entries : { type : "object" , additionalProperties : modelSchema } } ,
88+ : {
89+ entries : {
90+ type : "object" ,
91+ propertyNames : { type : "string" , pattern : "^[a-z0-9_][a-z0-9_-]{0,63}$" } ,
92+ additionalProperties : modelSchema ,
93+ } ,
94+ } ,
6695 } ,
6796 } ,
6897 } ) ;
@@ -94,6 +123,9 @@ describe("config form map integrity", () => {
94123 ) ;
95124
96125 renderValue ( ) ;
126+ if ( scope === "agent" ) {
127+ expect ( container . textContent ) . toContain ( "Unsupported schema node. Use Raw mode." ) ;
128+ }
97129 expect ( Array . from ( getSelect ( ) . options , ( option ) => option . textContent ?. trim ( ) ) ) . toEqual ( [
98130 "Default" ,
99131 "On" ,
@@ -170,13 +202,11 @@ describe("config form map integrity", () => {
170202 } ) ;
171203
172204 it . each ( [
173- [ "pattern" , { type : "string" , pattern : "^[a-z]+$" } ] ,
174- [ "minimum length" , { type : "string" , minLength : 1 } ] ,
175- [ "enumeration" , { enum : [ "primary" ] } ] ,
176205 [ "boolean" , true ] ,
177206 [ "null" , null ] ,
178207 [ "array" , [ { type : "string" } ] ] ,
179208 [ "wrong type" , { type : "number" } ] ,
209+ [ "unknown constraint" , { type : "string" , not : { const : "blocked" } } ] ,
180210 [ "inherited type" , Object . assign ( Object . create ( { type : "string" } ) , { unknown : true } ) ] ,
181211 ] ) ( "keeps %s property-name constraints fail-closed" , ( _label , propertyNames ) => {
182212 const analysis = analyzeConfigSchema ( {
@@ -193,6 +223,165 @@ describe("config form map integrity", () => {
193223 expect ( analysis . unsupportedPaths ) . toEqual ( [ "values" ] ) ;
194224 } ) ;
195225
226+ it . each ( [
227+ { label : "pattern" , names : { type : "string" , pattern : "^[a-z]+$" } , invalid : "bad/key" } ,
228+ { label : "minimum length" , names : { type : "string" , minLength : 3 } , invalid : "x" } ,
229+ { label : "enumeration" , names : { enum : [ "primary" , "backup" ] } , invalid : "other" } ,
230+ {
231+ label : "intersection" ,
232+ names : { allOf : [ { minLength : 3 } , { pattern : "^[a-z]+$" } ] } ,
233+ invalid : "bad/key" ,
234+ } ,
235+ ] ) (
236+ "rejects $label key edits even when an existing value is invalid" ,
237+ async ( { names, invalid } ) => {
238+ const analysis = analyzeConfigSchema ( {
239+ type : "object" ,
240+ properties : {
241+ values : {
242+ type : "object" ,
243+ propertyNames : names ,
244+ additionalProperties : { type : "string" , pattern : "^[0-9]+$" } ,
245+ } ,
246+ } ,
247+ } ) ;
248+ expect ( analysis . unsupportedPaths ) . toEqual ( [ ] ) ;
249+ const state = createInitialConfigState ( ) ;
250+ state . configSchema = analysis . schema ;
251+ state . configForm = { values : { primary : "invalid existing value" } } ;
252+ const onPatch = vi . fn ( ( path : Array < string | number > , value : unknown ) =>
253+ updateConfigFormValue ( state , path , value ) ,
254+ ) ;
255+ const container = document . createElement ( "div" ) ;
256+ document . body . append ( container ) ;
257+ try {
258+ render (
259+ renderConfigForm ( {
260+ schema : analysis . schema ,
261+ uiHints : { } ,
262+ unsupportedPaths : analysis . unsupportedPaths ,
263+ value : state . configForm ,
264+ showAdvanced : true ,
265+ onShowAdvanced : ( ) => { } ,
266+ onPatch,
267+ } ) ,
268+ container ,
269+ ) ;
270+ const keyInput = expectElement (
271+ container . querySelector < HTMLInputElement > ( '[aria-label="Key: primary"]' ) ,
272+ "existing key" ,
273+ ) ;
274+ keyInput . value = invalid ;
275+ keyInput . dispatchEvent ( new Event ( "change" , { bubbles : true } ) ) ;
276+ expect ( keyInput . value ) . toBe ( "primary" ) ;
277+ expect ( onPatch ) . not . toHaveBeenCalled ( ) ;
278+
279+ const draft = expectElement (
280+ container . querySelector < ConfigFormCollectionDraft > (
281+ "openclaw-config-form-collection-draft" ,
282+ ) ,
283+ "map draft" ,
284+ ) ;
285+ expectElement (
286+ Array . from ( container . querySelectorAll ( "button" ) ) . find (
287+ ( button ) => button . textContent ?. trim ( ) === "Add Entry" ,
288+ ) ,
289+ "add entry" ,
290+ ) . click ( ) ;
291+ await draft . updateComplete ;
292+ const key = expectElement (
293+ draft . querySelector < HTMLInputElement > ( "[data-collection-draft-key]" ) ,
294+ "draft key" ,
295+ ) ;
296+ const value = expectElement (
297+ draft . querySelector < HTMLInputElement > ( "[data-collection-draft-value]" ) ,
298+ "draft value" ,
299+ ) ;
300+ key . value = invalid ;
301+ key . dispatchEvent ( new Event ( "input" , { bubbles : true } ) ) ;
302+ value . value = "123" ;
303+ value . dispatchEvent ( new Event ( "input" , { bubbles : true } ) ) ;
304+ await draft . updateComplete ;
305+ expectElement (
306+ Array . from ( draft . querySelectorAll ( "button" ) ) . find (
307+ ( button ) => button . textContent ?. trim ( ) === "Add Entry" ,
308+ ) ,
309+ "commit entry" ,
310+ ) . click ( ) ;
311+ await draft . updateComplete ;
312+ expect ( key . getAttribute ( "aria-invalid" ) ) . toBe ( "true" ) ;
313+ expect ( key . value ) . toBe ( invalid ) ;
314+ expect ( onPatch ) . not . toHaveBeenCalled ( ) ;
315+ key . value = "backup" ;
316+ key . dispatchEvent ( new Event ( "input" , { bubbles : true } ) ) ;
317+ await draft . updateComplete ;
318+ expectElement (
319+ Array . from ( draft . querySelectorAll ( "button" ) ) . find (
320+ ( button ) => button . textContent ?. trim ( ) === "Add Entry" ,
321+ ) ,
322+ "commit valid entry" ,
323+ ) . click ( ) ;
324+ expect ( state . configForm ) . toEqual ( {
325+ values : { primary : "invalid existing value" , backup : "123" } ,
326+ } ) ;
327+ } finally {
328+ container . remove ( ) ;
329+ }
330+ } ,
331+ ) ;
332+
333+ it . each ( [ "map" , "array" ] ) (
334+ "keeps supported %s children editable beside Raw-only fields" ,
335+ ( kind ) => {
336+ const rowSchema = {
337+ type : "object" ,
338+ properties : {
339+ name : { type : "string" } ,
340+ retained : { anyOf : [ { type : "string" } , { const : false } ] } ,
341+ } ,
342+ } satisfies JsonSchema ;
343+ const collection = ( name : string ) =>
344+ kind === "map"
345+ ? { "example/model.v1" : { name, retained : false } }
346+ : [ { name, retained : false } ] ;
347+ const analysis = analyzeConfigSchema ( {
348+ type : "object" ,
349+ properties : {
350+ values :
351+ kind === "map"
352+ ? { type : "object" , additionalProperties : rowSchema }
353+ : { type : "array" , items : rowSchema } ,
354+ } ,
355+ } ) ;
356+ expect ( analysis . unsupportedPaths ) . toEqual ( [ "values.*.retained" ] ) ;
357+ const state = createInitialConfigState ( ) ;
358+ state . configSchema = analysis . schema ;
359+ state . configForm = { values : collection ( "before" ) } ;
360+ const container = document . createElement ( "div" ) ;
361+ render (
362+ renderConfigForm ( {
363+ schema : analysis . schema ,
364+ uiHints : { } ,
365+ unsupportedPaths : analysis . unsupportedPaths ,
366+ value : state . configForm ,
367+ showAdvanced : true ,
368+ onShowAdvanced : ( ) => { } ,
369+ onPatch : ( path , value ) => updateConfigFormValue ( state , path , value ) ,
370+ } ) ,
371+ container ,
372+ ) ;
373+ expect ( container . textContent ) . toContain ( "Unsupported schema node. Use Raw mode." ) ;
374+ expect ( container . querySelector ( '[aria-label="Retained"]' ) ) . toBeNull ( ) ;
375+ const name = expectElement (
376+ container . querySelector < HTMLInputElement > ( '[aria-label="Name"]' ) ,
377+ "supported name field" ,
378+ ) ;
379+ name . value = "after" ;
380+ name . dispatchEvent ( new Event ( "input" , { bubbles : true } ) ) ;
381+ expect ( JSON . parse ( serializeFormForSubmit ( state ) ) ) . toEqual ( { values : collection ( "after" ) } ) ;
382+ } ,
383+ ) ;
384+
196385 it ( "retains unset map drafts until the collection source changes" , async ( ) => {
197386 const container = document . createElement ( "div" ) ;
198387 document . body . append ( container ) ;
0 commit comments