#[cfg(test)]
mod unit_tests {
    use crate::parsers::wasot::arguments::parse_arguments;
    use crate::parsers::wasot::tokenizer::{
        find_first_equals_outside_quotes, tokenize_with_equals, tokenize_with_equals_and_quotes,
    };
    use crate::parsers::wasot::{
        extract_line_type, is_meta_line, split_into_parts, split_into_parts_and_trim, MetaLine,
        QuotedString,
    };

    #[cfg(feature = "python")]
    #[allow(unused_imports)]
    use pyo3::prelude::*;

    #[test]
    #[cfg(feature = "python")]
    fn test_pywasot_module_creation() {
        use pyo3::prepare_freethreaded_python;
        use pyo3::types::PyModule;
        use pyo3::Python;

        prepare_freethreaded_python();
        Python::with_gil(|py| {
            let module = PyModule::new_bound(py, "test_module").expect("Failed to create module");
            let result = crate::_pywasot(&module);
            assert!(result.is_ok(), "pywasot module setup should succeed");
        });
    }

    #[test]
    #[cfg(feature = "python")]
    fn test_pywasot_error_handling() {
        use pyo3::prepare_freethreaded_python;
        use pyo3::types::PyModule;
        use pyo3::Python;

        prepare_freethreaded_python();
        Python::with_gil(|py| {
            let module = PyModule::new_bound(py, "test_module").expect("Failed to create module");
            // Test that the module creation doesn't panic
            let result = crate::_pywasot(&module);
            assert!(
                result.is_ok(),
                "Module creation should handle errors gracefully"
            );
        });
    }

    #[test]
    fn test_is_meta_line_with_single_dot() {
        assert!(is_meta_line(".# This is a heading"));
        assert!(is_meta_line(".p"));
        assert!(is_meta_line(".## A new heading"));
        assert!(is_meta_line(".block1"));
    }

    #[test]
    fn test_is_meta_line_with_multiple_dots() {
        assert!(is_meta_line("..nested"));
        assert!(is_meta_line("...deeply nested"));
        assert!(is_meta_line("....very deeply nested"));
    }

    #[test]
    fn test_is_meta_line_with_quoted_types() {
        assert!(is_meta_line(".\"block type with spaces\""));
        assert!(is_meta_line(".\"another quoted type\""));
    }

    #[test]
    fn test_is_meta_line_with_args() {
        assert!(is_meta_line(".args arg1 arg2 kw1=1 kw1=2 foo=bar"));
        assert!(is_meta_line(".block key=value"));
    }

    #[test]
    fn test_is_meta_line_just_dot() {
        assert!(is_meta_line("."));
    }

    #[test]
    fn test_is_not_meta_line_regular_text() {
        assert!(!is_meta_line("This is regular text"));
        assert!(!is_meta_line("Another line of text"));
        assert!(!is_meta_line("Text with a . in the middle"));
    }

    #[test]
    fn test_is_not_meta_line_empty_string() {
        assert!(!is_meta_line(""));
    }

    #[test]
    fn test_is_not_meta_line_whitespace_before_dot() {
        assert!(!is_meta_line(" .not a meta line"));
        assert!(!is_meta_line("\t.not a meta line"));
        assert!(!is_meta_line("  .not a meta line"));
    }

    #[test]
    fn test_is_not_meta_line_dot_not_first() {
        assert!(!is_meta_line("text.dot"));
        assert!(!is_meta_line("some text . with dot"));
    }

    // MetaLine parsing tests

    // Case 1: Type only (no inline text, no params)
    #[test]
    fn test_metaline_parse_type_only() {
        let meta = MetaLine::parse(".heading", 0).unwrap();
        assert_eq!(meta.type_name, "heading");
        assert_eq!(meta.level, 1);
        assert_eq!(meta.inline_text, QuotedString::new(String::new(), false));
        // raw_params field removed from MetaLine
    }

    #[test]
    fn test_metaline_parse_type_only_with_level() {
        let meta = MetaLine::parse("...nested", 0).unwrap();
        assert_eq!(meta.type_name, "nested");
        assert_eq!(meta.level, 3);
        assert_eq!(meta.inline_text, QuotedString::new(String::new(), false));
        // raw_params field removed from MetaLine
    }

    #[test]
    fn test_metaline_parse_quoted_type_only() {
        let meta = MetaLine::parse(".\"block type with spaces\"", 0).unwrap();
        assert_eq!(meta.type_name, "block type with spaces");
        assert_eq!(meta.level, 1);
        assert_eq!(meta.inline_text, QuotedString::new(String::new(), false));
        // raw_params field removed from MetaLine
    }

    // Case 2: Type + inline text (no params)
    #[test]
    fn test_metaline_parse_type_with_inline_text() {
        let meta = MetaLine::parse(".block some inline text", 0).unwrap();
        assert_eq!(meta.type_name, "block");
        assert_eq!(meta.level, 1);
        assert_eq!(
            meta.inline_text,
            QuotedString::new("some inline text".to_string(), false)
        );
        // raw_params field removed from MetaLine
    }

    #[test]
    fn test_metaline_parse_quoted_type_with_inline_text() {
        let meta = MetaLine::parse(".\"block type\" some inline text", 0).unwrap();
        assert_eq!(meta.type_name, "block type");
        assert_eq!(meta.level, 1);
        assert_eq!(
            meta.inline_text,
            QuotedString::new("some inline text".to_string(), false)
        );
        // raw_params field removed from MetaLine
    }

    #[test]
    fn test_metaline_parse_type_with_quoted_inline_text() {
        let meta = MetaLine::parse(".block \"quoted inline text\"", 0).unwrap();
        assert_eq!(meta.type_name, "block");
        assert_eq!(meta.level, 1);
        assert_eq!(
            meta.inline_text,
            QuotedString::new("quoted inline text".to_string(), true)
        );
        // raw_params field removed from MetaLine
    }

    // Case 3: Type + params only (no inline text)
    #[test]
    fn test_metaline_parse_type_with_params_only() {
        let meta = MetaLine::parse(".block .. a b=c", 0).unwrap();
        assert_eq!(meta.type_name, "block");
        assert_eq!(meta.level, 1);
        assert_eq!(meta.inline_text, QuotedString::new(String::new(), false));
        // raw_params field removed from MetaLine
    }

    #[test]
    fn test_metaline_parse_quoted_type_with_params_only() {
        let meta = MetaLine::parse(".\"block type\" .. a b=c", 0).unwrap();
        assert_eq!(meta.type_name, "block type");
        assert_eq!(meta.level, 1);
        assert_eq!(meta.inline_text, QuotedString::new(String::new(), false));
        // raw_params field removed from MetaLine
    }

    #[test]
    fn test_metaline_parse_type_with_complex_params_only() {
        let meta = MetaLine::parse(
            ".block .. arg1 \"arg 2\" key=value \"a key\"=\"a value\"",
            0,
        )
        .unwrap();
        assert_eq!(meta.type_name, "block");
        assert_eq!(meta.level, 1);
        assert_eq!(meta.inline_text, QuotedString::new(String::new(), false));
        //         assert_eq!(
        //             // meta.raw_params, // field removed
        //             " arg1 \"arg 2\" key=value \"a key\"=\"a value\""
        //         );
    }

    // Case 4: Type + inline text + params
    #[test]
    fn test_metaline_parse_type_with_inline_text_and_params() {
        let meta = MetaLine::parse(".block some inline text .. a b=c", 0).unwrap();
        assert_eq!(meta.type_name, "block");
        assert_eq!(meta.level, 1);
        assert_eq!(
            meta.inline_text,
            QuotedString::new("some inline text ".to_string(), false)
        );
        // raw_params field removed from MetaLine
    }

    #[test]
    fn test_metaline_parse_quoted_type_with_inline_text_and_params() {
        let meta = MetaLine::parse(
            ".\"block type\" inline text .. param1 \"param 2\" key=\"value\"",
            0,
        )
        .unwrap();
        assert_eq!(meta.type_name, "block type");
        assert_eq!(meta.level, 1);
        assert_eq!(
            meta.inline_text,
            QuotedString::new("inline text ".to_string(), false)
        );
        // raw_params field removed from MetaLine
    }

    #[test]
    fn test_metaline_parse_type_with_quoted_inline_text_and_params() {
        let meta = MetaLine::parse(".block \"quoted inline text\" .. params", 0).unwrap();
        assert_eq!(meta.type_name, "block");
        assert_eq!(meta.level, 1);
        assert_eq!(
            meta.inline_text,
            QuotedString::new("quoted inline text".to_string(), true)
        );
        // raw_params field removed from MetaLine
    }

    // Case 5: Multiple dots in params (should be rejoined)
    #[test]
    fn test_metaline_parse_multiple_dots_in_params() {
        let meta = MetaLine::parse(".block inline .. param1 .. param2", 0).unwrap();
        assert_eq!(meta.type_name, "block");
        assert_eq!(meta.level, 1);
        assert_eq!(
            meta.inline_text,
            QuotedString::new("inline ".to_string(), false)
        );
        // raw_params field removed from MetaLine
    }

    #[test]
    fn test_metaline_parse_multiple_dots_params_only() {
        let meta = MetaLine::parse(".block .. param1 .. param2 .. param3", 0).unwrap();
        assert_eq!(meta.type_name, "block");
        assert_eq!(meta.level, 1);
        assert_eq!(meta.inline_text, QuotedString::new(String::new(), false));
        // raw_params field removed from MetaLine
    }

    // Case 6: Different nesting levels
    #[test]
    fn test_metaline_parse_nested_with_inline_text_and_params() {
        let meta = MetaLine::parse("..nested inline text .. params", 0).unwrap();
        assert_eq!(meta.type_name, "nested");
        assert_eq!(meta.level, 2);
        assert_eq!(
            meta.inline_text,
            QuotedString::new("inline text ".to_string(), false)
        );
        // raw_params field removed from MetaLine
    }

    #[test]
    fn test_metaline_parse_deeply_nested_params_only() {
        let meta = MetaLine::parse("....deep .. just params", 0).unwrap();
        assert_eq!(meta.type_name, "deep");
        assert_eq!(meta.level, 4);
        assert_eq!(meta.inline_text, QuotedString::new(String::new(), false));
        // raw_params field removed from MetaLine
    }

    // Case 7: Edge cases
    #[test]
    fn test_metaline_parse_just_dots() {
        let meta = MetaLine::parse("...", 0).unwrap();
        assert_eq!(meta.type_name, "");
        assert_eq!(meta.level, 3);
        assert_eq!(meta.inline_text, QuotedString::new(String::new(), false));
        // raw_params field removed from MetaLine
    }

    #[test]
    fn test_metaline_parse_empty_inline_text_with_params() {
        let meta = MetaLine::parse(".block .. params", 0).unwrap();
        assert_eq!(meta.type_name, "block");
        assert_eq!(meta.level, 1);
        assert_eq!(meta.inline_text, QuotedString::new(String::new(), false));
        // raw_params field removed from MetaLine
    }

    #[test]
    fn test_metaline_parse_whitespace_inline_text_with_params() {
        let meta = MetaLine::parse(".block   .. params", 0).unwrap();
        assert_eq!(meta.type_name, "block");
        assert_eq!(meta.level, 1);
        assert_eq!(meta.inline_text, QuotedString::new("  ".to_string(), false));
        // raw_params field removed from MetaLine
    }

    #[test]
    fn test_metaline_parse_non_meta_line() {
        assert!(MetaLine::parse("This is not a meta line", 0).is_none());
        assert!(MetaLine::parse("", 0).is_none());
        assert!(MetaLine::parse("  .not a meta line", 0).is_none());
    }

    // Case 8: Real-world examples from SYNTAX.md
    #[test]
    fn test_metaline_parse_syntax_example_1() {
        let meta = MetaLine::parse(
            ".block arg1 \"arg 2\" key=value \"a key\"=\"a value\" \"another key\"=value k=value",
            0,
        )
        .unwrap();
        assert_eq!(meta.type_name, "block");
        assert_eq!(meta.level, 1);
        assert_eq!(
            meta.inline_text,
            QuotedString::new(
                "arg1 \"arg 2\" key=value \"a key\"=\"a value\" \"another key\"=value k=value"
                    .to_string(),
                false
            )
        );
        // raw_params field removed from MetaLine
    }

    #[test]
    fn test_metaline_parse_syntax_example_2() {
        let meta = MetaLine::parse(
            ".block .. arg1 \"arg 2\" key=value \"a key\"=\"a value\"",
            0,
        )
        .unwrap();
        assert_eq!(meta.type_name, "block");
        assert_eq!(meta.level, 1);
        assert_eq!(meta.inline_text, QuotedString::new(String::new(), false));
        //         assert_eq!(
        //             // meta.raw_params, // field removed
        //             " arg1 \"arg 2\" key=value \"a key\"=\"a value\""
        //         );
    }

    // Parameter parsing tests

    // Normal cases
    #[test]
    fn test_parse_arguments_basic_args() {
        let params = parse_arguments("arg1 arg2 arg3");
        assert_eq!(
            params.args,
            vec![
                QuotedString::new("arg1".to_string(), false),
                QuotedString::new("arg2".to_string(), false),
                QuotedString::new("arg3".to_string(), false)
            ]
        );
        assert_eq!(params.kwargs, Vec::<(QuotedString, QuotedString)>::new());
    }

    #[test]
    fn test_parse_arguments_basic_kwargs() {
        let params = parse_arguments("key1=value1 key2=value2");
        assert_eq!(params.args, Vec::<QuotedString>::new());
        assert_eq!(
            params.kwargs,
            vec![
                (
                    QuotedString::new("key1".to_string(), false),
                    QuotedString::new("value1".to_string(), false)
                ),
                (
                    QuotedString::new("key2".to_string(), false),
                    QuotedString::new("value2".to_string(), false)
                )
            ]
        );
    }

    #[test]
    fn test_parse_arguments_mixed_args_kwargs() {
        let params = parse_arguments("arg1 arg2 key1=value1 key2=value2");
        assert_eq!(
            params.args,
            vec![
                QuotedString::new("arg1".to_string(), false),
                QuotedString::new("arg2".to_string(), false)
            ]
        );
        assert_eq!(
            params.kwargs,
            vec![
                (
                    QuotedString::new("key1".to_string(), false),
                    QuotedString::new("value1".to_string(), false)
                ),
                (
                    QuotedString::new("key2".to_string(), false),
                    QuotedString::new("value2".to_string(), false)
                )
            ]
        );
    }

    #[test]
    fn test_parse_arguments_quoted_args() {
        let params = parse_arguments("arg1 \"arg with spaces\" arg3");
        assert_eq!(
            params.args,
            vec![
                QuotedString::new("arg1".to_string(), false),
                QuotedString::new("arg with spaces".to_string(), true),
                QuotedString::new("arg3".to_string(), false)
            ]
        );
        assert_eq!(params.kwargs, Vec::<(QuotedString, QuotedString)>::new());
    }

    #[test]
    fn test_parse_arguments_quoted_kwargs() {
        let params = parse_arguments("\"key with spaces\"=\"value with spaces\" key2=value2");
        assert_eq!(params.args, Vec::<QuotedString>::new());
        assert_eq!(
            params.kwargs,
            vec![
                (
                    QuotedString::new("key with spaces".to_string(), true),
                    QuotedString::new("value with spaces".to_string(), true)
                ),
                (
                    QuotedString::new("key2".to_string(), false),
                    QuotedString::new("value2".to_string(), false)
                )
            ]
        );
    }

    #[test]
    fn test_parse_arguments_syntax_example() {
        let params = parse_arguments(
            "arg1 \"arg 2\" key=value \"a key\"=\"a value\" \"another key\"=value k=value",
        );
        assert_eq!(
            params.args,
            vec![
                QuotedString::new("arg1".to_string(), false),
                QuotedString::new("arg 2".to_string(), true)
            ]
        );
        assert_eq!(
            params.kwargs,
            vec![
                (
                    QuotedString::new("key".to_string(), false),
                    QuotedString::new("value".to_string(), false)
                ),
                (
                    QuotedString::new("a key".to_string(), true),
                    QuotedString::new("a value".to_string(), true)
                ),
                (
                    QuotedString::new("another key".to_string(), true),
                    QuotedString::new("value".to_string(), false)
                ),
                (
                    QuotedString::new("k".to_string(), false),
                    QuotedString::new("value".to_string(), false)
                )
            ]
        );
    }

    // Edge cases - honey badger style recovery
    #[test]
    fn test_parse_arguments_unclosed_quotes() {
        let params = parse_arguments("arg1 \"unclosed quote arg2");
        assert_eq!(
            params.args,
            vec![
                QuotedString::new("arg1".to_string(), false),
                QuotedString::new("unclosed quote arg2".to_string(), false)
            ]
        );
        assert_eq!(params.kwargs, Vec::<(QuotedString, QuotedString)>::new());
    }

    #[test]
    fn test_parse_arguments_unclosed_quotes_in_value() {
        let params = parse_arguments("key=\"unclosed value arg2");
        assert_eq!(params.args, Vec::<QuotedString>::new());
        assert_eq!(
            params.kwargs,
            vec![(
                QuotedString::new("key".to_string(), false),
                QuotedString::new("unclosed value arg2".to_string(), false)
            )]
        );
    }

    #[test]
    fn test_parse_arguments_empty_quotes() {
        let params = parse_arguments("arg1 \"\" key=\"\"");
        assert_eq!(
            params.args,
            vec![
                QuotedString::new("arg1".to_string(), false),
                QuotedString::new(String::new(), true)
            ]
        );
        assert_eq!(
            params.kwargs,
            vec![(
                QuotedString::new("key".to_string(), false),
                QuotedString::new(String::new(), true)
            )]
        );
    }

    #[test]
    fn test_parse_arguments_dangling_key() {
        let params = parse_arguments("arg1 key= arg2");
        assert_eq!(
            params.args,
            vec![
                QuotedString::new("arg1".to_string(), false),
                QuotedString::new("arg2".to_string(), false)
            ]
        );
        assert_eq!(
            params.kwargs,
            vec![(
                QuotedString::new("key".to_string(), false),
                QuotedString::new(String::new(), false)
            )]
        );
    }

    #[test]
    fn test_parse_arguments_dangling_key_at_end() {
        let params = parse_arguments("arg1 key=");
        assert_eq!(
            params.args,
            vec![QuotedString::new("arg1".to_string(), false)]
        );
        assert_eq!(
            params.kwargs,
            vec![(
                QuotedString::new("key".to_string(), false),
                QuotedString::new(String::new(), false)
            )]
        );
    }

    #[test]
    fn test_parse_arguments_multiple_equals() {
        let params = parse_arguments("key=value=with=equals");
        assert_eq!(params.args, Vec::<QuotedString>::new());
        assert_eq!(
            params.kwargs,
            vec![(
                QuotedString::new("key".to_string(), false),
                QuotedString::new("value=with=equals".to_string(), false)
            )]
        );
    }

    #[test]
    fn test_parse_arguments_equals_without_key() {
        let params = parse_arguments("=value arg1");
        assert_eq!(
            params.args,
            vec![QuotedString::new("arg1".to_string(), false)]
        );
        assert_eq!(
            params.kwargs,
            vec![(
                QuotedString::new(String::new(), false),
                QuotedString::new("value".to_string(), false)
            )]
        );
    }

    #[test]
    fn test_parse_arguments_multiple_spaces() {
        let params = parse_arguments("arg1    arg2     key=value    key2=value2");
        assert_eq!(
            params.args,
            vec![
                QuotedString::new("arg1".to_string(), false),
                QuotedString::new("arg2".to_string(), false)
            ]
        );
        assert_eq!(
            params.kwargs,
            vec![
                (
                    QuotedString::new("key".to_string(), false),
                    QuotedString::new("value".to_string(), false)
                ),
                (
                    QuotedString::new("key2".to_string(), false),
                    QuotedString::new("value2".to_string(), false)
                )
            ]
        );
    }

    #[test]
    fn test_parse_arguments_tabs_and_spaces() {
        let params = parse_arguments("arg1\t\targ2\t  key=value");
        assert_eq!(
            params.args,
            vec![
                QuotedString::new("arg1".to_string(), false),
                QuotedString::new("arg2".to_string(), false)
            ]
        );
        assert_eq!(
            params.kwargs,
            vec![(
                QuotedString::new("key".to_string(), false),
                QuotedString::new("value".to_string(), false)
            )]
        );
    }

    #[test]
    fn test_parse_arguments_quotes_in_middle() {
        let params = parse_arguments("arg1 ar\"g2 key=val\"ue");
        assert_eq!(
            params.args,
            vec![
                QuotedString::new("arg1".to_string(), false),
                QuotedString::new("arg2".to_string(), false)
            ]
        );
        assert_eq!(
            params.kwargs,
            vec![(
                QuotedString::new("key".to_string(), false),
                QuotedString::new("value".to_string(), false)
            )]
        );
    }

    #[test]
    fn test_parse_arguments_special_characters() {
        let params = parse_arguments("arg1 key=value! key2=\"value@#$%^&*()\"");
        assert_eq!(
            params.args,
            vec![QuotedString::new("arg1".to_string(), false)]
        );
        assert_eq!(
            params.kwargs,
            vec![
                (
                    QuotedString::new("key".to_string(), false),
                    QuotedString::new("value!".to_string(), false)
                ),
                (
                    QuotedString::new("key2".to_string(), false),
                    QuotedString::new("value@#$%^&*()".to_string(), true)
                )
            ]
        );
    }

    #[test]
    fn test_parse_arguments_empty_string() {
        let params = parse_arguments("");
        assert_eq!(params.args, Vec::<QuotedString>::new());
        assert_eq!(params.kwargs, Vec::<(QuotedString, QuotedString)>::new());
    }

    #[test]
    fn test_parse_arguments_only_whitespace() {
        let params = parse_arguments("   \t  ");
        assert_eq!(params.args, Vec::<QuotedString>::new());
        assert_eq!(params.kwargs, Vec::<(QuotedString, QuotedString)>::new());
    }

    #[test]
    fn test_parse_arguments_only_equals() {
        let params = parse_arguments("===");
        assert_eq!(params.args, Vec::<QuotedString>::new());
        assert_eq!(
            params.kwargs,
            vec![(
                QuotedString::new(String::new(), false),
                QuotedString::new(String::new(), false)
            )]
        );
    }

    #[test]
    fn test_parse_arguments_only_quotes() {
        let params = parse_arguments("\"\"\"");
        assert_eq!(
            params.args,
            vec![
                QuotedString::new(String::new(), true),
                QuotedString::new(String::new(), true)
            ]
        );
        assert_eq!(params.kwargs, Vec::<(QuotedString, QuotedString)>::new());
    }

    #[test]
    fn test_parse_arguments_nested_quotes() {
        let params = parse_arguments("key=\"value with \\\"nested\\\" quotes\"");
        assert_eq!(params.args, Vec::<QuotedString>::new());
        assert_eq!(
            params.kwargs,
            vec![(
                QuotedString::new("key".to_string(), false),
                QuotedString::new("value with \"nested\" quotes".to_string(), false)
            )]
        );
    }

    #[test]
    fn test_parse_arguments_malformed_mixed() {
        let params = parse_arguments("arg1 \"unclosed key= =value \"quoted arg");
        // For malformed input, the parser should recover gracefully
        // The actual result is reasonable honey-badger behavior
        assert_eq!(
            params.args,
            vec![
                QuotedString::new("arg1".to_string(), false),
                QuotedString::new("quoted".to_string(), false),
                QuotedString::new("arg".to_string(), false)
            ]
        );
        assert_eq!(
            params.kwargs,
            vec![(
                QuotedString::new("unclosed key".to_string(), false),
                QuotedString::new(" =value ".to_string(), false)
            )]
        );
    }

    #[test]
    fn test_parse_arguments_real_world_chaos() {
        let params = parse_arguments(
            "arg1 \"arg 2 key=value \"broken key\"=\"value with spaces = and quotes\" key2= \"\"",
        );
        // For very complex malformed input, the parser should recover gracefully
        // The actual result is reasonable honey-badger behavior
        assert_eq!(
            params.args,
            vec![
                QuotedString::new("arg1".to_string(), false),
                QuotedString::new("broken".to_string(), false),
                QuotedString::new(String::new(), false)
            ]
        );
        assert_eq!(
            params.kwargs,
            vec![
                (
                    QuotedString::new("arg 2 key".to_string(), false),
                    QuotedString::new("value ".to_string(), false)
                ),
                (
                    QuotedString::new("key".to_string(), false),
                    QuotedString::new("value with spaces = and quotes".to_string(), false)
                ),
                (
                    QuotedString::new("key2".to_string(), false),
                    QuotedString::new(String::new(), false)
                )
            ]
        );
    }

    // Escaped quotes tests
    #[test]
    fn test_parse_arguments_escaped_quotes_in_args() {
        let params = parse_arguments("arg1 \"arg with \\\"quotes\\\" inside\" arg3");
        assert_eq!(
            params.args,
            vec![
                QuotedString::new("arg1".to_string(), false),
                QuotedString::new("arg with \"quotes\" inside".to_string(), false),
                QuotedString::new("arg3".to_string(), false)
            ]
        );
        assert_eq!(params.kwargs, Vec::<(QuotedString, QuotedString)>::new());
    }

    #[test]
    fn test_parse_arguments_escaped_quotes_in_kwarg_key() {
        let params = parse_arguments("\"key with \\\"quotes\\\"\"=value");
        assert_eq!(params.args, Vec::<QuotedString>::new());
        assert_eq!(
            params.kwargs,
            vec![(
                QuotedString::new("key with \"quotes\"".to_string(), false),
                QuotedString::new("value".to_string(), false)
            )]
        );
    }

    #[test]
    fn test_parse_arguments_escaped_quotes_in_kwarg_value() {
        let params = parse_arguments("key=\"value with \\\"quotes\\\" inside\"");
        assert_eq!(params.args, Vec::<QuotedString>::new());
        assert_eq!(
            params.kwargs,
            vec![(
                QuotedString::new("key".to_string(), false),
                QuotedString::new("value with \"quotes\" inside".to_string(), false)
            )]
        );
    }

    #[test]
    fn test_parse_arguments_escaped_quotes_mixed() {
        let params = parse_arguments(
            "\"arg \\\"with\\\" quotes\" \"key\\\"1\"=\"value \\\"with\\\" quotes\" normal=value",
        );
        assert_eq!(
            params.args,
            vec![QuotedString::new("arg \"with\" quotes".to_string(), false)]
        );
        assert_eq!(
            params.kwargs,
            vec![
                (
                    QuotedString::new("key\"1".to_string(), false),
                    QuotedString::new("value \"with\" quotes".to_string(), false)
                ),
                (
                    QuotedString::new("normal".to_string(), false),
                    QuotedString::new("value".to_string(), false)
                )
            ]
        );
    }

    #[test]
    fn test_parse_arguments_escaped_backslash() {
        let params =
            parse_arguments("\"arg with \\\\ backslash\" key=\"value with \\\\ backslash\"");
        assert_eq!(
            params.args,
            vec![QuotedString::new(
                "arg with \\ backslash".to_string(),
                false
            )]
        );
        assert_eq!(
            params.kwargs,
            vec![(
                QuotedString::new("key".to_string(), false),
                QuotedString::new("value with \\ backslash".to_string(), false)
            )]
        );
    }

    #[test]
    fn test_parse_arguments_escaped_quotes_at_end() {
        let params = parse_arguments("\"ends with quote\\\"\" key=\"also ends\\\"\"");
        assert_eq!(
            params.args,
            vec![QuotedString::new("ends with quote\"".to_string(), false)]
        );
        assert_eq!(
            params.kwargs,
            vec![(
                QuotedString::new("key".to_string(), false),
                QuotedString::new("also ends\"".to_string(), false)
            )]
        );
    }

    #[test]
    fn test_parse_arguments_escaped_quotes_at_start() {
        let params = parse_arguments("\"\\\"starts with quote\" key=\"\\\"also starts\"");
        assert_eq!(
            params.args,
            vec![QuotedString::new("\"starts with quote".to_string(), false)]
        );
        assert_eq!(
            params.kwargs,
            vec![(
                QuotedString::new("key".to_string(), false),
                QuotedString::new("\"also starts".to_string(), false)
            )]
        );
    }

    // Line parts parsing tests
    #[test]
    fn test_split_into_parts_no_delimiter() {
        let result = split_into_parts("hello world");
        assert_eq!(result, vec!["hello world"]);
    }

    #[test]
    fn test_split_into_parts_single_delimiter() {
        let result = split_into_parts("hello..world");
        assert_eq!(result, vec!["hello", "world"]);
    }

    #[test]
    fn test_split_into_parts_inside_quotes_should_not_split() {
        let result = split_into_parts("hello..\"quoted\"..world");
        assert_eq!(result, vec!["hello", "\"quoted\"", "world"]);
    }

    #[test]
    fn test_split_into_parts_multiple_dots_outside_plus_quotes() {
        let result = split_into_parts("foo..bar..\"quoted..section\"..end");
        assert_eq!(result, vec!["foo", "bar", "\"quoted..section\"", "end"]);
    }

    #[test]
    fn test_split_into_parts_user_example() {
        let input = r#"..       .. a        b .. "All quoted" .. This has a ._ link _.! ..part 123.. part " .. " 4 .."#;
        let result = split_into_parts(input);
        let expected = vec![
            "",
            "       ",
            " a        b ",
            " \"All quoted\" ",
            " This has a ._ link _.! ",
            "part 123",
            " part \" .. \" 4 ",
            "",
        ];
        assert_eq!(result, expected);
    }

    #[test]
    fn test_split_into_parts_and_trim() {
        let result = split_into_parts_and_trim("  hello  ..  world  ");
        assert_eq!(result, vec!["hello", "world"]);
    }

    #[test]
    fn test_split_into_parts_and_trim_with_quotes() {
        let result = split_into_parts_and_trim("  foo  ..  \"quoted..section\"  ..  end  ");
        assert_eq!(result, vec!["foo", "\"quoted..section\"", "end"]);
    }

    // Escaped quotes in split_into_parts
    #[test]
    fn test_split_into_parts_escaped_quotes() {
        let result = split_into_parts("\"text with \\\"quotes\\\"\"..\"more \\\"text\\\"\"");
        assert_eq!(
            result,
            vec!["\"text with \\\"quotes\\\"\"", "\"more \\\"text\\\"\""]
        );
    }

    #[test]
    fn test_split_into_parts_escaped_quotes_should_not_split() {
        let result = split_into_parts("\"text with \\\"..\\\" should not split\"");
        assert_eq!(result, vec!["\"text with \\\"..\\\" should not split\""]);
    }

    #[test]
    fn test_extract_line_type_with_type_and_content() {
        let (type_name, remaining, _is_void) = extract_line_type(".heading This is content");
        assert_eq!(type_name, "heading");
        assert_eq!(remaining, "This is content");
    }

    #[test]
    fn test_extract_line_type_only_type() {
        let (type_name, remaining, _is_void) = extract_line_type(".heading");
        assert_eq!(type_name, "heading");
        assert_eq!(remaining, "");
    }

    #[test]
    fn test_extract_line_type_not_meta_line() {
        let (type_name, remaining, _is_void) = extract_line_type("Not a meta line");
        assert_eq!(type_name, "");
        assert_eq!(remaining, "Not a meta line");
    }

    #[test]
    fn test_extract_line_type_empty_after_dot() {
        let (type_name, remaining, _is_void) = extract_line_type(".");
        assert_eq!(type_name, "");
        assert_eq!(remaining, "");
    }

    #[test]
    fn test_extract_line_type_with_multiple_spaces() {
        let (type_name, remaining, _is_void) =
            extract_line_type(".heading   Multiple   spaces   here");
        assert_eq!(type_name, "heading");
        assert_eq!(remaining, "  Multiple   spaces   here");
    }

    #[test]
    fn test_extract_line_type_quoted_block_type() {
        let (type_name, remaining, _is_void) =
            extract_line_type(".\"block type with spaces\" Some content");
        assert_eq!(type_name, "block type with spaces");
        assert_eq!(remaining, "Some content");
    }

    #[test]
    fn test_extract_line_type_quoted_block_type_no_content() {
        let (type_name, remaining, _is_void) = extract_line_type(".\"block type with spaces\"");
        assert_eq!(type_name, "block type with spaces");
        assert_eq!(remaining, "");
    }

    #[test]
    fn test_extract_line_type_quoted_block_type_no_space_after() {
        let (type_name, remaining, _is_void) = extract_line_type(".\"block type\"content");
        assert_eq!(type_name, "block type");
        assert_eq!(remaining, "content");
    }

    #[test]
    fn test_extract_line_type_unclosed_quote() {
        let (type_name, remaining, _is_void) = extract_line_type(".\"unclosed quote content");
        assert_eq!(type_name, "\"unclosed");
        assert_eq!(remaining, "quote content");
    }

    #[test]
    fn test_extract_line_type_empty_quoted_type() {
        let (type_name, remaining, _is_void) = extract_line_type(".\"\" Some content");
        assert_eq!(type_name, "");
        assert_eq!(remaining, "Some content");
    }

    #[test]
    fn test_extract_line_type_quoted_with_special_chars() {
        let (type_name, remaining, _is_void) =
            extract_line_type(".\"special-type_123 with spaces\" content");
        assert_eq!(type_name, "special-type_123 with spaces");
        assert_eq!(remaining, "content");
    }

    #[test]
    fn test_extract_line_type_quoted_with_dots() {
        let (type_name, remaining, _is_void) = extract_line_type(".\"type.with.dots\" content");
        assert_eq!(type_name, "type.with.dots");
        assert_eq!(remaining, "content");
    }

    // Escaped quotes in block types
    #[test]
    fn test_extract_line_type_escaped_quotes_in_block_type() {
        let (type_name, remaining, _is_void) =
            extract_line_type(".\"block with \\\"quotes\\\"\" content");
        assert_eq!(type_name, "block with \"quotes\"");
        assert_eq!(remaining, "content");
    }

    #[test]
    fn test_extract_line_type_escaped_quotes_at_start() {
        let (type_name, remaining, _is_void) = extract_line_type(".\"\\\"quoted block\" content");
        assert_eq!(type_name, "\"quoted block");
        assert_eq!(remaining, "content");
    }

    #[test]
    fn test_extract_line_type_escaped_quotes_at_end() {
        let (type_name, remaining, _is_void) = extract_line_type(".\"block name\\\"\" content");
        assert_eq!(type_name, "block name\"");
        assert_eq!(remaining, "content");
    }

    #[test]
    fn test_extract_line_type_escaped_backslash() {
        let (type_name, remaining, _is_void) = extract_line_type(".\"block\\\\name\" content");
        assert_eq!(type_name, "block\\name");
        assert_eq!(remaining, "content");
    }

    // Tests specifically for quote detection functionality
    #[test]
    fn test_quote_detection_basic_quoted_args() {
        let params = parse_arguments("\"hello\" \"world\" normal");
        assert_eq!(params.args.len(), 3);
        assert_eq!(params.args[0], QuotedString::new("hello".to_string(), true));
        assert_eq!(params.args[1], QuotedString::new("world".to_string(), true));
        assert_eq!(
            params.args[2],
            QuotedString::new("normal".to_string(), false)
        );
    }

    #[test]
    fn test_quote_detection_mixed_quoted_and_unquoted() {
        let params = parse_arguments("unquoted \"quoted arg\" another \"second quoted\"");
        assert_eq!(params.args.len(), 4);
        assert_eq!(
            params.args[0],
            QuotedString::new("unquoted".to_string(), false)
        );
        assert_eq!(
            params.args[1],
            QuotedString::new("quoted arg".to_string(), true)
        );
        assert_eq!(
            params.args[2],
            QuotedString::new("another".to_string(), false)
        );
        assert_eq!(
            params.args[3],
            QuotedString::new("second quoted".to_string(), true)
        );
    }

    #[test]
    fn test_quote_detection_quoted_kwargs() {
        let params = parse_arguments(
            "\"quoted key\"=\"quoted value\" normal_key=\"quoted_val\" \"q_key\"=normal",
        );
        assert_eq!(params.kwargs.len(), 3);

        // First kwarg: both key and value quoted
        assert_eq!(
            params.kwargs[0].0,
            QuotedString::new("quoted key".to_string(), true)
        );
        assert_eq!(
            params.kwargs[0].1,
            QuotedString::new("quoted value".to_string(), true)
        );

        // Second kwarg: only value quoted
        assert_eq!(
            params.kwargs[1].0,
            QuotedString::new("normal_key".to_string(), false)
        );
        assert_eq!(
            params.kwargs[1].1,
            QuotedString::new("quoted_val".to_string(), true)
        );

        // Third kwarg: only key quoted
        assert_eq!(
            params.kwargs[2].0,
            QuotedString::new("q_key".to_string(), true)
        );
        assert_eq!(
            params.kwargs[2].1,
            QuotedString::new("normal".to_string(), false)
        );
    }

    #[test]
    fn test_quote_detection_empty_quoted_strings() {
        let params = parse_arguments("\"\" normal \"\"");
        assert_eq!(params.args.len(), 3);
        assert_eq!(params.args[0], QuotedString::new(String::new(), true));
        assert_eq!(
            params.args[1],
            QuotedString::new("normal".to_string(), false)
        );
        assert_eq!(params.args[2], QuotedString::new(String::new(), true));
    }

    #[test]
    fn test_quote_detection_quoted_with_special_chars() {
        let params =
            parse_arguments("\"arg with spaces\" \"arg@#$%\" normal_arg \"key with spaces\"=value");
        assert_eq!(params.args.len(), 3);
        assert_eq!(
            params.args[0],
            QuotedString::new("arg with spaces".to_string(), true)
        );
        assert_eq!(
            params.args[1],
            QuotedString::new("arg@#$%".to_string(), true)
        );
        assert_eq!(
            params.args[2],
            QuotedString::new("normal_arg".to_string(), false)
        );

        assert_eq!(params.kwargs.len(), 1);
        assert_eq!(
            params.kwargs[0].0,
            QuotedString::new("key with spaces".to_string(), true)
        );
        assert_eq!(
            params.kwargs[0].1,
            QuotedString::new("value".to_string(), false)
        );
    }

    #[test]
    fn test_quote_detection_all_quoted() {
        let params = parse_arguments("\"arg1\" \"arg2\" \"key1\"=\"value1\" \"key2\"=\"value2\"");
        assert_eq!(params.args.len(), 2);
        assert_eq!(params.kwargs.len(), 2);

        // All args should be quoted
        for arg in &params.args {
            assert!(arg.quoted);
        }

        // All keys and values should be quoted
        for (key, value) in &params.kwargs {
            assert!(key.quoted);
            assert!(value.quoted);
        }
    }

    #[test]
    fn test_quote_detection_none_quoted() {
        let params = parse_arguments("arg1 arg2 key1=value1 key2=value2");
        assert_eq!(params.args.len(), 2);
        assert_eq!(params.kwargs.len(), 2);

        // No args should be quoted
        for arg in &params.args {
            assert!(!arg.quoted);
        }

        // No keys or values should be quoted
        for (key, value) in &params.kwargs {
            assert!(!key.quoted);
            assert!(!value.quoted);
        }
    }

    // Test for metaline with empty kwarg value
    #[test]
    fn test_metaline_parse_empty_kwarg_value() {
        let meta = MetaLine::parse(".block key= .. key=", 0).unwrap();
        assert_eq!(meta.type_name, "block");
        assert_eq!(meta.level, 1);
        assert_eq!(
            meta.inline_text,
            QuotedString::new("key= ".to_string(), false)
        );
        // raw_params field removed from MetaLine

        // Now test the params parsing
        // let params = parse_arguments(&meta.raw_params); // raw_params field removed
        //         assert_eq!(params.args, Vec::<QuotedString>::new());
        //         assert_eq!(
        //             params.kwargs,
        //             vec![(
        //                 QuotedString::new(
        //                     content: "key".to_string(),
        //                     quoted: false
        //                 },
        //                 QuotedString::new(
        //                     content: String::new(),
        //                     quoted: false
        //                 }
        //             )]
        //         );
    }

    #[test]
    fn test_metaline_parse_just_empty_kwarg() {
        let meta = MetaLine::parse(".block .. key=", 0).unwrap();
        assert_eq!(meta.type_name, "block");
        assert_eq!(meta.level, 1);
        assert_eq!(meta.inline_text, QuotedString::new(String::new(), false));
        // raw_params field removed from MetaLine

        // Test the params parsing
        // let params = parse_arguments(&meta.raw_params); // raw_params field removed
        //         assert_eq!(params.args, Vec::<QuotedString>::new());
        //         assert_eq!(
        //             params.kwargs,
        //             vec![(
        //                 QuotedString::new(
        //                     content: "key".to_string(),
        //                     quoted: false
        //                 },
        //                 QuotedString::new(
        //                     content: String::new(),
        //                     quoted: false
        //                 }
        //             )]
        //         );
        //     }
    }
    // Tokenizer tests
    #[test]
    fn test_tokenize_with_equals_and_quotes_basic() {
        let result = tokenize_with_equals_and_quotes("arg1 arg2");
        assert_eq!(
            result,
            vec![
                ("arg1".to_string(), false, false),
                ("arg2".to_string(), false, false)
            ]
        );
    }

    #[test]
    fn test_tokenize_with_equals_and_quotes_quoted_args() {
        let result = tokenize_with_equals_and_quotes("\"quoted arg\" normal");
        assert_eq!(
            result,
            vec![
                ("quoted arg".to_string(), true, false),
                ("normal".to_string(), false, false)
            ]
        );
    }

    #[test]
    fn test_tokenize_with_equals_and_quotes_key_value() {
        let result = tokenize_with_equals_and_quotes("key=value");
        assert_eq!(result, vec![("key=value".to_string(), false, false)]);
    }

    #[test]
    fn test_tokenize_with_equals_and_quotes_quoted_key() {
        let result = tokenize_with_equals_and_quotes("\"quoted key\"=value");
        assert_eq!(result, vec![("quoted key=value".to_string(), false, false)]);
    }

    #[test]
    fn test_tokenize_with_equals_and_quotes_quoted_value() {
        let result = tokenize_with_equals_and_quotes("key=\"quoted value\"");
        assert_eq!(result, vec![("key=quoted value".to_string(), false, true)]);
    }

    #[test]
    fn test_tokenize_with_equals_and_quotes_escaped_quotes() {
        let result = tokenize_with_equals_and_quotes("\"arg with \\\"escaped\\\" quotes\"");
        assert_eq!(
            result,
            vec![("arg with \"escaped\" quotes".to_string(), true, false)]
        );
    }

    #[test]
    fn test_tokenize_with_equals_and_quotes_escaped_backslash() {
        let result = tokenize_with_equals_and_quotes("\"path\\\\to\\\\file\"");
        assert_eq!(result, vec![("path\\to\\file".to_string(), true, false)]);
    }

    #[test]
    fn test_tokenize_with_equals_and_quotes_unclosed_quotes() {
        let result = tokenize_with_equals_and_quotes("\"unclosed");
        assert_eq!(result, vec![("unclosed".to_string(), true, false)]);
    }

    #[test]
    fn test_tokenize_with_equals_and_quotes_backslash_at_end() {
        let result = tokenize_with_equals_and_quotes("\"test\\");
        assert_eq!(result, vec![("test\\".to_string(), true, false)]);
    }

    #[test]
    fn test_tokenize_with_equals_and_quotes_mixed_whitespace() {
        let result = tokenize_with_equals_and_quotes("arg1\t\t\"quoted\"\t  arg3");
        assert_eq!(
            result,
            vec![
                ("arg1".to_string(), false, false),
                ("quoted".to_string(), true, false),
                ("arg3".to_string(), false, false)
            ]
        );
    }

    #[test]
    fn test_tokenize_with_equals_basic() {
        let result = tokenize_with_equals("arg1 arg2");
        assert_eq!(result, vec!["arg1", "arg2"]);
    }

    #[test]
    fn test_tokenize_with_equals_quoted() {
        let result = tokenize_with_equals("\"quoted arg\" normal");
        assert_eq!(result, vec!["quoted arg", "normal"]);
    }

    #[test]
    fn test_tokenize_with_equals_key_value() {
        let result = tokenize_with_equals("key=value");
        assert_eq!(result, vec!["key=value"]);
    }

    #[test]
    fn test_tokenize_with_equals_quoted_after_equals() {
        let result = tokenize_with_equals("key=\"quoted value\"");
        assert_eq!(result, vec!["key=quoted value"]);
    }

    #[test]
    fn test_tokenize_with_equals_unclosed_quotes() {
        let result = tokenize_with_equals("\"unclosed");
        assert_eq!(result, vec!["unclosed"]);
    }

    #[test]
    fn test_find_first_equals_basic() {
        assert_eq!(find_first_equals_outside_quotes("key=value"), Some(3));
    }

    #[test]
    fn test_find_first_equals_no_equals() {
        assert_eq!(find_first_equals_outside_quotes("just_a_key"), None);
    }

    #[test]
    fn test_find_first_equals_quoted_key() {
        assert_eq!(
            find_first_equals_outside_quotes("\"quoted key\"=value"),
            Some(12)
        );
    }

    #[test]
    fn test_find_first_equals_equals_in_quotes() {
        assert_eq!(
            find_first_equals_outside_quotes("\"key=with=equals\""),
            None
        );
    }

    #[test]
    fn test_find_first_equals_multiple_equals() {
        assert_eq!(find_first_equals_outside_quotes("key=value=extra"), Some(3));
    }

    #[test]
    fn test_find_first_equals_escaped_quote() {
        assert_eq!(
            find_first_equals_outside_quotes("\"key\\\"with=quote\"=value"),
            Some(17)
        );
    }

    #[test]
    fn test_tokenize_with_equals_quotes_edge_cases() {
        // Empty string
        assert_eq!(
            tokenize_with_equals_and_quotes(""),
            Vec::<(String, bool, bool)>::new()
        );

        // Only whitespace
        assert_eq!(
            tokenize_with_equals_and_quotes("   \t  "),
            Vec::<(String, bool, bool)>::new()
        );

        // Only quotes
        assert_eq!(
            tokenize_with_equals_and_quotes("\"\""),
            vec![("".to_string(), true, false)]
        );

        // Quote in middle of token
        assert_eq!(
            tokenize_with_equals_and_quotes("arg\"with\"quote"),
            vec![("argwithquote".to_string(), false, false)]
        );
    }

    #[test]
    fn test_tokenize_other_escaped_chars() {
        let result = tokenize_with_equals_and_quotes("\"text\\nwith\\tescapes\"");
        assert_eq!(
            result,
            vec![("text\\nwith\\tescapes".to_string(), true, false)]
        );
    }
}
