@@ -712,3 +712,62 @@ fn check_accepts_raw_flag() {
712712 result. err( )
713713 ) ;
714714}
715+
716+ // Extension-based detection tests for ast command
717+
718+ #[ test]
719+ fn ast_detects_ptk_as_query ( ) {
720+ let cmd = ast_command ( ) ;
721+ let result = cmd. try_get_matches_from ( [ "ast" , "query.ptk" ] ) ;
722+ assert ! ( result. is_ok( ) ) ;
723+
724+ let m = result. unwrap ( ) ;
725+ let params = AstParams :: from_matches ( & m) ;
726+
727+ // .ptk extension → treat as query
728+ assert_eq ! ( params. query_path, Some ( PathBuf :: from( "query.ptk" ) ) ) ;
729+ assert_eq ! ( params. source_path, None ) ;
730+ }
731+
732+ #[ test]
733+ fn ast_detects_non_ptk_as_source ( ) {
734+ let cmd = ast_command ( ) ;
735+ let result = cmd. try_get_matches_from ( [ "ast" , "app.js" ] ) ;
736+ assert ! ( result. is_ok( ) ) ;
737+
738+ let m = result. unwrap ( ) ;
739+ let params = AstParams :: from_matches ( & m) ;
740+
741+ // Non-.ptk extension → treat as source
742+ assert_eq ! ( params. query_path, None ) ;
743+ assert_eq ! ( params. source_path, Some ( PathBuf :: from( "app.js" ) ) ) ;
744+ }
745+
746+ #[ test]
747+ fn ast_no_extension_detection_with_two_positionals ( ) {
748+ let cmd = ast_command ( ) ;
749+ let result = cmd. try_get_matches_from ( [ "ast" , "query.ptk" , "app.js" ] ) ;
750+ assert ! ( result. is_ok( ) ) ;
751+
752+ let m = result. unwrap ( ) ;
753+ let params = AstParams :: from_matches ( & m) ;
754+
755+ // Two positionals → first is query, second is source (no detection)
756+ assert_eq ! ( params. query_path, Some ( PathBuf :: from( "query.ptk" ) ) ) ;
757+ assert_eq ! ( params. source_path, Some ( PathBuf :: from( "app.js" ) ) ) ;
758+ }
759+
760+ #[ test]
761+ fn ast_no_extension_detection_with_inline_query ( ) {
762+ let cmd = ast_command ( ) ;
763+ let result = cmd. try_get_matches_from ( [ "ast" , "-q" , "(id) @x" , "app.js" ] ) ;
764+ assert ! ( result. is_ok( ) ) ;
765+
766+ let m = result. unwrap ( ) ;
767+ let params = AstParams :: from_matches ( & m) ;
768+
769+ // -q provided → positional shift takes precedence, no extension detection
770+ assert_eq ! ( params. query_path, None ) ;
771+ assert_eq ! ( params. query_text, Some ( "(id) @x" . to_string( ) ) ) ;
772+ assert_eq ! ( params. source_path, Some ( PathBuf :: from( "app.js" ) ) ) ;
773+ }
0 commit comments