Skip to content

Commit c81f6f2

Browse files
test(setup): expand WizardAnswerBuilder edge-case coverage (#749)
Add 10 tests covering previously untested paths in WizardAnswerBuilder: - BuildWireValue(select): falls back to string when option not found - BuildWireValue(multiselect): splits comma string when options list is empty - BuildWireValue(multiselect): splits comma string when answer not in options - ReadOptions: returns empty for non-object input - ReadOptions: returns empty when 'options' property is missing - ReadOptions: returns empty when 'options' is not an array - ValueKeys: returns [] for empty-string JSON element - ValueKeys: returns single-element array for non-array scalar - ValueKeys: returns multiple elements for JSON array - ReadOptions: option without 'value' property uses label as key These paths were introduced in b90ded9 (Preserve wizard option wire values) and are exercised at runtime but had no regression coverage. Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <[email protected]>
1 parent 6811d6a commit c81f6f2

1 file changed

Lines changed: 111 additions & 0 deletions

File tree

‎tests/OpenClaw.SetupEngine.Tests/WizardAnswerBuilderTests.cs‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,117 @@ public void BuildWireValue_MultiselectSkipKeepsSentinelStringArray()
100100
Assert.Equal("""{"value":["__skip__"]}""", SerializeValue(wireValue));
101101
}
102102

103+
[Fact]
104+
public void BuildWireValue_SelectFallsBackToString_WhenNoOptionMatch()
105+
{
106+
var options = ReadOptions("""{"options":[{"label":"Alpha","value":"alpha"}]}""");
107+
108+
// "beta" is not in the options list; should pass through as a plain string
109+
var wireValue = WizardAnswerBuilder.BuildWireValue("select", "beta", options);
110+
111+
Assert.Equal("""{"value":"beta"}""", SerializeValue(wireValue));
112+
}
113+
114+
[Fact]
115+
public void BuildWireValue_MultiselectSplitsCommaString_WhenOptionsEmpty()
116+
{
117+
var wireValue = WizardAnswerBuilder.BuildWireValue("multiselect", "opt-a,opt-b,opt-c", []);
118+
119+
// No options list to resolve against, so falls back to SplitMultiSelect
120+
Assert.Equal("""{"value":["opt-a","opt-b","opt-c"]}""", SerializeValue(wireValue));
121+
}
122+
123+
[Fact]
124+
public void BuildWireValue_MultiselectSplitsCommaString_WhenOptionNotInList()
125+
{
126+
var options = ReadOptions("""{"options":[{"label":"Alpha","value":"alpha"}]}""");
127+
128+
// "gamma" is not in the options list, so TryResolveOptions returns false; falls back to split
129+
var wireValue = WizardAnswerBuilder.BuildWireValue("multiselect", "alpha,gamma", options);
130+
131+
Assert.Equal("""{"value":["alpha","gamma"]}""", SerializeValue(wireValue));
132+
}
133+
134+
[Fact]
135+
public void ReadOptions_ReturnsEmpty_ForNonObjectInput()
136+
{
137+
var arrayElement = ParseElement("""["a","b"]""");
138+
139+
var options = WizardAnswerBuilder.ReadOptions(arrayElement);
140+
141+
Assert.Empty(options);
142+
}
143+
144+
[Fact]
145+
public void ReadOptions_ReturnsEmpty_WhenOptionsPropertyMissing()
146+
{
147+
var step = ParseElement("""{"stepType":"select","id":"step-1"}""");
148+
149+
var options = WizardAnswerBuilder.ReadOptions(step);
150+
151+
Assert.Empty(options);
152+
}
153+
154+
[Fact]
155+
public void ReadOptions_ReturnsEmpty_WhenOptionsIsNotArray()
156+
{
157+
var step = ParseElement("""{"options":"not-an-array"}""");
158+
159+
var options = WizardAnswerBuilder.ReadOptions(step);
160+
161+
Assert.Empty(options);
162+
}
163+
164+
[Fact]
165+
public void ValueKeys_ReturnsEmptyArray_ForEmptyStringElement()
166+
{
167+
// JSON empty string "" → ValueKey returns "" → ValueKeys returns []
168+
var element = ParseElement("\"\"");
169+
170+
var keys = WizardAnswerBuilder.ValueKeys(element);
171+
172+
Assert.Empty(keys);
173+
}
174+
175+
[Fact]
176+
public void ValueKeys_ReturnsSingleElementArray_ForNonArrayValue()
177+
{
178+
var element = ParseElement("42");
179+
180+
var keys = WizardAnswerBuilder.ValueKeys(element);
181+
182+
Assert.Equal(["42"], keys);
183+
}
184+
185+
[Fact]
186+
public void ValueKeys_ReturnsMultipleElements_ForJsonArray()
187+
{
188+
var element = ParseElement("""["a",42,true]""");
189+
190+
var keys = WizardAnswerBuilder.ValueKeys(element);
191+
192+
Assert.Equal(["a", "42", "true"], keys);
193+
}
194+
195+
[Fact]
196+
public void ReadOptions_OptionWithoutValueUsesLabelAsKey()
197+
{
198+
var step = ParseElement("""
199+
{
200+
"options": [
201+
{ "label": "My Label", "hint": "some hint" }
202+
]
203+
}
204+
""");
205+
206+
var options = WizardAnswerBuilder.ReadOptions(step);
207+
208+
var option = Assert.Single(options);
209+
Assert.Equal("My Label", option.Value);
210+
Assert.Equal("My Label", option.Label);
211+
Assert.Equal("some hint", option.Hint);
212+
}
213+
103214
private static IReadOnlyList<WizardOptionValue> ReadOptions(string stepJson) =>
104215
WizardAnswerBuilder.ReadOptions(ParseElement(stepJson));
105216

0 commit comments

Comments
 (0)