88
99 "github.com/mendixlabs/mxcli/mdl/ast"
1010 "github.com/mendixlabs/mxcli/model"
11+ "github.com/mendixlabs/mxcli/sdk/mpr"
1112 "github.com/mendixlabs/mxcli/sdk/pages"
1213 "go.mongodb.org/mongo-driver/bson"
1314)
@@ -17,8 +18,7 @@ func TestWidgetDefinitionJSONRoundTrip(t *testing.T) {
1718 WidgetID : "com.mendix.widget.web.combobox.Combobox" ,
1819 MDLName : "COMBOBOX" ,
1920 TemplateFile : "combobox.json" ,
20- DefaultEditable : "Always" ,
21- DefaultSelection : "Single" ,
21+ DefaultEditable : "Always" ,
2222 PropertyMappings : []PropertyMapping {
2323 {PropertyKey : "attributeEnumeration" , Source : "Attribute" , Operation : "attribute" },
2424 {PropertyKey : "optionsSourceType" , Value : "enumeration" , Operation : "primitive" },
@@ -62,9 +62,6 @@ func TestWidgetDefinitionJSONRoundTrip(t *testing.T) {
6262 if decoded .DefaultEditable != original .DefaultEditable {
6363 t .Errorf ("DefaultEditable: got %q, want %q" , decoded .DefaultEditable , original .DefaultEditable )
6464 }
65- if decoded .DefaultSelection != original .DefaultSelection {
66- t .Errorf ("DefaultSelection: got %q, want %q" , decoded .DefaultSelection , original .DefaultSelection )
67- }
6865
6966 // Verify property mappings
7067 if len (decoded .PropertyMappings ) != len (original .PropertyMappings ) {
@@ -119,10 +116,6 @@ func TestWidgetDefinitionJSONOmitsEmptyOptionalFields(t *testing.T) {
119116 t .Fatalf ("failed to unmarshal to map: %v" , err )
120117 }
121118
122- // defaultSelection should be omitted when empty
123- if _ , exists := raw ["defaultSelection" ]; exists {
124- t .Error ("defaultSelection should be omitted when empty" )
125- }
126119}
127120
128121func TestOperationRegistryLookupFound (t * testing.T ) {
@@ -517,29 +510,49 @@ func TestSetChildWidgets(t *testing.T) {
517510}
518511
519512func TestOpSelection (t * testing.T ) {
520- // Test opSelection directly on a Value bson.D (same level as setChildWidgets test)
521- // opSelection calls updateWidgetPropertyValue which needs TypePointer matching.
522- // Instead, test the inner logic: the Selection field update in a Value document.
523- val := bson.D {
524- {Key : "TypePointer" , Value : []byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 }},
525- {Key : "PrimitiveValue" , Value : "" },
526- {Key : "Selection" , Value : "None" },
513+ // Call the real opSelection function with a properly structured widget BSON.
514+ typePointerBytes := []byte {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 }
515+ typePointerUUID := mpr .BlobToUUID (typePointerBytes )
516+
517+ widgetObj := bson.D {
518+ {Key : "Properties" , Value : bson.A {
519+ int32 (2 ), // version marker
520+ bson.D {
521+ {Key : "TypePointer" , Value : typePointerBytes },
522+ {Key : "Value" , Value : bson.D {
523+ {Key : "PrimitiveValue" , Value : "" },
524+ {Key : "Selection" , Value : "None" },
525+ }},
526+ },
527+ }},
528+ }
529+
530+ propTypeIDs := map [string ]pages.PropertyTypeIDEntry {
531+ "selectionType" : {PropertyTypeID : typePointerUUID },
527532 }
528533
529- // Simulate what opSelection's inner function does
530534 ctx := & BuildContext {PrimitiveVal : "Multi" }
531- result := make (bson.D , 0 , len (val ))
532- for _ , elem := range val {
533- if elem .Key == "Selection" {
534- result = append (result , bson.E {Key : "Selection" , Value : ctx .PrimitiveVal })
535- } else {
536- result = append (result , elem )
535+ result := opSelection (widgetObj , propTypeIDs , "selectionType" , ctx )
536+
537+ // Extract the updated Value from Properties
538+ var props bson.A
539+ for _ , elem := range result {
540+ if elem .Key == "Properties" {
541+ props = elem .Value .(bson.A )
542+ }
543+ }
544+ prop := props [1 ].(bson.D ) // skip version marker at index 0
545+ var val bson.D
546+ for _ , elem := range prop {
547+ if elem .Key == "Value" {
548+ val = elem .Value .(bson.D )
537549 }
538550 }
539551
540- // Verify Selection was updated
541- for _ , elem := range result {
552+ selectionFound := false
553+ for _ , elem := range val {
542554 if elem .Key == "Selection" {
555+ selectionFound = true
543556 if elem .Value != "Multi" {
544557 t .Errorf ("Selection: got %q, want %q" , elem .Value , "Multi" )
545558 }
@@ -550,4 +563,20 @@ func TestOpSelection(t *testing.T) {
550563 }
551564 }
552565 }
566+ if ! selectionFound {
567+ t .Error ("Selection field not found in result" )
568+ }
569+ }
570+
571+ func TestOpSelectionEmptyValue (t * testing.T ) {
572+ widgetObj := bson.D {
573+ {Key : "Properties" , Value : bson.A {int32 (2 )}},
574+ }
575+ ctx := & BuildContext {PrimitiveVal : "" }
576+ result := opSelection (widgetObj , nil , "any" , ctx )
577+
578+ // With empty PrimitiveVal, opSelection returns obj unchanged
579+ if len (result ) != len (widgetObj ) {
580+ t .Errorf ("expected unchanged obj, got different length: %d vs %d" , len (result ), len (widgetObj ))
581+ }
553582}
0 commit comments