Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

9.5 KB
Raw Blame
//! `raea findings` (docs/direction.feature DR3): what renders and checks found, read from the project's record
//! (core/facts record.rs). For each thing rendered or checked, its latest entry against the one before (or the one
//! `--since` names): what was fixed, what is new and what is carried, and since which render.

use std::path::{Path, PathBuf};

use raea_core_facts::record::{self, Entry};
use raea_core_facts::{Finding, finding};
use raea_runtime_bundle::flag;
use serde_json::{Value, json};

pub const USAGE: &str = "raea findings [PROJECT|EDIT.tsx] [--about NAME] [--check ID] [--open] [--since RENDER] [--json]   (what each render and raea check found, from the project's record: fixed, new and carried since the one before, or since RENDER; --open: only what stands)";

/// The oldest entry of the unbroken run up to the latest that all found `f`.
fn carried_since(entries: &[&Entry], f: &Finding) -> String {
    let key = record::same(f);
    let run = entries.iter().rev().take_while(|e| e.findings.iter().any(|g| record::same(g) == key));
    run.last().map(|e| e.id.clone()).unwrap_or_default()
}

/// One thing's latest entry against the one before it (or `since`), each list filtered by `keep`.
fn compare(entries: &[&Entry], since: Option<&str>, keep: &dyn Fn(&Finding) -> bool, open: bool) -> Option<Value> {
    let (now, earlier) = entries.split_last()?;
    let before = match since {
        Some(id) => earlier.iter().rev().find(|e| e.id.starts_with(id)),
        None => earlier.last(),
    };
    let none = Vec::new();
    let ch = record::changes(before.map_or(&none, |b| &b.findings), &now.findings);
    let list = |fs: Vec<&Finding>| fs.into_iter().filter(|f| keep(f)).cloned().collect::<Vec<Finding>>();
    let carried: Vec<Value> = list(ch.carried).into_iter().map(|f| json!({"since": carried_since(entries, &f), "finding": f})).collect();
    let renamed: Vec<Value> = ch.renamed.into_iter().filter(|(_, n)| keep(n)).map(|(was, f)| json!({"was": was.about.iter().map(|a| &a.name).collect::<Vec<_>>(), "finding": f})).collect();
    let fixed = if open { Vec::new() } else { list(ch.fixed) };
    Some(json!({"of": now.of, "kind": now.kind, "id": now.id, "by": now.by, "commit": now.commit, "out": now.out, "against": before.map(|b| &b.id),
        "new": list(ch.new), "carried": carried, "renamed": renamed, "fixed": fixed, "despite": now.despite}))
}

/// One thing's lines: its entry, then what is new, carried and fixed.
fn lines(r: &Value) -> Vec<String> {
    let read = Finding::read;
    let mut t = vec![format!(
        "{} {} ({} {}{}, against {})",
        r["of"].as_str().unwrap_or(""),
        r["kind"].as_str().filter(|k| !k.is_empty()).unwrap_or("run"),
        r["by"].as_str().unwrap_or(""),
        r["id"].as_str().unwrap_or(""),
        r["commit"].as_str().map(|c| format!(" at {c}")).unwrap_or_default(),
        r["against"].as_str().unwrap_or("nothing: its first")
    )];
    t.extend(r["new"].as_array().into_iter().flatten().filter_map(read).map(|f| format!("  new      {}", f.line())));
    for c in r["carried"].as_array().into_iter().flatten() {
        if let Some(f) = read(&c["finding"]) {
            t.push(format!("  carried  {} (since {})", f.line(), c["since"].as_str().unwrap_or("")));
        }
    }
    for c in r["renamed"].as_array().into_iter().flatten() {
        if let Some(f) = read(&c["finding"]) {
            let was: Vec<&str> = c["was"].as_array().into_iter().flatten().filter_map(Value::as_str).collect();
            t.push(format!("  renamed  {} (the same, about {} before: not fixed)", f.line(), was.join(", ")));
        }
    }
    t.extend(r["fixed"].as_array().into_iter().flatten().filter_map(read).map(|f| format!("  fixed    {}", f.line())));
    // what was let through, and why: the error stands in the count all the same
    for d in r["despite"].as_array().into_iter().flatten() {
        t.push(format!("  despite  {}: {}", d["check"].as_str().unwrap_or(""), d["why"].as_str().unwrap_or("")));
    }
    t
}

/// What stands across the reports: each one's new and carried.
fn standing(reports: &[Value]) -> Vec<Finding> {
    let mut out = Vec::new();
    for r in reports {
        out.extend(r["new"].as_array().into_iter().flatten().filter_map(Finding::read));
        out.extend(["carried", "renamed"].iter().flat_map(|k| r[*k].as_array().into_iter().flatten()).filter_map(|c| Finding::read(&c["finding"])));
    }
    out
}

/// `raea findings ...` (args from "findings").
pub fn cli(args: &[String]) -> Result<String, String> {
    if args.iter().any(|a| a == "--help" || a == "-h") {
        return Ok(USAGE.into());
    }
    let target = args.get(1).filter(|a| !a.starts_with("--")).map_or_else(|| PathBuf::from("."), PathBuf::from);
    // made absolute first: a bare name's parent is "", which names no folder
    let from = if target.is_file() { dunce::canonicalize(&target).unwrap_or_else(|_| target.clone()).parent().map_or_else(|| PathBuf::from("."), Path::to_path_buf) } else { target.clone() };
    let project = record::up(&from, ".raea/project").ok_or_else(|| format!("raea findings: {} isn't in a raea project (no .raea/project at or above it), so nothing is recorded\n{USAGE}", target.display()))?;
    let (about, check, since) = (flag(args, "--about"), flag(args, "--check"), flag(args, "--since"));
    let keep = |f: &Finding| about.as_deref().is_none_or(|n| f.about.iter().any(|a| a.name == n || a.key.as_deref() == Some(n))) && check.as_deref().is_none_or(|c| f.check == c);
    let all = record::read(&project);
    let only = target.is_file().then(|| record::rel(&project, &target));
    // each thing by what kind of output it was (a video against the video before it, a review against a review)
    let mut ofs: Vec<(&str, &str)> = all.iter().filter(|e| only.as_deref().is_none_or(|w| w == e.of)).map(|e| (&*e.of, &*e.kind)).collect();
    ofs.sort();
    ofs.dedup();
    let open = args.iter().any(|a| a == "--open");
    let reports: Vec<Value> = ofs.iter().filter_map(|(of, kind)| compare(&all.iter().filter(|e| e.of == *of && e.kind == *kind).collect::<Vec<_>>(), since.as_deref(), &keep, open)).collect();
    let count = finding::count(&standing(&reports));
    if args.iter().any(|a| a == "--json") {
        return Ok(serde_json::to_string_pretty(&json!({"project": project, "entries": all.len(), "reports": reports, "standing": count})).unwrap_or_default());
    }
    let mut t = vec![format!("raea findings: {} ({} entries in .raea/record)", project.display(), all.len())];
    if reports.is_empty() {
        t.push("  nothing recorded yet: a render or raea check of a file in this project keeps what it found".into());
    }
    t.extend(reports.iter().flat_map(lines));
    t.push(format!("raea findings: {count} standing"));
    Ok(t.join("\n"))
}

#[cfg(test)]
mod tests {
    use super::*;
    use raea_core_facts::Level;

    fn entry(id: &str, fs: Vec<Finding>) -> Entry {
        Entry { id: id.into(), at: 0, by: "raea render".into(), of: "edit.tsx".into(), out: None, kind: "video".into(), commit: None, findings: fs, despite: vec![] }
    }

    #[test]
    fn the_latest_render_against_the_one_before_says_fixed_new_and_carried_since_when() {
        let clips = Finding::new("clips", Level::Error, "rider in seat", "raea render").about("rider", None);
        let tilt = Finding::new("tilted", Level::Warn, "vase leans", "raea render").about("vase", None);
        let float = Finding::new("floats", Level::Error, "lamp floats", "raea render").about("lamp", None);
        let es = [entry("a", vec![clips.clone(), tilt.clone()]), entry("b", vec![clips.clone(), tilt]), entry("c", vec![clips, float])];
        let refs: Vec<&Entry> = es.iter().collect();
        let all = |_: &Finding| true;
        let r = compare(&refs, None, &all, false).unwrap();
        assert_eq!((r["against"].as_str(), r["new"][0]["check"].as_str(), r["fixed"][0]["check"].as_str()), (Some("b"), Some("floats"), Some("tilted")));
        assert_eq!((r["carried"][0]["finding"]["check"].as_str(), r["carried"][0]["since"].as_str()), (Some("clips"), Some("a")));
        // against a named render, and only what stands
        let r = compare(&refs, Some("a"), &all, true).unwrap();
        assert_eq!((r["against"].as_str(), r["fixed"].as_array().map(Vec::len)), (Some("a"), Some(0)));
        assert_eq!(finding::count(&standing(&[r])), "2 errors, 0 warns, 0 notes");
        let lamp = |f: &Finding| f.about.iter().any(|a| a.name == "lamp");
        assert_eq!(compare(&refs, None, &lamp, false).unwrap()["carried"].as_array().map(Vec::len), Some(0));
    }

    #[test]
    fn a_renamed_part_or_an_error_let_through_is_never_read_as_fixed() {
        let crate_ = Finding::new("clips", Level::Error, "crate in pillar", "raea render").about("crate", None).about("pillar", None).frames(0, 30).measure("depth", 0.241, "m", None);
        let mut boxed = crate_.clone();
        boxed.about[0].name = "box".into();
        boxed.says = "box in pillar".into();
        let mut let_through = entry("b", vec![boxed]);
        let_through.despite = vec![record::Despite { check: "clips".into(), why: "posed through it".into() }];
        let es = [entry("a", vec![crate_]), let_through];
        let r = compare(&es.iter().collect::<Vec<_>>(), None, &|_: &Finding| true, false).unwrap();
        assert_eq!((r["fixed"].as_array().map(Vec::len), r["new"].as_array().map(Vec::len), r["renamed"][0]["was"][0].as_str()), (Some(0), Some(0), Some("crate")));
        let text = lines(&r).join("\n");
        assert!(text.contains("  renamed  error clips  frames 0-29  box, pillar") && text.contains("  despite  clips: posed through it"), "{text}");
        assert_eq!(finding::count(&standing(&[r])), "1 error, 0 warns, 0 notes");
    }
}