1mod canonical;
14pub mod helpers;
15pub mod jsonld;
16pub(crate) mod lang;
17mod robots;
18mod seo_plugin;
19
20pub use canonical::CanonicalPlugin;
21pub use jsonld::{
22 validate_jsonld, JsonLdConfig, JsonLdPlugin, JsonLdValidationError,
23};
24pub use robots::RobotsPlugin;
25pub use seo_plugin::SeoPlugin;
26
27#[cfg(test)]
28mod tests {
29 use super::helpers::*;
30 use super::*;
31 use crate::plugin::{Plugin, PluginContext};
32 use std::fs;
33 use std::path::Path;
34 use tempfile::tempdir;
35
36 fn make_html(title: &str, body: &str) -> String {
37 format!(
38 "<html><head><title>{title}</title></head>\
39 <body>{body}</body></html>"
40 )
41 }
42
43 fn test_ctx(site_dir: &Path) -> PluginContext {
44 crate::test_support::init_logger();
45 PluginContext::new(
46 Path::new("content"),
47 Path::new("build"),
48 site_dir,
49 Path::new("templates"),
50 )
51 }
52
53 #[test]
58 fn test_extract_title_present() {
59 let html = "<html><head><title>My Page</title></head></html>";
60 assert_eq!(extract_title(html), "My Page");
61 }
62
63 #[test]
64 fn test_extract_title_missing() {
65 let html = "<html><head></head><body></body></html>";
66 assert_eq!(extract_title(html), "");
67 }
68
69 #[test]
70 fn test_extract_description_truncates() {
71 let long = "word ".repeat(100);
72 let html =
73 format!("<html><head></head><body><p>{long}</p></body></html>");
74 let desc = extract_description(&html, 160);
75 assert!(desc.len() <= 160);
76 assert!(!desc.is_empty());
77 }
78
79 #[test]
84 fn test_seo_plugin_name() {
85 assert_eq!(SeoPlugin.name(), "seo");
86 }
87
88 #[test]
89 fn test_seo_plugin_injects_meta_tags() {
90 let tmp = tempdir().unwrap();
91 let ctx = test_ctx(tmp.path());
92 let html = make_html("Hello World", "<p>Some content here</p>");
93
94 let result = SeoPlugin
95 .transform_html(&html, Path::new("index.html"), &ctx)
96 .unwrap();
97 assert!(result.contains("<meta name=\"description\""));
98 assert!(result.contains("<meta property=\"og:title\""));
99 assert!(result.contains("Hello World"));
100 assert!(result.contains("<meta property=\"og:description\""));
101 assert!(
102 result.contains("<meta property=\"og:type\" content=\"website\"")
103 );
104 assert!(
105 result.contains("<meta name=\"twitter:card\" content=\"summary\"")
106 );
107 }
108
109 #[test]
110 fn test_seo_plugin_idempotent() {
111 let tmp = tempdir().unwrap();
112 let ctx = test_ctx(tmp.path());
113 let html = make_html("Test", "<p>Content</p>");
114
115 let first = SeoPlugin
116 .transform_html(&html, Path::new("page.html"), &ctx)
117 .unwrap();
118 let second = SeoPlugin
119 .transform_html(&first, Path::new("page.html"), &ctx)
120 .unwrap();
121
122 assert_eq!(first, second);
123 }
124
125 #[test]
126 fn test_extract_description_excludes_nav_header_footer() {
127 let html = r##"<html><head></head><body>
128 <a href="#main">Skip to content</a>
129 <nav><ul><li>Home</li><li>About</li><li>Search</li></ul></nav>
130 <header><h1>Site Header</h1></header>
131 <main><p>This is the actual page content that should be extracted.</p></main>
132 <footer><p>Copyright 2026</p></footer>
133 </body></html>"##;
134 let desc = extract_description(html, 160);
135 assert!(
136 desc.contains("actual page content"),
137 "description should contain main content, got: {desc}"
138 );
139 assert!(
140 !desc.contains("Skip to content"),
141 "description should not contain skip link text"
142 );
143 assert!(
144 !desc.contains("Site Header"),
145 "description should not contain header text"
146 );
147 assert!(
148 !desc.contains("Copyright"),
149 "description should not contain footer text"
150 );
151 }
152
153 #[test]
154 fn test_seo_plugin_handles_missing_title() {
155 let tmp = tempdir().unwrap();
156 let ctx = test_ctx(tmp.path());
157 let html =
158 "<html><head></head><body><p>No title here</p></body></html>";
159
160 let result = SeoPlugin
161 .transform_html(html, Path::new("no-title.html"), &ctx)
162 .unwrap();
163 assert!(result.contains("<meta property=\"og:type\""));
165 assert!(result.contains("<meta name=\"twitter:card\""));
166 assert!(!result.contains("<meta property=\"og:title\""));
168 }
169
170 #[test]
171 fn test_seo_plugin_empty_dir() {
172 let tmp = tempdir().unwrap();
173 let ctx = test_ctx(tmp.path());
174 assert!(SeoPlugin.after_compile(&ctx).is_ok());
175 }
176
177 #[test]
178 fn test_seo_plugin_nonexistent_dir() {
179 let ctx = test_ctx(Path::new("/nonexistent/path"));
180 assert!(SeoPlugin.after_compile(&ctx).is_ok());
181 }
182
183 #[test]
188 fn test_robots_plugin_name() {
189 let plugin = RobotsPlugin::new("https://example.com");
190 assert_eq!(plugin.name(), "robots");
191 }
192
193 #[test]
194 fn test_robots_plugin_creates_file() {
195 let tmp = tempdir().unwrap();
196 let ctx = test_ctx(tmp.path());
197 let plugin = RobotsPlugin::new("https://example.com");
198 plugin.after_compile(&ctx).unwrap();
199
200 let path = tmp.path().join("robots.txt");
201 assert!(path.exists());
202 }
203
204 #[test]
205 fn test_robots_plugin_correct_content() {
206 let tmp = tempdir().unwrap();
207 let ctx = test_ctx(tmp.path());
208 let plugin = RobotsPlugin::new("https://example.com");
209 plugin.after_compile(&ctx).unwrap();
210
211 let content =
212 fs::read_to_string(tmp.path().join("robots.txt")).unwrap();
213 assert!(content.contains("User-agent: *"));
214 assert!(content.contains("Allow: /"));
215 assert!(content.contains("Sitemap: https://example.com/sitemap.xml"));
216 }
217
218 #[test]
219 fn test_robots_plugin_does_not_overwrite() {
220 let tmp = tempdir().unwrap();
221 let robots_path = tmp.path().join("robots.txt");
222 fs::write(&robots_path, "User-agent: *\nDisallow: /secret\n").unwrap();
223
224 let ctx = test_ctx(tmp.path());
225 let plugin = RobotsPlugin::new("https://example.com");
226 plugin.after_compile(&ctx).unwrap();
227
228 let content = fs::read_to_string(&robots_path).unwrap();
229 assert!(content.contains("Disallow: /secret"));
230 assert!(!content.contains("Sitemap:"));
231 }
232
233 #[test]
234 fn test_robots_plugin_custom_base_url() {
235 let tmp = tempdir().unwrap();
236 let ctx = test_ctx(tmp.path());
237 let plugin = RobotsPlugin::new("https://my-site.org");
238 plugin.after_compile(&ctx).unwrap();
239
240 let content =
241 fs::read_to_string(tmp.path().join("robots.txt")).unwrap();
242 assert!(content.contains("Sitemap: https://my-site.org/sitemap.xml"));
243 }
244
245 #[test]
250 fn test_canonical_plugin_name() {
251 let plugin = CanonicalPlugin::new("https://example.com");
252 assert_eq!(plugin.name(), "canonical");
253 }
254
255 #[test]
256 fn test_canonical_plugin_injects_tag() {
257 let tmp = tempdir().unwrap();
258 let ctx = test_ctx(tmp.path());
259 let plugin = CanonicalPlugin::new("https://example.com");
260 let html = make_html("Home", "<p>Welcome</p>");
261 let page_path = tmp.path().join("index.html");
262
263 let result = plugin.transform_html(&html, &page_path, &ctx).unwrap();
264 assert!(result.contains("<link rel=\"canonical\""));
265 assert!(result.contains("href=\"https://example.com/\""));
270 }
271
272 #[test]
273 fn test_canonical_plugin_idempotent() {
274 let tmp = tempdir().unwrap();
275 let ctx = test_ctx(tmp.path());
276 let plugin = CanonicalPlugin::new("https://example.com");
277 let html = make_html("Page", "<p>Content</p>");
278 let page_path = tmp.path().join("page.html");
279
280 let first = plugin.transform_html(&html, &page_path, &ctx).unwrap();
281 let second = plugin.transform_html(&first, &page_path, &ctx).unwrap();
282
283 assert_eq!(first, second);
284 }
285
286 #[test]
287 fn test_canonical_plugin_nested_files() {
288 let tmp = tempdir().unwrap();
289 fs::create_dir_all(tmp.path().join("blog")).unwrap();
290 let ctx = test_ctx(tmp.path());
291 let plugin = CanonicalPlugin::new("https://example.com");
292 let html = make_html("Post", "<p>Blog post</p>");
293 let page_path = tmp.path().join("blog/post.html");
294
295 let result = plugin.transform_html(&html, &page_path, &ctx).unwrap();
296 assert!(result.contains("https://example.com/blog/post.html"));
297 }
298
299 #[test]
304 fn test_all_plugins_register() {
305 use crate::plugin::PluginManager;
306 let mut pm = PluginManager::new();
307 pm.register(SeoPlugin);
308 pm.register(RobotsPlugin::new("https://example.com"));
309 pm.register(CanonicalPlugin::new("https://example.com"));
310 assert_eq!(pm.len(), 3);
311 assert_eq!(pm.names(), vec!["seo", "robots", "canonical"]);
312 }
313
314 #[test]
319 fn extract_description_unicode_truncation_respects_char_boundary() {
320 let text = "café 日本語 ".repeat(30);
322 let html =
323 format!("<html><head></head><body><p>{text}</p></body></html>");
324
325 let desc = extract_description(&html, 50);
327
328 assert!(desc.len() <= 50);
330 assert!(!desc.is_empty());
331 let _ = desc.chars().count();
333 }
334
335 #[test]
336 fn extract_description_empty_main_falls_back_to_body() {
337 let html = "<html><head></head><body>\
339 <main></main>\
340 <p>Body fallback text</p>\
341 </body></html>";
342
343 let desc = extract_description(html, 160);
345
346 assert!(
348 desc.is_empty(),
349 "expected empty description from empty <main>, got: {desc}"
350 );
351 }
352
353 #[test]
354 fn extract_description_no_body_uses_raw_html() {
355 let html = "<div><p>Raw content without body</p></div>";
357
358 let desc = extract_description(html, 160);
360
361 assert!(
363 desc.contains("Raw content without body"),
364 "expected raw content fallback, got: {desc}"
365 );
366 }
367
368 #[test]
369 fn extract_title_with_nested_tags() {
370 let html = "<html><head><title><span>Foo</span></title></head></html>";
372
373 let title = extract_title(html);
375
376 assert_eq!(title, "Foo");
378 }
379
380 #[test]
381 fn escape_attr_all_special_chars() {
382 let input = r#"Tom & "Jerry" <script>alert('xss')</script>"#;
384
385 let escaped = escape_attr(input);
387
388 assert!(escaped.contains("&"), "& should be escaped");
390 assert!(escaped.contains("""), "\" should be escaped");
391 assert!(escaped.contains("<"), "< should be escaped");
392 assert!(escaped.contains(">"), "> should be escaped");
393 assert_eq!(
394 escaped,
395 "Tom & "Jerry" <script>alert('xss')</script>"
396 );
397 }
398
399 #[test]
400 fn seo_plugin_skips_existing_single_quote_meta() {
401 let html = "<html><head>\
403 <meta name='description' content='Already set'>\
404 <meta property='og:title' content='Title'>\
405 <meta property='og:description' content='Desc'>\
406 <meta property='og:type' content='website'>\
407 <meta name='twitter:card' content='summary'>\
408 <title>Test</title></head>\
409 <body><p>Content</p></body></html>";
410 let tmp = tempdir().unwrap();
411 let ctx = test_ctx(tmp.path());
412
413 let result = SeoPlugin
415 .transform_html(html, Path::new("single-quote.html"), &ctx)
416 .unwrap();
417 assert_eq!(
418 result.matches("meta name=\"description\"").count()
419 + result.matches("meta name='description'").count(),
420 1,
421 "description meta should not be duplicated"
422 );
423 assert_eq!(
424 result.matches("og:title").count(),
425 1,
426 "og:title should not be duplicated"
427 );
428 }
429
430 #[test]
431 fn canonical_plugin_trailing_slash_base_url() {
432 let tmp = tempdir().unwrap();
433 let ctx = test_ctx(tmp.path());
434 let plugin = CanonicalPlugin::new("https://example.com/");
435 let html = make_html("Home", "<p>Welcome</p>");
436 let page_path = tmp.path().join("index.html");
437
438 let result = plugin.transform_html(&html, &page_path, &ctx).unwrap();
439 assert!(
440 result.contains("href=\"https://example.com/\""),
444 "should produce clean URL without double slash"
445 );
446 assert!(
447 !result.contains("https://example.com//"),
448 "should not contain double slash in canonical URL"
449 );
450 }
451
452 #[test]
453 fn robots_plugin_trailing_slash_base_url() {
454 let tmp = tempdir().unwrap();
456 let ctx = test_ctx(tmp.path());
457 let plugin = RobotsPlugin::new("https://example.com/");
458
459 plugin.after_compile(&ctx).unwrap();
461
462 let content =
464 fs::read_to_string(tmp.path().join("robots.txt")).unwrap();
465 assert!(
466 content.contains("Sitemap: https://example.com/sitemap.xml"),
467 "sitemap URL should not have double slash, got: {content}"
468 );
469 assert!(
470 !content.contains("https://example.com//"),
471 "should not contain double slash"
472 );
473 }
474
475 #[test]
476 fn extract_description_nested_script_in_main() {
477 let html = "<html><head></head><body>\
479 <main>\
480 <script>var x = 'ignore me';</script>\
481 <p>Visible text after script</p>\
482 </main></body></html>";
483
484 let desc = extract_description(html, 160);
486
487 assert!(
489 desc.contains("Visible text after script"),
490 "should contain the paragraph text, got: {desc}"
491 );
492 assert!(
493 !desc.contains("ignore me"),
494 "should not contain script content, got: {desc}"
495 );
496 }
497
498 #[test]
503 fn test_jsonld_injects_webpage() {
504 let dir = tempdir().unwrap();
505 let site = dir.path().join("site");
506 fs::create_dir_all(&site).unwrap();
507
508 let html = make_html("About", "<p>About us</p>");
509 let ctx = test_ctx(&site);
510 let plugin = JsonLdPlugin::from_site("https://example.com", "Test Org");
511 let page_path = site.join("about.html");
512
513 let output = plugin.transform_html(&html, &page_path, &ctx).unwrap();
514 assert!(output.contains("application/ld+json"));
515 assert!(output.contains("\"@type\":\"WebPage\""));
516 assert!(output.contains("\"name\":\"About\""));
517 }
518
519 #[test]
520 fn test_jsonld_injects_article() {
521 let dir = tempdir().unwrap();
522 let site = dir.path().join("site");
523 fs::create_dir_all(&site).unwrap();
524
525 let html = "<html><head><title>Post</title></head>\
526 <body><article><h1>Post</h1></article></body></html>";
527 let ctx = test_ctx(&site);
528 let plugin = JsonLdPlugin::from_site("https://example.com", "My Org");
529 let page_path = site.join("post.html");
530
531 let output = plugin.transform_html(html, &page_path, &ctx).unwrap();
532 assert!(output.contains("\"@type\":\"Article\""));
533 assert!(output.contains("\"headline\":\"Post\""));
534 assert!(output.contains("My Org"));
535 }
536
537 #[test]
538 fn test_jsonld_breadcrumbs() {
539 let dir = tempdir().unwrap();
540 let site = dir.path().join("site");
541 let blog = site.join("blog");
542 fs::create_dir_all(&blog).unwrap();
543
544 let html = make_html("My Post", "<p>Content</p>");
545 let ctx = test_ctx(&site);
546 let plugin = JsonLdPlugin::from_site("https://example.com", "Org");
547 let page_path = blog.join("my-post.html");
548
549 let output = plugin.transform_html(&html, &page_path, &ctx).unwrap();
550 assert!(output.contains("BreadcrumbList"));
551 assert!(output.contains("\"name\":\"Home\""));
552 assert!(output.contains("\"name\":\"blog\""));
553 }
554
555 #[test]
556 fn test_jsonld_idempotent() {
557 let dir = tempdir().unwrap();
558 let site = dir.path().join("site");
559 fs::create_dir_all(&site).unwrap();
560
561 let html = "<html><head><title>X</title>\
562 <script type=\"application/ld+json\">{}</script>\
563 </head><body></body></html>";
564 let ctx = test_ctx(&site);
565 let plugin = JsonLdPlugin::from_site("https://example.com", "Org");
566 let page_path = site.join("x.html");
567
568 let output = plugin.transform_html(html, &page_path, &ctx).unwrap();
569 let count = output.matches("application/ld+json").count();
571 assert_eq!(count, 1);
572 }
573
574 #[test]
579 fn extract_title_empty_tag_returns_empty_string() {
580 assert_eq!(extract_title("<title></title>"), "");
581 assert_eq!(extract_title("<title> </title>"), "");
582 assert_eq!(extract_title("<title>\n\t </title>"), "");
583 }
584
585 #[test]
586 fn extract_title_without_closing_tag_consumes_remainder() {
587 assert_eq!(extract_title("<title>Unterminated"), "Unterminated");
592 }
593
594 #[test]
595 fn extract_title_strips_inner_html_tags() {
596 let out = extract_title("<title>Hello <em>World</em></title>");
597 assert!(out.contains("Hello"));
598 assert!(out.contains("World"));
599 }
600
601 #[test]
606 fn extract_description_prefers_main_over_body() {
607 let html = r"<html><head></head><body>
608 <nav>menu</nav>
609 <main>The primary content.</main>
610 <footer>Bottom</footer>
611 </body></html>";
612 let desc = extract_description(html, 200);
613 assert!(desc.contains("primary content"));
614 assert!(!desc.contains("menu"));
615 }
616
617 #[test]
618 fn extract_description_main_without_closing_tag_takes_rest() {
619 let html = r"<html><body><main>content without close";
620 let desc = extract_description(html, 200);
621 assert!(desc.contains("content without close"));
622 }
623
624 #[test]
625 fn extract_description_main_without_angle_bracket_returns_empty_fallback() {
626 let html = "<html><body><main";
627 let desc = extract_description(html, 200);
628 assert_eq!(desc, "");
629 }
630
631 #[test]
632 fn extract_description_fallback_to_body_strips_script_and_style() {
633 let html = r"<html><head></head><body>
634 <script>alert('skip');</script>
635 <style>body { color: red; }</style>
636 <nav>menu items here</nav>
637 <header>site title</header>
638 <p>The body text.</p>
639 <footer>copyright</footer>
640 </body></html>";
641 let desc = extract_description(html, 200);
642 assert!(desc.contains("body text"));
643 assert!(!desc.contains("alert"));
644 assert!(!desc.contains("color: red"));
645 assert!(!desc.contains("menu items"));
646 assert!(!desc.contains("site title"));
647 assert!(!desc.contains("copyright"));
648 }
649
650 #[test]
651 fn extract_description_body_without_closing_tag_uses_rest() {
652 let html = "<html><body><p>open-ended body paragraph";
653 let desc = extract_description(html, 200);
654 assert!(desc.contains("open-ended body paragraph"));
655 }
656
657 #[test]
658 fn extract_description_body_without_angle_bracket_returns_empty() {
659 let html = "<html><body";
660 let desc = extract_description(html, 200);
661 assert_eq!(desc, "");
662 }
663
664 #[test]
665 fn extract_description_no_body_no_main_uses_entire_html() {
666 let html = "just plain text no tags here";
667 let desc = extract_description(html, 200);
668 assert!(desc.contains("just plain text"));
669 }
670
671 #[test]
672 fn extract_description_unterminated_script_breaks_out() {
673 let html = "<html><body><main><script>unterminated<p>x</p>";
674 let desc = extract_description(html, 200);
675 let _ = desc;
676 }
677
678 #[test]
679 fn extract_description_truncates_at_word_boundary() {
680 let html = "<html><body><main>one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty twenty-one twenty-two twenty-three twenty-four twenty-five</main></body></html>";
681 let desc = extract_description(html, 80);
682 assert!(desc.len() <= 80);
683 assert!(!desc.ends_with('-'));
684 }
685
686 #[test]
687 fn extract_description_truncates_without_space_falls_to_byte_cut() {
688 let html =
689 "<html><body><main>oneverylongwordwithnospacesanywherehere</main></body></html>";
690 let desc = extract_description(html, 10);
691 assert!(desc.len() <= 10);
692 }
693
694 #[test]
695 fn extract_description_respects_char_boundary_on_truncation() {
696 let html = "<html><body><main>Rust programming — é ñ ü characters everywhere in this text that we want to truncate mid-char</main></body></html>";
697 let desc = extract_description(html, 30);
698 assert!(desc.is_ascii() || !desc.is_empty());
699 }
700
701 #[test]
702 fn extract_description_truncation_walks_back_multiple_bytes() {
703 let mut input = String::from("<html><body><main>");
704 input.push_str(&"a".repeat(20));
705 input.push('🎉'); input.push_str(&"b".repeat(20));
707 input.push_str("</main></body></html>");
708 let desc = extract_description(&input, 22);
709 assert!(!desc.is_empty(), "expected non-empty desc");
710 let _ = desc.len();
711 }
712
713 #[test]
714 fn extract_description_body_fallback_unterminated_nav_breaks() {
715 let html = "<html><body><nav>unterminated nav block<p>visible</p>";
716 let desc = extract_description(html, 200);
717 let _ = desc;
718 }
719
720 #[test]
725 fn seo_plugin_file_without_head_tag_is_unchanged() {
726 let dir = tempdir().unwrap();
727 fs::write(
728 dir.path().join("fragment.html"),
729 "<p>no html/head/body structure</p>",
730 )
731 .unwrap();
732 let ctx = test_ctx(dir.path());
733 SeoPlugin.after_compile(&ctx).unwrap();
734 let out = fs::read_to_string(dir.path().join("fragment.html")).unwrap();
735 assert_eq!(out, "<p>no html/head/body structure</p>");
736 }
737
738 #[test]
739 fn seo_plugin_missing_site_dir_returns_ok() {
740 let dir = tempdir().unwrap();
741 let missing = dir.path().join("missing");
742 let ctx = test_ctx(&missing);
743 SeoPlugin.after_compile(&ctx).unwrap();
744 }
745
746 #[test]
751 fn robots_plugin_skips_existing_robots_txt() {
752 let dir = tempdir().unwrap();
753 let existing = dir.path().join("robots.txt");
754 fs::write(&existing, "USER: existing").unwrap();
755
756 let plugin = RobotsPlugin::new("https://example.com");
757 let ctx = test_ctx(dir.path());
758 plugin.after_compile(&ctx).unwrap();
759
760 assert_eq!(fs::read_to_string(&existing).unwrap(), "USER: existing");
761 }
762
763 #[test]
764 fn robots_plugin_writes_user_agent_and_sitemap() {
765 let dir = tempdir().unwrap();
766 let plugin = RobotsPlugin::new("https://example.com/");
767 let ctx = test_ctx(dir.path());
768 plugin.after_compile(&ctx).unwrap();
769
770 let body = fs::read_to_string(dir.path().join("robots.txt")).unwrap();
771 assert!(body.contains("User-agent: *"));
772 assert!(body.contains("Sitemap: https://example.com/sitemap.xml"));
773 }
774
775 #[test]
776 fn robots_plugin_missing_site_dir_returns_ok() {
777 let dir = tempdir().unwrap();
778 let missing = dir.path().join("missing");
779 let plugin = RobotsPlugin::new("https://example.com");
780 let ctx = test_ctx(&missing);
781 plugin.after_compile(&ctx).unwrap();
782 }
783
784 #[test]
785 fn robots_plugin_name_returns_static_identifier() {
786 assert_eq!(RobotsPlugin::new("").name(), "robots");
787 }
788
789 #[test]
794 fn canonical_plugin_missing_site_dir_returns_ok() {
795 let dir = tempdir().unwrap();
796 let missing = dir.path().join("missing");
797 let plugin = CanonicalPlugin::new("https://example.com");
798 let ctx = test_ctx(&missing);
799 plugin.after_compile(&ctx).unwrap();
800 }
801
802 #[test]
803 fn canonical_plugin_replaces_existing_canonical_with_correct_url() {
804 let dir = tempdir().unwrap();
805 let html = r#"<html><head><link rel="canonical" href="/original"></head><body></body></html>"#;
806 let plugin = CanonicalPlugin::new("https://example.com");
807 let ctx = test_ctx(dir.path());
808 let page_path = dir.path().join("p.html");
809
810 let out = plugin.transform_html(html, &page_path, &ctx).unwrap();
811 assert_eq!(out.matches(r#"rel="canonical""#).count(), 1);
812 assert!(out.contains("https://example.com/p.html"));
813 }
814
815 #[test]
816 fn canonical_plugin_skips_pages_with_single_quoted_canonical() {
817 let dir = tempdir().unwrap();
818 let html =
819 r"<html><head><link rel='canonical' href='/x'></head></html>";
820 let plugin = CanonicalPlugin::new("https://example.com");
821 let ctx = test_ctx(dir.path());
822 let page_path = dir.path().join("p.html");
823
824 let out = plugin.transform_html(html, &page_path, &ctx).unwrap();
825 assert_eq!(out.matches("canonical").count(), 1);
826 }
827
828 #[test]
829 fn canonical_plugin_page_without_head_is_left_unchanged() {
830 let dir = tempdir().unwrap();
831 let html = "<p>no structure</p>";
832 let plugin = CanonicalPlugin::new("https://example.com");
833 let ctx = test_ctx(dir.path());
834 let page_path = dir.path().join("frag.html");
835
836 let out = plugin.transform_html(html, &page_path, &ctx).unwrap();
837 assert_eq!(out, html);
838 }
839
840 #[test]
841 fn canonical_plugin_injects_canonical_link_before_head_close() {
842 let dir = tempdir().unwrap();
843 let html = "<html><head><title>T</title></head><body></body></html>";
844 let plugin = CanonicalPlugin::new("https://example.com/");
845 let ctx = test_ctx(dir.path());
846 let page_path = dir.path().join("a.html");
847
848 let out = plugin.transform_html(html, &page_path, &ctx).unwrap();
849 assert!(out.contains(r#"rel="canonical""#));
850 assert!(out.contains("https://example.com/a.html"));
851 }
852
853 #[test]
854 fn canonical_plugin_name_returns_static_identifier() {
855 assert_eq!(CanonicalPlugin::new("").name(), "canonical");
856 }
857
858 #[test]
863 fn jsonld_plugin_missing_site_dir_returns_ok() {
864 let dir = tempdir().unwrap();
865 let missing = dir.path().join("missing");
866 let plugin = JsonLdPlugin::from_site("https://example.com", "Org");
867 let ctx = test_ctx(&missing);
868 plugin.after_compile(&ctx).unwrap();
869 }
870
871 #[test]
872 fn jsonld_plugin_skips_pages_without_head_tag() {
873 let dir = tempdir().unwrap();
874 let site = dir.path().join("site");
875 fs::create_dir_all(&site).unwrap();
876 let ctx = test_ctx(&site);
877 let plugin = JsonLdPlugin::from_site("https://example.com", "Org");
878 let page_path = site.join("frag.html");
879
880 let out = plugin
881 .transform_html("<p>no head</p>", &page_path, &ctx)
882 .unwrap();
883 assert_eq!(out, "<p>no head</p>");
884 }
885
886 #[test]
887 fn jsonld_plugin_generates_webpage_when_no_article_element() {
888 let dir = tempdir().unwrap();
889 let site = dir.path().join("site");
890 fs::create_dir_all(&site).unwrap();
891 let html = "<html><head><title>Hello</title></head><body><p>content</p></body></html>";
892 let ctx = test_ctx(&site);
893 let plugin = JsonLdPlugin::from_site("https://example.com", "Org");
894 let page_path = site.join("index.html");
895
896 let out = plugin.transform_html(html, &page_path, &ctx).unwrap();
897 assert!(out.contains("application/ld+json"));
898 assert!(out.contains("WebPage"));
899 }
900
901 #[test]
902 fn jsonld_plugin_generates_article_when_article_element_present() {
903 let dir = tempdir().unwrap();
904 let site = dir.path().join("site");
905 fs::create_dir_all(&site).unwrap();
906 let html = "<html><head><title>Post</title></head><body><article><h1>Post</h1></article></body></html>";
907 let ctx = test_ctx(&site);
908 let plugin = JsonLdPlugin::from_site("https://example.com", "Org");
909 let page_path = site.join("post.html");
910
911 let out = plugin.transform_html(html, &page_path, &ctx).unwrap();
912 assert!(out.contains("application/ld+json"));
913 assert!(out.contains(r#""Article""#));
914 }
915
916 #[test]
917 fn jsonld_plugin_new_stores_supplied_config() {
918 let cfg = JsonLdConfig {
919 base_url: "https://a".to_string(),
920 org_name: "Org".to_string(),
921 breadcrumbs: false,
922 };
923 let plugin = JsonLdPlugin::new(cfg);
924 assert_eq!(plugin.config.base_url, "https://a");
925 assert_eq!(plugin.config.org_name, "Org");
926 assert!(!plugin.config.breadcrumbs);
927 }
928
929 #[test]
930 fn jsonld_plugin_name_returns_static_identifier() {
931 let plugin = JsonLdPlugin::from_site("https://example.com", "Org");
932 assert_eq!(plugin.name(), "json-ld");
933 }
934
935 #[test]
940 fn collect_html_files_recursive_filters_and_sorts() {
941 let dir = tempdir().unwrap();
942 let sub = dir.path().join("sub");
943 fs::create_dir(&sub).unwrap();
944 fs::write(dir.path().join("z.html"), "").unwrap();
945 fs::write(dir.path().join("a.html"), "").unwrap();
946 fs::write(sub.join("m.html"), "").unwrap();
947 fs::write(dir.path().join("ignore.css"), "").unwrap();
948
949 let files = collect_html_files_recursive(dir.path()).unwrap();
950 assert_eq!(files.len(), 3);
951 }
952
953 #[test]
954 fn collect_html_files_recursive_missing_dir_returns_empty() {
955 let dir = tempdir().unwrap();
956 let result =
957 collect_html_files_recursive(&dir.path().join("missing")).unwrap();
958 assert!(result.is_empty());
959 }
960
961 #[test]
966 fn has_meta_tag_detects_name_double_quote() {
967 let html = r#"<meta name="description" content="hello">"#;
968 assert!(has_meta_tag(html, "description"));
969 }
970
971 #[test]
972 fn has_meta_tag_detects_name_single_quote() {
973 let html = "<meta name='description' content='hello'>";
974 assert!(has_meta_tag(html, "description"));
975 }
976
977 #[test]
978 fn has_meta_tag_detects_property_double_quote() {
979 let html = r#"<meta property="og:title" content="T">"#;
980 assert!(has_meta_tag(html, "og:title"));
981 }
982
983 #[test]
984 fn has_meta_tag_detects_property_single_quote() {
985 let html = "<meta property='og:title' content='T'>";
986 assert!(has_meta_tag(html, "og:title"));
987 }
988
989 #[test]
990 fn has_meta_tag_returns_false_when_absent() {
991 let html = "<html><head></head></html>";
992 assert!(!has_meta_tag(html, "description"));
993 }
994
995 #[test]
996 fn has_meta_tag_ignores_comment_markers() {
997 let html = "<!-- # Start Open Graph / Facebook Meta Tags -->\n\
998 <!-- # End Open Graph / Facebook Meta Tags -->";
999 assert!(!has_meta_tag(html, "og:title"));
1000 }
1001
1002 #[test]
1007 fn extract_canonical_finds_url() {
1008 let html = r#"<link rel="canonical" href="https://example.com/page">"#;
1009 assert_eq!(extract_canonical(html), "https://example.com/page");
1010 }
1011
1012 #[test]
1013 fn extract_canonical_returns_empty_when_missing() {
1014 let html = "<html><head><title>No canonical</title></head></html>";
1015 assert_eq!(extract_canonical(html), "");
1016 }
1017
1018 #[test]
1023 fn extract_existing_meta_name_variant() {
1024 let html = r#"<meta name="author" content="Alice">"#;
1025 assert_eq!(extract_existing_meta(html, "author"), "Alice");
1026 }
1027
1028 #[test]
1029 fn extract_existing_meta_property_variant() {
1030 let html =
1031 r#"<meta property="article:published_time" content="2026-01-01">"#;
1032 assert_eq!(
1033 extract_existing_meta(html, "article:published_time"),
1034 "2026-01-01"
1035 );
1036 }
1037
1038 #[test]
1039 fn extract_existing_meta_single_quote_variant() {
1040 let html = "<meta name='author' content='Bob'>";
1041 assert_eq!(extract_existing_meta(html, "author"), "Bob");
1042 }
1043
1044 #[test]
1045 fn extract_existing_meta_returns_empty_when_absent() {
1046 let html = "<html><head></head></html>";
1047 assert_eq!(extract_existing_meta(html, "author"), "");
1048 }
1049
1050 #[test]
1055 fn extract_meta_author_from_meta_tag() {
1056 let html = r#"<meta name="author" content="Jane Doe">"#;
1057 assert_eq!(extract_meta_author(html), "Jane Doe");
1058 }
1059
1060 #[test]
1061 fn extract_meta_author_from_class_author_span() {
1062 let html = r#"<span class="author">John Smith</span>"#;
1063 assert_eq!(extract_meta_author(html), "John Smith");
1064 }
1065
1066 #[test]
1067 fn extract_meta_author_strips_by_prefix() {
1068 let html = r#"<span class="author">by Alice Wonder</span>"#;
1069 assert_eq!(extract_meta_author(html), "Alice Wonder");
1070 }
1071
1072 #[test]
1073 fn extract_meta_author_returns_empty_when_absent() {
1074 let html = "<html><body><p>No author</p></body></html>";
1075 assert_eq!(extract_meta_author(html), "");
1076 }
1077
1078 #[test]
1083 fn extract_date_from_html_finds_date_published() {
1084 let html = r#"<script type="application/ld+json">{"datePublished":"2026-03-15"}</script>"#;
1085 assert_eq!(
1086 extract_date_from_html(html, "datePublished"),
1087 Some("2026-03-15".to_string())
1088 );
1089 }
1090
1091 #[test]
1092 fn extract_date_from_html_returns_none_when_absent() {
1093 let html = "<html><body></body></html>";
1094 assert_eq!(extract_date_from_html(html, "datePublished"), None);
1095 }
1096
1097 #[test]
1102 fn extract_meta_date_from_published_time() {
1103 let html =
1104 r#"<meta property="article:published_time" content="2026-06-01">"#;
1105 assert_eq!(extract_meta_date(html), Some("2026-06-01".to_string()));
1106 }
1107
1108 #[test]
1109 fn extract_meta_date_from_time_datetime() {
1110 let html = r#"<time datetime="2026-07-04">July 4</time>"#;
1111 assert_eq!(extract_meta_date(html), Some("2026-07-04".to_string()));
1112 }
1113
1114 #[test]
1115 fn extract_meta_date_returns_none_when_absent() {
1116 let html = "<html><body><p>No date</p></body></html>";
1117 assert_eq!(extract_meta_date(html), None);
1118 }
1119
1120 #[test]
1125 fn extract_html_lang_double_quotes() {
1126 let html = r#"<html lang="fr-FR"><head></head></html>"#;
1127 assert_eq!(extract_html_lang(html), "fr-FR");
1128 }
1129
1130 #[test]
1131 fn extract_html_lang_single_quotes() {
1132 let html = "<html lang='de-DE'><head></head></html>";
1133 assert_eq!(extract_html_lang(html), "de-DE");
1134 }
1135
1136 #[test]
1137 fn extract_html_lang_missing_returns_empty() {
1138 let html = "<html><head></head></html>";
1139 assert_eq!(extract_html_lang(html), "");
1140 }
1141
1142 #[test]
1147 fn extract_first_content_image_from_main() {
1148 let html = r#"<html><body><main><img src="/img/hero.jpg"></main></body></html>"#;
1149 assert_eq!(extract_first_content_image(html), "/img/hero.jpg");
1150 }
1151
1152 #[test]
1153 fn extract_first_content_image_from_article() {
1154 let html = r#"<html><body><article><img src="/img/post.png"></article></body></html>"#;
1155 assert_eq!(extract_first_content_image(html), "/img/post.png");
1156 }
1157
1158 #[test]
1159 fn extract_first_content_image_no_image_returns_empty() {
1160 let html = "<html><body><main><p>No images</p></main></body></html>";
1161 assert_eq!(extract_first_content_image(html), "");
1162 }
1163
1164 #[test]
1165 fn extract_first_content_image_no_main_or_article_returns_empty() {
1166 let html = r#"<html><body><div><img src="/img/sidebar.jpg"></div></body></html>"#;
1167 assert_eq!(extract_first_content_image(html), "");
1168 }
1169
1170 #[test]
1175 fn inject_seo_tags_article_page_uses_large_image_card() {
1176 let tmp = tempdir().unwrap();
1177 let html = "<html><head><title>Blog Post</title></head>\
1178 <body><article><p>Article content</p></article></body></html>";
1179 let ctx = test_ctx(tmp.path());
1180
1181 let result = SeoPlugin
1182 .transform_html(html, Path::new("post.html"), &ctx)
1183 .unwrap();
1184 assert!(
1185 result.contains("content=\"summary_large_image\""),
1186 "article pages should use summary_large_image twitter card"
1187 );
1188 assert!(
1189 result.contains("content=\"article\""),
1190 "article pages should use og:type=article"
1191 );
1192 }
1193
1194 #[test]
1199 fn canonical_plugin_replaces_not_skips_existing() {
1200 let tmp = tempdir().unwrap();
1201 let html = r#"<html><head><link rel="canonical" href="https://old.com/wrong"></head><body></body></html>"#;
1202 let plugin = CanonicalPlugin::new("https://correct.com");
1203 let ctx = test_ctx(tmp.path());
1204 let page_path = tmp.path().join("page.html");
1205
1206 let result = plugin.transform_html(html, &page_path, &ctx).unwrap();
1207 assert!(
1208 result.contains("https://correct.com/page.html"),
1209 "canonical should be replaced with correct URL"
1210 );
1211 assert!(
1212 !result.contains("https://old.com/wrong"),
1213 "old canonical should be removed"
1214 );
1215 assert_eq!(
1216 result.matches("canonical").count(),
1217 1,
1218 "should have exactly one canonical link"
1219 );
1220 }
1221}