From 76b64f99c22e8db86f7a9546b2bce6c0157f8a27 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Fri, 6 Nov 2026 16:45:18 -0700 Subject: [PATCH] test(parity): machine-check the catalog's statuses and floor docs/parity.md was only review-checked. `parity_floor.rs` now parses every table with a `Status` column and fails unless each status is exactly `done`, `partial` or `todo`, and unless the count of `done` rows equals the floor line exactly. Equal, not at least: adding a done row raises the line in the same commit, so a later drop cannot pass unnoticed. The parser's own cases (a bad status, an empty status, a missing floor line) are unit-tested inline. The catalog said 21 done but held 25. The floor line was stale, not the rows: P4b S5 added one done row (invoice sent tracking) and S6 added three (multi-document settlements, the processor's fee, bills due and aging), all four named in the notes with their tests. Raising the line to 25 fixes the discrepancy without demoting anything; the history sentence is kept and the reason is added to it. Co-Authored-By: Claude Opus 5.5 --- crates/ledger-cli/tests/parity_floor.rs | 193 ++++++++++++++++++++++++ docs/parity.md | 6 +- 2 files changed, 197 insertions(+), 2 deletions(-) create mode 100644 crates/ledger-cli/tests/parity_floor.rs diff --git a/crates/ledger-cli/tests/parity_floor.rs b/crates/ledger-cli/tests/parity_floor.rs new file mode 100644 index 00000000..5b2b0241 --- /dev/null +++ b/crates/ledger-cli/tests/parity_floor.rs @@ -0,0 +1,193 @@ +//! The parity catalog's floor, machine-checked (docs/parity.md; +//! AGENTS.md, "Parity tracking"). +//! +//! Every table with a `Status` column must hold only `done`, `partial` or +//! `todo`, and the number of `done` rows must equal the catalog's +//! `**Floor: N done**` line exactly. Equal, not at least: adding a done row +//! raises the floor in the same commit, so a later drop is always caught. +//! The history half (the floor never falls between commits) is +//! `scripts/parity-floor.sh`, run by CI before `just check`. + +use std::path::{Path, PathBuf}; + +fn root() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../..") + .canonicalize() + .unwrap() +} + +fn catalog() -> String { + let path = root().join("docs/parity.md"); + std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display())) +} + +/// The only statuses the catalog allows (docs/parity.md: +/// `done` = implemented + invariant-tested + golden-covered, +/// `partial` = implemented but missing tests or goldens, `todo` = not +/// started). +const STATUSES: [&str; 3] = ["done", "partial", "todo"]; + +#[derive(Debug)] +struct Row { + area: String, + status: String, +} + +/// A markdown table row's cells, trimmed, without the outer pipes. +fn cells(line: &str) -> Vec<&str> { + line.trim() + .trim_matches('|') + .split('|') + .map(str::trim) + .collect() +} + +/// The `**Floor: N done**` line, or `None` when the file has none. +fn parse_floor(text: &str) -> Option { + for line in text.lines() { + let Some(rest) = line.trim().strip_prefix("**Floor: ") else { + continue; + }; + let mut words = rest.split_whitespace(); + let (Some(number), Some(marker)) = (words.next(), words.next()) else { + continue; + }; + if marker != "done**" { + continue; + } + return number.parse().ok(); + } + None +} + +/// Every data row of every table whose header has a `Status` column. +/// Errors name the row's first cell and the bad status. +fn parse_tables(text: &str) -> Result, Vec> { + let mut rows = Vec::new(); + let mut errors = Vec::new(); + let mut status_col: Option = None; + + for (index, line) in text.lines().enumerate() { + let line_no = index + 1; + if !line.trim_start().starts_with('|') { + // A table ends at the first non-table line. + status_col = None; + continue; + } + let parts = cells(line); + let separator = parts + .iter() + .all(|cell| !cell.is_empty() && cell.chars().all(|c| c == '-' || c == ':')); + if separator { + continue; + } + let Some(col) = status_col else { + if let Some(col) = parts.iter().position(|cell| *cell == "Status") { + status_col = Some(col); + } + continue; + }; + let area = parts.first().copied().unwrap_or_default().to_string(); + match parts.get(col).copied() { + None => errors.push(format!( + "docs/parity.md:{line_no}: row `{area}` has no status cell" + )), + Some("") => errors.push(format!( + "docs/parity.md:{line_no}: row `{area}` has an empty status" + )), + Some(status) if !STATUSES.contains(&status) => errors.push(format!( + "docs/parity.md:{line_no}: row `{area}` has status `{status}`, \ + not one of done, partial, todo" + )), + Some(status) => rows.push(Row { + area, + status: status.to_string(), + }), + } + } + if errors.is_empty() { + Ok(rows) + } else { + Err(errors) + } +} + +#[test] +fn every_row_has_a_known_status() { + let text = catalog(); + if let Err(errors) = parse_tables(&text) { + panic!( + "docs/parity.md's statuses must be exactly done, partial or todo:\n{}", + errors.join("\n") + ); + } +} + +#[test] +fn the_done_count_is_the_floor() { + let text = catalog(); + let rows = parse_tables(&text).unwrap_or_else(|errors| { + panic!( + "docs/parity.md's statuses must be exactly done, partial or todo:\n{}", + errors.join("\n") + ) + }); + let done: Vec<&str> = rows + .iter() + .filter(|row| row.status == "done") + .map(|row| row.area.as_str()) + .collect(); + let floor = parse_floor(&text).unwrap_or_else(|| { + panic!( + "docs/parity.md has no `**Floor: N done**` line; restore it \ + (docs/parity.md's intro and AGENTS.md, \"Parity tracking\")" + ) + }); + if done.len() > floor as usize { + panic!( + "docs/parity.md has {} `done` rows but its floor line says {floor}. \ + Raise the line to `**Floor: {} done**` in this commit: adding a \ + done row raises the floor in the same commit, so a later drop is \ + always caught.\nDone rows:\n {}", + done.len(), + done.len(), + done.join("\n ") + ); + } + if done.len() < floor as usize { + panic!( + "docs/parity.md has {} `done` rows but its floor line says {floor}. \ + Restore the missing row: the floor never decreases, and lowering \ + it needs the owner's decision.\nDone rows:\n {}", + done.len(), + done.join("\n ") + ); + } +} + +#[test] +fn a_bad_status_names_its_row() { + let text = "| Area | Status | Notes |\n|---|---|---|\n| Ledger | finished | — |\n"; + let errors = parse_tables(text).unwrap_err(); + assert_eq!(errors.len(), 1, "{errors:?}"); + assert!(errors[0].contains("Ledger"), "{errors:?}"); + assert!(errors[0].contains("finished"), "{errors:?}"); +} + +#[test] +fn a_missing_status_cell_is_an_error() { + let text = "| Area | Status | Notes |\n|---|---|---|\n| Ledger | | — |\n"; + let errors = parse_tables(text).unwrap_err(); + assert_eq!(errors.len(), 1, "{errors:?}"); + assert!(errors[0].contains("Ledger"), "{errors:?}"); +} + +#[test] +fn a_missing_floor_line_is_none() { + assert_eq!(parse_floor("# Parity catalog\n\nNo floor here.\n"), None); + assert_eq!( + parse_floor("**Floor: 7 done** (2026-01-01; the sections below record each rise)"), + Some(7) + ); +} diff --git a/docs/parity.md b/docs/parity.md index eeaf8c88..9e4318b2 100644 --- a/docs/parity.md +++ b/docs/parity.md @@ -6,9 +6,11 @@ same commit as the feature. **The floor never decreases**: the count of Statuses: `done` = implemented + invariant-tested + golden-covered. `partial` = implemented but missing tests or goldens. `todo` = not started. -No other statuses. +No other statuses. `crates/ledger-cli/tests/parity_floor.rs` enforces the +statuses and the floor in `cargo test`; `scripts/parity-floor.sh` enforces +in CI that the floor never falls between commits. -**Floor: 21 done** (2026-10-28; it was 10 on 2026-10-10, when the fixed-asset register was added, and the sections below record each rise). +**Floor: 25 done** (2026-10-28; it was 10 on 2026-10-10, when the fixed-asset register was added, and the sections below record each rise; 2026-11-06: P4b S5 added one `done` row and S6 three, to 25, which the line had missed — the four rows' implementations and tests are named below). | Area | GnuCash counterpart (a cross-check, not the spec) | Status | Notes | |---|---|---|---|