#![cfg(test)]
use indexmap::IndexMap;
use tempfile::TempDir;
use crate::backend::MemBackend;
use crate::engine::{CreateEntityArgs, Engine, RelateAction, RelateEntityArgs, UpdateEntityArgs};
use crate::ops::WarningHint;
use crate::storage::FilesystemBackend;
use crate::vcs::Actor;
use crate::workspace::{Mount, MountCapability, MountLifecycle, MountStorage};
fn deriv_schema() -> std::sync::Arc<memstead_schema::Schema> {
let manifest = r#"name: deriv
version: 0.1.0
description: derivation fixture
when_to_use: tests
types:
- note
relationships:
mode: strict
definitions:
- name: PART_OF
description: hier
default_weight: 3.0
- name: DERIVED_FROM
description: source derives from target
default_weight: 2.0
derivation: true
- name: SUPPORTS
description: plain edge
default_weight: 1.0
- name: _default
description: fallback
default_weight: 1.0
community:
resolution: 1.0
seed: 42
"#;
let type_yaml = r#"name: note
description: t
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
write_rules: []
"#;
std::sync::Arc::new(
memstead_schema::loader::load_schema_from_memory(
manifest,
&[("note".to_string(), type_yaml.to_string())],
)
.expect("fixture schema loads"),
)
}
fn engine_at(tmp: &TempDir) -> Engine {
let mem_dir = tmp.path().to_path_buf();
let writer = FilesystemBackend::new(mem_dir.clone());
let mount = Mount {
mem: "m".to_string(),
schema: Some("deriv@0.1.0".parse().unwrap()),
storage: MountStorage::Folder { path: mem_dir },
capability: MountCapability::Write,
lifecycle: MountLifecycle::Eager,
cross_linkable: true,
migration_target: None,
};
Engine::from_mounts_with_schemas_dir_and_extra(
vec![(mount, Box::new(writer) as Box<dyn MemBackend>)],
None,
vec![deriv_schema()],
)
.unwrap()
}
fn note(title: &str, body: &str) -> CreateEntityArgs {
CreateEntityArgs {
anchors: Vec::new(),
mem: "m".to_string(),
title: title.to_string(),
entity_type: "note".to_string(),
sections: IndexMap::from_iter([("body".to_string(), body.to_string())]),
metadata: IndexMap::new(),
relations: Vec::new(),
dry_run: false,
}
}
fn relate_args(from: &crate::EntityId, rel: &str, to: &crate::EntityId) -> RelateEntityArgs {
RelateEntityArgs {
source: from.clone(),
expected_hash: None,
rel_type: rel.to_string(),
target: to.clone(),
remove: false,
description: None,
dry_run: false,
}
}
#[test]
fn derivation_write_edit_report_reassert_clear() {
let tmp = TempDir::new().unwrap();
let mut engine = engine_at(&tmp);
let target = engine
.create_entity(note("Target", "v1"), Actor::Cli, None, None)
.unwrap();
let source = engine
.create_entity(note("Source", "conclusion"), Actor::Cli, None, None)
.unwrap();
let added = engine
.relate_entity(
relate_args(&source.id, "DERIVED_FROM", &target.id),
Actor::Cli,
None,
None,
)
.unwrap();
assert_eq!(added.action, RelateAction::Added);
assert!(
engine.derivation_report("m").unwrap().is_empty(),
"freshly baselined edge must not report"
);
let t_now = engine.get_entity(&target.id).unwrap().content_hash.clone();
engine
.update_entity(
UpdateEntityArgs {
anchors: Vec::new(),
id: target.id.clone(),
expected_hash: Some(t_now),
sections: IndexMap::from_iter([("body".to_string(), "v2".to_string())]),
append_sections: IndexMap::new(),
patch_sections: IndexMap::new(),
sections_unset: Vec::new(),
metadata: IndexMap::new(),
metadata_unset: Vec::new(),
declare_relations: Vec::new(),
dry_run: false,
relations_unset: Vec::new(),
anchors_unset: Vec::new(),
},
Actor::Cli,
None,
None,
)
.unwrap();
let report = engine.derivation_report("m").unwrap();
assert_eq!(report.len(), 1, "{report:?}");
assert_eq!(report[0].source, source.id);
assert_eq!(report[0].rel_type, "DERIVED_FROM");
assert_eq!(report[0].target, target.id);
assert_eq!(report[0].state, "stale");
assert!(report[0].baseline.is_some());
let s_now = engine.get_entity(&source.id).unwrap().content_hash.clone();
engine
.update_entity(
UpdateEntityArgs {
anchors: Vec::new(),
id: source.id.clone(),
expected_hash: Some(s_now),
sections: IndexMap::from_iter([("body".to_string(), "conclusion v2".to_string())]),
append_sections: IndexMap::new(),
patch_sections: IndexMap::new(),
sections_unset: Vec::new(),
metadata: IndexMap::new(),
metadata_unset: Vec::new(),
declare_relations: Vec::new(),
dry_run: false,
relations_unset: Vec::new(),
anchors_unset: Vec::new(),
},
Actor::Cli,
None,
None,
)
.unwrap();
let report = engine.derivation_report("m").unwrap();
assert_eq!(report.len(), 1, "source edit adds nothing: {report:?}");
assert_eq!(report[0].state, "stale");
let md_before = std::fs::read(tmp.path().join("source.md")).unwrap();
let hash_before = engine.get_entity(&source.id).unwrap().content_hash.clone();
let refreshed = engine
.relate_entity(
relate_args(&source.id, "DERIVED_FROM", &target.id),
Actor::Cli,
None,
None,
)
.unwrap();
assert_eq!(refreshed.action, RelateAction::NoOpAlreadyPresent);
assert_eq!(refreshed.content_hash, hash_before, "_hash unchanged");
assert!(
!refreshed.write_id.is_empty(),
"the sidecar refresh persists via a real commit"
);
assert!(
refreshed
.warnings
.iter()
.any(|w| matches!(w, WarningHint::DerivationBaselineRefreshed { .. })),
"the refresh is STATED, never a bare no-op: {:?}",
refreshed.warnings
);
let md_after = std::fs::read(tmp.path().join("source.md")).unwrap();
assert_eq!(md_before, md_after, "baselines never touch the markdown");
assert!(
engine.derivation_report("m").unwrap().is_empty(),
"re-assert clears the staleness"
);
engine
.relate_entity(
relate_args(&source.id, "SUPPORTS", &target.id),
Actor::Cli,
None,
None,
)
.unwrap();
let noop = engine
.relate_entity(
relate_args(&source.id, "SUPPORTS", &target.id),
Actor::Cli,
None,
None,
)
.unwrap();
assert_eq!(noop.action, RelateAction::NoOpAlreadyPresent);
assert!(noop.write_id.is_empty(), "undeclared no-op stays bare");
assert!(
!noop
.warnings
.iter()
.any(|w| matches!(w, WarningHint::DerivationBaselineRefreshed { .. })),
);
assert!(engine.derivation_report("m").unwrap().is_empty());
}
#[test]
fn missing_baseline_reports_unbaselined_never_fabricated() {
let tmp = TempDir::new().unwrap();
let mut engine = engine_at(&tmp);
let target = engine
.create_entity(note("Target", "v1"), Actor::Cli, None, None)
.unwrap();
let source = engine
.create_entity(note("Source", "conclusion"), Actor::Cli, None, None)
.unwrap();
engine
.relate_entity(
relate_args(&source.id, "DERIVED_FROM", &target.id),
Actor::Cli,
None,
None,
)
.unwrap();
let sidecar = tmp.path().join(".memstead").join("derivations.json");
assert!(sidecar.exists(), "baseline persisted to the sidecar");
assert!(
engine
.get_entity(&crate::EntityId::new("m", "derivations"))
.is_none()
);
std::fs::remove_file(&sidecar).unwrap();
let report = engine.derivation_report("m").unwrap();
assert_eq!(report.len(), 1, "{report:?}");
assert_eq!(report[0].state, "unbaselined");
assert!(report[0].baseline.is_none());
}