KeiSeiKit-1.0/_assembler/tests/validator_negative.rs
Parfii-bot fdf1545631 refactor(tests): rename fixtures + regenerate snapshots for kei- prefix
- Rename 4 fixture manifests under _assembler/tests/fixtures/_manifests/
  ({code-implementer,cost-guardian,patent-compliance,researcher}.toml
  -> kei-<name>.toml) via git mv. Copy updated top-level manifests into
  fixtures so they stay byte-identical (fixtures mirror real manifests).
- Rename 4 snapshot files under _assembler/tests/snapshots/ to match
  the new insta snapshot keys.
- Update snapshot bodies to reflect the kei- prefix in:
  * frontmatter name field (name: kei-<n>)
  * GENERATED comment (_manifests/kei-<n>.toml)
  * handoff target lines
  * === HEADER === REPORT header (uppercased name in output_format)
- Update test code (golden.rs, roundtrip.rs, validator_negative.rs,
  determinism.rs) to use the new manifest filenames + snapshot keys.
  Rust function names (e.g. golden_researcher) untouched — they are
  internal identifiers, not manifest refs, and the word-boundary rule
  (no "_" preceding match) correctly skipped them.

Verify:
  cd _assembler && cargo test
  -> 17 tests passed (0 + 3 + 4 + 2 + 2 + 6 across 6 test files)
  -> Re-run produces no *.snap.new files (snapshots stable)

Regeneration path: because cargo-insta CLI is not installed on the
build host, the .snap.new files produced by the first (failing) test
run were accepted by renaming .snap.new -> .snap. Second cargo test
run passed cleanly.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 13:40:41 +08:00

158 lines
5.5 KiB
Rust

//! Validator negative-path tests.
//!
//! Locks the error contract of validator.rs: each flavour of bad
//! manifest produces a non-zero exit status AND a stderr message
//! that names the offending invariant.
//!
//! Note: the unsubstituted-`{{placeholder}}` check is being added
//! in a parallel PR (fix/remaining-findings). That specific test
//! is deliberately NOT included here; when the check lands, add a
//! case here and re-run.
mod common;
use common::{run_assemble, seed_tempdir};
use std::fs;
use std::path::Path;
/// Write a minimal valid manifest then mutate one field to break it.
/// Returns the tempdir guard (keeps it alive) and the manifest path.
fn write_broken(
root: &Path,
filename: &str,
mutate: impl FnOnce(&mut String),
) -> std::path::PathBuf {
let src = fs::read_to_string(root.join("_manifests/kei-researcher.toml")).unwrap();
let mut buf = src;
mutate(&mut buf);
let target = root.join("_manifests").join(filename);
fs::write(&target, buf).unwrap();
target
}
fn assert_fails_with(root: &Path, manifest: &Path, needle: &str) {
let out = run_assemble(root, &[manifest.to_str().unwrap()]);
assert!(
!out.status.success(),
"expected non-zero exit for broken manifest {}; stdout={:?} stderr={:?}",
manifest.display(),
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr),
);
let combined = format!(
"{}{}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr)
);
assert!(
combined.contains(needle),
"stderr did not mention {needle:?}; full output:\n{combined}"
);
}
#[test]
fn validator_rejects_unknown_block_ref() {
let (_tmp, root) = seed_tempdir();
// Add an extra block name that doesn't exist on disk.
let manifest = write_broken(&root, "broken-unknown-block.toml", |s| {
*s = s.replace(
"\"memory-protocol\", # OBLIGATORY\n]",
"\"memory-protocol\",\n \"this-block-does-not-exist\",\n]",
);
});
assert_fails_with(&root, &manifest, "this-block-does-not-exist");
}
#[test]
fn validator_rejects_missing_obligatory_block() {
let (_tmp, root) = seed_tempdir();
// Drop "memory-protocol" from the blocks list.
let manifest = write_broken(&root, "broken-missing-obligatory.toml", |s| {
*s = s.replace("\"memory-protocol\", # OBLIGATORY\n", "");
});
assert_fails_with(&root, &manifest, "memory-protocol");
}
#[test]
fn validator_rejects_empty_handoff() {
let (_tmp, root) = seed_tempdir();
// Strip every `[[handoff]]` table from the manifest.
let manifest = write_broken(&root, "broken-no-handoff.toml", |s| {
let mut out = String::new();
let mut skip = false;
for line in s.lines() {
if line.trim_start().starts_with("[[handoff]]") {
skip = true;
continue;
}
if skip && (line.trim_start().starts_with("[") || line.trim().is_empty()) {
// End of the handoff block (next [table] or blank-line gap).
if line.trim_start().starts_with("[") && !line.trim_start().starts_with("[[handoff]]") {
skip = false;
} else if line.trim().is_empty() {
// Tolerate blank line inside handoff table separator.
continue;
}
}
if !skip {
out.push_str(line);
out.push('\n');
}
}
*s = out;
});
assert_fails_with(&root, &manifest, "handoff");
}
#[test]
fn validator_rejects_empty_role() {
let (_tmp, root) = seed_tempdir();
// Replace the role with whitespace only.
let manifest = write_broken(&root, "broken-empty-role.toml", |s| {
// The kei-researcher manifest uses triple-quoted `role = """..."""`.
let start = s.find("role = \"\"\"").expect("role block marker missing");
let end_rel = s[start..]
.find("\"\"\"\n")
.and_then(|_| s[start + 10..].find("\"\"\""))
.expect("role closing marker missing");
let end = start + 10 + end_rel + 3;
let before = &s[..start];
let after = &s[end..];
*s = format!("{before}role = \" \"\n{after}");
});
assert_fails_with(&root, &manifest, "role");
}
#[test]
fn validator_rejects_empty_domain_in() {
let (_tmp, root) = seed_tempdir();
// Replace domain_in array with an empty one.
let manifest = write_broken(&root, "broken-empty-domain-in.toml", |s| {
let start = s.find("domain_in = [").expect("domain_in marker missing");
let end_rel = s[start..].find("]\n").expect("domain_in close marker missing");
let end = start + end_rel + 2;
let before = &s[..start];
let after = &s[end..];
*s = format!("{before}domain_in = []\n{after}");
});
assert_fails_with(&root, &manifest, "domain_in");
}
#[test]
fn validate_only_flag_skips_write() {
// --validate must NOT write anything under _generated/.
let (_tmp, root) = seed_tempdir();
let manifest = root.join("_manifests/kei-researcher.toml");
let out = run_assemble(&root, &["--validate", manifest.to_str().unwrap()]);
assert!(
out.status.success(),
"--validate on a valid manifest failed: {}",
String::from_utf8_lossy(&out.stderr)
);
let generated = root.join("_generated/kei-researcher.md");
assert!(
!generated.exists(),
"--validate wrote an output file at {}",
generated.display()
);
}