use assay_core::model::{TestResultRow, TestStatus};
use assay_core::report::sarif;
use std::path::{Path, PathBuf};
fn sourceless_failure() -> Vec<TestResultRow> {
vec![TestResultRow {
test_id: "sourceless_failure_demo".to_string(),
status: TestStatus::Fail,
score: Some(0.0),
cached: false,
message: "Config error or internal logic failure without file context".to_string(),
details: serde_json::json!({"error": "trace_not_found"}),
duration_ms: Some(10),
fingerprint: None,
skip_reason: None,
attempts: None,
error_policy_applied: None,
}]
}
fn fixture_path() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join(".repro_artifacts/sourceless_sarif.json")
}
fn without_one_trailing_newline(s: &str) -> &str {
s.strip_suffix('\n').unwrap_or(s)
}
fn generate_into(dir: &Path) -> String {
let out = dir.join("sourceless_sarif.json");
sarif::write_sarif("assay", &sourceless_failure(), &out).expect("SARIF generation failed");
std::fs::read_to_string(&out).expect("read generated SARIF")
}
#[test]
fn the_checked_in_fixture_still_matches_what_the_writer_produces() {
let tmp = tempfile::tempdir().expect("tempdir");
let generated = generate_into(tmp.path());
let golden = std::fs::read_to_string(fixture_path()).expect(
"checked-in fixture is missing; regenerate with \
`cargo test -p assay-core --test generate_fixture -- --ignored regenerate`",
);
assert_eq!(
without_one_trailing_newline(&generated),
without_one_trailing_newline(&golden),
"the SARIF writer's output no longer matches the checked-in fixture. If the change is \
intended, regenerate with \
`cargo test -p assay-core --test generate_fixture -- --ignored regenerate`. If the two \
differ only by \\r, this checkout predates the `text eol=lf` pin in .gitattributes: \
re-check out that one fixture under the current attributes, preserving any intentional \
local edits first, with `git restore --source=HEAD --worktree -- \
crates/assay-core/.repro_artifacts/sourceless_sarif.json`. Do not regenerate; that would \
commit the wrong bytes."
);
}
#[test]
fn a_finding_without_a_source_location_gets_the_fallback_uri() {
let tmp = tempfile::tempdir().expect("tempdir");
let generated = generate_into(tmp.path());
let doc: serde_json::Value =
serde_json::from_str(&generated).expect("the writer must emit valid JSON");
let uri = doc
.pointer("/runs/0/results/0/locations/0/physicalLocation/artifactLocation/uri")
.unwrap_or_else(|| {
panic!("no artifact location on the first result of the first run: {generated}")
});
assert_eq!(
uri, ".assay/eval.yaml",
"a finding with no source location must fall back to the config URI"
);
}
#[test]
#[ignore = "rewrites the checked-in SARIF fixture; run deliberately"]
fn regenerate_the_checked_in_fixture() {
let path = fixture_path();
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).expect("fixture directory");
}
let tmp = tempfile::tempdir().expect("tempdir");
let generated = generate_into(tmp.path());
let mut contents = without_one_trailing_newline(&generated).to_string();
contents.push('\n');
std::fs::write(&path, contents).expect("write fixture");
println!("regenerated {}", path.display());
}