use super::*;
fn proof_create(
engine: &mut Engine,
entity_type: &str,
title: &str,
metadata: &[(&str, &str)],
relations: Vec<crate::ops::RelateArg>,
) -> Result<CreateEntityOutcome, EngineError> {
let (actor, client) = cli_actor();
let mut sections = IndexMap::new();
sections.insert("body".to_string(), format!("{title} body."));
let mut md = IndexMap::new();
for (k, v) in metadata {
md.insert(k.to_string(), v.to_string());
}
engine.create_entity(
CreateEntityArgs {
anchors: Vec::new(),
mem: "proof".to_string(),
title: title.to_string(),
entity_type: entity_type.to_string(),
sections,
metadata: md,
relations,
dry_run: false,
},
actor,
Some(&client),
None,
)
}
const GROUNDING_MANIFEST: &str = r#"name: grounding
version: 0.1.0
description: grounding proof schema
when_to_use: constraint-proof tests
types:
- anchor
- tradeoff
relationships:
mode: strict
definitions:
- name: FOLLOWS_FROM
description: stands on
default_weight: 3.0
- name: SUPPORTS
description: pro
default_weight: 1.0
- name: OPPOSES
description: contra
default_weight: 1.0
- name: PART_OF
description: hier
default_weight: 1.0
- name: _default
description: fallback
default_weight: 1.0
community:
resolution: 1.0
seed: 42
"#;
const GROUNDING_ANCHOR: &str = r#"name: anchor
description: a judgment standing on others
when_to_use: tests
sections:
- key: body
heading: Body
required: true
search_weight: 10.0
catch_all: true
write_rules: []
metadata_fields:
- key: status
description: lifecycle
field_type: string
enum_values: [open, checked, fallen]
- key: checked_by
description: who checked
field_type: string
title_weight: 100.0
text_fields:
- body
hierarchy_relationship: PART_OF
no_self_loop_relationships: []
updatable_fields:
- title
- body
- status
- checked_by
health_required_fields:
- body
staleness_threshold_days: 90
constraints:
- kind: requires_when
field: checked_by
when_field: status
when_value: checked
- kind: status_propagation
field: status
value: fallen
relationships: [FOLLOWS_FROM]
direction: in
write_rules: []
"#;
const GROUNDING_TRADEOFF: &str = r#"name: tradeoff
description: a claim with two sides
when_to_use: tests
sections:
- key: body
heading: Body
required: true
search_weight: 10.0
catch_all: true
write_rules: []
metadata_fields: []
title_weight: 100.0
text_fields:
- body
hierarchy_relationship: PART_OF
no_self_loop_relationships: []
updatable_fields:
- title
- body
health_required_fields:
- body
staleness_threshold_days: 90
required_outgoing:
- relationships: [SUPPORTS]
cardinality: at_least_one
- relationships: [OPPOSES]
cardinality: at_least_one
write_rules: []
"#;
#[test]
fn grounding_schema_answers_chain_check_questions_from_health() {
let tmp = TempDir::new().unwrap();
let mut engine = engine_with_proof_schema(
&tmp,
"grounding",
GROUNDING_MANIFEST,
&[
("anchor", GROUNDING_ANCHOR),
("tradeoff", GROUNDING_TRADEOFF),
],
);
let root = proof_create(
&mut engine,
"anchor",
"Root",
&[("status", "fallen")],
vec![],
)
.unwrap();
let child = proof_create(
&mut engine,
"anchor",
"Child",
&[],
vec![rel(&root.id.0, "FOLLOWS_FROM")],
)
.unwrap();
let grandchild = proof_create(
&mut engine,
"anchor",
"Grandchild",
&[],
vec![rel(&child.id.0, "FOLLOWS_FROM")],
)
.unwrap();
let standing = proof_create(
&mut engine,
"anchor",
"Standing Root",
&[("status", "open")],
vec![],
)
.unwrap();
let standing_child = proof_create(
&mut engine,
"anchor",
"Standing Child",
&[],
vec![rel(&standing.id.0, "FOLLOWS_FROM")],
)
.unwrap();
let unchecked = proof_create(
&mut engine,
"anchor",
"Checked No Checker",
&[("status", "checked")],
vec![],
)
.unwrap();
let onesided = proof_create(
&mut engine,
"tradeoff",
"One Sided",
&[],
vec![rel(&standing.id.0, "SUPPORTS")],
)
.unwrap();
let findings = crate::ops::health::collect_constraint_findings(
engine.store(),
None,
engine.schemas(),
None,
);
let tainted_of = |id: &crate::entity::EntityId| -> Vec<String> {
findings
.iter()
.filter(|r| &r.id == id)
.flat_map(|r| &r.violations)
.filter_map(|v| match v {
crate::ops::health::UnsatisfiedConstraint::StatusPropagation {
tainted_by, ..
} => Some(tainted_by.clone()),
_ => None,
})
.collect()
};
assert_eq!(tainted_of(&child.id), vec![root.id.to_string()]);
assert_eq!(
tainted_of(&grandchild.id),
vec![root.id.to_string()],
"the taint is transitive and names the terminal ancestor"
);
assert!(tainted_of(&standing_child.id).is_empty());
assert!(
tainted_of(&root.id).is_empty(),
"the source is not its own finding"
);
assert!(
findings.iter().any(|r| r.id == unchecked.id
&& r.violations.iter().any(|v| matches!(
v,
crate::ops::health::UnsatisfiedConstraint::RequiresWhen { field, .. }
if field == "checked_by"
))),
"checked-without-checker must be a health finding"
);
let missing = crate::ops::health::collect_missing_required_outgoing(
engine.store(),
None,
engine.schemas(),
);
let onesided_report = missing
.iter()
.find(|r| r.id == onesided.id)
.expect("one-sided trade-off flagged");
assert_eq!(onesided_report.missing.len(), 1);
assert_eq!(onesided_report.missing[0].relationships, vec!["OPPOSES"]);
}
const RECORD_MANIFEST: &str = r#"name: record-proof
version: 0.1.0
description: uniqueness and vocabulary proof schema
when_to_use: constraint-proof tests
types:
- record
- vocabulary
relationships:
mode: strict
definitions:
- name: REFERENCES
description: soft ref
default_weight: 0.5
- name: PART_OF
description: hier
default_weight: 1.0
- name: _default
description: fallback
default_weight: 1.0
community:
resolution: 1.0
seed: 42
"#;
fn record_type(unique_severity: &str) -> String {
format!(
r#"name: record
description: one imported record
when_to_use: tests
sections:
- key: body
heading: Body
required: true
search_weight: 10.0
catch_all: true
write_rules: []
metadata_fields:
- key: record_id
description: source id
field_type: string
- key: record_sha256
description: content hash
field_type: string
- key: category
description: category from the shared vocabulary
field_type: string
title_weight: 100.0
text_fields:
- body
hierarchy_relationship: PART_OF
no_self_loop_relationships: []
updatable_fields:
- title
- body
- record_id
- record_sha256
- category
health_required_fields:
- body
staleness_threshold_days: 90
constraints:
- kind: unique
fields: [record_id, record_sha256]
severity: {unique_severity}
- kind: enum_from_neighbour
field: category
relationships: [REFERENCES]
section: terms
write_rules: []
"#
)
}
const RECORD_VOCABULARY: &str = r#"name: vocabulary
description: the shared term list
when_to_use: tests
sections:
- key: terms
heading: Terms
required: false
search_weight: 5.0
catch_all: false
write_rules: []
- key: body
heading: Body
required: true
search_weight: 10.0
catch_all: true
write_rules: []
metadata_fields: []
title_weight: 100.0
text_fields:
- body
hierarchy_relationship: PART_OF
no_self_loop_relationships: []
updatable_fields:
- title
- body
- terms
health_required_fields:
- body
staleness_threshold_days: 90
write_rules: []
"#;
#[test]
fn unique_constraint_refuses_duplicates_and_health_reports_planted_ones() {
let tmp = TempDir::new().unwrap();
let record = record_type("block");
let mut engine = engine_with_proof_schema(
&tmp,
"record-proof",
RECORD_MANIFEST,
&[("record", &record), ("vocabulary", RECORD_VOCABULARY)],
);
let first = proof_create(
&mut engine,
"record",
"Record One",
&[("record_id", "19-42"), ("record_sha256", "abc123")],
vec![],
)
.unwrap();
let err = proof_create(
&mut engine,
"record",
"Record One Duplicate",
&[("record_id", "19-42"), ("record_sha256", "abc123")],
vec![],
)
.unwrap_err();
assert_eq!(err.code(), "CONSTRAINT_UNSATISFIED");
assert_eq!(
err.details()["violations"][0]["colliding"],
first.id.to_string(),
"the refusal names the colliding entity"
);
proof_create(
&mut engine,
"record",
"Record Two",
&[("record_id", "19-43"), ("record_sha256", "def456")],
vec![],
)
.unwrap();
let tmp = TempDir::new().unwrap();
let record = record_type("warn");
let mut engine = engine_with_proof_schema(
&tmp,
"record-proof",
RECORD_MANIFEST,
&[("record", &record), ("vocabulary", RECORD_VOCABULARY)],
);
proof_create(
&mut engine,
"record",
"Planted A",
&[("record_id", "19-42"), ("record_sha256", "abc123")],
vec![],
)
.unwrap();
let planted = proof_create(
&mut engine,
"record",
"Planted B",
&[("record_id", "19-42"), ("record_sha256", "abc123")],
vec![],
)
.unwrap();
assert!(
planted
.warnings
.iter()
.any(|w| matches!(w, WarningHint::ConstraintUnsatisfied { .. })),
"warn tier surfaces the duplicate as a warning and commits"
);
let findings = crate::ops::health::collect_constraint_findings(
engine.store(),
None,
engine.schemas(),
None,
);
assert_eq!(
findings.len(),
2,
"both sides of the planted duplicate are findings: {findings:?}"
);
}
#[test]
fn enum_from_neighbour_flags_stale_holders_after_rename() {
let tmp = TempDir::new().unwrap();
let record = record_type("warn");
let mut engine = engine_with_proof_schema(
&tmp,
"record-proof",
RECORD_MANIFEST,
&[("record", &record), ("vocabulary", RECORD_VOCABULARY)],
);
let (actor, client) = cli_actor();
let mut sections = IndexMap::new();
sections.insert("body".to_string(), "the term list.".to_string());
sections.insert("terms".to_string(), "- red\n- green\n".to_string());
let vocab = engine
.create_entity(
CreateEntityArgs {
anchors: Vec::new(),
mem: "proof".to_string(),
title: "Categories".to_string(),
entity_type: "vocabulary".to_string(),
sections,
metadata: IndexMap::new(),
relations: vec![],
dry_run: false,
},
actor,
Some(&client),
None,
)
.unwrap();
let holder = proof_create(
&mut engine,
"record",
"Holder",
&[("category", "red")],
vec![rel(&vocab.id.0, "REFERENCES")],
)
.unwrap();
let findings = crate::ops::health::collect_constraint_findings(
engine.store(),
None,
engine.schemas(),
None,
);
assert!(
findings.iter().all(|r| r.id != holder.id),
"backed value produces no finding: {findings:?}"
);
let current = engine.get_entity(&vocab.id).unwrap().content_hash.clone();
let mut sections = IndexMap::new();
sections.insert("terms".to_string(), "- crimson\n- green\n".to_string());
engine
.update_entity(
crate::engine::UpdateEntityArgs {
anchors: Vec::new(),
id: vocab.id.clone(),
expected_hash: crate::ExpectedHash::Current(current),
sections,
append_sections: IndexMap::new(),
patch_sections: IndexMap::new(),
sections_unset: Vec::new(),
metadata: IndexMap::new(),
metadata_unset: Vec::new(),
declare_relations: vec![],
dry_run: false,
relations_unset: Vec::new(),
anchors_unset: Vec::new(),
},
actor,
Some(&client),
None,
)
.unwrap();
let findings = crate::ops::health::collect_constraint_findings(
engine.store(),
None,
engine.schemas(),
None,
);
let stale = findings
.iter()
.find(|r| r.id == holder.id)
.expect("stale holder is flagged after the rename");
assert!(stale.violations.iter().any(|v| matches!(
v,
crate::ops::health::UnsatisfiedConstraint::EnumFromNeighbour { value, .. }
if value == "red"
)));
}
#[test]
fn create_warns_missing_required_outgoing_and_still_commits() {
let tmp = TempDir::new().unwrap();
let mut engine = engine_with_required_outgoing_schema(&tmp);
let (actor, client) = cli_actor();
let outcome = engine
.create_entity(
task_create_args("Orphan Task", vec![]),
actor,
Some(&client),
None,
)
.unwrap();
assert!(
!outcome.write_id.is_empty(),
"the warning never blocks the mutation"
);
let blocks = missing_outgoing_of(&outcome.warnings);
assert_eq!(
blocks,
vec![(vec!["PART_OF".to_string()], "at_least_one".to_string())],
"warning names the unsatisfied block with cardinality; warnings = {:?}",
outcome.warnings
);
let reports = crate::ops::health::collect_missing_required_outgoing(
engine.store(),
None,
engine.schemas(),
);
assert_eq!(reports.len(), 1);
assert_eq!(reports[0].id, outcome.id);
assert_eq!(reports[0].missing.len(), 1);
assert_eq!(reports[0].missing[0].relationships, vec!["PART_OF"]);
assert_eq!(reports[0].missing[0].cardinality, "at_least_one");
let satisfied = engine
.create_entity(
task_create_args(
"Child Task",
vec![crate::ops::RelateArg {
target: outcome.id.clone(),
rel_type: "PART_OF".to_string(),
description: None,
}],
),
actor,
Some(&client),
None,
)
.unwrap();
assert!(
missing_outgoing_of(&satisfied.warnings).is_empty(),
"satisfied block emits no warning: {:?}",
satisfied.warnings
);
}
#[test]
fn update_warns_missing_required_outgoing_until_satisfied() {
let tmp = TempDir::new().unwrap();
let mut engine = engine_with_required_outgoing_schema(&tmp);
let (actor, client) = cli_actor();
let a = engine
.create_entity(
task_create_args("Task A", vec![]),
actor,
Some(&client),
None,
)
.unwrap();
let b = engine
.create_entity(
task_create_args("Task B", vec![]),
actor,
Some(&client),
None,
)
.unwrap();
let update =
|engine: &mut Engine, id: &crate::entity::EntityId, declare: Vec<crate::ops::RelateArg>| {
let current = engine.get_entity(id).unwrap().content_hash.clone();
let mut sections = IndexMap::new();
sections.insert("body".to_string(), format!("edited at {:?}", declare.len()));
engine
.update_entity(
crate::engine::UpdateEntityArgs {
anchors: Vec::new(),
id: id.clone(),
expected_hash: crate::ExpectedHash::Current(current),
sections,
append_sections: IndexMap::new(),
patch_sections: IndexMap::new(),
sections_unset: Vec::new(),
metadata: IndexMap::new(),
metadata_unset: Vec::new(),
declare_relations: declare,
dry_run: false,
relations_unset: Vec::new(),
anchors_unset: Vec::new(),
},
actor,
Some(&client),
None,
)
.unwrap()
};
let outcome = update(&mut engine, &a.id, vec![]);
assert!(!outcome.write_id.is_empty());
assert_eq!(
missing_outgoing_of(&outcome.warnings),
vec![(vec!["PART_OF".to_string()], "at_least_one".to_string())]
);
let outcome = update(
&mut engine,
&a.id,
vec![crate::ops::RelateArg {
target: b.id.clone(),
rel_type: "PART_OF".to_string(),
description: None,
}],
);
assert!(
missing_outgoing_of(&outcome.warnings).is_empty(),
"satisfied block emits no warning: {:?}",
outcome.warnings
);
}