use std::path::PathBuf;
use crate::model::{
AdrStatus, Item, ItemBuilder, ItemId, ItemType, Relationship, RelationshipType, SourceLocation,
};
#[must_use]
pub fn create_test_item(id: &str, item_type: ItemType) -> Item {
create_test_item_with_name(id, item_type, &format!("Test {id}"))
}
#[must_use]
pub fn create_test_item_with_name(id: &str, item_type: ItemType, name: &str) -> Item {
let source = SourceLocation::new(PathBuf::from("/test-repo"), format!("{id}.md"));
let mut builder = ItemBuilder::new()
.id(ItemId::new_unchecked(id))
.item_type(item_type)
.name(name)
.source(source);
if item_type.requires_specification() {
builder = builder.specification("The system SHALL meet this test specification");
}
if item_type.requires_deciders() {
builder = builder
.status(AdrStatus::Proposed)
.deciders(vec!["Test Decider".to_string()]);
}
builder
.build()
.expect("Test item should build successfully")
}
#[must_use]
pub fn create_test_item_with_relationships(
id: &str,
item_type: ItemType,
relationships: Vec<Relationship>,
) -> Item {
let source = SourceLocation::new(PathBuf::from("/test-repo"), format!("{id}.md"));
let mut builder = ItemBuilder::new()
.id(ItemId::new_unchecked(id))
.item_type(item_type)
.name(format!("Test {id}"))
.source(source)
.relationships(relationships);
if item_type.requires_specification() {
builder = builder.specification("The system SHALL meet this test specification");
}
if item_type.requires_deciders() {
builder = builder
.status(AdrStatus::Proposed)
.deciders(vec!["Test Decider".to_string()]);
}
builder
.build()
.expect("Test item should build successfully")
}
#[must_use]
pub fn create_test_adr(id: &str, justifies: &[&str], supersedes: &[&str]) -> Item {
let source = SourceLocation::new(PathBuf::from("/test-repo"), format!("{id}.md"));
let relationships: Vec<Relationship> = justifies
.iter()
.map(|s| Relationship::new(ItemId::new_unchecked(*s), RelationshipType::Justifies))
.collect();
ItemBuilder::new()
.id(ItemId::new_unchecked(id))
.item_type(ItemType::ArchitectureDecisionRecord)
.name(format!("Test {id}"))
.source(source)
.relationships(relationships)
.status(AdrStatus::Proposed)
.deciders(vec!["Test Decider".to_string()])
.supersedes_all(
supersedes
.iter()
.map(|s| ItemId::new_unchecked(*s))
.collect(),
)
.build()
.expect("Test ADR should build successfully")
}
#[must_use]
pub fn create_test_item_at(id: &str, item_type: ItemType, file_path: &str) -> Item {
let source = SourceLocation::new(PathBuf::from("/test-repo"), PathBuf::from(file_path));
let mut builder = ItemBuilder::new()
.id(ItemId::new_unchecked(id))
.item_type(item_type)
.name(format!("Test {id}"))
.source(source);
if item_type.requires_specification() {
builder = builder.specification("The system SHALL meet this test specification");
}
if item_type.requires_deciders() {
builder = builder
.status(AdrStatus::Proposed)
.deciders(vec!["Test Decider".to_string()]);
}
builder
.build()
.expect("Test item should build successfully")
}
#[must_use]
pub fn create_simple_hierarchy() -> Vec<Item> {
vec![
create_test_item("SOL-001", ItemType::Solution),
create_test_item_with_relationships(
"UC-001",
ItemType::UseCase,
vec![Relationship::new(
ItemId::new_unchecked("SOL-001"),
RelationshipType::Refines,
)],
),
create_test_item_with_relationships(
"SCEN-001",
ItemType::Scenario,
vec![Relationship::new(
ItemId::new_unchecked("UC-001"),
RelationshipType::Refines,
)],
),
]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_test_item_solution() {
let item = create_test_item("SOL-001", ItemType::Solution);
assert_eq!(item.id.as_str(), "SOL-001");
assert_eq!(item.item_type, ItemType::Solution);
}
#[test]
fn test_create_test_item_requirement() {
let item = create_test_item("SYSREQ-001", ItemType::SystemRequirement);
assert_eq!(item.id.as_str(), "SYSREQ-001");
assert!(item.attributes.specification().is_some());
}
#[test]
fn test_create_test_adr() {
let item = create_test_adr("ADR-001", &["SYSARCH-001"], &["ADR-000"]);
assert_eq!(item.id.as_str(), "ADR-001");
assert_eq!(item.attributes.status(), Some(AdrStatus::Proposed));
let justifies: Vec<_> = item.relationship_ids(RelationshipType::Justifies).collect();
assert_eq!(justifies.len(), 1);
assert_eq!(item.attributes.supersedes().len(), 1);
}
#[test]
fn test_create_simple_hierarchy() {
let items = create_simple_hierarchy();
assert_eq!(items.len(), 3);
assert_eq!(items[0].item_type, ItemType::Solution);
assert_eq!(items[1].item_type, ItemType::UseCase);
assert_eq!(items[2].item_type, ItemType::Scenario);
}
}