aiscript-runtime 0.2.0

The web runtime of AIScript programming language interpreter
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
use aiscript_directive::DirectiveParser;
use aiscript_directive::route::RouteAnnotation;
use serde_json::Value;
use std::ops::{Deref, DerefMut};

use crate::ast::*;
use crate::lexer::{Scanner, TokenType};

pub struct Parser<'a> {
    scanner: Scanner<'a>,
}

impl<'a> Deref for Parser<'a> {
    type Target = Scanner<'a>;
    fn deref(&self) -> &Self::Target {
        &self.scanner
    }
}

impl DerefMut for Parser<'_> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.scanner
    }
}

impl<'a> Parser<'a> {
    pub fn new(source: &'a str) -> Self {
        let mut scanner = Scanner::new(source);
        scanner.advance();
        Parser { scanner }
    }

    fn parse_docs(&mut self) -> String {
        let mut docs = String::new();
        if self.match_token(TokenType::Doc) {
            let lines = self
                .previous
                .lexeme
                .lines()
                .map(|line| line.trim())
                .collect::<Vec<_>>();
            docs = lines.join("\n");
        }
        docs
    }

    pub fn parse_route(&mut self) -> Result<Route, String> {
        let annotation = self.parse_route_annotation();
        let mut docs = String::new();
        let mut path = (String::from("/"), Vec::new());

        // Check if this is a top-level route declaration
        let is_top_route = self.check_identifier("route");
        if is_top_route {
            self.advance(); // consume 'route'
            path = self.parse_path()?;
            self.consume(TokenType::OpenBrace, "Expect '{' after route path")?;

            docs = self.parse_docs();
        }

        let mut endpoints = Vec::new();
        while !self.is_at_end() && !self.check(TokenType::CloseBrace) {
            endpoints.push(self.parse_endpoint()?);
        }

        if is_top_route {
            self.consume(TokenType::CloseBrace, "Expect '}' after route body")?;
        }

        Ok(Route {
            annotation,
            prefix: path.0,
            params: path.1,
            endpoints,
            docs,
        })
    }

    fn parse_endpoint(&mut self) -> Result<Endpoint, String> {
        let annotation = self.parse_route_annotation();
        let path_specs = self.parse_path_specs()?;

        self.consume(TokenType::OpenBrace, "Expect '{' before endpoint")?;

        // Parse docs
        let docs = self.parse_docs();

        // Parse structured parts (query and body)
        let mut path = Vec::new();
        let mut query = Vec::new();
        let mut body = RequestBody::default();

        // Only parse structured blocks (query/body) and directives
        while !self.is_at_end() {
            if self.scanner.check_identifier("query") {
                self.advance();
                query = self.parse_fields()?;
            } else if self.scanner.check_identifier("body") {
                self.advance();
                body.fields = self.parse_fields()?;
            } else if self.scanner.check_identifier("path") {
                self.advance();
                path = self.parse_fields()?;
            } else if self.scanner.check(TokenType::At) {
                let directives = DirectiveParser::new(&mut self.scanner).parse_directives();
                for directive in directives {
                    match directive.name.as_str() {
                        "form" => body.kind = BodyKind::Form,
                        "json" => body.kind = BodyKind::Json,
                        name => {
                            return Err(format!(
                                "Invalid directive, only @form or @json are allowed on body block, current: @{name}"
                            ));
                        }
                    }

                    if !self.check_identifier("body") {
                        return Err("Only body block supports @form or @json directive".into());
                    }
                }
            } else {
                // Break for anything else to handle raw script
                break;
            }
        }

        if self.check(TokenType::CloseBrace) {
            return Err("Route without handler script is not allowed.".to_string());
        }
        // Parse the handler function body
        let script = self.read_raw_script()?;
        let statements = format!(
            "ai fn handler(path, query, body, request, header){{{}}}",
            script
        );
        self.consume(TokenType::CloseBrace, "Expect '}' after endpoint")?;

        let endpoint = Endpoint {
            annotation,
            path_specs,
            return_type: None,
            path,
            query,
            body,
            statements,
            docs,
        };
        // Validate path parameters
        self.validate_path_params(&endpoint)?;
        Ok(endpoint)
    }

    fn parse_route_annotation(&mut self) -> RouteAnnotation {
        let mut annotation = RouteAnnotation::default();
        let directives = DirectiveParser::new(&mut self.scanner).parse_directives();
        for directive in directives {
            let line = directive.line;
            if let Err(error) = annotation.parse_directive(directive) {
                self.error_with_line(line, &error);
            }
        }
        annotation
    }

    fn parse_fields(&mut self) -> Result<Vec<Field>, String> {
        self.consume(TokenType::OpenBrace, "Expected '{' after field block")?;

        let mut fields = Vec::new();
        while !self.check(TokenType::CloseBrace) {
            // Parse doc comments
            let docs = self.parse_docs();

            // Parse validators
            let validators = DirectiveParser::new(&mut self.scanner).parse_validators();

            // Parse field name
            if !self.check(TokenType::Identifier) {
                return Err("Expected field name".to_string());
            }
            let name = self.current.lexeme.to_string();
            self.advance();

            self.consume(TokenType::Colon, "Expected ':' after field name")?;

            // Parse field type
            if !self.check(TokenType::Identifier) {
                return Err("Expected field type".to_string());
            }
            let field_type = match self.current.lexeme {
                "str" => FieldType::Str,
                "int" | "float" => FieldType::Number,
                "bool" => FieldType::Bool,
                _ => return Err(format!("Invalid field type: {}", self.current.lexeme)),
            };
            self.advance();

            // Parse default value
            let mut default = None;
            if self.check(TokenType::Equal) {
                self.advance();
                default = Some(self.parse_value()?);
            }

            fields.push(Field {
                name,
                _type: field_type,
                required: default.is_none(),
                default,
                validators: validators.into_boxed_slice(),
                docs,
            });

            // If this is not the last field (not followed by a closing brace),
            // then a comma is required
            if !self.check(TokenType::CloseBrace) {
                self.consume(TokenType::Comma, "Expected ',' after field definition")?;
            } else {
                // We've reached the closing brace, we can optionally have a comma
                if self.check(TokenType::Comma) {
                    self.advance(); // consume the optional trailing comma
                }
                break;
            }
        }

        self.consume(TokenType::CloseBrace, "Expected '}' after fields")?;
        Ok(fields)
    }

    fn parse_value(&mut self) -> Result<Value, String> {
        let value = match self.current.kind {
            TokenType::Number => {
                if self.current.lexeme.contains(".") {
                    let num = self
                        .current
                        .lexeme
                        .parse::<f64>()
                        .map_err(|_| "Invalid number".to_string())?;
                    Value::Number(serde_json::Number::from_f64(num).ok_or("Invalid number")?)
                } else {
                    let num = self
                        .current
                        .lexeme
                        .parse::<i64>()
                        .map_err(|_| "Invalid number".to_string())?;
                    Value::Number(serde_json::Number::from(num))
                }
            }
            TokenType::String => {
                let lexeme = self.current.lexeme;
                let escaped_string = self
                    .escape_string(lexeme)
                    .ok_or_else(|| String::from("Invalid string"))?;
                Value::String(escaped_string)
            }
            TokenType::True => Value::Bool(true),
            TokenType::False => Value::Bool(false),
            _ => return Err("Expected value".to_string()),
        };
        self.advance();
        Ok(value)
    }

    fn parse_path_specs(&mut self) -> Result<Vec<PathSpec>, String> {
        let mut specs = Vec::new();

        loop {
            // Parse HTTP method
            if !self.check(TokenType::Identifier) {
                return Err("Expected HTTP method".to_string());
            }

            let method = match self.current.lexeme {
                "get" => HttpMethod::Get,
                "post" => HttpMethod::Post,
                "put" => HttpMethod::Put,
                "delete" => HttpMethod::Delete,
                _ => return Err(format!("Invalid HTTP method: {}", self.current.lexeme)),
            };
            self.advance();

            // Parse path
            let (path, params) = self.parse_path()?;

            specs.push(PathSpec {
                method,
                path,
                params,
            });

            // Check for more paths
            if self.check(TokenType::Comma) {
                self.advance();
                continue;
            }
            break;
        }

        Ok(specs)
    }

    fn validate_path_params(&self, endpoint: &Endpoint) -> Result<(), String> {
        // Check if any path spec contains parameters
        let has_path_params = endpoint
            .path_specs
            .iter()
            .any(|spec| !spec.params.is_empty());

        // If there are path parameters but no path block, that's an error
        if has_path_params && endpoint.path.is_empty() {
            return Err("Path parameters found in URL but no path block defined".to_string());
        }

        // Skip further validation if no path block is defined (and no params exist)
        if endpoint.path.is_empty() {
            return Ok(());
        }

        // For each path spec, validate that path params match
        for path_spec in &endpoint.path_specs {
            // First check for case-insensitive matches to provide better error messages
            let mut missing_params = Vec::new();
            let mut extra_params = Vec::new();
            let mut case_mismatches = Vec::new();

            // Track which path block params correspond to path spec params
            let mut matched_path_params = std::collections::HashSet::new();

            // Check each path spec parameter
            for param_name in &path_spec.params {
                // Try to find exact match first
                let exact_match = endpoint.path.iter().find(|f| &f.name == param_name);

                if exact_match.is_some() {
                    matched_path_params.insert(param_name.clone());
                    continue;
                }

                // Try case-insensitive match
                let case_insensitive_match = endpoint
                    .path
                    .iter()
                    .find(|f| f.name.to_lowercase() == param_name.to_lowercase());

                if let Some(field) = case_insensitive_match {
                    case_mismatches.push((param_name.clone(), field.name.clone()));
                    matched_path_params.insert(field.name.clone());
                } else {
                    missing_params.push(param_name.clone());
                }
            }

            // Check for extra parameters in path block
            for field in &endpoint.path {
                if !matched_path_params.contains(&field.name) {
                    // Check if it's a case mismatch before marking as extra
                    let is_case_mismatch = path_spec
                        .params
                        .iter()
                        .any(|p| p.to_lowercase() == field.name.to_lowercase());

                    if !is_case_mismatch {
                        extra_params.push(field.name.clone());
                    }
                }
            }

            // Report case mismatches first (most likely cause of issues)
            if !case_mismatches.is_empty() {
                let mismatch_desc = case_mismatches
                    .iter()
                    .map(|(url, block)| format!("'{}' in URL vs '{}' in path block", url, block))
                    .collect::<Vec<_>>()
                    .join(", ");

                return Err(format!("Path parameter case mismatch: {}", mismatch_desc));
            }

            // Report missing parameters
            if !missing_params.is_empty() {
                return Err(format!(
                    "Missing path parameter(s) in path block: {}",
                    missing_params
                        .iter()
                        .map(|s| format!("'{}'", s))
                        .collect::<Vec<_>>()
                        .join(", ")
                ));
            }

            // Report extra parameters
            if !extra_params.is_empty() {
                return Err(format!(
                    "Unknown path parameter(s) in path block: {}",
                    extra_params
                        .iter()
                        .map(|s| format!("'{}'", s))
                        .collect::<Vec<_>>()
                        .join(", ")
                ));
            }
        }

        Ok(())
    }

    fn parse_path(&mut self) -> Result<(String, Vec<String>), String> {
        let mut path = String::new();
        let mut params = Vec::new();

        // Handle leading slash
        if self.check(TokenType::Slash) {
            path.push('/');
            self.advance();
        }

        while !self.is_at_end() {
            match self.current.kind {
                TokenType::Slash => {
                    path.push('/');
                    self.advance();
                }
                TokenType::Colon => {
                    self.advance(); // Consume :

                    // Parse parameter name
                    if !self.check(TokenType::Identifier) {
                        return Err("Expected parameter name after ':'".to_string());
                    }
                    let name = self.current.lexeme.to_string();
                    self.advance();

                    // Add parameter to path in the format Axum expects: {id}
                    path.push('{');
                    path.push_str(&name);
                    path.push('}');

                    // Add parameter name to our list
                    params.push(name);
                }
                TokenType::Identifier => {
                    path.push_str(self.current.lexeme);
                    self.advance();
                }
                TokenType::Minus => {
                    path.push('-');
                    self.advance();
                }
                TokenType::OpenBrace | TokenType::Comma => break,
                _ => return Err(format!("Unexpected token in path: {:?}", self.current.kind)),
            }
        }

        Ok((path, params))
    }
    fn consume(&mut self, expected: TokenType, message: &str) -> Result<(), String> {
        if self.check(expected) {
            self.advance();
            Ok(())
        } else {
            Err(message.to_string())
        }
    }
}

pub fn parse_route(input: &str) -> Result<Route, String> {
    let mut parser = Parser::new(input);
    parser.parse_route()
}

#[cfg(test)]
mod tests {
    use aiscript_directive::{
        Validator,
        validator::{AnyValidator, InValidator, NotValidator, StringValidator},
    };

    use super::*;

    #[test]
    fn test_basic_route() {
        let input = r#"
            route /test/:id {
                """
                Test route line1
                Test route line2
                """

                get /a {
                    """Test endpoint"""
                    query {
                        """field name"""
                        name: str = "hello",
                        age: int = 18,
                    }
                    body {
                        """field a"""
                        @length(max=10)
                        a: str,
                        b: bool = false,
                    }

                    let greeting = "Hello" + name;
                    if greeting {
                        print(greeting);
                    }
                    return greeting;
                }

                post /b {
                    """Test endpoint2"""

                    return "endpoint2";
                }
            }
        "#;

        let mut parser = Parser::new(input);
        let result = parser.parse_route();

        let route = result.unwrap();
        assert_eq!(route.docs, "Test route line1\nTest route line2");
        assert_eq!(route.prefix, "/test/{id}");
        assert_eq!(route.params.len(), 1);
        assert_eq!(route.params[0], "id");

        let endpoint = &route.endpoints[0];
        assert_eq!(endpoint.docs, "Test endpoint");
        assert_eq!(endpoint.path_specs[0].method, HttpMethod::Get);
        assert_eq!(endpoint.path_specs[0].path, "/a");

        // Verify query parameters
        assert_eq!(endpoint.query.len(), 2);
        assert_eq!(endpoint.query[0].docs, "field name");
        assert_eq!(endpoint.query[0].name, "name");
        assert_eq!(endpoint.query[1].name, "age");

        // Verify body fields
        assert_eq!(endpoint.body.fields.len(), 2);
        assert_eq!(endpoint.body.fields[0].docs, "field a");
        assert_eq!(endpoint.body.fields[0].name, "a");
        assert_eq!(endpoint.body.fields[1].name, "b");

        // Verify script capture
        assert!(endpoint.statements.contains("let greeting"));
        assert!(endpoint.statements.contains("return greeting"));

        // Verify endpoint2
        let endpoint2 = &route.endpoints[1];
        assert_eq!(endpoint2.docs, "Test endpoint2");
        assert_eq!(endpoint2.path_specs[0].method, HttpMethod::Post);
        assert_eq!(endpoint2.path_specs[0].path, "/b");
        assert!(endpoint2.statements.contains("return \"endpoint2\""));
    }

    #[test]
    fn test_no_top_route() {
        let input = r#"
            get / {
                return "hello";
            }

            post / {
                return "hello";
            }
        "#;
        let mut parser = Parser::new(input);
        let result = parser.parse_route();
        // assert!(result.is_ok());
        let route = result.unwrap();
        assert_eq!(route.endpoints.len(), 2);
        assert_eq!(route.endpoints[0].path_specs[0].method, HttpMethod::Get);
        assert_eq!(route.endpoints[1].path_specs[0].method, HttpMethod::Post);
        assert_eq!(route.endpoints[0].path_specs[0].path, "/");
        assert_eq!(route.endpoints[1].path_specs[0].path, "/");
    }

    #[test]
    fn test_multiple_methods() {
        let input = r#"
            route /api {
                get /, post / {
                    return "hello";
                }
            }
        "#;

        let mut parser = Parser::new(input);
        let result = parser.parse_route();
        // assert!(result.is_ok());

        let route = result.unwrap();
        let endpoint = &route.endpoints[0];
        assert_eq!(endpoint.path_specs.len(), 2);
        assert_eq!(endpoint.path_specs[0].method, HttpMethod::Get);
        assert_eq!(endpoint.path_specs[1].method, HttpMethod::Post);
    }

    #[test]
    fn test_validators() {
        let input = r#"
            route / {
                post / {
                    @json
                    body {
                        @string(max_len=10)
                        @not(@string(min_len=5))
                        field: str,
                        @in(["a" ,"b", "c"])
                        x: str = "a",
                        @in([1, 2, 3])
                        y: int = 1,
                        @any(@in(["a", "b"]), @string(min_len=1))
                        z: str
                    }

                    return "hi";
                }
            }
        "#;

        let mut parser = Parser::new(input);
        let result = parser.parse_route();
        assert!(result.is_ok());

        let route = result.unwrap();
        let endpoint = &route.endpoints[0];

        let field = &endpoint.body.fields[0];
        assert_eq!(field.name, "field");
        assert_eq!(field.validators.len(), 2);
        assert_eq!(field.validators[0].name(), "@string");
        assert_eq!(
            field.validators[0]
                .downcast_ref::<StringValidator>()
                .unwrap()
                .max_len,
            Some(10)
        );
        assert_eq!(
            field.validators[1]
                .downcast_ref::<NotValidator<Box<dyn Validator>>>()
                .unwrap()
                .0
                .downcast_ref::<StringValidator>()
                .unwrap()
                .min_len,
            Some(5)
        );

        let field = &endpoint.body.fields[1];
        assert_eq!(field.name, "x");
        assert_eq!(field.default, Some(Value::from("a")));
        assert_eq!(field.validators.len(), 1);
        assert_eq!(
            field.validators[0].downcast_ref::<InValidator>().unwrap().0,
            vec![Value::from("a"), Value::from("b"), Value::from("c")]
        );

        let field = &endpoint.body.fields[2];
        assert_eq!(field.name, "y");
        assert_eq!(field.default, Some(Value::from(1)));
        assert_eq!(field.validators.len(), 1);
        assert_eq!(
            field.validators[0].downcast_ref::<InValidator>().unwrap().0,
            vec![Value::from(1), Value::from(2), Value::from(3),]
        );

        let field = &endpoint.body.fields[3];
        assert_eq!(field.name, "z");
        assert_eq!(field.validators.len(), 1);
        let validators = &field.validators[0]
            .downcast_ref::<AnyValidator<Box<dyn Validator>>>()
            .unwrap()
            .0;
        assert_eq!(
            validators[0].downcast_ref::<InValidator>().unwrap().0,
            vec![Value::from("a"), Value::from("b")]
        );
        assert_eq!(
            validators[1]
                .downcast_ref::<StringValidator>()
                .unwrap()
                .min_len,
            Some(1)
        );
    }

    #[test]
    fn test_multiple_methods_single_path() {
        let input = r#"
            route /api {
                get /, post / {
                    return "hello";
                }
            }
        "#;

        let mut parser = Parser::new(input);
        let result = parser.parse_route().unwrap();

        assert_eq!(result.prefix, "/api");
        assert_eq!(result.endpoints.len(), 1);

        let endpoint = &result.endpoints[0];
        assert_eq!(endpoint.path_specs.len(), 2);
        assert_eq!(endpoint.path_specs[0].method, HttpMethod::Get);
        assert_eq!(endpoint.path_specs[0].path, "/");
        assert_eq!(endpoint.path_specs[1].method, HttpMethod::Post);
        assert_eq!(endpoint.path_specs[1].path, "/");
    }

    #[test]
    #[ignore = "temporary ignore"]
    fn test_multiple_paths_with_params() {
        let input = r#"
            route /api {
                get /users/:id, post /users {
                    return "ok";
                }
            }
        "#;

        let mut parser = Parser::new(input);
        let result = parser.parse_route().unwrap();

        let endpoint = &result.endpoints[0];
        assert_eq!(endpoint.path_specs.len(), 2);

        assert_eq!(endpoint.path_specs[0].method, HttpMethod::Get);
        assert_eq!(endpoint.path_specs[0].path, "/users/{id}");
        assert_eq!(endpoint.path_specs[0].params.len(), 1);
        assert_eq!(endpoint.path_specs[0].params[0], "id");

        assert_eq!(endpoint.path_specs[1].method, HttpMethod::Post);
        assert_eq!(endpoint.path_specs[1].path, "/users");
        assert_eq!(endpoint.path_specs[1].params.len(), 0);
    }

    #[test]
    fn test_empty_paths() {
        let input = r#"
            route / {
                get /, post /, put / {
                    return "root";
                }

                get /hi {
                    return "hi;
                }
            }
        "#;

        let mut parser = Parser::new(input);
        let result = parser.parse_route().unwrap();

        assert_eq!(result.endpoints.len(), 2);
        let endpoint = &result.endpoints[0];
        assert_eq!(endpoint.path_specs.len(), 3);
        assert_eq!(endpoint.path_specs[0].path, "/");
        assert_eq!(endpoint.path_specs[1].path, "/");
        assert_eq!(endpoint.path_specs[2].path, "/");
    }

    #[test]
    fn test_path_param_validation() {
        // Test 1: Successful case - path parameters in path spec match path block
        let input = r#"
            get /users/:id/posts/:postId {
                path {
                    @string(min_len=3)
                    id: str,
                    postId: int,
                }
                
                return "Valid";
            }
        "#;

        let mut parser = Parser::new(input);
        let result = parser.parse_route();
        assert!(result.is_ok());
        let route = result.unwrap();
        assert_eq!(route.endpoints.len(), 1);

        // Test 2: Path parameter name mismatch (postid vs postId)
        let input = r#"
            get /users/:id/posts/:postid {
                path {
                    @string(min_len=3)
                    id: str,
                    postId: int,
                }
                
                return "Invalid";
            }
        "#;

        let mut parser = Parser::new(input);
        let result = parser.parse_route();
        assert!(result.is_err());
        let error = result.unwrap_err();
        assert!(error.contains("Path parameter case mismatch"));
        assert!(error.contains("'postid' in URL vs 'postId' in path block"));

        // Test 3: Extra parameter in path block
        let input = r#"
            get /users/:id/posts/:postId {
                path {
                    @string(min_len=3)
                    id: str,
                    postId: int,
                    name: str
                }
                
                return "Invalid";
            }
        "#;

        let mut parser = Parser::new(input);
        let result = parser.parse_route();
        assert!(result.is_err());
        let error = result.unwrap_err();
        assert!(error.contains("Unknown path parameter(s)"));
        assert!(error.contains("'name'"));

        // Test 4: Missing parameter in path block
        let input = r#"
            get /users/:id/posts/:postId {
                path {
                    postId: int,
                }
                
                return "Invalid";
            }
        "#;

        let mut parser = Parser::new(input);
        let result = parser.parse_route();
        assert!(result.is_err());
        let error = result.unwrap_err();
        assert!(error.contains("Missing path parameter(s)"));
        assert!(error.contains("'id'"));

        // Test 5: Case sensitivity check
        let input = r#"
            get /users/:ID/posts/:postId {
                path {
                    id: str,
                    postId: int,
                }
                
                return "Invalid";
            }
        "#;

        let mut parser = Parser::new(input);
        let result = parser.parse_route();
        assert!(result.is_err());
        let error = result.unwrap_err();
        assert!(error.contains("case mismatch"));

        // // Test 6: Type mismatch
        // let input = r#"
        //     get /users/:id/posts/:postId {
        //         path {
        //             id: int,  // Should be string based on the path parameter type
        //             postId: str, // Should be int based on the path parameter type
        //         }

        //         return "Invalid";
        //     }
        // "#;
        // let mut parser = Parser::new(input);
        // let result = parser.parse_route();
        // assert!(result.is_err());
        // let error = result.unwrap_err();
        // assert!(error.contains("Type mismatch"));

        // Test 7: Multiple path specs with the same parameters
        let input = r#"
            get /users/:id, post /users/:id {
                path {
                    id: str,
                }
                
                return "Valid";
            }
        "#;

        let mut parser = Parser::new(input);
        let result = parser.parse_route();
        assert!(result.is_ok());

        // Test 8: Multiple path specs with different parameters (should fail)
        let input = r#"
            get /users/:id, post /users/:userId {
                path {
                    id: str,
                }
                
                return "Invalid";
            }
        "#;

        let mut parser = Parser::new(input);
        let result = parser.parse_route();
        assert!(result.is_err());
        let error = result.unwrap_err();
        assert!(error.contains("parameter(s)"));

        // Test 9: No path block but path params - should be invalid
        let input = r#"
            get /users/:id {
                return "Invalid - missing path block";
            }
        "#;

        let mut parser = Parser::new(input);
        let result = parser.parse_route();
        assert!(result.is_err());
        let error = result.unwrap_err();
        assert!(error.contains("Path parameters found in URL but no path block defined"));

        // Test 10: No path params and no path block - should be valid
        let input = r#"
            get /users/all {
                return "Valid - no path params";
            }
        "#;

        let mut parser = Parser::new(input);
        let result = parser.parse_route();
        assert!(result.is_ok());
    }

    #[test]
    fn test_path_with_dash() {
        let input = r#"
            route /api {
                get /get-messages {
                    return "Messages";
                }

                post /user-profile/update-settings {
                    return "Settings updated";
                }
            }
        "#;

        let mut parser = Parser::new(input);
        let result = parser.parse_route();

        let route = result.unwrap();
        assert_eq!(route.prefix, "/api");

        let endpoint1 = &route.endpoints[0];
        assert_eq!(endpoint1.path_specs[0].method, HttpMethod::Get);
        assert_eq!(endpoint1.path_specs[0].path, "/get-messages");

        let endpoint2 = &route.endpoints[1];
        assert_eq!(endpoint2.path_specs[0].method, HttpMethod::Post);
        assert_eq!(
            endpoint2.path_specs[0].path,
            "/user-profile/update-settings"
        );
    }
}