|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package executor |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "go.mongodb.org/mongo-driver/v2/bson" |
| 10 | + |
| 11 | + "github.com/mendixlabs/mxcli/modelsdk/widgets/mpk" |
| 12 | +) |
| 13 | + |
| 14 | +func widthTestID(b byte) bson.Binary { |
| 15 | + return bson.Binary{Subtype: 0x00, Data: bytes.Repeat([]byte{b}, 16)} |
| 16 | +} |
| 17 | + |
| 18 | +// slotWidget is a pluggable widget shaped like a Fieldset holding a text box in |
| 19 | +// its `content` slot, with a second property `legend` that the definition below |
| 20 | +// no longer declares, so a sync removes it. Integers are stored the way |
| 21 | +// Studio Pro and `exec` store them: int32 markers and fields, one int64. |
| 22 | +func fieldsetWithSlotTextBox() bson.D { |
| 23 | + textBox := bson.D{ |
| 24 | + {Key: "$ID", Value: widthTestID(0x20)}, |
| 25 | + {Key: "$Type", Value: "Forms$TextBox"}, |
| 26 | + {Key: "DecimalPrecision", Value: int64(2)}, |
| 27 | + {Key: "MaxLengthCode", Value: int32(-1)}, |
| 28 | + {Key: "Name", Value: "tbInner"}, |
| 29 | + {Key: "TabIndex", Value: int32(0)}, |
| 30 | + {Key: "Weight", Value: float64(0.5)}, |
| 31 | + } |
| 32 | + prop := func(id, typeID byte, value bson.D) bson.D { |
| 33 | + return bson.D{ |
| 34 | + {Key: "$ID", Value: widthTestID(id)}, |
| 35 | + {Key: "$Type", Value: "CustomWidgets$WidgetProperty"}, |
| 36 | + {Key: "TypePointer", Value: widthTestID(typeID)}, |
| 37 | + {Key: "Value", Value: value}, |
| 38 | + } |
| 39 | + } |
| 40 | + propType := func(id byte, key, typ string) bson.D { |
| 41 | + return bson.D{ |
| 42 | + {Key: "$ID", Value: widthTestID(id)}, |
| 43 | + {Key: "$Type", Value: "CustomWidgets$WidgetPropertyType"}, |
| 44 | + {Key: "Caption", Value: key}, |
| 45 | + {Key: "Category", Value: "General"}, |
| 46 | + {Key: "Description", Value: ""}, |
| 47 | + {Key: "IsDefault", Value: false}, |
| 48 | + {Key: "PropertyKey", Value: key}, |
| 49 | + {Key: "ValueType", Value: bson.D{ |
| 50 | + {Key: "$ID", Value: widthTestID(id + 1)}, |
| 51 | + {Key: "$Type", Value: "CustomWidgets$WidgetValueType"}, |
| 52 | + {Key: "EnumerationValues", Value: bson.A{int32(2)}}, |
| 53 | + {Key: "Translations", Value: bson.A{int32(2)}}, |
| 54 | + {Key: "Type", Value: typ}, |
| 55 | + }}, |
| 56 | + } |
| 57 | + } |
| 58 | + return bson.D{ |
| 59 | + {Key: "$ID", Value: widthTestID(0x01)}, |
| 60 | + {Key: "$Type", Value: "CustomWidgets$CustomWidget"}, |
| 61 | + {Key: "Name", Value: "fs"}, |
| 62 | + {Key: "Object", Value: bson.D{ |
| 63 | + {Key: "$ID", Value: widthTestID(0x02)}, |
| 64 | + {Key: "$Type", Value: "CustomWidgets$WidgetObject"}, |
| 65 | + {Key: "Properties", Value: bson.A{ |
| 66 | + int32(2), |
| 67 | + prop(0x03, 0x10, bson.D{ |
| 68 | + {Key: "$ID", Value: widthTestID(0x04)}, |
| 69 | + {Key: "$Type", Value: "CustomWidgets$WidgetValue"}, |
| 70 | + {Key: "PrimitiveValue", Value: "Legend"}, |
| 71 | + {Key: "Widgets", Value: bson.A{int32(2)}}, |
| 72 | + }), |
| 73 | + prop(0x05, 0x12, bson.D{ |
| 74 | + {Key: "$ID", Value: widthTestID(0x06)}, |
| 75 | + {Key: "$Type", Value: "CustomWidgets$WidgetValue"}, |
| 76 | + {Key: "PrimitiveValue", Value: ""}, |
| 77 | + {Key: "Widgets", Value: bson.A{int32(2), textBox}}, |
| 78 | + }), |
| 79 | + }}, |
| 80 | + }}, |
| 81 | + {Key: "Type", Value: bson.D{ |
| 82 | + {Key: "$ID", Value: widthTestID(0x07)}, |
| 83 | + {Key: "$Type", Value: "CustomWidgets$CustomWidgetType"}, |
| 84 | + {Key: "ObjectType", Value: bson.D{ |
| 85 | + {Key: "$ID", Value: widthTestID(0x08)}, |
| 86 | + {Key: "$Type", Value: "CustomWidgets$WidgetObjectType"}, |
| 87 | + {Key: "PropertyTypes", Value: bson.A{ |
| 88 | + int32(2), |
| 89 | + propType(0x10, "legend", "String"), |
| 90 | + propType(0x12, "content", "Widgets"), |
| 91 | + }}, |
| 92 | + }}, |
| 93 | + {Key: "WidgetId", Value: "com.mendix.widget.web.fieldset.Fieldset"}, |
| 94 | + }}, |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// ako/mxcli#1976 (mendixlabs/mxcli#1748): the map form turned every int32 and |
| 99 | +// int64 into float64 and nothing turned them back, so a synced widget was |
| 100 | +// written with doubles throughout, and a text box in its slot read |
| 101 | +// MaxLengthCode -1.0 as "unlimited" (CE6510). The round trip with no |
| 102 | +// reconciliation must be byte-identical for every numeric width Mendix stores, |
| 103 | +// a genuine double included. |
| 104 | +func TestWidgetRoundTripKeepsNumericWidths(t *testing.T) { |
| 105 | + widget := fieldsetWithSlotTextBox() |
| 106 | + original, err := bson.Marshal(widget) |
| 107 | + if err != nil { |
| 108 | + t.Fatal(err) |
| 109 | + } |
| 110 | + back, ok := mapToWidgetDoc(widgetToMap(widget)).(bson.D) |
| 111 | + if !ok { |
| 112 | + t.Fatal("mapToWidgetDoc did not return a bson.D") |
| 113 | + } |
| 114 | + encoded, err := bson.Marshal(back) |
| 115 | + if err != nil { |
| 116 | + t.Fatal(err) |
| 117 | + } |
| 118 | + if !bytes.Equal(original, encoded) { |
| 119 | + t.Errorf("round trip changed the document:\n got %v", back) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// The same, through the sync itself: dropping the stale `legend` property must |
| 124 | +// leave the text box in the slot, and every marker, at its stored width. |
| 125 | +func TestWidgetSyncKeepsNumericWidthsInSlot(t *testing.T) { |
| 126 | + def := &mpk.WidgetDefinition{ |
| 127 | + ID: "com.mendix.widget.web.fieldset.Fieldset", |
| 128 | + IsPluggable: true, |
| 129 | + Properties: []mpk.PropertyDef{{Key: "content", Type: "widgets", Caption: "content", Category: "General"}}, |
| 130 | + } |
| 131 | + got, ok := applyToWidget(fieldsetWithSlotTextBox(), def) |
| 132 | + if !ok { |
| 133 | + t.Fatal("applyToWidget reported no change") |
| 134 | + } |
| 135 | + raw, err := bson.Marshal(got) |
| 136 | + if err != nil { |
| 137 | + t.Fatal(err) |
| 138 | + } |
| 139 | + var doubles []string |
| 140 | + walkRaw(bson.Raw(raw), "", func(path string, v bson.RawValue) { |
| 141 | + if v.Type == bson.TypeDouble && path != "Object.Properties.1.Value.Widgets.1.Weight" { |
| 142 | + doubles = append(doubles, path) |
| 143 | + } |
| 144 | + }) |
| 145 | + if len(doubles) > 0 { |
| 146 | + t.Errorf("sync wrote integers as doubles at: %v", doubles) |
| 147 | + } |
| 148 | + if bytes.Contains(raw, []byte("legend")) { |
| 149 | + t.Error("the stale property was not removed; the test does not exercise a change") |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +// walkRaw visits every leaf of a raw document with its dotted path. |
| 154 | +func walkRaw(doc bson.Raw, prefix string, visit func(string, bson.RawValue)) { |
| 155 | + elems, _ := doc.Elements() |
| 156 | + for _, e := range elems { |
| 157 | + path := e.Key() |
| 158 | + if prefix != "" { |
| 159 | + path = prefix + "." + path |
| 160 | + } |
| 161 | + v := e.Value() |
| 162 | + switch v.Type { |
| 163 | + case bson.TypeEmbeddedDocument: |
| 164 | + walkRaw(v.Document(), path, visit) |
| 165 | + case bson.TypeArray: |
| 166 | + walkRaw(bson.Raw(v.Array()), path, visit) |
| 167 | + default: |
| 168 | + visit(path, v) |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments