use rumdl_lib::rule::Rule;
use rumdl_lib::rules::MD045NoAltText;
#[test]
fn test_valid_alt_text() {
let rule = MD045NoAltText::new();
let content = "\n";
let ctx = rumdl_lib::lint_context::LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(result.is_empty());
}
#[test]
fn test_missing_alt_text() {
let rule = MD045NoAltText::new();
let content = "";
let ctx = rumdl_lib::lint_context::LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1);
assert!(result[0].fix.is_none(), "MD045 should not offer auto-fix");
}
#[test]
fn test_empty_alt_text() {
let rule = MD045NoAltText::new();
let content = "";
let ctx = rumdl_lib::lint_context::LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1);
assert!(result[0].fix.is_none());
}
#[test]
fn test_multiple_images() {
let rule = MD045NoAltText::new();
let content = "\n\n";
let ctx = rumdl_lib::lint_context::LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 2);
let fixed = rule.fix(&ctx).unwrap();
assert_eq!(fixed, content);
}
#[test]
fn test_complex_urls() {
let rule = MD045NoAltText::new();
let content = "";
let ctx = rumdl_lib::lint_context::LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1);
}
#[test]
fn test_mixed_content() {
let rule = MD045NoAltText::new();
let content = "# Images\n\nSome text here\n\n\n\nMore text\n\n";
let ctx = rumdl_lib::lint_context::LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1);
assert_eq!(result[0].line, 9);
}
#[test]
fn test_inline_images() {
let rule = MD045NoAltText::new();
let content = "Text with  and  images.";
let ctx = rumdl_lib::lint_context::LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1);
}
#[test]
fn test_images_in_code_blocks() {
let rule = MD045NoAltText::new();
let content = r#"# Documentation
Here's an actual image:

Here's how to use images in markdown:
```markdown



```
Another actual image:

And in inline code: `` should also be ignored.
"#;
let ctx = rumdl_lib::lint_context::LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 2, "Should only detect images outside code blocks");
assert_eq!(result[0].line, 4); assert_eq!(result[1].line, 15); }
#[test]
fn test_obsidian_wikilink_image_without_alt() {
let rule = MD045NoAltText::new();
let content = "# Test\n\n![[screenshot.png]]";
let ctx = rumdl_lib::lint_context::LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Obsidian, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Wikilink image without alt text should warn");
assert_eq!(result[0].line, 3);
}
#[test]
fn test_obsidian_wikilink_image_with_alt() {
let rule = MD045NoAltText::new();
let content = "# Test\n\n![[screenshot.png|Screenshot of the app]]";
let ctx = rumdl_lib::lint_context::LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Obsidian, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Wikilink image with pipe alt text should not warn, got: {result:?}"
);
}
#[test]
fn test_obsidian_wikilink_image_with_whitespace_only_alt() {
let rule = MD045NoAltText::new();
let content = "![[image.png| ]]";
let ctx = rumdl_lib::lint_context::LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Obsidian, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Wikilink image with whitespace-only alt should warn");
}
#[test]
fn test_obsidian_wikilink_image_pipe_in_filename() {
let rule = MD045NoAltText::new();
let content = "![[image.png|]]";
let ctx = rumdl_lib::lint_context::LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Obsidian, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Wikilink image with pipe but no alt text should warn");
}
#[test]
fn test_obsidian_mixed_image_styles() {
let rule = MD045NoAltText::new();
let content = "\n![[wikilink-no-alt.png]]\n![[wikilink-with-alt.png|Alt text]]\n";
let ctx = rumdl_lib::lint_context::LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Obsidian, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
2,
"Should warn on wikilink without alt and standard without alt, got: {result:?}"
);
}
#[test]
fn test_obsidian_wikilink_image_with_path() {
let rule = MD045NoAltText::new();
let content = "![[subfolder/image.png|A descriptive alt]]";
let ctx = rumdl_lib::lint_context::LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Obsidian, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Wikilink image with path and alt text should not warn"
);
}
#[test]
fn test_obsidian_wikilink_image_in_code_block() {
let rule = MD045NoAltText::new();
let content = "```\n![[in-code.png]]\n```\n\n![[outside-code.png]]";
let ctx = rumdl_lib::lint_context::LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Obsidian, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Only the image outside the code block should warn");
assert_eq!(result[0].line, 5);
}
#[test]
fn test_standard_flavor_wikilink_image_still_detected() {
let rule = MD045NoAltText::new();
let content = "![[image.png]]";
let ctx = rumdl_lib::lint_context::LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
1,
"Wikilink image without alt should warn in Standard flavor"
);
let content_with_alt = "![[image.png|Alt text here]]";
let ctx2 =
rumdl_lib::lint_context::LintContext::new(content_with_alt, rumdl_lib::config::MarkdownFlavor::Standard, None);
let result2 = rule.check(&ctx2).unwrap();
assert!(
result2.is_empty(),
"Wikilink image with alt should not warn in Standard flavor, got: {result2:?}"
);
}
#[test]
fn test_obsidian_wikilink_non_image_link_ignored() {
let rule = MD045NoAltText::new();
let content = "# Test\n\n[[some-page]]\n[[another|Display text]]";
let ctx = rumdl_lib::lint_context::LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Obsidian, None);
let result = rule.check(&ctx).unwrap();
assert!(result.is_empty(), "Non-image wikilinks should not trigger MD045");
}
#[test]
fn test_descriptive_filenames_not_used_for_alt() {
let rule = MD045NoAltText::new();
let content = "\n\n";
let ctx = rumdl_lib::lint_context::LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 3);
let fixed = rule.fix(&ctx).unwrap();
assert_eq!(fixed, content, "MD045 should not modify content (diagnostic-only)");
}