silcrow 0.2.1

Deterministic CLI for a citable, checkable graph of laws in a git tree.
use super::*;

#[test]
fn forms_that_render_the_same_stay() {
    let table = |delim: &str| format!("# S\n\n| Plan | Fee |\n{delim}\n| Basic | 5% |\n");
    stays(
        &MARKDOWN,
        &table("| --- | --- |"),
        &table("|------|-----|"),
        1,
    );
    stays(
        &MARKDOWN,
        &table("| :-: | --: |"),
        &table("| :---: | -----: |"),
        1,
    );
    moves(
        &MARKDOWN,
        &table("| --- | --- |"),
        &table("| :-- | --- |"),
        1,
    );
    moves(
        &MARKDOWN,
        &table("| --- | --- |"),
        &table("| --- | --: |"),
        1,
    );
    let heading = |h: &str| format!("{h}\n\nText.\n");
    stays(&MARKDOWN, &heading("## Fees"), &heading("## Fees ##"), 1);
    stays(
        &MARKDOWN,
        &heading("## Fees"),
        &heading("## Fees #####  "),
        1,
    );
    moves(&MARKDOWN, &heading("## Fees"), &heading("## Fees#"), 1);
    moves(&MARKDOWN, &heading("## Fees"), &heading("## Fees \\#"), 1);
    let list = |a: &str, b: &str| format!("# S\n\n{a} first\n{b} second\n");
    stays(&MARKDOWN, &list("1.", "2."), &list("1.", "3."), 1);
    stays(&MARKDOWN, &list("1.", "2."), &list("01.", "1."), 1);
    moves(&MARKDOWN, &list("1.", "2."), &list("2.", "3."), 1);
    stays(&MARKDOWN, &list("1)", "2)"), &list("1.", "2."), 1);
    moves(&MARKDOWN, &list("1.", "2."), &list("1.", "2)"), 1);
    stays(&MARKDOWN, &list("*", "*"), &list("-", "-"), 1);
    stays(&MARKDOWN, &list("+", "+"), &list("-", "-"), 1);
    moves(&MARKDOWN, &list("-", "-"), &list("-", "*"), 1);
    moves(&MARKDOWN, &list("-", "-"), &list("1.", "2."), 1);
    let link = |title: &str| {
        format!("# S\n\nThe [fee](https://e.com/f {title}) and ![c](c.png {title}).\n")
    };
    stays(&MARKDOWN, &link("\"Fee table\""), &link("'Fee table'"), 1);
    stays(&MARKDOWN, &link("\"Fee table\""), &link("(Fee table)"), 1);
    moves(&MARKDOWN, &link("\"Fee table\""), &link("'Old table'"), 1);
    moves(&MARKDOWN, &link("'a \"b\"'"), &link("\"a \\\"b\\\"\""), 1);
    let text = |title: &str| format!("# S\n\nThe \\[fee](https://e.com/f {title}).\n");
    moves(&MARKDOWN, &text("\"Fee\""), &text("'Fee'"), 1);
    let def = |title: &str| format!("# S\n\nSee [rt].\n\n[rt]: https://e.com/r {title}\n");
    stays(&MARKDOWN, &def("\"Rates\""), &def("'Rates'"), 1);
    moves(&MARKDOWN, &def("\"Rates\""), &def("'Fees'"), 1);
}

/// What deno fmt and Prettier rewrite in a Markdown file renders the same, and a near edit
/// that renders otherwise still moves.
#[test]
fn what_a_markdown_formatter_rewrites_stays() {
    let doc = |h1: &str, h2: &str, body: &str| {
        format!("{h1}\n\n<!-- pin -->\n\nIntro.\n\n{h2}\n\n{body}\n\nMore.\n")
    };
    let body = "Some *a* and __b__, a [link](https://e.com \"t\").";
    let atx = doc("# Pay", "## Refunds", body);
    stays(
        &MARKDOWN,
        &atx,
        &doc("Pay\n===", "Refunds\n-------", body),
        3,
    );
    moves(
        &MARKDOWN,
        &atx,
        &doc("Pay\n===", "Refunds\n=======", body),
        3,
    );
    moves(&MARKDOWN, &atx, &doc("# Pay", "### Refunds", body), 3);
    moves(&MARKDOWN, &atx, &doc("# Pay", "Refunds", body), 3);
    let formatted = "Some _a_ and **b**, a [link]( https://e.com \"t\" ).";
    stays(&MARKDOWN, &atx, &doc("# Pay", "## Refunds", formatted), 3);
    for other in [
        "Some *a* and *b*, a [link](https://e.com \"t\").",
        "Some **a** and __b__, a [link](https://e.com \"t\").",
        "Some a and __b__, a [link](https://e.com \"t\").",
        "Some *a* and __b__, a [link](https://e.org \"t\").",
        "Some *a* and __b__, a [link] (https://e.com \"t\").",
    ] {
        moves(&MARKDOWN, &atx, &doc("# Pay", "## Refunds", other), 3);
    }
    // `*_a_*` nests two emphases, which `**a**` does not.
    moves(
        &MARKDOWN,
        &doc("# Pay", "## R", "*_a_*"),
        &doc("# Pay", "## R", "**a**"),
        3,
    );
    // An underscore inside a word is no emphasis, a star is.
    moves(
        &MARKDOWN,
        &doc("# Pay", "## R", "snake_case_name"),
        &doc("# Pay", "## R", "snake*case*name"),
        3,
    );
    let block = "# S\n\n<!-- pin -->\n\n[^n]: Note.\n\n    Code line.";
    stays(&MARKDOWN, block, &format!("{block}\n"), 3);
    moves(&MARKDOWN, block, &format!("{block}\n    Two."), 3);
}

#[test]
fn a_reference_link_reads_the_definition_it_resolves_to() {
    let doc = |link: &str, def: &str| {
        format!("# Refunds\n\n<!-- pin -->\nGo through the {link}.\n\n# Links\n\n{def}\n")
    };
    let full = |def: &str| doc("[approval portal][portal]", def);
    moves(
        &MARKDOWN,
        &full("[portal]: https://pay.example.com/approve"),
        &full("[portal]: https://evil.example.com/approve"),
        3,
    );
    moves(
        &MARKDOWN,
        &full("[portal]: https://pay.example.com \"Pay\""),
        &full("[portal]: https://pay.example.com \"Evil\""),
        3,
    );
    moves(
        &MARKDOWN,
        &full("[portal]: https://pay.example.com"),
        &full("[other]: https://pay.example.com"),
        3,
    );
    stays(
        &MARKDOWN,
        &full("[Portal]: https://pay.example.com\n[portal]: https://one.example.com"),
        &full("[Portal]: https://pay.example.com\n[portal]: https://two.example.com"),
        3,
    );
    for link in ["[portal]", "[portal][]", "![a chart][portal]"] {
        moves(
            &MARKDOWN,
            &doc(link, "[portal]: https://pay.example.com"),
            &doc(link, "[portal]: https://evil.example.com"),
            3,
        );
    }
    stays(
        &MARKDOWN,
        &doc(
            "[x](https://a.example)",
            "[portal]: https://pay.example.com",
        ),
        &doc(
            "[x](https://a.example)",
            "[portal]: https://evil.example.com",
        ),
        3,
    );
}

#[test]
fn a_footnote_reads_its_definition() {
    let doc = |def: &str| {
        format!("# Refunds\n\n<!-- pin -->\nPaid within 5 days.[^when]\n\n# Other\n\nUnrelated.\n\n{def}\n")
    };
    moves(
        &MARKDOWN,
        &doc("[^when]: Business days, excluding holidays."),
        &doc("[^when]: Calendar days."),
        3,
    );
    moves(
        &MARKDOWN,
        &doc("[^when]: Business days,\n    excluding holidays."),
        &doc("[^when]: Business days,\n    including holidays."),
        3,
    );
    moves(
        &MARKDOWN,
        &doc("[^when]: Business days.\n\n    Excluding holidays."),
        &doc("[^when]: Business days.\n\n    Including holidays."),
        3,
    );
    moves(
        &MARKDOWN,
        &doc("[^when]: Business days."),
        &doc("[^other]: Business days."),
        3,
    );
    stays(
        &MARKDOWN,
        &doc("[^when]: Business  days."),
        &doc("[^when]: Business days."),
        3,
    );
}

#[test]
fn heading_text_moves() {
    let a = "# Title\n\nhello\n";
    let b = "# Other\n\nhello\n";
    assert_ne!(pair(&MARKDOWN, a, 1).shape, pair(&MARKDOWN, b, 1).shape);
}

#[test]
fn paragraph_moves() {
    let a = "# Title\n\nhello\n";
    let b = "# Title\n\nhello world\n";
    assert_ne!(pair(&MARKDOWN, a, 1).shape, pair(&MARKDOWN, b, 1).shape);
}

#[test]
fn a_code_block_keeps_its_bytes() {
    moves(
        &MARKDOWN,
        "# S\n\n```js\npay(10)\n```\n",
        "# S\n\n```js\npay(20)\n```\n",
        1,
    );
    moves(
        &MARKDOWN,
        "# S\n\n```js\npay( 1)\n```\n",
        "# S\n\n```js\npay(1)\n```\n",
        1,
    );
    moves(
        &MARKDOWN,
        "# S\n\nText.\n\n    pay(10)\n",
        "# S\n\nText.\n\n    pay(20)\n",
        1,
    );
}

#[test]
fn only_a_closed_unescaped_comment_is_a_comment() {
    let doc =
        |tail: &str| format!("# Refunds\n\n<!-- pin -->\n\nRefunds are paid in 3 days. {tail}\n");
    moves(
        &MARKDOWN,
        &doc("<!-- Never pay in cash."),
        &doc("<!-- Always pay in cash."),
        3,
    );
    moves(
        &MARKDOWN,
        &doc("\\<!-- Never pay in cash. -->"),
        &doc("\\<!-- Always pay in cash. -->"),
        3,
    );
    moves(
        &MARKDOWN,
        &doc("<!--> Never pay in cash. <!-- x -->"),
        &doc("<!--> Always pay in cash. <!-- x -->"),
        3,
    );
    moves(
        &MARKDOWN,
        &doc("<!---> Never pay in cash. <!-- x -->"),
        &doc("<!---> Always pay in cash. <!-- x -->"),
        3,
    );
    stays(
        &MARKDOWN,
        &doc("<!-- Never pay in cash. -->"),
        &doc("<!-- Always pay in cash. -->"),
        3,
    );
    stays(
        &MARKDOWN,
        &doc("\\\\<!-- Never pay in cash. -->"),
        &doc("\\\\<!-- Always pay in cash. -->"),
        3,
    );
}

#[test]
fn raw_html_keeps_its_attribute_values_and_the_text_of_pre_script_and_style() {
    let doc = |body: &str| format!("# S\n\n{body}\n");
    for (a, b) in [
        ("<p title=\"a b\">t</p>", "<p title=\"a  b\">t</p>"),
        ("<p title='a b'>t</p>", "<p title='a\n b'>t</p>"),
        (
            "<script>\nlet s = 'a b';\n</script>",
            "<script>\nlet s = 'a  b';\n</script>",
        ),
        (
            "<style>\np::after { content: \"a b\"; }\n</style>",
            "<style>\np::after { content: \"a  b\"; }\n</style>",
        ),
        ("<pre>\na b\n</pre>", "<pre>\na  b\n</pre>"),
        (
            "Pay <span title=\"a b\">here</span>.",
            "Pay <span title=\"a  b\">here</span>.",
        ),
        (
            "Pay <code title='a b'>here</code>.",
            "Pay <code title='a\tb'>here</code>.",
        ),
        (
            "Run <script>go('a b')</script> now.",
            "Run <script>go('a  b')</script> now.",
        ),
        ("<p>a b</p>", "<p>a\u{a0}b</p>"),
        ("Pay a b.", "Pay a\u{3000}b."),
    ] {
        moves(&MARKDOWN, &doc(a), &doc(b), 1);
    }
    for (a, b) in [
        ("<p title=\"a b\">t</p>", "<p  title='a b'>t</p>"),
        ("<p>a b</p>", "<p>a\n   b</p>"),
        (
            "Pay <span title=\"a b\">here</span>.",
            "Pay <span  title='a b'>here</span>.",
        ),
        (
            "Pay \\<span title=\"a b\">here.",
            "Pay \\<span title=\"a  b\">here.",
        ),
        ("Pay a b.", "Pay a \t b."),
    ] {
        stays(&MARKDOWN, &doc(a), &doc(b), 1);
    }
}

#[test]
fn html_after_a_comment_is_hashed_and_the_comment_is_not() {
    moves(
        &MARKDOWN,
        "# S\n\n<!-- c --> <p>Price is 10</p>\n",
        "# S\n\n<!-- c --> <p>Price is 20</p>\n",
        1,
    );
    stays(
        &MARKDOWN,
        "# S\n\n<p>Price</p>\n",
        "# S\n\n<!-- note --><p>Price</p>\n",
        1,
    );
    stays(
        &MARKDOWN,
        "# S\n\nSome  prose\nwrapped.\n",
        "# S\n\nSome prose wrapped.\n",
        1,
    );
}

/// MDX reads a `{/* */}` comment as a comment, as Markdown reads an HTML one, and keeps one in
/// inline code or one that does not close, which its compiler renders or fails. Markdown keeps it.
#[test]
fn an_mdx_comment_is_a_comment() {
    let page = |body: &str| format!("# S\n\n{body}\n");
    stays(
        &MDX,
        &page("Fees {/* old */} apply."),
        &page("Fees {/* new */} apply."),
        1,
    );
    stays(
        &MDX,
        &page("Fees apply.\n\n{/* old */}"),
        &page("Fees apply.\n\n{/* new */}"),
        1,
    );
    moves(
        &MDX,
        &page("Fees `{/* old */}` apply."),
        &page("Fees `{/* new */}` apply."),
        1,
    );
    moves(
        &MDX,
        &page("Fees {/* old apply."),
        &page("Fees {/* new apply."),
        1,
    );
    moves(
        &MARKDOWN,
        &page("Fees {/* old */} apply."),
        &page("Fees {/* new */} apply."),
        1,
    );
}