#[cfg(test)]
#[allow(clippy::module_inception)]
mod honey_badger_tests {
    use super::super::arguments::parse_arguments;
    use super::super::parser::{extract_line_type, split_into_parts, MetaLine};

    #[test]
    #[ignore = "slow"] // Slow test: takes 210ms. Run with --ignored to include.
    fn test_extreme_nesting_levels() {
        // Test with 100 dots
        let extreme_dots = ".".repeat(100) + "div";
        let meta = MetaLine::parse(&extreme_dots, 0).unwrap();
        assert_eq!(meta.level, 100);
        assert_eq!(meta.type_name, "div");

        // Test with 1000 dots
        let very_extreme_dots = ".".repeat(1000) + "p";
        let meta = MetaLine::parse(&very_extreme_dots, 0).unwrap();
        assert_eq!(meta.level, 1000);
        assert_eq!(meta.type_name, "p");
    }

    #[test]
    fn test_null_bytes_and_control_characters() {
        // Null bytes in type name
        let meta = MetaLine::parse(".type\0name", 0).unwrap();
        assert_eq!(meta.type_name, "type\0name");

        // Control characters
        let meta = MetaLine::parse(".type\x01\x02\x03", 0).unwrap();
        assert_eq!(meta.type_name, "type\x01\x02\x03");

        // Bell, backspace, tab, etc.
        let meta = MetaLine::parse(".type\x07\x08\t\x0B", 0).unwrap();
        assert_eq!(meta.type_name, "type\x07\x08\t\x0B");
    }

    #[test]
    #[ignore = "slow"] // Slow test: takes 245ms. Run with --ignored to include.
    fn test_unicode_edge_cases() {
        // Zero-width spaces and joiners
        let meta = MetaLine::parse(".type\u{200B}\u{200C}\u{200D}", 0).unwrap();
        assert_eq!(meta.type_name, "type\u{200B}\u{200C}\u{200D}");

        // Right-to-left override
        let meta = MetaLine::parse(".‏עברית‏", 0).unwrap();
        assert_eq!(meta.type_name, "‏עברית‏");

        // Emoji and combining characters
        let meta = MetaLine::parse(".👨‍👩‍👧‍👦🏳️‍🌈", 0).unwrap();
        assert_eq!(meta.type_name, "👨‍👩‍👧‍👦🏳️‍🌈");

        // Mathematical symbols and other scripts
        let meta = MetaLine::parse(".∑∏∫日本語한글", 0).unwrap();
        assert_eq!(meta.type_name, "∑∏∫日本語한글");
    }

    #[test]
    #[ignore = "slow"] // Very slow test: takes 1181ms. Run with --ignored to include.
    fn test_extremely_long_lines() {
        // 10MB type name
        let huge_type = "a".repeat(10_000_000);
        let line = format!(".{huge_type}");
        let meta = MetaLine::parse(&line, 0).unwrap();
        assert_eq!(meta.type_name.len(), 10_000_000);

        // 10MB inline text
        let huge_text = "b".repeat(10_000_000);
        let line = format!(".div {huge_text}");
        let meta = MetaLine::parse(&line, 0).unwrap();
        assert_eq!(meta.inline_text.content.len(), 10_000_000);
    }

    #[test]
    fn test_quote_chaos() {
        // Unclosed quotes everywhere
        let meta = MetaLine::parse(".\"type", 0).unwrap();
        assert_eq!(meta.type_name, "\"type");

        // Mismatched quotes
        let meta = MetaLine::parse(".\"type' mixed quotes", 0).unwrap();
        assert_eq!(meta.type_name, "\"type'");
        assert_eq!(meta.inline_text.content, "mixed quotes");

        // Escaped quotes madness
        let meta = MetaLine::parse(".\"\\\"\\\"\\\"\" inline \"\\\"\\\"", 0).unwrap();
        assert_eq!(meta.type_name, "\"\"\"");
        assert_eq!(meta.inline_text.content, "inline \"\\\"\\\"");

        // Multiple quote levels - parser only handles one level of quotes
        let meta = MetaLine::parse(".\"outer \"inner\" outer\" text", 0).unwrap();
        assert_eq!(meta.type_name, "outer "); // Closes at first unescaped quote
        assert_eq!(meta.inline_text.content, "inner\" outer\" text");
    }

    #[test]
    #[ignore = "slow"] // Slow test: takes 227ms. Run with --ignored to include.
    fn test_double_dot_insanity() {
        // Many double dots - parser treats first part as inline text, rest as params
        let meta = MetaLine::parse(".type .. .. .. .. .. text", 0).unwrap();
        assert_eq!(meta.type_name, "type");
        assert_eq!(meta.inline_text.content, "");
        // The rest after first .. becomes arguments
        assert!(!meta.arguments.args.is_empty() || !meta.arguments.kwargs.is_empty());

        // Double dots in quotes - quotes are stripped when processing inline text
        let meta = MetaLine::parse(".type \"text..with..dots\" .. param", 0).unwrap();
        assert_eq!(meta.type_name, "type");
        // Quoted inline text has quotes stripped and escape sequences processed
        assert_eq!(meta.inline_text.content, "text..with..dots");

        // Mixed dots and content
        let line = ".type text1 .. param1 .. text2 .. param2";
        let meta = MetaLine::parse(line, 0).unwrap();
        assert_eq!(meta.type_name, "type");
        assert_eq!(meta.inline_text.content, "text1 ");
    }

    #[test]
    fn test_id_extraction_edge_cases() {
        // Multiple colons
        let meta = MetaLine::parse(".div :id:with:colons", 0).unwrap();
        assert_eq!(meta.id.as_str(), "id:with:colons");

        // ID with underscores and dashes (dots no longer allowed - reserved for document references)
        let meta = MetaLine::parse(".div :id_sub-section_2", 0).unwrap();
        assert_eq!(meta.id.as_str(), "id_sub-section_2");

        // Empty ID after colon
        let meta = MetaLine::parse(".div :", 0).unwrap();
        assert_eq!(meta.id.as_str(), "");

        // ID with spaces before and after
        let meta = MetaLine::parse(".div     :id     ", 0).unwrap();
        assert_eq!(meta.id.as_str(), "id");

        // Very long ID
        let long_id = "a".repeat(10000);
        let line = format!(".div :{long_id}");
        let meta = MetaLine::parse(&line, 0).unwrap();
        assert_eq!(meta.id.value.len(), 10000);
    }

    #[test]
    fn test_mixed_parameter_chaos() {
        // Parameters with special characters
        let meta = MetaLine::parse(
            ".div .. key=\"value with \\\"quotes\\\"\" key2=@#$%^&*()",
            0,
        )
        .unwrap();
        assert_eq!(meta.arguments.kwargs.len(), 2);

        // Empty parameters
        let meta = MetaLine::parse(".div .. \"\" key=\"\" \"\"", 0).unwrap();
        assert_eq!(meta.arguments.args.len(), 2);
        assert_eq!(meta.arguments.kwargs.len(), 1);

        // Parameters with equals signs
        let meta = MetaLine::parse(".div .. key=value=with=equals", 0).unwrap();
        assert_eq!(meta.arguments.kwargs.len(), 1);
        assert_eq!(meta.arguments.kwargs[0].0.content, "key");
        assert_eq!(meta.arguments.kwargs[0].1.content, "value=with=equals");
    }

    #[test]
    fn test_whitespace_variations() {
        // Tabs instead of spaces - tabs are NOT treated as space separators
        let meta = MetaLine::parse(".div\ttext\t..\tparam", 0).unwrap();
        assert_eq!(meta.type_name, "div\ttext\t..\tparam"); // Everything is the type name
        assert_eq!(meta.inline_text.content, "");

        // Mixed whitespace
        let meta = MetaLine::parse(".div \t \t text", 0).unwrap();
        assert_eq!(meta.type_name, "div");
        assert_eq!(meta.inline_text.content, "\t \t text");

        // Non-breaking spaces - treated as part of type name
        let meta = MetaLine::parse(".div\u{00A0}text", 0).unwrap();
        assert_eq!(meta.type_name, "div\u{00A0}text"); // Non-breaking space is not a separator
        assert_eq!(meta.inline_text.content, "");
    }

    #[test]
    fn test_parser_memory_safety() {
        // Empty string after dots
        let meta = MetaLine::parse(".", 0).unwrap();
        assert_eq!(meta.type_name, "");
        assert_eq!(meta.level, 1);

        // Just dots, no type
        let meta = MetaLine::parse(".....", 0).unwrap();
        assert_eq!(meta.type_name, "");
        assert_eq!(meta.level, 5);

        // Type with no content
        let meta = MetaLine::parse(".type", 0).unwrap();
        assert_eq!(meta.type_name, "type");
        assert_eq!(meta.inline_text.content, "");
    }

    #[test]
    fn test_recursive_quote_escaping() {
        // Multiple levels of escaping
        let input = ".type \"\\\\\\\"nested\\\\\\\"\"";
        let meta = MetaLine::parse(input, 0).unwrap();
        assert_eq!(meta.type_name, "type");

        // Backslashes at end of quoted string - actually closes the quote properly
        let meta = MetaLine::parse(".\"type\\\\\"", 0).unwrap();
        assert_eq!(meta.type_name, "type\\"); // Quote is actually closed
    }

    #[test]
    fn test_boundary_conditions() {
        // Line with exactly one character
        let meta = MetaLine::parse(".", 0).unwrap();
        assert_eq!(meta.level, 1);

        // Maximum representable line index
        let meta = MetaLine::parse(".type", usize::MAX).unwrap();
        assert_eq!(meta.line_index, Some(usize::MAX));

        // Empty inline text with parameters
        let meta = MetaLine::parse(".type .. param", 0).unwrap();
        assert_eq!(meta.inline_text.content, "");
        assert_eq!(meta.arguments.args.len(), 1);
    }

    #[test]
    fn test_split_into_parts_edge_cases() {
        // Consecutive dots - only double dots (..) split
        let parts = split_into_parts("....text");
        assert_eq!(parts.len(), 3);
        assert_eq!(parts[0], "");
        assert_eq!(parts[1], "");
        assert_eq!(parts[2], "text");

        // Dots at boundaries
        let parts = split_into_parts("..text..");
        assert_eq!(parts.len(), 3);
        assert_eq!(parts[0], "");
        assert_eq!(parts[1], "text");
        assert_eq!(parts[2], "");

        // Escaped characters outside quotes - backslash doesn't escape outside quotes
        let parts = split_into_parts("text\\..more");
        assert_eq!(parts.len(), 2);
        assert_eq!(parts[0], "text\\");
        assert_eq!(parts[1], "more");
    }

    #[test]
    #[ignore = "slow"] // Slow test: takes 225ms. Run with --ignored to include.
    fn test_extract_line_type_edge_cases() {
        // Empty after dot
        let (t, r, _is_void) = extract_line_type(".");
        assert_eq!(t, "");
        assert_eq!(r, "");

        // Just quoted type, no closing quote
        let (t, r, _is_void) = extract_line_type(".\"unclosed");
        assert_eq!(t, "\"unclosed");
        assert_eq!(r, "");

        // Escaped quote at end - unclosed quote means we take everything as-is
        let (t, r, _is_void) = extract_line_type(".\"type\\\"");
        assert_eq!(t, "\"type\\\""); // Unclosed quote, so whole thing is type
        assert_eq!(r, "");
    }

    #[test]
    #[ignore = "slow"] // Slow test: takes 218ms. Run with --ignored to include.
    fn test_argument_parser_edge_cases() {
        // Empty string
        let args = parse_arguments("");
        assert_eq!(args.args.len(), 0);
        assert_eq!(args.kwargs.len(), 0);

        // Just spaces
        let args = parse_arguments("   ");
        assert_eq!(args.args.len(), 0);
        assert_eq!(args.kwargs.len(), 0);

        // Key without value
        let args = parse_arguments("key=");
        assert_eq!(args.kwargs.len(), 1);
        assert_eq!(args.kwargs[0].1.content, "");

        // Multiple equals
        let args = parse_arguments("a=b=c=d");
        assert_eq!(args.kwargs.len(), 1);
        assert_eq!(args.kwargs[0].0.content, "a");
        assert_eq!(args.kwargs[0].1.content, "b=c=d");
    }

    #[test]
    fn test_special_dot_patterns() {
        // Dots in type name (quoted)
        let meta = MetaLine::parse(".\".type.with.dots\"", 0).unwrap();
        assert_eq!(meta.type_name, ".type.with.dots");

        // Mixed dots and content
        let meta = MetaLine::parse("...\"..\" text .. args", 0).unwrap();
        assert_eq!(meta.level, 3);
        assert_eq!(meta.type_name, "..");
        assert_eq!(meta.inline_text.content, "text ");
    }

    #[test]
    #[ignore = "slow"] // Slow test: takes 231ms. Run with --ignored to include.
    fn test_parser_resilience() {
        // Parser should never panic, just return None or handle gracefully
        assert!(MetaLine::parse("", 0).is_none());
        assert!(MetaLine::parse("no dot at start", 0).is_none());

        // These should parse without panic
        let _ = MetaLine::parse(".".repeat(10_000).as_str(), 0);
        let _ = MetaLine::parse(&format!(".{}", "\0".repeat(1000)), 0);
        let _ = MetaLine::parse(&format!(".{}", "🦡".repeat(1000)), 0); // honey badger emoji!
    }

    #[test]
    fn test_combined_chaos() {
        // Everything at once - note: dots are NOT allowed in IDs (reserved for document references)
        // so we use underscores instead
        let chaos = r#"...."τύπος😈"   "text with \"quotes\" and .. dots"  ..  arg1 "arg 2" key="val\"ue" 🔥=🌊  :id_with-everything_123"#;
        let meta = MetaLine::parse(chaos, 0).unwrap();
        assert_eq!(meta.level, 4); // 4 dots
        assert_eq!(meta.type_name, "τύπος😈");
        assert!(meta.inline_text.content.contains("quotes"));
        assert!(!meta.id.is_empty());
        assert!(!meta.arguments.args.is_empty());
        assert!(!meta.arguments.kwargs.is_empty());
    }
}
