forked from galaxyproject/galaxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.test.js
More file actions
804 lines (724 loc) · 30.9 KB
/
utilities.test.js
File metadata and controls
804 lines (724 loc) · 30.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
import { describe, expect, it } from "vitest";
import toolModel from "./test-data/tool";
import { buildNestedState, matchCase, matchInputs, validateInputs, visitAllInputs, visitInputs } from "./utilities";
function visitInputsString(inputs) {
let results = "";
visitInputs(inputs, (input, identifier) => {
results += `${identifier}=${input.value};`;
});
return results;
}
describe("form component utilities", () => {
it("tool model test", () => {
const visits = [];
visitInputs(toolModel.inputs, function (node, name) {
visits.push({ name: name, node: node });
});
const result =
'[{"name":"a","node":{"name":"a","type":"text"}},{"name":"b|c","node":{"name":"c","type":"select","value":"h","options":[["d","d",false],["h","h",false]]}},{"name":"b|i","node":{"name":"i","type":"text","value":"i"}},{"name":"b|j","node":{"name":"j","type":"text","value":"j"}},{"name":"k_0|l","node":{"name":"l","type":"text","value":"l"}},{"name":"k_0|m|n","node":{"name":"n","type":"select","value":"r","options":[["o","o",false],["r","r",false]]}},{"name":"k_0|m|s","node":{"name":"s","type":"text","value":"s"}},{"name":"k_0|m|t","node":{"name":"t","type":"text","value":"t"}}]';
expect(JSON.stringify(visits)).toEqual(result);
});
it("conditional case matching", () => {
const input = {
name: "a",
type: "conditional",
test_param: {
name: "b",
type: "boolean",
value: "true",
truevalue: undefined,
falsevalue: undefined,
},
cases: [
{
value: "true",
inputs: [
{
name: "c",
type: "text",
value: "cvalue",
},
],
},
{
value: "false",
inputs: [
{
name: "d",
type: "text",
value: "dvalue",
},
],
},
],
};
// test simple case matching
expect(matchCase(input, "true")).toEqual(0);
expect(matchCase(input, true)).toEqual(0);
expect(matchCase(input, "false")).toEqual(1);
expect(matchCase(input, false)).toEqual(1);
// test truevalue
input.test_param.truevalue = "truevalue";
expect(matchCase(input, "true")).toEqual(-1);
input.cases[0].value = "truevalue";
expect(matchCase(input, "true")).toEqual(0);
// test falsevalue
input.test_param.falsevalue = "falsevalue";
expect(matchCase(input, "true")).toEqual(0);
expect(matchCase(input, "false")).toEqual(-1);
input.cases[1].value = "falsevalue";
expect(matchCase(input, "false")).toEqual(1);
// test (empty) truevalue
input.test_param.truevalue = undefined;
input.cases[0].value = "true";
expect(matchCase(input, "true")).toEqual(0);
input.test_param.truevalue = "";
expect(matchCase(input, "true")).toEqual(-1);
input.cases[0].value = "";
expect(matchCase(input, "true")).toEqual(0);
// test visit inputs
expect(visitInputsString([input])).toEqual("a|b=true;a|c=cvalue;");
input.test_param.value = "false";
expect(visitInputsString([input])).toEqual("a|b=false;a|d=dvalue;");
// switch test parameter to other type than boolean e.g. select
input.test_param.type = "select";
expect(matchCase(input, "")).toEqual(0);
expect(matchCase(input, "unavailable")).toEqual(-1);
expect(matchCase(input, "falsevalue")).toEqual(1);
});
it("test basic value validation", () => {
const index = {
input_a: {},
input_b: {},
input_c: {},
};
const values = {
input_a: "1",
input_b: "2",
input_c: { values: [{ id: 0 }] },
};
let result = validateInputs(index, values);
expect(result).toEqual(null);
values.input_a = undefined;
result = validateInputs(index, values);
expect(JSON.stringify(result)).toEqual('["input_a","Please provide a value for this option."]');
index.input_a.optional = true;
result = validateInputs(index, values);
expect(result).toEqual(null);
values.input_c.values = [];
result = validateInputs(index, values);
expect(JSON.stringify(result)).toEqual('["input_c","Please provide data for this input."]');
});
it("test error matching", () => {
const index = {
input_a: {},
input_b_0: {},
"input_c|input_d": {},
};
const values = {
input_a: "error_a",
input_b: ["error_b"],
input_c: { input_d: "error_d" },
};
const result = matchInputs(index, values);
expect(result["input_a"]).toEqual("error_a");
expect(result["input_b_0"]).toEqual("error_b");
expect(result["input_c|input_d"]).toEqual("error_d");
});
it("test multiple validators", () => {
const index = {
path: {
validators: [
{
type: "regex",
expression: "^.*[^/]$",
message: "Value cannot end with a trailing slash",
negate: false,
},
{
type: "regex",
expression: "^(?!\\s)(?!.*\\s$).*$",
message: "Value cannot have leading or trailing whitespace",
negate: false,
},
],
},
};
// Valid path
let values = { path: "clean/path" };
let result = validateInputs(index, values);
expect(result).toEqual(null);
// Invalid: trailing slash
values = { path: "path/" };
result = validateInputs(index, values);
expect(result).toEqual(["path", "Value cannot end with a trailing slash"]);
// Invalid: trailing whitespace
values = { path: "path " };
result = validateInputs(index, values);
expect(result).toEqual(["path", "Value cannot have leading or trailing whitespace"]);
});
it("test validators skip empty optional values", () => {
const index = {
optional_field: {
optional: true,
validators: [
{
type: "regex",
expression: "^(?!\\s)(?!.*\\s$)(?!.*/$).+$",
message: "Value cannot have leading/trailing spaces or trailing slashes",
negate: false,
},
],
},
};
// Empty value should not trigger validator
const values = { optional_field: "" };
const result = validateInputs(index, values);
expect(result).toEqual(null);
});
it("test validators skip null values", () => {
const index = {
optional_field: {
optional: true, // Make it optional so null is allowed
validators: [
{
type: "regex",
expression: "^(?!\\s)(?!.*\\s$)(?!.*/$).+$",
message: "Value cannot have leading/trailing spaces or trailing slashes",
negate: false,
},
],
},
};
// Null value should not trigger validator for optional fields
const values = { optional_field: null };
const result = validateInputs(index, values);
expect(result).toEqual(null);
});
it("test length validator", () => {
const index = {
username: {
validators: [
{
type: "length",
min: 3,
max: 20,
message: "Username must be between 3 and 20 characters",
},
],
},
};
// Valid length
let values = { username: "john" };
let result = validateInputs(index, values);
expect(result).toEqual(null);
values = { username: "a".repeat(20) };
result = validateInputs(index, values);
expect(result).toEqual(null);
// Too short
values = { username: "ab" };
result = validateInputs(index, values);
expect(result).toEqual(["username", "Username must be between 3 and 20 characters"]);
// Too long
values = { username: "a".repeat(21) };
result = validateInputs(index, values);
expect(result).toEqual(["username", "Username must be between 3 and 20 characters"]);
});
it("test in_range validator", () => {
const index = {
age: {
validators: [
{
type: "in_range",
min: 18,
max: 120,
message: "Age must be between 18 and 120",
},
],
},
};
// Valid range
let values = { age: 25 };
let result = validateInputs(index, values);
expect(result).toEqual(null);
values = { age: 18 };
result = validateInputs(index, values);
expect(result).toEqual(null);
values = { age: 120 };
result = validateInputs(index, values);
expect(result).toEqual(null);
// Too small
values = { age: 17 };
result = validateInputs(index, values);
expect(result).toEqual(["age", "Age must be between 18 and 120"]);
// Too large
values = { age: 121 };
result = validateInputs(index, values);
expect(result).toEqual(["age", "Age must be between 18 and 120"]);
});
it("test in_range validator with non-numeric value", () => {
const index = {
age: {
validators: [
{
type: "in_range",
min: 18,
max: 120,
message: "Age must be between 18 and 120",
},
],
},
};
// Non-numeric value
const values = { age: "not a number" };
const result = validateInputs(index, values);
expect(result).toEqual(["age", "Value must be numeric for range validation"]);
});
it("test validators run on optional fields with values", () => {
const index = {
optional_field: {
optional: true,
validators: [
{
type: "length",
min: 3,
message: "Must be at least 3 characters",
},
],
},
};
// Empty value should not trigger validator
let values = { optional_field: "" };
let result = validateInputs(index, values);
expect(result).toEqual(null);
// null value should not trigger validator
values = { optional_field: null };
result = validateInputs(index, values);
expect(result).toEqual(null);
// undefined should not trigger validator
values = { optional_field: undefined };
result = validateInputs(index, values);
expect(result).toEqual(null);
// But when optional field has a value, validator should run
values = { optional_field: "ab" }; // Too short
result = validateInputs(index, values);
expect(result).toEqual(["optional_field", "Must be at least 3 characters"]);
// Valid value should pass
values = { optional_field: "valid" };
result = validateInputs(index, values);
expect(result).toEqual(null);
});
it("test validators with undefined min or max values", () => {
const index = {
field_with_undefined_min: {
validators: [
{
type: "length",
min: undefined,
max: 10,
message: "Must be at most 10 characters",
},
],
},
};
// Should only validate max when min is undefined
let values = { field_with_undefined_min: "a" }; // Very short but no min
let result = validateInputs(index, values);
expect(result).toEqual(null);
values = { field_with_undefined_min: "a".repeat(11) }; // Too long
result = validateInputs(index, values);
expect(result).toEqual(["field_with_undefined_min", "Must be at most 10 characters"]);
});
it("test validators with null min or max values", () => {
const index = {
field_with_null_max: {
validators: [
{
type: "length",
min: 3,
max: null,
message: "Must be at least 3 characters",
},
],
},
};
// Should only validate min when max is null
let values = { field_with_null_max: "a".repeat(100) }; // Very long but no max
let result = validateInputs(index, values);
expect(result).toEqual(null);
values = { field_with_null_max: "ab" }; // Too short
result = validateInputs(index, values);
expect(result).toEqual(["field_with_null_max", "Must be at least 3 characters"]);
});
it("test required field validation with empty string vs undefined vs null", () => {
const index = {
required_field: {
optional: false,
},
};
// Empty string should PASS for required field (default behavior - allowEmptyValueOnRequiredInput=false)
// Note: allowEmptyValueOnRequiredInput is misnamed - it should be rejectEmptyRequiredInputs
let values = { required_field: "" };
let result = validateInputs(index, values);
expect(result).toEqual(null);
// undefined should fail for required field
values = { required_field: undefined };
result = validateInputs(index, values);
expect(result).toEqual(["required_field", "Please provide a value for this option."]);
// null should fail for required field
values = { required_field: null };
result = validateInputs(index, values);
expect(result).toEqual(["required_field", "Please provide a value for this option."]);
// Non-empty value should pass
values = { required_field: "value" };
result = validateInputs(index, values);
expect(result).toEqual(null);
// When allowEmptyValueOnRequiredInput=true (misnamed, really means reject empty strings too),
// empty string should also fail
values = { required_field: "" };
result = validateInputs(index, values, true);
expect(result).toEqual(["required_field", "Please provide a value for this option."]);
});
describe("visitAllInputs", () => {
it("should match visitInputs traversal order for non-conditional inputs", () => {
const inputs = [
{ name: "a", type: "text", value: "a" },
{
name: "r",
type: "repeat",
cache: [
[
{ name: "x", type: "text", value: "x" },
{ name: "y", type: "text", value: "y" },
],
],
inputs: [
{ name: "x", type: "text", value: "x" },
{ name: "y", type: "text", value: "y" },
],
},
{
name: "s",
type: "section",
inputs: [
{ name: "p", type: "text", value: "p" },
{ name: "q", type: "text", value: "q" },
],
},
];
const visitOrder = [];
const allOrder = [];
visitInputs(inputs, (node, name) => visitOrder.push(name));
visitAllInputs(inputs, (node, name) => allOrder.push(name));
expect(allOrder).toEqual(visitOrder);
});
it("should visit all conditional cases, not just the active one", () => {
const inputs = [
{
name: "cond",
type: "conditional",
test_param: { name: "tp", type: "select", value: "a" },
cases: [
{
value: "a",
inputs: [{ name: "in_a", type: "text", value: "va" }],
},
{
value: "b",
inputs: [{ name: "in_b", type: "text", value: "vb" }],
},
],
},
];
const visitNames = [];
const allNames = [];
visitInputs(inputs, (node, name) => visitNames.push(name));
visitAllInputs(inputs, (node, name) => allNames.push(name));
// visitInputs only visits active case (a)
expect(visitNames).toEqual(["cond|tp", "cond|in_a"]);
// visitAllInputs visits both cases
expect(allNames).toEqual(["cond|tp", "cond|in_a", "cond|in_b"]);
});
it("should visit nested conditional inside repeat", () => {
const inputs = [
{
name: "rep",
type: "repeat",
cache: [
[
{
name: "nested_cond",
type: "conditional",
test_param: { name: "sel", type: "select", value: "x" },
cases: [
{
value: "x",
inputs: [{ name: "leaf_x", type: "text", value: "1" }],
},
{
value: "y",
inputs: [{ name: "leaf_y", type: "text", value: "2" }],
},
],
},
],
],
inputs: [],
},
];
const allNames = [];
visitAllInputs(inputs, (node, name) => allNames.push(name));
expect(allNames).toEqual(["rep_0|nested_cond|sel", "rep_0|nested_cond|leaf_x", "rep_0|nested_cond|leaf_y"]);
});
});
describe("buildNestedState", () => {
it("should build nested state for simple params", () => {
const inputs = [
{ name: "a", type: "text" },
{ name: "b", type: "integer" },
{ name: "c", type: "boolean" },
];
const formData = { a: "hello", b: 42, c: true };
expect(buildNestedState(inputs, formData)).toEqual({
a: "hello",
b: 42,
c: true,
});
});
it("should build nested state for sections", () => {
const inputs = [
{
name: "sect",
type: "section",
inputs: [
{ name: "p", type: "text" },
{ name: "q", type: "integer" },
],
},
];
const formData = { "sect|p": "val_p", "sect|q": 10 };
expect(buildNestedState(inputs, formData)).toEqual({
sect: { p: "val_p", q: 10 },
});
});
it("should build nested state for conditionals (active case only)", () => {
const inputs = [
{
name: "cond",
type: "conditional",
test_param: { name: "tp", type: "select", value: "a" },
cases: [
{ value: "a", inputs: [{ name: "in_a", type: "text" }] },
{ value: "b", inputs: [{ name: "in_b", type: "text" }] },
],
},
];
const formData = { "cond|tp": "a", "cond|in_a": "val_a", "cond|in_b": "val_b" };
expect(buildNestedState(inputs, formData)).toEqual({
cond: { tp: "a", in_a: "val_a" },
});
});
it("should build nested state for repeats as arrays", () => {
const inputs = [
{
name: "rep",
type: "repeat",
cache: [[{ name: "x", type: "text" }], [{ name: "x", type: "text" }]],
inputs: [{ name: "x", type: "text" }],
},
];
const formData = { "rep_0|x": "first", "rep_1|x": "second" };
expect(buildNestedState(inputs, formData)).toEqual({
rep: [{ x: "first" }, { x: "second" }],
});
});
it("should produce empty array for repeats with no instances", () => {
const inputs = [
{
name: "rep",
type: "repeat",
cache: [],
inputs: [{ name: "x", type: "text" }],
},
];
expect(buildNestedState(inputs, {})).toEqual({ rep: [] });
});
it("should build nested state for nested repeats with conditionals", () => {
const inputs = [
{
name: "rep",
type: "repeat",
cache: [
[
{ name: "param", type: "text" },
{
name: "cond",
type: "conditional",
test_param: { name: "sel", type: "select", value: "x" },
cases: [
{ value: "x", inputs: [{ name: "leaf", type: "text" }] },
{ value: "y", inputs: [{ name: "other", type: "text" }] },
],
},
],
],
inputs: [],
},
];
const formData = {
"rep_0|param": "val",
"rep_0|cond|sel": "x",
"rep_0|cond|leaf": "leaf_val",
};
expect(buildNestedState(inputs, formData)).toEqual({
rep: [{ param: "val", cond: { sel: "x", leaf: "leaf_val" } }],
});
});
it("should wrap single dataset value as {src, id}", () => {
const inputs = [{ name: "input1", type: "data" }];
const formData = {
input1: { batch: false, product: false, values: [{ id: "abc123", src: "hda", map_over_type: null }] },
};
expect(buildNestedState(inputs, formData)).toEqual({
input1: { src: "hda", id: "abc123" },
});
});
it("should wrap data_collection value as {src, id}", () => {
const inputs = [{ name: "input1", type: "data_collection" }];
const formData = {
input1: {
batch: false,
product: false,
values: [{ id: "col456", src: "hdca", map_over_type: null }],
},
};
expect(buildNestedState(inputs, formData)).toEqual({
input1: { src: "hdca", id: "col456" },
});
});
it("should wrap multiple dataset values as array of {src, id}", () => {
const inputs = [{ name: "input1", type: "data" }];
const formData = {
input1: {
batch: false,
product: false,
values: [
{ id: "id1", src: "hda", map_over_type: null },
{ id: "id2", src: "hda", map_over_type: null },
],
},
};
expect(buildNestedState(inputs, formData)).toEqual({
input1: [
{ src: "hda", id: "id1" },
{ src: "hda", id: "id2" },
],
});
});
it("should wrap batch dataset values with __class__: Batch", () => {
const inputs = [{ name: "input1", type: "data" }];
const formData = {
input1: {
batch: true,
product: false,
values: [{ id: "id1", src: "hda", map_over_type: null }],
},
};
expect(buildNestedState(inputs, formData)).toEqual({
input1: { __class__: "Batch", values: [{ src: "hda", id: "id1" }] },
});
});
it("should wrap single dataset value as array when multiple is true", () => {
const inputs = [{ name: "input1", type: "data", multiple: true }];
const formData = {
input1: { batch: false, product: false, values: [{ id: "abc123", src: "hda", map_over_type: null }] },
};
expect(buildNestedState(inputs, formData)).toEqual({
input1: [{ src: "hda", id: "abc123" }],
});
});
it("should return null for empty dataset values", () => {
const inputs = [{ name: "input1", type: "data" }];
const formData = { input1: { batch: false, values: [] } };
expect(buildNestedState(inputs, formData)).toEqual({ input1: null });
});
it("should return null for null/undefined dataset values", () => {
const inputs = [{ name: "input1", type: "data" }];
const formData = { input1: null };
expect(buildNestedState(inputs, formData)).toEqual({ input1: null });
});
it("should coerce data_column string values to integers", () => {
const inputs = [{ name: "col", type: "data_column" }];
const formData = { col: "3" };
expect(buildNestedState(inputs, formData)).toEqual({ col: 3 });
});
it("should coerce data_column array of strings to integers", () => {
const inputs = [{ name: "col", type: "data_column" }];
const formData = { col: ["1", "2", "5"] };
expect(buildNestedState(inputs, formData)).toEqual({ col: [1, 2, 5] });
});
it("should convert cleared data_column values to null but keep undefined as-is", () => {
const inputs = [{ name: "col", type: "data_column" }];
expect(buildNestedState(inputs, { col: null })).toEqual({ col: null });
expect(buildNestedState(inputs, { col: "" })).toEqual({ col: null });
expect(buildNestedState(inputs, { col: undefined })).toEqual({ col: undefined });
});
it("should preserve data_column already-numeric values", () => {
const inputs = [{ name: "col", type: "data_column" }];
expect(buildNestedState(inputs, { col: 5 })).toEqual({ col: 5 });
expect(buildNestedState(inputs, { col: [1, 2] })).toEqual({ col: [1, 2] });
});
it("should coerce integer string values to integers", () => {
const inputs = [{ name: "num", type: "integer" }];
expect(buildNestedState(inputs, { num: "42" })).toEqual({ num: 42 });
});
it("should convert cleared integer values to null but keep undefined as-is", () => {
const inputs = [{ name: "num", type: "integer" }];
expect(buildNestedState(inputs, { num: null })).toEqual({ num: null });
expect(buildNestedState(inputs, { num: "" })).toEqual({ num: null });
expect(buildNestedState(inputs, { num: undefined })).toEqual({ num: undefined });
});
it("should preserve integer already-numeric values", () => {
const inputs = [{ name: "num", type: "integer" }];
expect(buildNestedState(inputs, { num: 7 })).toEqual({ num: 7 });
});
it("should coerce float string values to floats", () => {
const inputs = [{ name: "val", type: "float" }];
expect(buildNestedState(inputs, { val: "3.14" })).toEqual({ val: 3.14 });
});
it("should convert cleared float values to null but keep undefined as-is", () => {
const inputs = [{ name: "val", type: "float" }];
expect(buildNestedState(inputs, { val: null })).toEqual({ val: null });
expect(buildNestedState(inputs, { val: "" })).toEqual({ val: null });
expect(buildNestedState(inputs, { val: undefined })).toEqual({ val: undefined });
});
it("should preserve float already-numeric values", () => {
const inputs = [{ name: "val", type: "float" }];
expect(buildNestedState(inputs, { val: 2.718 })).toEqual({ val: 2.718 });
});
it("should wrap multiple select string value as array", () => {
const inputs = [{ name: "sel", type: "select", multiple: true }];
expect(buildNestedState(inputs, { sel: "a_check" })).toEqual({ sel: ["a_check"] });
});
it("should preserve multiple select array value", () => {
const inputs = [{ name: "sel", type: "select", multiple: true }];
expect(buildNestedState(inputs, { sel: ["a", "b"] })).toEqual({ sel: ["a", "b"] });
});
it("should preserve multiple select null value", () => {
const inputs = [{ name: "sel", type: "select", multiple: true }];
expect(buildNestedState(inputs, { sel: null })).toEqual({ sel: null });
});
it("should handle the full tool model from test data", () => {
// Build formData from the tool model (mimics what visitInputs collects)
const formData = {};
visitInputs(toolModel.inputs, (input, name) => {
formData[name] = input.value;
});
const result = buildNestedState(toolModel.inputs, formData);
// Conditional "b" has test_param "c" with value "h" → case "h" is active
expect(result.b).toEqual({ c: "h", i: "i", j: "j" });
// Repeat "k" has one instance with conditional "m", test_param "n" with value "r" → case "r"
expect(result.k).toEqual([{ l: "l", m: { n: "r", s: "s", t: "t" } }]);
});
});
});