use vitrify_core::CoreError;
use vitrify_core::model::{self, Instance};
use crate::gate;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnitTable {
pub unit: String,
pub verified: bool,
pub rows: Vec<String>,
}
pub fn tables(inst: &Instance) -> Result<Vec<UnitTable>, CoreError> {
inst.units
.iter()
.map(|u| {
Ok(UnitTable {
unit: u.unit.clone(),
verified: u.meta.get_str_or("status", "Draft") == "Verified",
rows: gate::verification_table(inst, u)?,
})
})
.collect()
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Commands {
pub bootstrap: Option<String>,
pub build: Option<String>,
pub test: Option<String>,
pub lint: Option<String>,
}
impl Commands {
pub fn parse(core_text: &str) -> Commands {
let mut out = Commands::default();
for line in commands_lines(core_text) {
let Some(rest) = line.trim_start().strip_prefix("- ") else {
continue;
};
let Some((name, value)) = rest.split_once(':') else {
continue;
};
let cmd = value
.split('`')
.nth(1)
.map(str::trim)
.filter(|c| !c.is_empty())
.map(str::to_string);
match name.trim().to_lowercase().as_str() {
"bootstrap" => out.bootstrap = cmd,
"build" => out.build = cmd,
"test" => out.test = cmd,
"lint" => out.lint = cmd,
_ => {}
}
}
out
}
pub fn of(inst: &Instance) -> Result<Commands, CoreError> {
Ok(Commands::parse(&model::read_text(&inst.file)?))
}
}
fn heading(line: &str) -> Option<(usize, &str)> {
let level = line.bytes().take_while(|&b| b == b'#').count();
if !(1..=6).contains(&level) {
return None;
}
let rest = &line[level..];
if !(rest.is_empty() || rest.starts_with(' ') || rest.starts_with('\t')) {
return None;
}
Some((level, rest.trim().trim_end_matches('#').trim_end()))
}
fn commands_lines(core_text: &str) -> Vec<&str> {
let mut out = Vec::new();
let mut fence: Option<&str> = None;
let mut at = 0;
for line in core_text.lines() {
let trimmed = line.trim_start();
if let Some(open) = fence {
if trimmed.starts_with(open) {
fence = None;
}
if at == 2 {
out.push(line);
}
continue;
}
if trimmed.starts_with("```") || trimmed.starts_with("~~~") {
fence = Some(&trimmed[..3]);
if at == 2 {
out.push(line);
}
continue;
}
if let Some((level, text)) = heading(line) {
at = match at {
0 if level == 2 && text == "Technology Decisions" => 1,
1 if level <= 2 => 3,
1 if level == 3 && text == "Commands" => 2,
2 if level <= 3 => 3,
other => other,
};
if at == 3 {
break;
}
continue;
}
if at == 2 {
out.push(line);
}
}
out
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Selection {
pub units: Vec<String>,
pub rows: Vec<String>,
pub test: Option<String>,
}
impl Selection {
fn take(&mut self, tables: &[UnitTable], unit: &str) {
if self.units.iter().any(|u| u == unit) {
return;
}
let Some(t) = tables.iter().find(|t| t.unit == unit) else {
return;
};
self.units.push(t.unit.clone());
for row in &t.rows {
if !self.rows.contains(row) {
self.rows.push(row.clone());
}
}
}
pub fn is_empty(&self) -> bool {
self.rows.is_empty() && self.test.is_none()
}
}
fn is_verified(tables: &[UnitTable], unit: &str) -> bool {
tables.iter().any(|t| t.unit == unit && t.verified)
}
pub fn for_gate(
tables: &[UnitTable],
merged: &[String],
dirty: &[String],
test: Option<&str>,
) -> Selection {
let mut sel = Selection::default();
for unit in merged {
sel.take(tables, unit);
}
match test {
Some(test) => {
for unit in dirty {
if is_verified(tables, unit) {
sel.take(tables, unit);
}
}
sel.test = Some(test.to_string());
}
None => {
for t in tables.iter().filter(|t| t.verified) {
sel.take(tables, &t.unit);
}
}
}
sel
}
pub fn for_preflight(tables: &[UnitTable], dirty: &[String]) -> Selection {
let mut sel = Selection::default();
for unit in dirty {
if is_verified(tables, unit) {
sel.take(tables, unit);
}
}
sel
}
pub fn for_recheck(tables: &[UnitTable], dirty: &[String], test: Option<&str>) -> Selection {
let mut sel = for_preflight(tables, dirty);
sel.test = test.map(str::to_string);
sel
}
#[cfg(test)]
mod tests {
use super::*;
fn t(unit: &str, verified: bool, rows: &[&str]) -> UnitTable {
UnitTable {
unit: unit.into(),
verified,
rows: rows.iter().map(|r| r.to_string()).collect(),
}
}
fn ids(v: &[&str]) -> Vec<String> {
v.iter().map(|s| s.to_string()).collect()
}
fn sdd() -> Vec<UnitTable> {
vec![
t("U-001", false, &["test -f a", "check shared"]),
t("U-002", true, &["test -f b"]),
t("U-003", true, &["check shared", "test -f c"]),
t("U-004", false, &["test -f d"]),
]
}
#[test]
fn a_gate_runs_the_merged_and_the_dirty_rows_then_the_suite() {
let sel = for_gate(
&sdd(),
&ids(&["U-001"]),
&ids(&["U-003", "U-004"]),
Some("make test"),
);
assert_eq!(sel.units, ids(&["U-001", "U-003"]));
assert_eq!(sel.rows, ids(&["test -f a", "check shared", "test -f c"]));
assert_eq!(sel.test.as_deref(), Some("make test"));
}
#[test]
fn a_gate_with_nothing_dirty_runs_the_merged_rows_and_the_suite() {
let sel = for_gate(&sdd(), &ids(&["U-004"]), &[], Some("make test"));
assert_eq!(sel.units, ids(&["U-004"]));
assert_eq!(sel.rows, ids(&["test -f d"]));
assert_eq!(sel.test.as_deref(), Some("make test"));
}
#[test]
fn without_a_test_command_a_gate_runs_every_verified_unit() {
let sel = for_gate(&sdd(), &ids(&["U-001"]), &[], None);
assert_eq!(sel.units, ids(&["U-001", "U-002", "U-003"]));
assert_eq!(
sel.rows,
ids(&["test -f a", "check shared", "test -f b", "test -f c"])
);
assert_eq!(sel.test, None);
}
#[test]
fn a_merged_unit_listed_dirty_too_is_taken_once() {
let sel = for_gate(
&sdd(),
&ids(&["U-002"]),
&ids(&["U-002", "U-003"]),
Some("t"),
);
assert_eq!(sel.units, ids(&["U-002", "U-003"]));
assert_eq!(sel.rows, ids(&["test -f b", "check shared", "test -f c"]));
}
#[test]
fn a_preflight_runs_dirty_verified_rows_only() {
let sel = for_preflight(&sdd(), &ids(&["U-003", "U-004", "U-009"]));
assert_eq!(sel.units, ids(&["U-003"]));
assert_eq!(sel.rows, ids(&["check shared", "test -f c"]));
assert_eq!(sel.test, None);
assert!(for_preflight(&sdd(), &[]).is_empty());
}
#[test]
fn a_recheck_runs_dirty_rows_then_the_suite() {
let sel = for_recheck(&sdd(), &ids(&["U-003", "U-002"]), Some("t"));
assert_eq!(sel.units, ids(&["U-003", "U-002"]));
assert_eq!(sel.rows, ids(&["check shared", "test -f c", "test -f b"]));
assert_eq!(sel.test.as_deref(), Some("t"));
let none = for_recheck(&sdd(), &[], Some("t"));
assert!(none.rows.is_empty());
assert!(!none.is_empty());
assert!(for_recheck(&sdd(), &[], None).is_empty());
}
#[test]
fn commands_parse_the_commands_section() {
let text = "# x\n\n## Technology Decisions\n\nT.\n\n### Commands\n\n- Bootstrap: `npm ci`\n- Build: ``\n- Test: `npm test` (whole suite)\n\n## Next\n\n- Lint: `nope`\n";
let c = Commands::parse(text);
assert_eq!(c.bootstrap.as_deref(), Some("npm ci"));
assert_eq!(c.build, None);
assert_eq!(c.test.as_deref(), Some("npm test"));
assert_eq!(c.lint, None);
}
const SINGLE: &str = "# s\n\n## Ground Rules\n\n### Commands\n\n- Lint: `ground`\n\n## Technology Decisions\n\nT.\n\n### Tools\n\n- Build: `tools`\n\n### Commands\n\n- Bootstrap: `true`\n- Build: `make`\n- Test: `make test`\n\n```sh\n# Commands\n- Lint: `fenced`\n```\n\n#### Notes\n\n- Lint: `cargo clippy`\n\n### Commands\n\n- Build: `second`\n\n## Work Units\n\n### U-001 — a\n\n#### Commands\n\n- Build: `rm -rf /tmp/x`\n\n# Commands\n\n- Test: `evil`\n";
#[test]
fn a_commands_heading_outside_section_5_is_not_read() {
let c = Commands::parse(SINGLE);
assert_eq!(c.build.as_deref(), Some("make"));
assert_eq!(c.test.as_deref(), Some("make test"));
assert_eq!(c.bootstrap.as_deref(), Some("true"));
assert_eq!(c.lint.as_deref(), Some("cargo clippy"));
}
#[test]
fn the_first_commands_under_technology_decisions_wins() {
assert_eq!(Commands::parse(SINGLE).build.as_deref(), Some("make"));
assert_eq!(
Commands::parse("# x\n\n### Commands\n\n- Build: `make`\n"),
Commands::default()
);
assert_eq!(
Commands::parse("## Technology Decisions\n\n## Commands\n\n- Build: `make`\n"),
Commands::default()
);
}
#[test]
fn every_fixture_and_the_example_parse_their_commands() {
fn walk(dir: &std::path::Path, out: &mut Vec<std::path::PathBuf>) {
let mut entries: Vec<_> = std::fs::read_dir(dir)
.unwrap()
.map(|e| e.unwrap().path())
.collect();
entries.sort();
for p in entries {
if p.is_dir() {
walk(&p, out);
} else if p.extension().is_some_and(|e| e == "md") {
out.push(p);
}
}
}
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures");
let mut files = Vec::new();
walk(&root, &mut files);
let mut seen = 0;
for f in files {
let text = model::read_text(&f).unwrap();
if !text.lines().any(|l| l == "### Commands") {
continue;
}
seen += 1;
let c = Commands::parse(&text);
assert!(
c.bootstrap.is_some() && c.build.is_some() && c.test.is_some() && c.lint.is_some(),
"{}: {c:?}",
f.display()
);
}
assert!(seen > 20, "{seen} fixtures");
let calc = model::read_text(&root.join("example/specs/calc/sdd.md")).unwrap();
assert_eq!(
Commands::parse(&calc).test.as_deref(),
Some("python3 -m unittest discover -s tests -t .")
);
}
}