wsb 0.258.6441

Workspace tools for every project under $PRJ: git across submodules, template stamping, GitHub, and the workbench
Documentation
//! What the parity document says, and what it must never say.
//!
//! Built on an in-memory catalogue rather than the workspace's. The workspace
//! changes every time anybody writes a test, so a test that read it would
//! assert whatever happened to be true this afternoon — and the properties
//! worth pinning are properties of the RENDERING, which a fixture states
//! exactly.

use super::*;
use crate::persist::local::models::catalogued_test::TestLanguage;
use std::sync::Arc;

const MEMBERS: [&str; 3] = ["family/one", "family/two", "family/three"];

fn members() -> Vec<String> {
    MEMBERS.iter().map(|m| m.to_string()).collect()
}

fn test(repo: &str, number: i64, name: &str, description: &str) -> CataloguedTest {
    CataloguedTest {
        id: format!("{repo}:{name}"),
        repo: repo.to_string(),
        language: TestLanguage::Rust,
        number: Some(number),
        function_name: name.to_string(),
        description: description.to_string(),
        // A row per line: the table keys on repo + file + line, which is the
        // right key — two tests cannot share a line — and a fixture that
        // ignored it would be writing rows the real scanner never produces.
        file_path: "src/lib.rs".to_string(),
        line_number: number,
        feature_id: None,
    }
}

/// A catalogue holding exactly what a test puts in it.
fn catalogue(rows: &[CataloguedTest]) -> CatalogueRepo {
    let service = crate::persist::local::database::initialize_database_in_memory()
        .expect("an in-memory database");
    service.run_migrations(crate::persist::global::Schema::Worktree).expect("the schema is created");
    let repo = CatalogueRepo::new(Arc::new(service));
    for member in MEMBERS {
        let owned: Vec<CataloguedTest> = rows
            .iter()
            .filter(|row| row.repo == member)
            .cloned()
            .collect();
        repo.replace_repo(member, &owned).expect("the rows are written");
    }
    repo
}

/// TEST11254: a number missing from a mirror is reported as a gap, and one
/// present everywhere is not.
///
/// The whole claim of the document. Five implementations of one protocol are
/// one protocol only while the same number means the same thing in each, and
/// this report is the only thing that says where that stopped being true — so a
/// report that missed a gap, or invented one, is worse than none.
#[test]
fn test11254_a_number_missing_from_a_mirror_is_reported_as_a_gap() {
    let rows = vec![
        test("family/one", 100, "test0100_everywhere", "everywhere"),
        test("family/two", 100, "test0100_everywhere", "everywhere"),
        test("family/three", 100, "test0100_everywhere", "everywhere"),
        test("family/one", 200, "test0200_only_here", "only here"),
        test("family/two", 200, "test0200_only_here", "only here"),
    ];
    let document = parity_markdown(&catalogue(&rows), &members()).expect("the report renders");

    let gaps = section(&document, "## Parity gaps");
    assert!(
        gaps.contains("TEST200") && gaps.contains("three"),
        "the gap must be reported, and must name the mirror it is missing from:\n{gaps}"
    );
    assert!(
        !gaps.contains("TEST100"),
        "a number every mirror has is not a gap:\n{gaps}"
    );
    assert!(
        document.contains("- Shared numbers with a parity gap (missing from ≥1 mirror): **1**"),
        "the summary must agree with the table:\n{document}"
    );
}

/// TEST11255: a number one mirror alone claims is a port target below 8000 and
/// nothing to answer for at or above it.
///
/// The range is the whole reason a solo test is sometimes a backlog item and
/// sometimes correct. A report that did not tell them apart would put every
/// implementation-specific test on the port worklist, which is how the previous
/// one came to list hundreds of entries nobody could act on.
#[test]
fn test11255_the_shared_range_decides_whether_a_solo_test_is_a_backlog_item() {
    let rows = vec![
        test("family/one", 300, "test0300_shared_range_solo", "ports to the others"),
        test("family/one", 8300, "test8300_impl_specific", "this mirror's own"),
    ];
    let document = parity_markdown(&catalogue(&rows), &members()).expect("the report renders");

    let worklist = section(&document, "## Solo tests in the shared range");
    assert!(
        worklist.contains("TEST300"),
        "a solo test below the ceiling is a port target:\n{worklist}"
    );
    assert!(
        !worklist.contains("TEST8300"),
        "a solo test at or above the ceiling is correctly placed, not a backlog item:\n{worklist}"
    );
    assert!(
        section(&document, "## Parity gaps").lines().all(|l| !l.contains("TEST8300")),
        "an implementation-specific number is not a parity gap:\n{document}"
    );
}

/// TEST11256: two mirrors describing one number differently are shown side by
/// side, and identical wording is not.
///
/// This is the check that catches "same number, different test" — the one
/// failure the number scheme cannot survive. It has to ignore wording that
/// differs only in whitespace, or the section fills with noise and stops being
/// read, which is what happened to its predecessor.
#[test]
fn test11256_divergent_descriptions_are_shown_and_whitespace_is_not_divergence() {
    let rows = vec![
        test("family/one", 400, "test0400_a", "the parser rejects an empty header"),
        test("family/two", 400, "test0400_a", "the parser  rejects an empty\nheader"),
        test("family/one", 500, "test0500_b", "a cap resolves to its alias"),
        test("family/two", 500, "test0500_b", "a media resolves to its alias"),
    ];
    let document = parity_markdown(&catalogue(&rows), &members()).expect("the report renders");

    let divergent = section(&document, "## Divergent descriptions");
    assert!(
        divergent.contains("### TEST500"),
        "two mirrors describing one number differently must be shown:\n{divergent}"
    );
    assert!(
        divergent.contains("a cap resolves to its alias")
            && divergent.contains("a media resolves to its alias"),
        "and shown side by side, because that is the only way to judge them:\n{divergent}"
    );
    assert!(
        !divergent.contains("### TEST400"),
        "the same sentence wrapped differently is not a divergence:\n{divergent}"
    );
}

/// TEST11257: a description does not break the table it is written into.
///
/// Descriptions are doc comments. They carry newlines, leading `///` and pipe
/// characters, and each of those ends a markdown row early — silently, so the
/// document renders as a table that has quietly lost its remaining columns.
#[test]
fn test11257_a_description_cannot_break_the_row_it_is_written_into() {
    let rows = vec![test(
        "family/one",
        600,
        "test0600_awkward",
        "/// a | b\n/// and a second line",
    )];
    let document = parity_markdown(&catalogue(&rows), &members()).expect("the report renders");

    let row = document
        .lines()
        .find(|line| line.contains("TEST600"))
        .expect("the solo test is listed")
        .to_string();
    assert!(
        !row.contains("\\n"),
        "the description must be flattened onto the row: {row}"
    );
    assert_eq!(
        row.matches(" | ").count(),
        4,
        "a five-column row has four separators; an unescaped pipe adds one: {row}"
    );
    assert!(
        row.contains("a \\| b"),
        "a pipe in the description must be escaped, not dropped: {row}"
    );
}

/// TEST11258: the document never names the private tool that wrote it.
///
/// It is written into `capdag/docs/`, and the capdag mirrors are PUBLIC on
/// GitHub. A generated header naming the tool puts that name in a public
/// repository, which is the one thing this workspace's publishing rules forbid
/// outright.
#[test]
fn test11258_the_parity_document_does_not_name_the_private_tool() {
    let rows = vec![test("family/one", 700, "test0700_a", "something")];
    let document = parity_markdown(&catalogue(&rows), &members()).expect("the report renders");
    let tool = concat!("sd", "x");
    assert!(
        !document.to_lowercase().contains(tool),
        "the document names the private tool:\n{document}"
    );
}

/// One `##` section of the document, up to the next one.
fn section<'a>(document: &'a str, heading: &str) -> &'a str {
    let start = document
        .find(heading)
        .unwrap_or_else(|| panic!("the document has no '{heading}' section:\n{document}"));
    let rest = &document[start + heading.len()..];
    match rest.find("\n## ") {
        Some(end) => &rest[..end],
        None => rest,
    }
}