#![allow( unused_imports, unused_variables, clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::indexing_slicing, clippy::redundant_closure, clippy::useless_conversion, clippy::bool_assert_comparison, clippy::collapsible_if, clippy::len_zero, clippy::needless_borrow, clippy::redundant_locals, clippy::too_many_lines, clippy::needless_pass_by_value, suspicious_double_ref_op )] //! Final-output deep verification: 10,000 scenarios (5,000 red-team content //! verification + 5,000 blue-team structural verification). //! Each input is rendered through all six surfaces and assertions check the //! actual output content — fallback preservation, coverage integrity, //! payload types, serialization round-trip, schema correctness, and //! surface-specific formatting. use std::collections::BTreeSet; use vak_delivery::AnswerDraft; use vak_delivery::DELIVERY_SCHEMA_VERSION; use vak_delivery::DeliveryContent; use vak_delivery::DeliveryJob; use vak_delivery::DeliveryKind; use vak_delivery::DeliveryPacket; use vak_delivery::DeliveryPayload; use vak_delivery::DeliveryPosture; use vak_delivery::DeliveryProfile; use vak_delivery::Markup; use vak_delivery::render; type Scenario = (String, Box); fn tc(name: &str, f: F) -> Scenario { (name.to_string(), Box::new(f)) } fn run_fn(label: &str, scenarios: Vec) { let total = scenarios.len(); let mut passed = 0usize; let mut failures: Vec = Vec::new(); for (name, test) in scenarios { match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| test())) { Ok(()) => passed += 1, Err(_) => failures.push(name), } } assert_eq!( passed, total, "{label}: {passed}/{total} passed — failures: {failures:?}" ); println!(" ✓ {label}: {total} scenarios passed"); } fn make_job(source: &str, markup: Markup, surface: &str) -> DeliveryJob { DeliveryJob { job_id: "test-job".into(), target: format!("{surface}:one"), kind: DeliveryKind::Assistant, content: DeliveryContent::Answer(AnswerDraft::from_markdown(source)), profile: DeliveryProfile { surface: surface.into(), markup, max_chars: None, supports_tables: true, supports_code_blocks: true, supports_links: true, supports_actions: false, template: None, posture: DeliveryPosture::default(), }, skill_registry: None, } } fn pt(packet: &DeliveryPacket) -> String { match &packet.payload { DeliveryPayload::Text(s) => s.clone(), DeliveryPayload::Structured(_) => packet.fallback_markdown.clone(), } } const SURFACES: &[(&str, Markup)] = &[ ("telegram", Markup::TelegramHtml), ("discord", Markup::DiscordMarkdown), ("slack", Markup::SlackMrkdwn), ("desktop", Markup::Markdown), ("terminal", Markup::Plain), ("admin", Markup::Plain), ]; fn src_idx(base: &str, i: usize) -> String { format!("{}\n\n", base, i) } fn render_all(source: &str) -> Vec<(DeliveryPacket, &'static str)> { let mut results = Vec::new(); for (surface, markup) in SURFACES { let job = make_job(source, *markup, surface); let packet = render(&job).expect("render"); results.push((packet, *surface)); } results } fn is_h1(template: &str) -> bool { template.starts_with("# ") && !template.starts_with("## ") } #[test] fn content_headings_output() { let mut scenarios: Vec = vec![]; let templates: &[&str] = &[ "# Title", "## Subtitle", "###### H6", "# T\n## S\n### H", "# Title with **bold**", "# Multiple\n\n# Another", "# H\n\nSome text.", ]; for i in 0..500 { let t = templates[i % templates.len()]; let source = src_idx(t, i); let i = i; scenarios.push(tc(&format!("heading_{i}"), move || { for (packet, surface) in render_all(&source) { let text = pt(&packet); let src = source.as_str(); match surface { "telegram" => { assert!(text.contains(""), "telegram heading missing : {src}"); } "discord" => { if is_h1(t) { assert!(text.contains(""), "discord H1 missing : {src}"); } else { assert!(!text.is_empty(), "discord H2+ empty: {src}"); } } "desktop" | "admin" => { assert!( text.contains("Title") || text.contains("Subtitle") || text.contains("H") || src.contains('#'), "desktop heading text missing: {src}" ); } _ => {} } } })); } run_fn("content_headings_output", scenarios); } #[test] fn content_code_blocks_output() { let mut scenarios: Vec = vec![]; let templates: &[&str] = &[ "```rust\nfn main() {}\n```", "```python\nprint('hello')\n```", "```\nplain code\n```", "```diff\n- removed\n+ added\n```", "```\n```\n```", r#"``` fn hello() { println!("hi"); } ```"#, ]; for i in 0..500 { let t = templates[i % templates.len()]; let source = src_idx(t, i); let i = i; scenarios.push(tc(&format!("code_{i}"), move || { for (packet, surface) in render_all(&source) { let text = pt(&packet); let src = source.as_str(); match surface { "telegram" => { assert!( text.contains("
") || text.contains(""),
                            "telegram code missing 
/: {src}"
                        );
                    }
                    "desktop" | "discord" | "slack" => {
                        assert!(
                            text.contains("```")
                                || text.contains("code")
                                || text.contains("fn")
                                || text.contains("removed")
                                || text.contains("print"),
                            "surface code missing content: {surface}: {src}"
                        );
                    }
                    _ => {
                        // Plain surfaces strip code fences; empty code blocks yield empty text
                        if t.contains("fn")
                            || t.contains("removed")
                            || t.contains("print")
                            || t.contains("hello")
                        {
                            assert!(
                                !text.is_empty()
                                    && (text.contains("code")
                                        || text.contains("fn")
                                        || text.contains("removed")
                                        || text.contains("print")
                                        || text.contains("hello")),
                                "plain code missing content: {surface}: {src}"
                            );
                        }
                    }
                }
            }
        }));
    }
    run_fn("content_code_blocks_output", scenarios);
}

#[test]
fn content_lists_output() {
    let mut scenarios: Vec = vec![];
    let templates: &[&str] = &[
        "- a\n- b\n- c",
        "1. first\n2. second\n3. third",
        "- [ ] todo\n- [x] done",
        "1. one\n   1. nested",
        "* star list\n* second",
        "+ plus list\n+ second",
    ];
    for i in 0..500 {
        let t = templates[i % templates.len()];
        let source = src_idx(t, i);
        let i = i;
        scenarios.push(tc(&format!("list_{i}"), move || {
            for (packet, surface) in render_all(&source) {
                let text = pt(&packet);
                let src = source.as_str();
                assert!(
                    text.contains("a")
                        || text.contains("first")
                        || text.contains("todo")
                        || text.contains("done"),
                    "list content missing for {src}"
                );
                if src.contains("- [ ]") || src.contains("- [x]") {
                    assert!(
                        text.contains('☐')
                            || text.contains("[ ]")
                            || text.contains('☑')
                            || text.contains("[x]"),
                        "task list marker missing for {surface}: {src}"
                    );
                }
            }
        }));
    }
    run_fn("content_lists_output", scenarios);
}

#[test]
fn content_emphasis_output() {
    let mut scenarios: Vec = vec![];
    let templates: &[&str] = &[
        "**bold**",
        "*italic*",
        "~~strikethrough~~",
        "**a** *b* ~~c~~",
        "`inline code`",
        "**bold** and *italic* and ~~strike~~",
    ];
    for i in 0..500 {
        let t = templates[i % templates.len()];
        let source = src_idx(t, i);
        let i = i;
        scenarios.push(tc(&format!("emph_{i}"), move || {
            for (packet, surface) in render_all(&source) {
                let text = pt(&packet);
                let src = source.as_str();
                if src.contains("**bold**") {
                    match surface {
                        "telegram" => {
                            assert!(text.contains(""), "bold missing  for telegram: {src}");
                        }
                        _ => {
                            assert!(!text.is_empty(), "bold empty: {src}");
                        }
                    }
                }
                if src.contains("~~strikethrough~~") {
                    match surface {
                        "telegram" => {
                            assert!(
                                text.contains(""),
                                "strike missing  for telegram: {src}"
                            );
                        }
                        _ => {
                            assert!(!text.is_empty(), "strike empty: {src}");
                        }
                    }
                }
            }
        }));
    }
    run_fn("content_emphasis_output", scenarios);
}

#[test]
fn content_tables_output() {
    let mut scenarios: Vec = vec![];
    let templates: &[&str] = &[
        "| A | B |\n|---|---|\n| 1 | 2 |",
        "| a | b | c |\n|---|---|---|\n| 1 | 2 | 3 |",
        "| Col1 | Col2 |\n| --- | --- |\n| val1 | val2 |",
        "| single |\n|---|\n| val |",
    ];
    for i in 0..500 {
        let t = templates[i % templates.len()];
        let source = src_idx(t, i);
        let i = i;
        scenarios.push(tc(&format!("table_{i}"), move || {
            for (packet, surface) in render_all(&source) {
                let text = pt(&packet);
                let src = source.as_str();
                assert!(
                    text.contains("1")
                        || text.contains("val1")
                        || text.contains("val")
                        || text.contains("A"),
                    "table data missing for {surface}: {src}"
                );
            }
        }));
    }
    run_fn("content_tables_output", scenarios);
}

#[test]
fn content_links_output() {
    let mut scenarios: Vec = vec![];
    let templates: &[&str] = &[
        "[label](https://example.com)",
        "[text](https://x.com \"Title\")",
        "![alt](https://x.com/img.png)",
        "[a](https://a.com)\n\n[b](https://b.com)",
    ];
    for i in 0..500 {
        let t = templates[i % templates.len()];
        let source = src_idx(t, i);
        let i = i;
        scenarios.push(tc(&format!("link_{i}"), move || {
            for (packet, surface) in render_all(&source) {
                let text = pt(&packet);
                let src = source.as_str();
                assert!(
                    text.contains("label")
                        || text.contains("text")
                        || text.contains("alt")
                        || text.contains("https://")
                        || text.contains("example.com")
                        || text.contains("a.com"),
                    "link text/url missing in {surface}: {src}"
                );
            }
        }));
    }
    run_fn("content_links_output", scenarios);
}

#[test]
fn content_quotes_output() {
    let mut scenarios: Vec = vec![];
    let templates: &[&str] = &[
        "> quoted text",
        "> > nested quote",
        "> quote\n> second line",
        "# T\n\n> important",
    ];
    for i in 0..500 {
        let t = templates[i % templates.len()];
        let source = src_idx(t, i);
        let i = i;
        scenarios.push(tc(&format!("quote_{i}"), move || {
            for (packet, surface) in render_all(&source) {
                let text = pt(&packet);
                let src = source.as_str();
                if src.contains("quoted") || src.contains("important") {
                    assert!(
                        text.contains("quoted")
                            || text.contains("important")
                            || text.contains("
"), "quote content missing for {surface}: {src}" ); } else { assert!(!text.is_empty(), "quote empty for {surface}: {src}"); } } })); } run_fn("content_quotes_output", scenarios); } #[test] fn content_html_escaping_output() { let mut scenarios: Vec = vec![]; let templates: &[&str] = &[ "", "bold", "", "Text < > & ", "