oxidize-pdf 2.5.0

A pure Rust PDF generation and manipulation library with zero external dependencies
Documentation
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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
//! Forms Validation and JavaScript Integration Tests
//!
//! This test suite covers advanced form validation scenarios and JavaScript
//! integration capabilities, addressing medium-priority gaps identified in
//! the coverage analysis. These tests ensure that forms properly handle
//! validation rules, JavaScript actions, and interactive behaviors.
//!
//! Test categories:
//! - Field validation rules and constraints
//! - JavaScript action parsing and preservation
//! - Calculate order and dependencies
//! - Format and keystroke actions
//! - Custom validation functions
//! - Cross-field dependencies and calculations

use oxidize_pdf::forms::{
    ComboBox, FieldFlags, FieldOptions, FormField, FormManager, TextField, Widget,
};
use oxidize_pdf::geometry::{Point, Rectangle};
use oxidize_pdf::objects::{Dictionary, Object};

/// Test 1: Basic field validation constraints
#[test]
fn test_basic_field_validation_constraints() {
    let mut form_manager = FormManager::new();

    // Test required field validation
    let required_flags = FieldFlags {
        read_only: false,
        required: true,
        no_export: false,
    };

    let required_options = FieldOptions {
        flags: required_flags,
        default_appearance: Some("/Helv 12 Tf 0 g".to_string()),
        quadding: None,
    };

    let required_field = TextField::new("required_email")
        .with_value("") // Empty value should trigger validation
        .with_max_length(100);

    let widget = Widget::new(Rectangle::new(
        Point::new(50.0, 600.0),
        Point::new(300.0, 620.0),
    ));

    let _field_ref = form_manager
        .add_text_field(required_field, widget, Some(required_options))
        .unwrap();

    // Verify required flag is set
    if let Some(form_field) = form_manager.get_field("required_email") {
        if let Some(Object::Integer(flags)) = form_field.field_dict.get("Ff") {
            assert_ne!(*flags & 2, 0); // Required flag (bit 1)
            println!("Required field validation constraint set correctly");
        }
    }

    // Test read-only field constraint
    let readonly_flags = FieldFlags {
        read_only: true,
        required: false,
        no_export: false,
    };

    let readonly_options = FieldOptions {
        flags: readonly_flags,
        default_appearance: None,
        quadding: None,
    };

    let readonly_field = TextField::new("readonly_field")
        .with_value("Cannot be modified")
        .with_max_length(50);

    let readonly_widget = Widget::new(Rectangle::new(
        Point::new(50.0, 550.0),
        Point::new(300.0, 570.0),
    ));

    form_manager
        .add_text_field(readonly_field, readonly_widget, Some(readonly_options))
        .unwrap();

    if let Some(form_field) = form_manager.get_field("readonly_field") {
        if let Some(Object::Integer(flags)) = form_field.field_dict.get("Ff") {
            assert_ne!(*flags & 1, 0); // ReadOnly flag (bit 0)
            println!("Read-only field constraint set correctly");
        }
    }

    println!("Basic field validation constraints test completed");
}

/// Test 2: JavaScript format actions
#[test]
fn test_javascript_format_actions() {
    let mut field_dict = Dictionary::new();
    field_dict.set("T", Object::String("currency_field".to_string()));
    field_dict.set("FT", Object::Name("Tx".to_string()));
    field_dict.set("V", Object::String("1234.56".to_string()));

    // Additional Actions (AA) dictionary
    let mut aa_dict = Dictionary::new();

    // Format action - converts number to currency format
    let format_js = r#"
        if (event.value != "") {
            var num = parseFloat(event.value);
            if (!isNaN(num)) {
                event.value = "$" + num.toFixed(2);
            }
        }
    "#;
    aa_dict.set("F", Object::String(format_js.to_string()));

    // Keystroke action - validates numeric input
    let keystroke_js = r#"
        if (event.willCommit) {
            var re = /^\d*\.?\d{0,2}$/;
            if (!re.test(event.value)) {
                app.alert("Please enter a valid currency amount");
                event.rc = false;
            }
        }
    "#;
    aa_dict.set("K", Object::String(keystroke_js.to_string()));

    field_dict.set("AA", Object::Dictionary(aa_dict));

    let form_field = FormField::new(field_dict);

    // Verify JavaScript actions are preserved
    if let Some(Object::Dictionary(actions)) = form_field.field_dict.get("AA") {
        if let Some(Object::String(format_action)) = actions.get("F") {
            assert!(format_action.contains("toFixed(2)"));
            assert!(format_action.contains("parseFloat"));
            println!("Format JavaScript action preserved correctly");
        }

        if let Some(Object::String(keystroke_action)) = actions.get("K") {
            assert!(keystroke_action.contains("willCommit"));
            assert!(keystroke_action.contains("event.rc"));
            println!("Keystroke JavaScript action preserved correctly");
        }
    }

    println!("JavaScript format actions test completed");
}

/// Test 3: Field validation actions
#[test]
fn test_field_validation_actions() {
    let mut field_dict = Dictionary::new();
    field_dict.set("T", Object::String("email_field".to_string()));
    field_dict.set("FT", Object::Name("Tx".to_string()));

    let mut aa_dict = Dictionary::new();

    // Validation action - email format validation
    let validate_js = r#"
        var email = event.value;
        var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        
        if (email != "" && !emailRegex.test(email)) {
            app.alert("Please enter a valid email address");
            event.rc = false;
        } else {
            event.rc = true;
        }
    "#;
    aa_dict.set("V", Object::String(validate_js.to_string()));

    // Focus action - show help message
    let focus_js = r#"
        app.alert("Please enter your email address in the format: user@domain.com");
    "#;
    aa_dict.set("Fo", Object::String(focus_js.to_string()));

    // Blur action - clear help message
    let blur_js = r#"
        // Clear any validation messages when leaving field
        this.print({
            bUI: false,
            bSilent: true,
            bShrinkToFit: false
        });
    "#;
    aa_dict.set("Bl", Object::String(blur_js.to_string()));

    field_dict.set("AA", Object::Dictionary(aa_dict));

    let form_field = FormField::new(field_dict);

    // Verify validation actions
    if let Some(Object::Dictionary(actions)) = form_field.field_dict.get("AA") {
        if let Some(Object::String(validate_action)) = actions.get("V") {
            assert!(validate_action.contains("emailRegex"));
            assert!(validate_action.contains("event.rc"));
            println!("Email validation action preserved correctly");
        }

        if let Some(Object::String(focus_action)) = actions.get("Fo") {
            assert!(focus_action.contains("app.alert"));
            println!("Focus action preserved correctly");
        }

        if let Some(Object::String(blur_action)) = actions.get("Bl") {
            assert!(blur_action.contains("print"));
            println!("Blur action preserved correctly");
        }
    }

    println!("Field validation actions test completed");
}

/// Test 4: Calculate order and field dependencies
#[test]
fn test_calculate_order_and_dependencies() {
    let mut form_manager = FormManager::new();

    // Create base price field
    let price_field = TextField::new("base_price")
        .with_value("100.00")
        .with_max_length(10);

    let price_widget = Widget::new(Rectangle::new(
        Point::new(50.0, 650.0),
        Point::new(150.0, 670.0),
    ));

    form_manager
        .add_text_field(price_field, price_widget, None)
        .unwrap();

    // Create quantity field
    let quantity_field = TextField::new("quantity")
        .with_value("1")
        .with_max_length(5);

    let quantity_widget = Widget::new(Rectangle::new(
        Point::new(50.0, 620.0),
        Point::new(150.0, 640.0),
    ));

    form_manager
        .add_text_field(quantity_field, quantity_widget, None)
        .unwrap();

    // Create calculated total field
    let mut total_dict = Dictionary::new();
    total_dict.set("T", Object::String("total".to_string()));
    total_dict.set("FT", Object::Name("Tx".to_string()));
    total_dict.set("V", Object::String("100.00".to_string()));
    total_dict.set("Ff", Object::Integer(1)); // ReadOnly

    // Calculate action for total field
    let mut aa_dict = Dictionary::new();
    let calculate_js = r#"
        var price = this.getField("base_price").value;
        var qty = this.getField("quantity").value;
        
        if (price != "" && qty != "") {
            var priceNum = parseFloat(price);
            var qtyNum = parseFloat(qty);
            
            if (!isNaN(priceNum) && !isNaN(qtyNum)) {
                event.value = (priceNum * qtyNum).toFixed(2);
            }
        }
    "#;
    aa_dict.set("C", Object::String(calculate_js.to_string()));
    total_dict.set("AA", Object::Dictionary(aa_dict));

    let total_field = FormField::new(total_dict);

    // Verify calculate action
    if let Some(Object::Dictionary(actions)) = total_field.field_dict.get("AA") {
        if let Some(Object::String(calc_action)) = actions.get("C") {
            assert!(calc_action.contains("getField"));
            assert!(calc_action.contains("base_price"));
            assert!(calc_action.contains("quantity"));
            assert!(calc_action.contains("toFixed(2)"));
            println!("Calculate action with field dependencies preserved correctly");
        }
    }

    // Verify field can be marked as read-only (calculated field)
    if let Some(Object::Integer(flags)) = total_field.field_dict.get("Ff") {
        assert_ne!(*flags & 1, 0); // ReadOnly flag
        println!("Calculated field marked as read-only correctly");
    }

    println!("Calculate order and dependencies test completed");
}

/// Test 5: Complex validation with multiple conditions
#[test]
fn test_complex_validation_conditions() {
    let mut field_dict = Dictionary::new();
    field_dict.set("T", Object::String("credit_card".to_string()));
    field_dict.set("FT", Object::Name("Tx".to_string()));
    field_dict.set("MaxLen", Object::Integer(19)); // 16 digits + 3 spaces

    let mut aa_dict = Dictionary::new();

    // Complex credit card validation
    let validation_js = r#"
        var cc = event.value.replace(/\s/g, ""); // Remove spaces
        var isValid = false;
        var cardType = "";
        
        // Luhn algorithm check
        function luhnCheck(cardNum) {
            var sum = 0;
            var alternate = false;
            
            for (var i = cardNum.length - 1; i >= 0; i--) {
                var n = parseInt(cardNum.charAt(i), 10);
                
                if (alternate) {
                    n *= 2;
                    if (n > 9) {
                        n = (n % 10) + 1;
                    }
                }
                
                sum += n;
                alternate = !alternate;
            }
            
            return (sum % 10) == 0;
        }
        
        // Check card type and length
        if (/^4\d{15}$/.test(cc)) {
            cardType = "Visa";
            isValid = luhnCheck(cc);
        } else if (/^5[1-5]\d{14}$/.test(cc)) {
            cardType = "MasterCard";
            isValid = luhnCheck(cc);
        } else if (/^3[47]\d{13}$/.test(cc)) {
            cardType = "American Express";
            isValid = luhnCheck(cc);
        }
        
        if (!isValid && cc != "") {
            app.alert("Invalid credit card number");
            event.rc = false;
        } else {
            event.rc = true;
        }
    "#;
    aa_dict.set("V", Object::String(validation_js.to_string()));

    // Format action to add spaces
    let format_js = r#"
        var cc = event.value.replace(/\s/g, "");
        if (cc.length > 0) {
            var formatted = cc.replace(/(.{4})/g, "$1 ").trim();
            event.value = formatted;
        }
    "#;
    aa_dict.set("F", Object::String(format_js.to_string()));

    field_dict.set("AA", Object::Dictionary(aa_dict));

    let form_field = FormField::new(field_dict);

    // Verify complex validation is preserved
    if let Some(Object::Dictionary(actions)) = form_field.field_dict.get("AA") {
        if let Some(Object::String(validate_action)) = actions.get("V") {
            assert!(validate_action.contains("luhnCheck"));
            assert!(validate_action.contains("Visa"));
            assert!(validate_action.contains("MasterCard"));
            assert!(validate_action.contains("American Express"));
            println!("Complex credit card validation preserved correctly");
        }

        if let Some(Object::String(format_action)) = actions.get("F") {
            assert!(format_action.contains("replace"));
            assert!(format_action.contains("formatted"));
            println!("Credit card formatting action preserved correctly");
        }
    }

    println!("Complex validation conditions test completed");
}

/// Test 6: Choice field validation (ComboBox/ListBox)
#[test]
fn test_choice_field_validation() {
    let mut combo_dict = Dictionary::new();
    combo_dict.set("T", Object::String("country_combo".to_string()));
    combo_dict.set("FT", Object::Name("Ch".to_string()));
    combo_dict.set("Ff", Object::Integer((1 << 17) | (1 << 18))); // Combo + Edit

    // Options with validation
    let options = vec![
        Object::Array(vec![
            Object::String("US".to_string()),
            Object::String("United States".to_string()),
        ]),
        Object::Array(vec![
            Object::String("CA".to_string()),
            Object::String("Canada".to_string()),
        ]),
        Object::Array(vec![
            Object::String("UK".to_string()),
            Object::String("United Kingdom".to_string()),
        ]),
    ];
    combo_dict.set("Opt", Object::Array(options));

    let mut aa_dict = Dictionary::new();

    // Validation for custom entries (when editable)
    let validate_js = r#"
        var validCodes = ["US", "CA", "UK", "DE", "FR", "JP", "AU"];
        var enteredCode = event.value.toUpperCase();
        
        if (event.value != "" && validCodes.indexOf(enteredCode) == -1) {
            app.alert("Please select a valid country code or choose from the list");
            event.rc = false;
        } else {
            event.rc = true;
        }
    "#;
    aa_dict.set("V", Object::String(validate_js.to_string()));

    combo_dict.set("AA", Object::Dictionary(aa_dict));

    let combo_field = FormField::new(combo_dict);

    // Verify combo box validation
    if let Some(Object::Array(opts)) = combo_field.field_dict.get("Opt") {
        assert_eq!(opts.len(), 3);
        println!("ComboBox options preserved correctly");
    }

    if let Some(Object::Dictionary(actions)) = combo_field.field_dict.get("AA") {
        if let Some(Object::String(validate_action)) = actions.get("V") {
            assert!(validate_action.contains("validCodes"));
            assert!(validate_action.contains("indexOf"));
            println!("ComboBox validation action preserved correctly");
        }
    }

    println!("Choice field validation test completed");
}

/// Test 7: Cross-field validation dependencies
#[test]
fn test_cross_field_validation_dependencies() {
    let mut form_manager = FormManager::new();

    // Create password field
    let password_field = TextField::new("password")
        .with_value("")
        .password() // This should set password flag
        .with_max_length(50);

    let password_widget = Widget::new(Rectangle::new(
        Point::new(50.0, 700.0),
        Point::new(250.0, 720.0),
    ));

    form_manager
        .add_text_field(password_field, password_widget, None)
        .unwrap();

    // Create confirm password field with cross-validation
    let mut confirm_dict = Dictionary::new();
    confirm_dict.set("T", Object::String("confirm_password".to_string()));
    confirm_dict.set("FT", Object::Name("Tx".to_string()));
    confirm_dict.set("Ff", Object::Integer(1 << 13)); // Password flag
    confirm_dict.set("MaxLen", Object::Integer(50));

    let mut aa_dict = Dictionary::new();

    // Cross-field validation
    let validate_js = r#"
        var password = this.getField("password").value;
        var confirm = event.value;
        
        if (confirm != "" && password != confirm) {
            app.alert("Passwords do not match");
            event.rc = false;
        } else if (confirm != "" && password == confirm) {
            // Passwords match - could add strength validation here
            if (password.length < 8) {
                app.alert("Password must be at least 8 characters long");
                event.rc = false;
            } else {
                event.rc = true;
            }
        }
    "#;
    aa_dict.set("V", Object::String(validate_js.to_string()));

    confirm_dict.set("AA", Object::Dictionary(aa_dict));

    let confirm_field = FormField::new(confirm_dict);

    // Verify cross-field validation
    if let Some(Object::Dictionary(actions)) = confirm_field.field_dict.get("AA") {
        if let Some(Object::String(validate_action)) = actions.get("V") {
            assert!(validate_action.contains("getField(\"password\")"));
            assert!(validate_action.contains("do not match"));
            assert!(validate_action.contains("at least 8 characters"));
            println!("Cross-field password validation preserved correctly");
        }
    }

    // Verify password flags
    if let Some(Object::Integer(flags)) = confirm_field.field_dict.get("Ff") {
        assert_ne!(*flags & (1 << 13), 0); // Password flag
        println!("Password field flag set correctly");
    }

    println!("Cross-field validation dependencies test completed");
}

/// Test 8: Date and time validation
#[test]
fn test_date_time_validation() {
    let mut field_dict = Dictionary::new();
    field_dict.set("T", Object::String("birth_date".to_string()));
    field_dict.set("FT", Object::Name("Tx".to_string()));
    field_dict.set("MaxLen", Object::Integer(10)); // MM/DD/YYYY

    let mut aa_dict = Dictionary::new();

    // Date validation and formatting
    let validate_js = r#"
        var dateStr = event.value;
        var dateRegex = /^(0[1-9]|1[0-2])\/(0[1-9]|[12][0-9]|3[01])\/\d{4}$/;
        
        if (dateStr != "" && !dateRegex.test(dateStr)) {
            app.alert("Please enter date in MM/DD/YYYY format");
            event.rc = false;
        } else if (dateStr != "") {
            // Validate actual date
            var parts = dateStr.split('/');
            var month = parseInt(parts[0], 10);
            var day = parseInt(parts[1], 10);
            var year = parseInt(parts[2], 10);
            
            var testDate = new Date(year, month - 1, day);
            
            if (testDate.getMonth() != month - 1 || 
                testDate.getDate() != day || 
                testDate.getFullYear() != year) {
                app.alert("Invalid date");
                event.rc = false;
            } else if (testDate > new Date()) {
                app.alert("Birth date cannot be in the future");
                event.rc = false;
            } else {
                event.rc = true;
            }
        }
    "#;
    aa_dict.set("V", Object::String(validate_js.to_string()));

    // Keystroke formatting
    let keystroke_js = r#"
        if (!event.willCommit) {
            var value = event.value;
            // Auto-add slashes
            if (value.length == 2 || value.length == 5) {
                if (event.change != "/" && value.charAt(value.length-1) != "/") {
                    event.value = value + "/";
                }
            }
        }
    "#;
    aa_dict.set("K", Object::String(keystroke_js.to_string()));

    field_dict.set("AA", Object::Dictionary(aa_dict));

    let form_field = FormField::new(field_dict);

    // Verify date validation
    if let Some(Object::Dictionary(actions)) = form_field.field_dict.get("AA") {
        if let Some(Object::String(validate_action)) = actions.get("V") {
            assert!(validate_action.contains("dateRegex"));
            assert!(validate_action.contains("MM/DD/YYYY"));
            assert!(validate_action.contains("new Date()"));
            println!("Date validation preserved correctly");
        }

        if let Some(Object::String(keystroke_action)) = actions.get("K") {
            assert!(keystroke_action.contains("willCommit"));
            assert!(keystroke_action.contains("Auto-add slashes"));
            println!("Date formatting keystroke action preserved correctly");
        }
    }

    println!("Date and time validation test completed");
}

/// Test 9: Numeric range validation
#[test]
fn test_numeric_range_validation() {
    let mut field_dict = Dictionary::new();
    field_dict.set("T", Object::String("age_field".to_string()));
    field_dict.set("FT", Object::Name("Tx".to_string()));
    field_dict.set("MaxLen", Object::Integer(3));

    let mut aa_dict = Dictionary::new();

    // Numeric range validation
    let validate_js = r#"
        var age = event.value;
        
        if (age != "") {
            var ageNum = parseInt(age, 10);
            
            if (isNaN(ageNum)) {
                app.alert("Please enter a valid number");
                event.rc = false;
            } else if (ageNum < 0) {
                app.alert("Age cannot be negative");
                event.rc = false;
            } else if (ageNum > 150) {
                app.alert("Please enter a realistic age");
                event.rc = false;
            } else if (ageNum < 18) {
                app.alert("Must be 18 years or older");
                event.rc = false;
            } else {
                event.rc = true;
            }
        }
    "#;
    aa_dict.set("V", Object::String(validate_js.to_string()));

    // Numeric input filter
    let keystroke_js = r#"
        if (!event.willCommit) {
            var key = event.change;
            if (key && !/^\d$/.test(key)) {
                event.rc = false; // Block non-numeric input
            }
        }
    "#;
    aa_dict.set("K", Object::String(keystroke_js.to_string()));

    field_dict.set("AA", Object::Dictionary(aa_dict));

    let form_field = FormField::new(field_dict);

    // Verify numeric validation
    if let Some(Object::Dictionary(actions)) = form_field.field_dict.get("AA") {
        if let Some(Object::String(validate_action)) = actions.get("V") {
            assert!(validate_action.contains("parseInt"));
            assert!(validate_action.contains("isNaN"));
            assert!(validate_action.contains("18 years or older"));
            println!("Numeric range validation preserved correctly");
        }

        if let Some(Object::String(keystroke_action)) = actions.get("K") {
            assert!(keystroke_action.contains("/^\\d$/"));
            assert!(keystroke_action.contains("Block non-numeric"));
            println!("Numeric input filter preserved correctly");
        }
    }

    println!("Numeric range validation test completed");
}

/// Test 10: Form submission validation
#[test]
fn test_form_submission_validation() {
    let mut form_manager = FormManager::new();

    // Create multiple fields for submission validation
    let fields_data = vec![
        ("first_name", "Required field", true),
        ("last_name", "Required field", true),
        ("email", "Optional field", false),
        ("phone", "Optional field", false),
    ];

    for (name, value, is_required) in fields_data {
        let flags = FieldFlags {
            read_only: false,
            required: is_required,
            no_export: false,
        };

        let options = FieldOptions {
            flags,
            default_appearance: None,
            quadding: None,
        };

        let field = TextField::new(name).with_value(value).with_max_length(100);

        let widget = Widget::new(Rectangle::new(
            Point::new(50.0, 700.0),
            Point::new(300.0, 720.0),
        ));

        form_manager
            .add_text_field(field, widget, Some(options))
            .unwrap();
    }

    // Create submit button with validation
    let mut button_dict = Dictionary::new();
    button_dict.set("T", Object::String("submit_button".to_string()));
    button_dict.set("FT", Object::Name("Btn".to_string()));
    button_dict.set("Ff", Object::Integer(1 << 16)); // PushButton flag

    let mut aa_dict = Dictionary::new();

    // Mouse up action for submit button
    let submit_js = r#"
        // Validate all required fields before submission
        var requiredFields = ["first_name", "last_name"];
        var missingFields = [];
        
        for (var i = 0; i < requiredFields.length; i++) {
            var field = this.getField(requiredFields[i]);
            if (field && (field.value == "" || field.value == null)) {
                missingFields.push(requiredFields[i]);
            }
        }
        
        if (missingFields.length > 0) {
            app.alert("Please fill in the following required fields: " + missingFields.join(", "));
            return;
        }
        
        // Additional validation for email if provided
        var emailField = this.getField("email");
        if (emailField && emailField.value != "") {
            var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            if (!emailRegex.test(emailField.value)) {
                app.alert("Please enter a valid email address");
                return;
            }
        }
        
        // If we get here, form is valid
        app.alert("Form validation successful - ready to submit");
        
        // In a real scenario, this would submit the form
        // this.submitForm("http://example.com/submit");
    "#;
    aa_dict.set("U", Object::String(submit_js.to_string()));

    button_dict.set("AA", Object::Dictionary(aa_dict));

    let submit_button = FormField::new(button_dict);

    // Verify submit validation
    if let Some(Object::Dictionary(actions)) = submit_button.field_dict.get("AA") {
        if let Some(Object::String(submit_action)) = actions.get("U") {
            assert!(submit_action.contains("requiredFields"));
            assert!(submit_action.contains("missingFields"));
            assert!(submit_action.contains("emailRegex"));
            assert!(submit_action.contains("validation successful"));
            println!("Form submission validation preserved correctly");
        }
    }

    // Verify we have the required fields in form manager
    assert_eq!(form_manager.field_count(), 4);
    assert!(form_manager.get_field("first_name").is_some());
    assert!(form_manager.get_field("last_name").is_some());

    println!("Form submission validation test completed");
}

/// Test 11: Custom validation functions
#[test]
fn test_custom_validation_functions() {
    let mut field_dict = Dictionary::new();
    field_dict.set("T", Object::String("ssn_field".to_string()));
    field_dict.set("FT", Object::Name("Tx".to_string()));
    field_dict.set("MaxLen", Object::Integer(11)); // XXX-XX-XXXX

    let mut aa_dict = Dictionary::new();

    // Custom SSN validation with helper functions
    let validate_js = r#"
        // Custom validation helper function
        function validateSSN(ssn) {
            // Remove dashes
            var cleanSSN = ssn.replace(/-/g, "");
            
            // Check format
            if (!/^\d{9}$/.test(cleanSSN)) {
                return "SSN must be 9 digits in format XXX-XX-XXXX";
            }
            
            // Check for invalid patterns
            var invalidPatterns = [
                "000000000", "111111111", "222222222", "333333333",
                "444444444", "555555555", "666666666", "777777777",
                "888888888", "999999999"
            ];
            
            if (invalidPatterns.indexOf(cleanSSN) !== -1) {
                return "Invalid SSN pattern";
            }
            
            // Check area number (first 3 digits) - basic validation
            var areaNum = parseInt(cleanSSN.substring(0, 3));
            if (areaNum === 0 || areaNum === 666 || areaNum >= 900) {
                return "Invalid SSN area number";
            }
            
            // Check group number (middle 2 digits)
            var groupNum = parseInt(cleanSSN.substring(3, 5));
            if (groupNum === 0) {
                return "Invalid SSN group number";
            }
            
            // Check serial number (last 4 digits)
            var serialNum = parseInt(cleanSSN.substring(5, 9));
            if (serialNum === 0) {
                return "Invalid SSN serial number";
            }
            
            return null; // Valid
        }
        
        if (event.value != "") {
            var validationError = validateSSN(event.value);
            if (validationError) {
                app.alert(validationError);
                event.rc = false;
            } else {
                event.rc = true;
            }
        }
    "#;
    aa_dict.set("V", Object::String(validate_js.to_string()));

    // Format action to add dashes
    let format_js = r#"
        var ssn = event.value.replace(/\D/g, ""); // Remove non-digits
        if (ssn.length >= 3) {
            if (ssn.length >= 5) {
                event.value = ssn.substring(0,3) + "-" + ssn.substring(3,5) + "-" + ssn.substring(5,9);
            } else {
                event.value = ssn.substring(0,3) + "-" + ssn.substring(3);
            }
        }
    "#;
    aa_dict.set("F", Object::String(format_js.to_string()));

    field_dict.set("AA", Object::Dictionary(aa_dict));

    let form_field = FormField::new(field_dict);

    // Verify custom validation function
    if let Some(Object::Dictionary(actions)) = form_field.field_dict.get("AA") {
        if let Some(Object::String(validate_action)) = actions.get("V") {
            assert!(validate_action.contains("function validateSSN"));
            assert!(validate_action.contains("invalidPatterns"));
            assert!(validate_action.contains("area number"));
            assert!(validate_action.contains("group number"));
            assert!(validate_action.contains("serial number"));
            println!("Custom SSN validation function preserved correctly");
        }

        if let Some(Object::String(format_action)) = actions.get("F") {
            assert!(format_action.contains("replace(/\\D/g"));
            assert!(format_action.contains("substring"));
            println!("SSN formatting action preserved correctly");
        }
    }

    println!("Custom validation functions test completed");
}

/// Test 12: Conditional field validation based on other fields
#[test]
fn test_conditional_field_validation() {
    let mut form_manager = FormManager::new();

    // Create employment status field
    let employment_field = ComboBox::new("employment_status")
        .add_option("employed", "Employed")
        .add_option("unemployed", "Unemployed")
        .add_option("student", "Student")
        .add_option("retired", "Retired")
        .with_value("employed");

    let employment_widget = Widget::new(Rectangle::new(
        Point::new(50.0, 650.0),
        Point::new(200.0, 670.0),
    ));

    form_manager
        .add_combo_box(employment_field, employment_widget, None)
        .unwrap();

    // Create employer field with conditional validation
    let mut employer_dict = Dictionary::new();
    employer_dict.set("T", Object::String("employer_name".to_string()));
    employer_dict.set("FT", Object::Name("Tx".to_string()));
    employer_dict.set("MaxLen", Object::Integer(100));

    let mut aa_dict = Dictionary::new();

    // Conditional validation based on employment status
    let validate_js = r#"
        var employmentStatus = this.getField("employment_status").value;
        var employerName = event.value;
        
        // If employed, employer name is required
        if (employmentStatus == "employed") {
            if (employerName == "" || employerName == null) {
                app.alert("Employer name is required when employment status is 'Employed'");
                event.rc = false;
            } else if (employerName.length < 2) {
                app.alert("Please enter a valid employer name");
                event.rc = false;
            } else {
                event.rc = true;
            }
        } else {
            // Not employed - field is optional but validate if provided
            if (employerName != "" && employerName.length < 2) {
                app.alert("Please enter a valid employer name or leave blank");
                event.rc = false;
            } else {
                event.rc = true;
            }
        }
    "#;
    aa_dict.set("V", Object::String(validate_js.to_string()));

    employer_dict.set("AA", Object::Dictionary(aa_dict));

    let employer_field = FormField::new(employer_dict);

    // Verify conditional validation
    if let Some(Object::Dictionary(actions)) = employer_field.field_dict.get("AA") {
        if let Some(Object::String(validate_action)) = actions.get("V") {
            assert!(validate_action.contains("getField(\"employment_status\")"));
            assert!(validate_action.contains("employed"));
            assert!(validate_action.contains("required when employment status"));
            println!("Conditional field validation preserved correctly");
        }
    }

    println!("Conditional field validation test completed");
}

/// Test 13: Multi-step validation workflow
#[test]
fn test_multi_step_validation_workflow() {
    let mut step_dict = Dictionary::new();
    step_dict.set("T", Object::String("wizard_step".to_string()));
    step_dict.set("FT", Object::Name("Btn".to_string()));
    step_dict.set("Ff", Object::Integer(1 << 16)); // PushButton

    let mut aa_dict = Dictionary::new();

    // Multi-step validation workflow
    let validate_js = r#"
        // Get current step from hidden field
        var currentStep = parseInt(this.getField("current_step").value || "1");
        
        function validateStep1() {
            var required = ["first_name", "last_name", "email"];
            for (var i = 0; i < required.length; i++) {
                var field = this.getField(required[i]);
                if (!field || field.value == "") {
                    return "Step 1: Please fill in " + required[i];
                }
            }
            return null;
        }
        
        function validateStep2() {
            var address = this.getField("address").value;
            var city = this.getField("city").value;
            var zipcode = this.getField("zipcode").value;
            
            if (!address || !city || !zipcode) {
                return "Step 2: Please complete address information";
            }
            
            // Validate zipcode format
            if (!/^\d{5}(-\d{4})?$/.test(zipcode)) {
                return "Step 2: Invalid ZIP code format";
            }
            
            return null;
        }
        
        function validateStep3() {
            var agreement = this.getField("terms_agreement");
            if (!agreement || agreement.value != "Yes") {
                return "Step 3: Please accept the terms and conditions";
            }
            return null;
        }
        
        var error = null;
        switch(currentStep) {
            case 1: error = validateStep1(); break;
            case 2: error = validateStep2(); break;
            case 3: error = validateStep3(); break;
        }
        
        if (error) {
            app.alert(error);
            event.rc = false;
        } else {
            // Move to next step or complete
            if (currentStep < 3) {
                this.getField("current_step").value = (currentStep + 1).toString();
                app.alert("Step " + currentStep + " completed. Proceeding to step " + (currentStep + 1));
            } else {
                app.alert("All validation steps completed successfully!");
            }
            event.rc = true;
        }
    "#;
    aa_dict.set("U", Object::String(validate_js.to_string()));

    step_dict.set("AA", Object::Dictionary(aa_dict));

    let step_field = FormField::new(step_dict);

    // Verify multi-step validation
    if let Some(Object::Dictionary(actions)) = step_field.field_dict.get("AA") {
        if let Some(Object::String(validate_action)) = actions.get("U") {
            assert!(validate_action.contains("validateStep1"));
            assert!(validate_action.contains("validateStep2"));
            assert!(validate_action.contains("validateStep3"));
            assert!(validate_action.contains("current_step"));
            assert!(validate_action.contains("switch(currentStep)"));
            println!("Multi-step validation workflow preserved correctly");
        }
    }

    println!("Multi-step validation workflow test completed");
}

/// Test 14: Real-time validation feedback
#[test]
fn test_realtime_validation_feedback() {
    let mut field_dict = Dictionary::new();
    field_dict.set("T", Object::String("username_field".to_string()));
    field_dict.set("FT", Object::Name("Tx".to_string()));
    field_dict.set("MaxLen", Object::Integer(30));

    let mut aa_dict = Dictionary::new();

    // Real-time keystroke validation
    let keystroke_js = r#"
        if (!event.willCommit) {
            var currentValue = event.value + event.change;
            var feedbackField = this.getField("username_feedback");
            
            // Real-time validation rules
            var issues = [];
            
            if (currentValue.length < 3) {
                issues.push("Must be at least 3 characters");
            }
            
            if (currentValue.length > 20) {
                issues.push("Must be 20 characters or less");
            }
            
            if (!/^[a-zA-Z0-9_]+$/.test(currentValue)) {
                issues.push("Only letters, numbers, and underscores allowed");
            }
            
            if (/^[0-9]/.test(currentValue)) {
                issues.push("Cannot start with a number");
            }
            
            // Reserved usernames
            var reserved = ["admin", "root", "user", "test", "guest"];
            if (reserved.indexOf(currentValue.toLowerCase()) !== -1) {
                issues.push("Username is reserved");
            }
            
            // Update feedback field with issues or success message
            if (feedbackField) {
                if (issues.length > 0) {
                    feedbackField.value = "Issues: " + issues.join(", ");
                    feedbackField.textColor = color.red;
                } else if (currentValue.length >= 3) {
                    feedbackField.value = "Username looks good!";
                    feedbackField.textColor = color.green;
                } else {
                    feedbackField.value = "";
                }
            }
        }
    "#;
    aa_dict.set("K", Object::String(keystroke_js.to_string()));

    // Final validation on blur
    let blur_js = r#"
        var username = event.value;
        var feedbackField = this.getField("username_feedback");
        
        if (username != "") {
            // Simulate availability check (in real scenario, this might be an AJAX call)
            var unavailableUsernames = ["john123", "admin123", "testuser"];
            
            if (unavailableUsernames.indexOf(username.toLowerCase()) !== -1) {
                if (feedbackField) {
                    feedbackField.value = "Username not available";
                    feedbackField.textColor = color.red;
                }
            }
        }
    "#;
    aa_dict.set("Bl", Object::String(blur_js.to_string()));

    field_dict.set("AA", Object::Dictionary(aa_dict));

    let form_field = FormField::new(field_dict);

    // Verify real-time validation
    if let Some(Object::Dictionary(actions)) = form_field.field_dict.get("AA") {
        if let Some(Object::String(keystroke_action)) = actions.get("K") {
            assert!(keystroke_action.contains("willCommit"));
            assert!(keystroke_action.contains("Real-time validation"));
            assert!(keystroke_action.contains("username_feedback"));
            assert!(keystroke_action.contains("textColor"));
            assert!(keystroke_action.contains("reserved"));
            println!("Real-time keystroke validation preserved correctly");
        }

        if let Some(Object::String(blur_action)) = actions.get("Bl") {
            assert!(blur_action.contains("availability check"));
            assert!(blur_action.contains("unavailableUsernames"));
            println!("Username availability check preserved correctly");
        }
    }

    println!("Real-time validation feedback test completed");
}

/// Test 15: Performance test for JavaScript action processing
#[test]
fn test_javascript_action_performance() {
    let start_time = std::time::Instant::now();
    let field_count = 100;

    let mut processed_fields = Vec::new();

    // Create many fields with JavaScript actions
    for i in 0..field_count {
        let mut field_dict = Dictionary::new();
        field_dict.set("T", Object::String(format!("js_field_{i}")));
        field_dict.set("FT", Object::Name("Tx".to_string()));

        let mut aa_dict = Dictionary::new();

        // Complex JavaScript action
        let validate_js = format!(
            r#"
            var value = event.value;
            var fieldNumber = {i};
            
            // Complex validation logic
            if (value != "") {{
                var isValid = true;
                var errors = [];
                
                // Length validation
                if (value.length < 5) {{
                    errors.push("Too short");
                    isValid = false;
                }}
                
                // Pattern validation
                if (!/^[A-Za-z0-9]+$/.test(value)) {{
                    errors.push("Invalid characters");
                    isValid = false;
                }}
                
                // Custom validation based on field number
                if (fieldNumber % 2 == 0 && !value.includes("even")) {{
                    errors.push("Even numbered fields must contain 'even'");
                    isValid = false;
                }}
                
                if (!isValid) {{
                    app.alert("Field {i}: " + errors.join(", "));
                    event.rc = false;
                }} else {{
                    event.rc = true;
                }}
            }}
        "#
        );

        aa_dict.set("V", Object::String(validate_js));
        field_dict.set("AA", Object::Dictionary(aa_dict));

        let form_field = FormField::new(field_dict);
        processed_fields.push(form_field);
    }

    let processing_time = start_time.elapsed();

    // Verify all fields were processed
    assert_eq!(processed_fields.len(), field_count);

    // Check processing performance
    assert!(
        processing_time.as_secs() < 2,
        "Processing {field_count} JavaScript actions took too long: {processing_time:?}"
    );

    // Verify a few random fields have their JavaScript preserved
    if let Some(Object::Dictionary(actions)) = processed_fields[0].field_dict.get("AA") {
        if let Some(Object::String(validate_action)) = actions.get("V") {
            assert!(validate_action.contains("fieldNumber = 0"));
        }
    }

    if let Some(Object::Dictionary(actions)) = processed_fields[50].field_dict.get("AA") {
        if let Some(Object::String(validate_action)) = actions.get("V") {
            assert!(validate_action.contains("fieldNumber = 50"));
        }
    }

    println!("Processed {field_count} fields with JavaScript actions in {processing_time:?}");
    println!("JavaScript action performance test completed");
}