Skip to main content

weave_content/
html.rs

1//! Static HTML generator for case and entity pages.
2//!
3//! Produces semantic HTML fragments (no `<html>`/`<head>`/`<body>` wrapper)
4//! suitable for embedding in a Phoenix layout. Each fragment includes
5//! `data-og-*` attributes on the root element for meta tag extraction,
6//! Schema.org microdata, and a `<script type="application/ld+json">` block.
7
8#![allow(clippy::format_push_string)]
9
10use std::fmt::Write as _;
11
12use crate::domain::Jurisdiction;
13use crate::output::{CaseOutput, NodeOutput, RelOutput};
14use crate::parser::SourceEntry;
15use sha2::{Digest, Sha256};
16
17/// Configuration for HTML generation.
18#[derive(Debug, Default, Clone)]
19pub struct HtmlConfig {
20    /// Base URL for rewriting thumbnail image sources.
21    ///
22    /// When set, original thumbnail URLs are rewritten to
23    /// `{base_url}/thumbnails/{sha256_hex[0..32]}.webp` using the same
24    /// deterministic key as `weave-image::thumbnail_key`.
25    ///
26    /// Example: `http://files.web.garage.localhost:3902`
27    pub thumbnail_base_url: Option<String>,
28}
29
30/// Length of the hex-encoded SHA-256 prefix used for thumbnail keys.
31const THUMB_KEY_HEX_LEN: usize = 32;
32
33/// Maximum size for a single HTML fragment file (500 KB).
34const MAX_FRAGMENT_BYTES: usize = 512_000;
35
36/// Generate a complete case page HTML fragment.
37///
38/// # Errors
39///
40/// Returns an error if the rendered HTML exceeds [`MAX_FRAGMENT_BYTES`].
41pub fn render_case(case: &CaseOutput, config: &HtmlConfig) -> Result<String, String> {
42    let mut html = String::with_capacity(8192);
43
44    let og_title = truncate(&case.title, 120);
45    let og_description = build_case_og_description(case);
46
47    // Root element with OG data attributes
48    html.push_str(&format!(
49        "<article class=\"loom-case\" itemscope itemtype=\"https://schema.org/Article\" \
50         data-og-title=\"{}\" \
51         data-og-description=\"{}\" \
52         data-og-type=\"article\" \
53         data-og-url=\"/{}\"{}>\n",
54        escape_attr(&og_title),
55        escape_attr(&og_description),
56        escape_attr(case.slug.as_deref().unwrap_or(&case.case_id)),
57        og_image_attr(case_hero_image(case).as_deref(), config),
58    ));
59
60    // Header
61    let country = case
62        .slug
63        .as_deref()
64        .and_then(extract_country_from_case_slug);
65    render_case_header(&mut html, case, country.as_deref());
66
67    // Financial details — prominent position right after header
68    render_financial_details(&mut html, &case.relationships, &case.nodes);
69
70    // Sources
71    render_sources(&mut html, &case.sources);
72
73    // People section
74    let people: Vec<&NodeOutput> = case.nodes.iter().filter(|n| n.label == "person").collect();
75    if !people.is_empty() {
76        render_entity_section(&mut html, "People", &people, config);
77    }
78
79    // Organizations section
80    let orgs: Vec<&NodeOutput> = case
81        .nodes
82        .iter()
83        .filter(|n| n.label == "organization")
84        .collect();
85    if !orgs.is_empty() {
86        render_entity_section(&mut html, "Organizations", &orgs, config);
87    }
88
89    // Timeline section (events sorted by occurred_at)
90    let mut events: Vec<&NodeOutput> = case.nodes.iter().filter(|n| n.label == "event").collect();
91    events.sort_by(|a, b| a.occurred_at.cmp(&b.occurred_at));
92    if !events.is_empty() {
93        render_timeline(&mut html, &events);
94    }
95
96    // Related Cases section
97    render_related_cases(&mut html, &case.relationships, &case.nodes);
98
99    // JSON-LD
100    render_case_json_ld(&mut html, case);
101
102    html.push_str("</article>\n");
103
104    if html.len() > MAX_FRAGMENT_BYTES {
105        return Err(format!(
106            "HTML fragment exceeds {MAX_FRAGMENT_BYTES} bytes ({} bytes)",
107            html.len()
108        ));
109    }
110
111    Ok(html)
112}
113
114/// Generate a person page HTML fragment.
115///
116/// # Errors
117///
118/// Returns an error if the rendered HTML exceeds [`MAX_FRAGMENT_BYTES`].
119pub fn render_person(
120    node: &NodeOutput,
121    cases: &[(String, String)], // (case_id, case_title)
122    config: &HtmlConfig,
123) -> Result<String, String> {
124    let mut html = String::with_capacity(4096);
125
126    let og_title = truncate(&node.name, 120);
127    let og_description = build_person_og_description(node);
128
129    html.push_str(&format!(
130        "<article class=\"loom-person\" itemscope itemtype=\"https://schema.org/Person\" \
131         data-og-title=\"{}\" \
132         data-og-description=\"{}\" \
133         data-og-type=\"profile\" \
134         data-og-url=\"/{}\"{}>\n",
135        escape_attr(&og_title),
136        escape_attr(&og_description),
137        escape_attr(node.slug.as_deref().unwrap_or(&node.id)),
138        og_image_attr(node.thumbnail.as_deref(), config),
139    ));
140
141    render_entity_detail(&mut html, node, config);
142    render_cases_list(&mut html, cases);
143    render_person_json_ld(&mut html, node);
144
145    html.push_str("</article>\n");
146
147    check_size(&html)
148}
149
150/// Generate an organization page HTML fragment.
151///
152/// # Errors
153///
154/// Returns an error if the rendered HTML exceeds [`MAX_FRAGMENT_BYTES`].
155pub fn render_organization(
156    node: &NodeOutput,
157    cases: &[(String, String)],
158    config: &HtmlConfig,
159) -> Result<String, String> {
160    let mut html = String::with_capacity(4096);
161
162    let og_title = truncate(&node.name, 120);
163    let og_description = build_org_og_description(node);
164
165    html.push_str(&format!(
166        "<article class=\"loom-organization\" itemscope itemtype=\"https://schema.org/Organization\" \
167         data-og-title=\"{}\" \
168         data-og-description=\"{}\" \
169         data-og-type=\"profile\" \
170         data-og-url=\"/{}\"{}>\n",
171        escape_attr(&og_title),
172        escape_attr(&og_description),
173        escape_attr(node.slug.as_deref().unwrap_or(&node.id)),
174        og_image_attr(node.thumbnail.as_deref(), config),
175    ));
176
177    render_entity_detail(&mut html, node, config);
178    render_cases_list(&mut html, cases);
179    render_org_json_ld(&mut html, node);
180
181    html.push_str("</article>\n");
182
183    check_size(&html)
184}
185
186// --- Case sections ---
187
188fn render_case_header(html: &mut String, case: &CaseOutput, country: Option<&str>) {
189    html.push_str(&format!(
190        "  <header class=\"loom-case-header\">\n    <h1 itemprop=\"headline\">{}</h1>\n",
191        escape(&case.title)
192    ));
193
194    if !case.amounts.is_empty() {
195        html.push_str("    <div class=\"loom-case-amounts\">\n");
196        for entry in &case.amounts {
197            let approx_cls = if entry.approximate { " loom-amount-approx" } else { "" };
198            let label_cls = entry.label.as_deref().unwrap_or("unlabeled").replace('_', "-");
199            html.push_str(&format!(
200                "      <span class=\"loom-amount-badge loom-amount-{label_cls}{approx_cls}\">{}</span>\n",
201                escape(&entry.format_display())
202            ));
203        }
204        html.push_str("    </div>\n");
205    }
206
207    if !case.tags.is_empty() {
208        html.push_str("    <div class=\"loom-tags\">\n");
209        for tag in &case.tags {
210            let href = match country {
211                Some(cc) => format!("/tags/{}/{}", escape_attr(cc), escape_attr(tag)),
212                None => format!("/tags/{}", escape_attr(tag)),
213            };
214            html.push_str(&format!(
215                "      <a href=\"{}\" class=\"loom-tag\">{}</a>\n",
216                href,
217                escape(tag)
218            ));
219        }
220        html.push_str("    </div>\n");
221    }
222
223    if !case.summary.is_empty() {
224        html.push_str(&format!(
225            "    <p class=\"loom-summary\" itemprop=\"description\">{}</p>\n",
226            escape(&case.summary)
227        ));
228    }
229
230    // Canvas link for the case node
231    html.push_str(&format!(
232        "    <a href=\"/canvas/{}\" class=\"loom-canvas-link\">View on canvas</a>\n",
233        escape_attr(&case.id)
234    ));
235
236    html.push_str("  </header>\n");
237}
238
239fn render_sources(html: &mut String, sources: &[SourceEntry]) {
240    if sources.is_empty() {
241        return;
242    }
243    html.push_str("  <section class=\"loom-sources\">\n    <h2>Sources</h2>\n    <ol>\n");
244    for source in sources {
245        match source {
246            SourceEntry::Url(url) => {
247                html.push_str(&format!(
248                    "      <li><a href=\"{}\" rel=\"noopener noreferrer\" target=\"_blank\">{}</a></li>\n",
249                    escape_attr(url),
250                    escape(url)
251                ));
252            }
253            SourceEntry::Structured { url, title, .. } => {
254                let display = title.as_deref().unwrap_or(url.as_str());
255                html.push_str(&format!(
256                    "      <li><a href=\"{}\" rel=\"noopener noreferrer\" target=\"_blank\">{}</a></li>\n",
257                    escape_attr(url),
258                    escape(display)
259                ));
260            }
261        }
262    }
263    html.push_str("    </ol>\n  </section>\n");
264}
265
266fn render_entity_section(
267    html: &mut String,
268    title: &str,
269    nodes: &[&NodeOutput],
270    config: &HtmlConfig,
271) {
272    html.push_str(&format!(
273        "  <section class=\"loom-entities loom-entities-{}\">\n    <h2>{title}</h2>\n    <div class=\"loom-entity-cards\">\n",
274        title.to_lowercase()
275    ));
276    for node in nodes {
277        render_entity_card(html, node, config);
278    }
279    html.push_str("    </div>\n  </section>\n");
280}
281
282fn render_entity_card(html: &mut String, node: &NodeOutput, config: &HtmlConfig) {
283    let schema_type = match node.label.as_str() {
284        "person" => "Person",
285        "organization" => "Organization",
286        _ => "Thing",
287    };
288    html.push_str(&format!(
289        "      <div class=\"loom-entity-card\" itemscope itemtype=\"https://schema.org/{schema_type}\">\n"
290    ));
291
292    if let Some(thumb) = &node.thumbnail {
293        let thumb_url = rewrite_thumbnail_url(thumb, config);
294        html.push_str(&format!(
295            "        <img src=\"{}\" alt=\"{}\" class=\"loom-thumbnail\" itemprop=\"image\" loading=\"lazy\" width=\"64\" height=\"64\" />\n",
296            escape_attr(&thumb_url),
297            escape_attr(&node.name)
298        ));
299    }
300
301    // Link to static view when slug is available, otherwise fall back to canvas
302    let entity_href = if let Some(slug) = &node.slug {
303        format!("/{}", escape_attr(slug))
304    } else {
305        format!("/canvas/{}", escape_attr(&node.id))
306    };
307
308    html.push_str(&format!(
309        "        <div class=\"loom-entity-info\">\n          \
310         <a href=\"{}\" class=\"loom-entity-name\" itemprop=\"name\">{}</a>\n",
311        entity_href,
312        escape(&node.name)
313    ));
314
315    if let Some(q) = &node.qualifier {
316        html.push_str(&format!(
317            "          <span class=\"loom-qualifier\">{}</span>\n",
318            escape(q)
319        ));
320    }
321
322    // Label-specific fields
323    match node.label.as_str() {
324        "person" => {
325            let roles: Vec<_> = node.role.iter().map(|r| format_enum(r)).collect();
326            render_dl_field(html, "Role", &roles.join(", "));
327            render_dl_opt_country(html, "Nationality", node.nationality.as_ref());
328        }
329        "organization" => {
330            render_dl_opt_formatted(html, "Type", node.org_type.as_ref());
331            if let Some(j) = &node.jurisdiction {
332                render_dl_field(html, "Jurisdiction", &format_jurisdiction(j));
333            }
334        }
335        "asset" => {
336            render_dl_opt_formatted(html, "Type", node.asset_type.as_ref());
337            if let Some(m) = &node.value {
338                render_dl_field(html, "Value", &m.display);
339            }
340            render_dl_opt_formatted(html, "Status", node.status.as_ref());
341        }
342        "document" => {
343            render_dl_opt_formatted(html, "Type", node.doc_type.as_ref());
344            render_dl_opt(html, "Issued", node.issued_at.as_ref());
345        }
346        "event" => {
347            render_dl_opt_formatted(html, "Type", node.event_type.as_ref());
348            render_dl_opt(html, "Date", node.occurred_at.as_ref());
349        }
350        _ => {}
351    }
352
353    html.push_str("        </div>\n      </div>\n");
354}
355
356fn render_timeline(html: &mut String, events: &[&NodeOutput]) {
357    html.push_str(
358        "  <section class=\"loom-timeline\">\n    <h2>Timeline</h2>\n    <ol class=\"loom-events\">\n",
359    );
360    for event in events {
361        html.push_str("      <li class=\"loom-event\">\n");
362        if let Some(date) = &event.occurred_at {
363            html.push_str(&format!(
364                "        <time datetime=\"{}\" class=\"loom-event-date\">{}</time>\n",
365                escape_attr(date),
366                escape(date)
367            ));
368        }
369        html.push_str("        <div class=\"loom-event-body\">\n");
370        html.push_str(&format!(
371            "          <span class=\"loom-event-name\">{}</span>\n",
372            escape(&event.name)
373        ));
374        if let Some(et) = &event.event_type {
375            html.push_str(&format!(
376                "          <span class=\"loom-event-type\">{}</span>\n",
377                escape(&format_enum(et))
378            ));
379        }
380        if let Some(desc) = &event.description {
381            html.push_str(&format!(
382                "          <p class=\"loom-event-description\">{}</p>\n",
383                escape(desc)
384            ));
385        }
386        html.push_str("        </div>\n");
387        html.push_str("      </li>\n");
388    }
389    html.push_str("    </ol>\n  </section>\n");
390}
391
392fn render_related_cases(html: &mut String, relationships: &[RelOutput], nodes: &[NodeOutput]) {
393    let related: Vec<&RelOutput> = relationships
394        .iter()
395        .filter(|r| r.rel_type == "related_to")
396        .collect();
397    if related.is_empty() {
398        return;
399    }
400    html.push_str(
401        "  <section class=\"loom-related-cases\">\n    <h2>Related Cases</h2>\n    <div class=\"loom-related-list\">\n",
402    );
403    for rel in &related {
404        if let Some(node) = nodes
405            .iter()
406            .find(|n| n.id == rel.target_id && n.label == "case")
407        {
408            let href = node
409                .slug
410                .as_deref()
411                .map_or_else(|| format!("/cases/{}", node.id), |s| format!("/{s}"));
412            let desc = rel.description.as_deref().unwrap_or("");
413            html.push_str(&format!(
414                "      <a href=\"{}\" class=\"loom-related-card\">\n        <span class=\"loom-related-title\">{}</span>\n",
415                escape_attr(&href),
416                escape(&node.name)
417            ));
418            if !desc.is_empty() {
419                html.push_str(&format!(
420                    "        <span class=\"loom-related-desc\">{}</span>\n",
421                    escape(desc)
422                ));
423            }
424            html.push_str("      </a>\n");
425        }
426    }
427    html.push_str("    </div>\n  </section>\n");
428}
429
430// --- Financial details ---
431
432fn render_financial_details(html: &mut String, relationships: &[RelOutput], nodes: &[NodeOutput]) {
433    let financial: Vec<&RelOutput> = relationships
434        .iter()
435        .filter(|r| !r.amounts.is_empty())
436        .collect();
437    if financial.is_empty() {
438        return;
439    }
440
441    let node_name = |id: &str| -> String {
442        nodes
443            .iter()
444            .find(|n| n.id == id).map_or_else(|| id.to_string(), |n| n.name.clone())
445    };
446
447    html.push_str(
448        "  <section class=\"loom-financial\">\n    <h2>Financial Details</h2>\n    <dl class=\"loom-financial-list\">\n",
449    );
450    for rel in &financial {
451        let source = node_name(&rel.source_id);
452        let target = node_name(&rel.target_id);
453        let rel_label = format_enum(&rel.rel_type);
454        html.push_str(&format!(
455            "      <div class=\"loom-financial-entry\">\n        <dt>{} &rarr; {} <span class=\"loom-rel-label\">{}</span></dt>\n",
456            escape(&source), escape(&target), escape(&rel_label)
457        ));
458        for entry in &rel.amounts {
459            let approx_cls = if entry.approximate { " loom-amount-approx" } else { "" };
460            html.push_str(&format!(
461                "        <dd><span class=\"loom-amount-badge{}\">{}</span></dd>\n",
462                approx_cls,
463                escape(&entry.format_display())
464            ));
465        }
466        html.push_str("      </div>\n");
467    }
468    html.push_str("    </dl>\n  </section>\n");
469}
470
471// --- Entity detail page ---
472
473fn render_entity_detail(html: &mut String, node: &NodeOutput, config: &HtmlConfig) {
474    html.push_str("  <header class=\"loom-entity-header\">\n");
475
476    if let Some(thumb) = &node.thumbnail {
477        let thumb_url = rewrite_thumbnail_url(thumb, config);
478        html.push_str(&format!(
479            "    <img src=\"{}\" alt=\"{}\" class=\"loom-thumbnail-large\" itemprop=\"image\" loading=\"lazy\" width=\"128\" height=\"128\" />\n",
480            escape_attr(&thumb_url),
481            escape_attr(&node.name)
482        ));
483    }
484
485    html.push_str(&format!(
486        "    <h1 itemprop=\"name\">{}</h1>\n",
487        escape(&node.name)
488    ));
489
490    if let Some(q) = &node.qualifier {
491        html.push_str(&format!(
492            "    <p class=\"loom-qualifier\">{}</p>\n",
493            escape(q)
494        ));
495    }
496
497    html.push_str(&format!(
498        "    <a href=\"/canvas/{}\" class=\"loom-canvas-link\">View on canvas</a>\n",
499        escape_attr(&node.id)
500    ));
501    html.push_str("  </header>\n");
502
503    // Description
504    if let Some(desc) = &node.description {
505        html.push_str(&format!(
506            "  <p class=\"loom-description\" itemprop=\"description\">{}</p>\n",
507            escape(desc)
508        ));
509    }
510
511    // Fields as definition list
512    html.push_str("  <dl class=\"loom-fields\">\n");
513
514    match node.label.as_str() {
515        "person" => {
516            let roles: Vec<_> = node.role.iter().map(|r| format_enum(r)).collect();
517            render_dl_item(html, "Role", &roles.join(", "));
518            render_dl_opt_country_item(html, "Nationality", node.nationality.as_ref());
519            render_dl_opt_item(html, "Date of Birth", node.date_of_birth.as_ref());
520            render_dl_opt_item(html, "Place of Birth", node.place_of_birth.as_ref());
521            render_dl_opt_formatted_item(html, "Status", node.status.as_ref());
522        }
523        "organization" => {
524            render_dl_opt_formatted_item(html, "Type", node.org_type.as_ref());
525            if let Some(j) = &node.jurisdiction {
526                render_dl_item(html, "Jurisdiction", &format_jurisdiction(j));
527            }
528            render_dl_opt_item(html, "Headquarters", node.headquarters.as_ref());
529            render_dl_opt_item(html, "Founded", node.founded_date.as_ref());
530            render_dl_opt_item(html, "Registration", node.registration_number.as_ref());
531            render_dl_opt_formatted_item(html, "Status", node.status.as_ref());
532        }
533        "asset" => {
534            render_dl_opt_formatted_item(html, "Type", node.asset_type.as_ref());
535            if let Some(m) = &node.value {
536                render_dl_item(html, "Value", &m.display);
537            }
538            render_dl_opt_formatted_item(html, "Status", node.status.as_ref());
539        }
540        "document" => {
541            render_dl_opt_formatted_item(html, "Type", node.doc_type.as_ref());
542            render_dl_opt_item(html, "Issued", node.issued_at.as_ref());
543            render_dl_opt_item(html, "Issuing Authority", node.issuing_authority.as_ref());
544            render_dl_opt_item(html, "Case Number", node.case_number.as_ref());
545        }
546        "event" => {
547            render_dl_opt_formatted_item(html, "Type", node.event_type.as_ref());
548            render_dl_opt_item(html, "Date", node.occurred_at.as_ref());
549            render_dl_opt_formatted_item(html, "Severity", node.severity.as_ref());
550            if let Some(j) = &node.jurisdiction {
551                render_dl_item(html, "Jurisdiction", &format_jurisdiction(j));
552            }
553        }
554        _ => {}
555    }
556
557    html.push_str("  </dl>\n");
558
559    // Aliases
560    if !node.aliases.is_empty() {
561        html.push_str("  <div class=\"loom-aliases\">\n    <h3>Also known as</h3>\n    <ul>\n");
562        for alias in &node.aliases {
563            html.push_str(&format!("      <li>{}</li>\n", escape(alias)));
564        }
565        html.push_str("    </ul>\n  </div>\n");
566    }
567
568    // URLs
569    if !node.urls.is_empty() {
570        html.push_str("  <div class=\"loom-urls\">\n    <h3>Links</h3>\n    <ul>\n");
571        for url in &node.urls {
572            html.push_str(&format!(
573                "      <li><a href=\"{}\" rel=\"noopener noreferrer\" target=\"_blank\">{}</a></li>\n",
574                escape_attr(url),
575                escape(url)
576            ));
577        }
578        html.push_str("    </ul>\n  </div>\n");
579    }
580}
581
582fn render_cases_list(html: &mut String, cases: &[(String, String)]) {
583    if cases.is_empty() {
584        return;
585    }
586    html.push_str(
587        "  <section class=\"loom-cases\">\n    <h2>Cases</h2>\n    <ul class=\"loom-case-list\">\n",
588    );
589    for (case_slug, case_title) in cases {
590        html.push_str(&format!(
591            "      <li><a href=\"/{}\">{}</a></li>\n",
592            escape_attr(case_slug),
593            escape(case_title)
594        ));
595    }
596    html.push_str("    </ul>\n  </section>\n");
597}
598
599// --- JSON-LD ---
600
601fn render_case_json_ld(html: &mut String, case: &CaseOutput) {
602    let mut ld = serde_json::json!({
603        "@context": "https://schema.org",
604        "@type": "Article",
605        "headline": truncate(&case.title, 120),
606        "description": truncate(&case.summary, 200),
607        "url": format!("/{}", case.slug.as_deref().unwrap_or(&case.case_id)),
608    });
609
610    if !case.sources.is_empty() {
611        let urls: Vec<&str> = case
612            .sources
613            .iter()
614            .map(|s| match s {
615                SourceEntry::Url(u) => u.as_str(),
616                SourceEntry::Structured { url, .. } => url.as_str(),
617            })
618            .collect();
619        ld["citation"] = serde_json::json!(urls);
620    }
621
622    html.push_str(&format!(
623        "  <script type=\"application/ld+json\">{}</script>\n",
624        serde_json::to_string(&ld).unwrap_or_default()
625    ));
626}
627
628fn render_person_json_ld(html: &mut String, node: &NodeOutput) {
629    let mut ld = serde_json::json!({
630        "@context": "https://schema.org",
631        "@type": "Person",
632        "name": &node.name,
633        "url": format!("/{}", node.slug.as_deref().unwrap_or(&node.id)),
634    });
635
636    if let Some(nat) = &node.nationality {
637        ld["nationality"] = serde_json::json!(nat);
638    }
639    if let Some(desc) = &node.description {
640        ld["description"] = serde_json::json!(truncate(desc, 200));
641    }
642    if let Some(thumb) = &node.thumbnail {
643        ld["image"] = serde_json::json!(thumb);
644    }
645
646    html.push_str(&format!(
647        "  <script type=\"application/ld+json\">{}</script>\n",
648        serde_json::to_string(&ld).unwrap_or_default()
649    ));
650}
651
652fn render_org_json_ld(html: &mut String, node: &NodeOutput) {
653    let mut ld = serde_json::json!({
654        "@context": "https://schema.org",
655        "@type": "Organization",
656        "name": &node.name,
657        "url": format!("/{}", node.slug.as_deref().unwrap_or(&node.id)),
658    });
659
660    if let Some(desc) = &node.description {
661        ld["description"] = serde_json::json!(truncate(desc, 200));
662    }
663    if let Some(thumb) = &node.thumbnail {
664        ld["logo"] = serde_json::json!(thumb);
665    }
666
667    html.push_str(&format!(
668        "  <script type=\"application/ld+json\">{}</script>\n",
669        serde_json::to_string(&ld).unwrap_or_default()
670    ));
671}
672
673// --- Tag pages ---
674
675/// A case entry associated with a tag, used for tag page rendering.
676pub struct TagCaseEntry {
677    /// Display slug for the case link (e.g. `cases/id/corruption/2024/test-case`).
678    pub slug: String,
679    /// Case title.
680    pub title: String,
681    /// Structured amounts for display badge.
682    pub amounts: Vec<crate::domain::AmountEntry>,
683}
684
685/// Generate a tag page HTML fragment listing all cases with this tag.
686///
687/// # Errors
688///
689/// Returns an error if the rendered HTML exceeds [`MAX_FRAGMENT_BYTES`].
690pub fn render_tag_page(tag: &str, cases: &[TagCaseEntry]) -> Result<String, String> {
691    render_tag_page_with_path(tag, &format!("/tags/{}", escape_attr(tag)), cases)
692}
693
694pub fn render_tag_page_scoped(
695    tag: &str,
696    country: &str,
697    cases: &[TagCaseEntry],
698) -> Result<String, String> {
699    let display_tag = format!("{} ({})", tag.replace('-', " "), country.to_uppercase());
700    render_tag_page_with_path(
701        &display_tag,
702        &format!("/tags/{}/{}", escape_attr(country), escape_attr(tag)),
703        cases,
704    )
705}
706
707fn render_tag_page_with_path(
708    display: &str,
709    og_url: &str,
710    cases: &[TagCaseEntry],
711) -> Result<String, String> {
712    let mut html = String::with_capacity(2048);
713
714    let og_title = format!("Cases tagged \"{display}\"");
715
716    html.push_str(&format!(
717        "<article class=\"loom-tag-page\" \
718         data-og-title=\"{}\" \
719         data-og-description=\"{} cases tagged with {}\" \
720         data-og-type=\"website\" \
721         data-og-url=\"{}\">\n",
722        escape_attr(&og_title),
723        cases.len(),
724        escape_attr(display),
725        escape_attr(og_url),
726    ));
727
728    html.push_str(&format!(
729        "  <header class=\"loom-tag-header\">\n    \
730         <h1>{}</h1>\n    \
731         <p class=\"loom-tag-count\">{} cases</p>\n  \
732         </header>\n",
733        escape(display),
734        cases.len(),
735    ));
736
737    html.push_str("  <ul class=\"loom-case-list\">\n");
738    for entry in cases {
739        let amount_badges = if entry.amounts.is_empty() {
740            String::new()
741        } else {
742            let badges: Vec<String> = entry
743                .amounts
744                .iter()
745                .map(|a| {
746                    format!(
747                        " <span class=\"loom-amount-badge\">{}</span>",
748                        escape(&a.format_display())
749                    )
750                })
751                .collect();
752            badges.join("")
753        };
754        html.push_str(&format!(
755            "    <li><a href=\"/{}\">{}</a>{}</li>\n",
756            escape_attr(&entry.slug),
757            escape(&entry.title),
758            amount_badges,
759        ));
760    }
761    html.push_str("  </ul>\n");
762
763    html.push_str("</article>\n");
764
765    check_size(&html)
766}
767
768// --- Sitemap ---
769
770/// Generate a sitemap XML string.
771///
772/// All tuples are `(slug, display_name)` where slug is the full file-path slug
773/// (e.g. `cases/id/corruption/2024/hambalang-case`).
774pub fn render_sitemap(
775    cases: &[(String, String)],
776    people: &[(String, String)],
777    organizations: &[(String, String)],
778    base_url: &str,
779) -> String {
780    let mut xml = String::with_capacity(4096);
781    xml.push_str("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
782    xml.push_str("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n");
783
784    for (slug, _) in cases {
785        xml.push_str(&format!(
786            "  <url><loc>{base_url}/{}</loc></url>\n",
787            escape(slug)
788        ));
789    }
790    for (slug, _) in people {
791        xml.push_str(&format!(
792            "  <url><loc>{base_url}/{}</loc></url>\n",
793            escape(slug)
794        ));
795    }
796    for (slug, _) in organizations {
797        xml.push_str(&format!(
798            "  <url><loc>{base_url}/{}</loc></url>\n",
799            escape(slug)
800        ));
801    }
802
803    xml.push_str("</urlset>\n");
804    xml
805}
806
807// --- Helpers ---
808
809fn build_case_og_description(case: &CaseOutput) -> String {
810    if !case.summary.is_empty() {
811        return truncate(&case.summary, 200);
812    }
813    let people_count = case.nodes.iter().filter(|n| n.label == "person").count();
814    let org_count = case
815        .nodes
816        .iter()
817        .filter(|n| n.label == "organization")
818        .count();
819    truncate(
820        &format!(
821            "{} people, {} organizations, {} connections",
822            people_count,
823            org_count,
824            case.relationships.len()
825        ),
826        200,
827    )
828}
829
830fn build_person_og_description(node: &NodeOutput) -> String {
831    let mut parts = Vec::new();
832    if let Some(q) = &node.qualifier {
833        parts.push(q.clone());
834    }
835    if !node.role.is_empty() {
836        let roles: Vec<_> = node.role.iter().map(|r| format_enum(r)).collect();
837        parts.push(roles.join(", "));
838    }
839    if let Some(nat) = &node.nationality {
840        parts.push(country_name(nat));
841    }
842    if parts.is_empty() {
843        return truncate(&node.name, 200);
844    }
845    truncate(&format!("{} — {}", node.name, parts.join(" · ")), 200)
846}
847
848fn build_org_og_description(node: &NodeOutput) -> String {
849    let mut parts = Vec::new();
850    if let Some(q) = &node.qualifier {
851        parts.push(q.clone());
852    }
853    if let Some(ot) = &node.org_type {
854        parts.push(format_enum(ot));
855    }
856    if let Some(j) = &node.jurisdiction {
857        parts.push(format_jurisdiction(j));
858    }
859    if parts.is_empty() {
860        return truncate(&node.name, 200);
861    }
862    truncate(&format!("{} — {}", node.name, parts.join(" · ")), 200)
863}
864
865fn check_size(html: &str) -> Result<String, String> {
866    if html.len() > MAX_FRAGMENT_BYTES {
867        Err(format!(
868            "HTML fragment exceeds {MAX_FRAGMENT_BYTES} bytes ({} bytes)",
869            html.len()
870        ))
871    } else {
872        Ok(html.to_string())
873    }
874}
875
876fn truncate(s: &str, max: usize) -> String {
877    if s.len() <= max {
878        s.to_string()
879    } else {
880        let truncated: String = s.chars().take(max.saturating_sub(3)).collect();
881        format!("{truncated}...")
882    }
883}
884
885fn escape(s: &str) -> String {
886    s.replace('&', "&amp;")
887        .replace('<', "&lt;")
888        .replace('>', "&gt;")
889        .replace('"', "&quot;")
890}
891
892fn escape_attr(s: &str) -> String {
893    escape(s)
894}
895
896/// Rewrite a thumbnail source URL to a hosted URL.
897///
898/// When `config.thumbnail_base_url` is set, computes the deterministic
899/// key `thumbnails/{sha256_hex[0..32]}.webp` (matching `weave-image`)
900/// and returns `{base_url}/thumbnails/{hash}.webp`.
901///
902/// When not set, returns the original URL unchanged.
903fn rewrite_thumbnail_url(source_url: &str, config: &HtmlConfig) -> String {
904    match &config.thumbnail_base_url {
905        Some(base) => {
906            // If already rewritten (e.g. by loom-seed thumbnail processor), return as-is
907            if source_url.starts_with(base.as_str()) {
908                return source_url.to_string();
909            }
910            let key = thumbnail_key(source_url);
911            format!("{base}/{key}")
912        }
913        None => source_url.to_string(),
914    }
915}
916
917/// Compute the thumbnail object key from a source URL.
918///
919/// Returns `thumbnails/{sha256_hex[0..32]}.webp`, matching the algorithm
920/// in `weave-image::thumbnail_key`.
921fn thumbnail_key(source_url: &str) -> String {
922    let mut hasher = Sha256::new();
923    hasher.update(source_url.as_bytes());
924    let hash = hasher.finalize();
925    let hex = hex_encode(&hash);
926    format!("thumbnails/{}.webp", &hex[..THUMB_KEY_HEX_LEN])
927}
928
929/// Encode bytes as lowercase hex string.
930fn hex_encode(bytes: &[u8]) -> String {
931    let mut s = String::with_capacity(bytes.len() * 2);
932    for b in bytes {
933        let _ = write!(s, "{b:02x}");
934    }
935    s
936}
937
938/// Build `data-og-image` attribute string if a URL is available.
939fn og_image_attr(url: Option<&str>, config: &HtmlConfig) -> String {
940    match url {
941        Some(u) if !u.is_empty() => {
942            let rewritten = rewrite_thumbnail_url(u, config);
943            format!(" data-og-image=\"{}\"", escape_attr(&rewritten))
944        }
945        _ => String::new(),
946    }
947}
948
949/// Find the first person thumbnail in a case to use as hero image.
950fn case_hero_image(case: &CaseOutput) -> Option<String> {
951    case.nodes
952        .iter()
953        .filter(|n| n.label == "person")
954        .find_map(|n| n.thumbnail.clone())
955}
956
957fn format_jurisdiction(j: &Jurisdiction) -> String {
958    let country = country_name(&j.country);
959    match &j.subdivision {
960        Some(sub) => format!("{country}, {sub}"),
961        None => country,
962    }
963}
964
965/// Map ISO 3166-1 alpha-2 codes to country names.
966/// Returns the code itself if not found (graceful fallback).
967fn country_name(code: &str) -> String {
968    match code.to_uppercase().as_str() {
969        "AF" => "Afghanistan",
970        "AL" => "Albania",
971        "DZ" => "Algeria",
972        "AR" => "Argentina",
973        "AU" => "Australia",
974        "AT" => "Austria",
975        "BD" => "Bangladesh",
976        "BE" => "Belgium",
977        "BR" => "Brazil",
978        "BN" => "Brunei",
979        "KH" => "Cambodia",
980        "CA" => "Canada",
981        "CN" => "China",
982        "CO" => "Colombia",
983        "HR" => "Croatia",
984        "CZ" => "Czech Republic",
985        "DK" => "Denmark",
986        "EG" => "Egypt",
987        "FI" => "Finland",
988        "FR" => "France",
989        "DE" => "Germany",
990        "GH" => "Ghana",
991        "GR" => "Greece",
992        "HK" => "Hong Kong",
993        "HU" => "Hungary",
994        "IN" => "India",
995        "ID" => "Indonesia",
996        "IR" => "Iran",
997        "IQ" => "Iraq",
998        "IE" => "Ireland",
999        "IL" => "Israel",
1000        "IT" => "Italy",
1001        "JP" => "Japan",
1002        "KE" => "Kenya",
1003        "KR" => "South Korea",
1004        "KW" => "Kuwait",
1005        "LA" => "Laos",
1006        "LB" => "Lebanon",
1007        "MY" => "Malaysia",
1008        "MX" => "Mexico",
1009        "MM" => "Myanmar",
1010        "NL" => "Netherlands",
1011        "NZ" => "New Zealand",
1012        "NG" => "Nigeria",
1013        "NO" => "Norway",
1014        "PK" => "Pakistan",
1015        "PH" => "Philippines",
1016        "PL" => "Poland",
1017        "PT" => "Portugal",
1018        "QA" => "Qatar",
1019        "RO" => "Romania",
1020        "RU" => "Russia",
1021        "SA" => "Saudi Arabia",
1022        "SG" => "Singapore",
1023        "ZA" => "South Africa",
1024        "ES" => "Spain",
1025        "LK" => "Sri Lanka",
1026        "SE" => "Sweden",
1027        "CH" => "Switzerland",
1028        "TW" => "Taiwan",
1029        "TH" => "Thailand",
1030        "TL" => "Timor-Leste",
1031        "TR" => "Turkey",
1032        "AE" => "United Arab Emirates",
1033        "GB" => "United Kingdom",
1034        "US" => "United States",
1035        "VN" => "Vietnam",
1036        _ => return code.to_uppercase(),
1037    }
1038    .to_string()
1039}
1040
1041/// Extract a 2-letter country code from a case slug like `cases/id/corruption/2024/...`.
1042fn extract_country_from_case_slug(slug: &str) -> Option<String> {
1043    let parts: Vec<&str> = slug.split('/').collect();
1044    if parts.len() >= 2 {
1045        let candidate = parts[1];
1046        if candidate.len() == 2 && candidate.chars().all(|c| c.is_ascii_lowercase()) {
1047            return Some(candidate.to_string());
1048        }
1049    }
1050    None
1051}
1052
1053fn format_enum(s: &str) -> String {
1054    if let Some(custom) = s.strip_prefix("custom:") {
1055        return custom.to_string();
1056    }
1057    s.split('_')
1058        .map(|word| {
1059            let mut chars = word.chars();
1060            match chars.next() {
1061                None => String::new(),
1062                Some(c) => {
1063                    let upper: String = c.to_uppercase().collect();
1064                    upper + chars.as_str()
1065                }
1066            }
1067        })
1068        .collect::<Vec<_>>()
1069        .join(" ")
1070}
1071
1072fn render_dl_field(html: &mut String, label: &str, value: &str) {
1073    if !value.is_empty() {
1074        html.push_str(&format!(
1075            "          <span class=\"loom-field\"><strong>{label}:</strong> {}</span>\n",
1076            escape(value)
1077        ));
1078    }
1079}
1080
1081fn render_dl_opt(html: &mut String, label: &str, value: Option<&String>) {
1082    if let Some(v) = value {
1083        render_dl_field(html, label, v);
1084    }
1085}
1086
1087fn render_dl_opt_formatted(html: &mut String, label: &str, value: Option<&String>) {
1088    if let Some(v) = value {
1089        render_dl_field(html, label, &format_enum(v));
1090    }
1091}
1092
1093fn render_dl_item(html: &mut String, label: &str, value: &str) {
1094    if !value.is_empty() {
1095        html.push_str(&format!(
1096            "    <dt>{label}</dt>\n    <dd>{}</dd>\n",
1097            escape(value)
1098        ));
1099    }
1100}
1101
1102fn render_dl_opt_item(html: &mut String, label: &str, value: Option<&String>) {
1103    if let Some(v) = value {
1104        render_dl_item(html, label, v);
1105    }
1106}
1107
1108fn render_dl_opt_country(html: &mut String, label: &str, value: Option<&String>) {
1109    if let Some(v) = value {
1110        render_dl_field(html, label, &country_name(v));
1111    }
1112}
1113
1114fn render_dl_opt_country_item(html: &mut String, label: &str, value: Option<&String>) {
1115    if let Some(v) = value {
1116        render_dl_item(html, label, &country_name(v));
1117    }
1118}
1119
1120fn render_dl_opt_formatted_item(html: &mut String, label: &str, value: Option<&String>) {
1121    if let Some(v) = value {
1122        render_dl_item(html, label, &format_enum(v));
1123    }
1124}
1125
1126#[cfg(test)]
1127mod tests {
1128    use super::*;
1129    use crate::output::{CaseOutput, NodeOutput, RelOutput};
1130    use crate::parser::SourceEntry;
1131
1132    fn make_case() -> CaseOutput {
1133        CaseOutput {
1134            id: "01TESTCASE0000000000000000".into(),
1135            case_id: "test-case".into(),
1136            title: "Test Corruption Case".into(),
1137            summary: "A politician was caught accepting bribes.".into(),
1138            tags: vec!["bribery".into(), "government".into()],
1139            slug: None,
1140            case_type: None,
1141            amounts: vec![],
1142            status: None,
1143            nodes: vec![
1144                NodeOutput {
1145                    id: "01AAA".into(),
1146                    label: "person".into(),
1147                    name: "John Doe".into(),
1148                    slug: Some("people/id/john-doe--governor-of-test-province".into()),
1149                    qualifier: Some("Governor of Test Province".into()),
1150                    description: None,
1151                    thumbnail: Some("https://files.example.com/thumb.webp".into()),
1152                    aliases: vec![],
1153                    urls: vec![],
1154                    role: vec!["politician".into()],
1155                    nationality: Some("ID".into()),
1156                    date_of_birth: None,
1157                    place_of_birth: None,
1158                    status: Some("convicted".into()),
1159                    org_type: None,
1160                    jurisdiction: None,
1161                    headquarters: None,
1162                    founded_date: None,
1163                    registration_number: None,
1164                    event_type: None,
1165                    occurred_at: None,
1166                    severity: None,
1167                    doc_type: None,
1168                    issued_at: None,
1169                    issuing_authority: None,
1170                    case_number: None,
1171                    case_type: None,
1172                    amounts: vec![],
1173                    asset_type: None,
1174                    value: None,
1175                    tags: vec![],
1176                },
1177                NodeOutput {
1178                    id: "01BBB".into(),
1179                    label: "organization".into(),
1180                    name: "KPK".into(),
1181                    slug: Some("organizations/id/kpk--anti-corruption-commission".into()),
1182                    qualifier: Some("Anti-Corruption Commission".into()),
1183                    description: None,
1184                    thumbnail: None,
1185                    aliases: vec![],
1186                    urls: vec![],
1187                    role: vec![],
1188                    nationality: None,
1189                    date_of_birth: None,
1190                    place_of_birth: None,
1191                    status: None,
1192                    org_type: Some("government_agency".into()),
1193                    jurisdiction: Some(Jurisdiction {
1194                        country: "ID".into(),
1195                        subdivision: None,
1196                    }),
1197                    headquarters: None,
1198                    founded_date: None,
1199                    registration_number: None,
1200                    event_type: None,
1201                    occurred_at: None,
1202                    severity: None,
1203                    doc_type: None,
1204                    issued_at: None,
1205                    issuing_authority: None,
1206                    case_number: None,
1207                    case_type: None,
1208                    amounts: vec![],
1209                    asset_type: None,
1210                    value: None,
1211                    tags: vec![],
1212                },
1213                NodeOutput {
1214                    id: "01CCC".into(),
1215                    label: "event".into(),
1216                    name: "Arrest".into(),
1217                    slug: None,
1218                    qualifier: None,
1219                    description: Some("John Doe arrested by KPK.".into()),
1220                    thumbnail: None,
1221                    aliases: vec![],
1222                    urls: vec![],
1223                    role: vec![],
1224                    nationality: None,
1225                    date_of_birth: None,
1226                    place_of_birth: None,
1227                    status: None,
1228                    org_type: None,
1229                    jurisdiction: None,
1230                    headquarters: None,
1231                    founded_date: None,
1232                    registration_number: None,
1233                    event_type: Some("arrest".into()),
1234                    occurred_at: Some("2024-03-15".into()),
1235                    severity: None,
1236                    doc_type: None,
1237                    issued_at: None,
1238                    issuing_authority: None,
1239                    case_number: None,
1240                    case_type: None,
1241                    amounts: vec![],
1242                    asset_type: None,
1243                    value: None,
1244                    tags: vec![],
1245                },
1246            ],
1247            relationships: vec![RelOutput {
1248                id: "01DDD".into(),
1249                rel_type: "investigated_by".into(),
1250                source_id: "01BBB".into(),
1251                target_id: "01CCC".into(),
1252                source_urls: vec![],
1253                description: None,
1254                amounts: vec![],
1255                valid_from: None,
1256                valid_until: None,
1257            }],
1258            sources: vec![SourceEntry::Url("https://example.com/article".into())],
1259        }
1260    }
1261
1262    #[test]
1263    fn render_case_produces_valid_html() {
1264        let case = make_case();
1265        let config = HtmlConfig::default();
1266        let html = render_case(&case, &config).unwrap();
1267
1268        assert!(html.starts_with("<article"));
1269        assert!(html.ends_with("</article>\n"));
1270        assert!(html.contains("data-og-title=\"Test Corruption Case\""));
1271        assert!(html.contains("data-og-description="));
1272        assert!(html.contains("<h1 itemprop=\"headline\">Test Corruption Case</h1>"));
1273        assert!(html.contains("loom-tag"));
1274        assert!(html.contains("bribery"));
1275        assert!(html.contains("John Doe"));
1276        assert!(html.contains("KPK"));
1277        assert!(html.contains("Arrest"));
1278        assert!(html.contains("2024-03-15"));
1279        assert!(html.contains("application/ld+json"));
1280        // Case header has canvas link
1281        assert!(html.contains("View on canvas"));
1282        assert!(html.contains("/canvas/01TESTCASE0000000000000000"));
1283    }
1284
1285    #[test]
1286    fn render_case_has_sources() {
1287        let case = make_case();
1288        let config = HtmlConfig::default();
1289        let html = render_case(&case, &config).unwrap();
1290        assert!(html.contains("Sources"));
1291        assert!(html.contains("https://example.com/article"));
1292    }
1293
1294    #[test]
1295    fn render_case_entity_cards_link_to_static_views() {
1296        let case = make_case();
1297        let config = HtmlConfig::default();
1298        let html = render_case(&case, &config).unwrap();
1299
1300        // Entity cards should link to static views, not canvas
1301        assert!(html.contains("href=\"/people/id/john-doe--governor-of-test-province\""));
1302        assert!(html.contains("href=\"/organizations/id/kpk--anti-corruption-commission\""));
1303        // Should NOT link to /canvas/ for entities with slugs
1304        assert!(!html.contains("href=\"/canvas/01AAA\""));
1305        assert!(!html.contains("href=\"/canvas/01BBB\""));
1306    }
1307
1308    #[test]
1309    fn render_case_entity_cards_fallback_to_canvas() {
1310        let mut case = make_case();
1311        let config = HtmlConfig::default();
1312        // Remove slugs from entities
1313        for node in &mut case.nodes {
1314            node.slug = None;
1315        }
1316        let html = render_case(&case, &config).unwrap();
1317
1318        // Without slugs, entity cards fall back to canvas links
1319        assert!(html.contains("href=\"/canvas/01AAA\""));
1320        assert!(html.contains("href=\"/canvas/01BBB\""));
1321    }
1322
1323    #[test]
1324    fn render_case_omits_connections_table() {
1325        let case = make_case();
1326        let config = HtmlConfig::default();
1327        let html = render_case(&case, &config).unwrap();
1328        // Connections table is intentionally omitted — relationships are
1329        // already expressed in People/Organizations cards and Timeline
1330        assert!(!html.contains("Connections"));
1331        assert!(!html.contains("loom-rel-table"));
1332    }
1333
1334    #[test]
1335    fn render_person_page() {
1336        let case = make_case();
1337        let config = HtmlConfig::default();
1338        let person = &case.nodes[0];
1339        let cases_list = vec![("test-case".into(), "Test Corruption Case".into())];
1340        let html = render_person(person, &cases_list, &config).unwrap();
1341
1342        assert!(html.contains("itemtype=\"https://schema.org/Person\""));
1343        assert!(html.contains("John Doe"));
1344        assert!(html.contains("Governor of Test Province"));
1345        assert!(html.contains("/canvas/01AAA"));
1346        assert!(html.contains("Test Corruption Case"));
1347        assert!(html.contains("application/ld+json"));
1348    }
1349
1350    #[test]
1351    fn render_organization_page() {
1352        let case = make_case();
1353        let config = HtmlConfig::default();
1354        let org = &case.nodes[1];
1355        let cases_list = vec![("test-case".into(), "Test Corruption Case".into())];
1356        let html = render_organization(org, &cases_list, &config).unwrap();
1357
1358        assert!(html.contains("itemtype=\"https://schema.org/Organization\""));
1359        assert!(html.contains("KPK"));
1360        assert!(html.contains("Indonesia")); // jurisdiction (resolved from ID)
1361    }
1362
1363    #[test]
1364    fn render_sitemap_includes_all_urls() {
1365        let cases = vec![("cases/id/corruption/2024/test-case".into(), "Case 1".into())];
1366        let people = vec![("people/id/john-doe".into(), "John".into())];
1367        let orgs = vec![("organizations/id/test-corp".into(), "Corp".into())];
1368        let xml = render_sitemap(&cases, &people, &orgs, "https://redberrythread.org");
1369
1370        assert!(xml.contains("<?xml"));
1371        assert!(xml.contains("/cases/id/corruption/2024/test-case"));
1372        assert!(xml.contains("/people/id/john-doe"));
1373        assert!(xml.contains("/organizations/id/test-corp"));
1374    }
1375
1376    #[test]
1377    fn escape_html_special_chars() {
1378        assert_eq!(escape("<script>"), "&lt;script&gt;");
1379        assert_eq!(escape("AT&T"), "AT&amp;T");
1380        assert_eq!(escape("\"quoted\""), "&quot;quoted&quot;");
1381    }
1382
1383    #[test]
1384    fn truncate_short_string() {
1385        assert_eq!(truncate("hello", 10), "hello");
1386    }
1387
1388    #[test]
1389    fn truncate_long_string() {
1390        let long = "a".repeat(200);
1391        let result = truncate(&long, 120);
1392        assert!(result.len() <= 120);
1393        assert!(result.ends_with("..."));
1394    }
1395
1396    #[test]
1397    fn format_enum_underscore() {
1398        assert_eq!(format_enum("investigated_by"), "Investigated By");
1399        assert_eq!(format_enum("custom:Special Type"), "Special Type");
1400    }
1401
1402    #[test]
1403    fn thumbnail_key_deterministic() {
1404        let k1 = thumbnail_key("https://example.com/photo.jpg");
1405        let k2 = thumbnail_key("https://example.com/photo.jpg");
1406        assert_eq!(k1, k2);
1407        assert!(k1.starts_with("thumbnails/"));
1408        assert!(k1.ends_with(".webp"));
1409        // Key hex part is 32 chars
1410        let hex_part = k1
1411            .strip_prefix("thumbnails/")
1412            .and_then(|s| s.strip_suffix(".webp"))
1413            .unwrap_or("");
1414        assert_eq!(hex_part.len(), THUMB_KEY_HEX_LEN);
1415    }
1416
1417    #[test]
1418    fn thumbnail_key_different_urls_differ() {
1419        let k1 = thumbnail_key("https://example.com/a.jpg");
1420        let k2 = thumbnail_key("https://example.com/b.jpg");
1421        assert_ne!(k1, k2);
1422    }
1423
1424    #[test]
1425    fn rewrite_thumbnail_url_no_config() {
1426        let config = HtmlConfig::default();
1427        let result = rewrite_thumbnail_url("https://example.com/photo.jpg", &config);
1428        assert_eq!(result, "https://example.com/photo.jpg");
1429    }
1430
1431    #[test]
1432    fn rewrite_thumbnail_url_with_base() {
1433        let config = HtmlConfig {
1434            thumbnail_base_url: Some("http://files.garage.local:3902/files".into()),
1435        };
1436        let result = rewrite_thumbnail_url("https://example.com/photo.jpg", &config);
1437        assert!(result.starts_with("http://files.garage.local:3902/files/thumbnails/"));
1438        assert!(result.ends_with(".webp"));
1439        assert!(!result.contains("example.com"));
1440    }
1441
1442    #[test]
1443    fn rewrite_thumbnail_url_already_rewritten() {
1444        let config = HtmlConfig {
1445            thumbnail_base_url: Some("https://files.redberrythread.org".into()),
1446        };
1447        let already = "https://files.redberrythread.org/thumbnails/6fc3a49567393053be6138aa346fa97a.webp";
1448        let result = rewrite_thumbnail_url(already, &config);
1449        assert_eq!(result, already, "should not double-hash already-rewritten URLs");
1450    }
1451
1452    #[test]
1453    fn render_case_rewrites_thumbnails() {
1454        let case = make_case();
1455        let config = HtmlConfig {
1456            thumbnail_base_url: Some("http://garage.local/files".into()),
1457        };
1458        let html = render_case(&case, &config).unwrap();
1459
1460        // Original URL should not appear in img src
1461        assert!(!html.contains("src=\"https://files.example.com/thumb.webp\""));
1462        // Rewritten URL should appear
1463        assert!(html.contains("src=\"http://garage.local/files/thumbnails/"));
1464        // OG image should also be rewritten
1465        assert!(html.contains("data-og-image=\"http://garage.local/files/thumbnails/"));
1466    }
1467
1468    #[test]
1469    fn render_person_rewrites_thumbnails() {
1470        let case = make_case();
1471        let person = &case.nodes[0];
1472        let config = HtmlConfig {
1473            thumbnail_base_url: Some("http://garage.local/files".into()),
1474        };
1475        let html = render_person(person, &[], &config).unwrap();
1476
1477        assert!(!html.contains("src=\"https://files.example.com/thumb.webp\""));
1478        assert!(html.contains("src=\"http://garage.local/files/thumbnails/"));
1479    }
1480
1481    #[test]
1482    fn render_case_with_related_cases() {
1483        let mut case = make_case();
1484        // Add a related_to relationship and target case node
1485        case.relationships.push(RelOutput {
1486            id: "01RELID".into(),
1487            rel_type: "related_to".into(),
1488            source_id: "01TESTCASE0000000000000000".into(),
1489            target_id: "01TARGETCASE000000000000000".into(),
1490            source_urls: vec![],
1491            description: Some("Connected bribery scandal".into()),
1492            amounts: vec![],
1493            valid_from: None,
1494            valid_until: None,
1495        });
1496        case.nodes.push(NodeOutput {
1497            id: "01TARGETCASE000000000000000".into(),
1498            label: "case".into(),
1499            name: "Target Scandal Case".into(),
1500            slug: Some("cases/id/corruption/2002/target-scandal".into()),
1501            qualifier: None,
1502            description: None,
1503            thumbnail: None,
1504            aliases: vec![],
1505            urls: vec![],
1506            role: vec![],
1507            nationality: None,
1508            date_of_birth: None,
1509            place_of_birth: None,
1510            status: None,
1511            org_type: None,
1512            jurisdiction: None,
1513            headquarters: None,
1514            founded_date: None,
1515            registration_number: None,
1516            event_type: None,
1517            occurred_at: None,
1518            severity: None,
1519            doc_type: None,
1520            issued_at: None,
1521            issuing_authority: None,
1522            case_number: None,
1523            case_type: None,
1524            amounts: vec![],
1525            asset_type: None,
1526            value: None,
1527            tags: vec![],
1528        });
1529
1530        let config = HtmlConfig::default();
1531        let html = render_case(&case, &config).unwrap();
1532
1533        assert!(html.contains("loom-related-cases"));
1534        assert!(html.contains("Related Cases"));
1535        assert!(html.contains("Target Scandal Case"));
1536        assert!(html.contains("loom-related-card"));
1537        assert!(html.contains("Connected bribery scandal"));
1538    }
1539
1540    #[test]
1541    fn render_case_without_related_cases() {
1542        let case = make_case();
1543        let config = HtmlConfig::default();
1544        let html = render_case(&case, &config).unwrap();
1545
1546        assert!(!html.contains("loom-related-cases"));
1547    }
1548}