cartulary 0.3.0-alpha.1

The knowledge layer of your project — decisions, issues, docs, all in one place.
Documentation
use crate::domain::model::issue::IssueLinks;
use crate::domain::model::record_ref::IssueRef;
use crate::domain::usecases::issue::IssueRepository;

/// Return the links for an existing issue.
///
/// Returns `Err` if the issue is not found.
pub fn list_links(repo: &dyn IssueRepository, id: &IssueRef) -> anyhow::Result<IssueLinks> {
    let issue = repo
        .find_by_id(id)?
        .ok_or_else(|| anyhow::anyhow!("issue {id} not found"))?;
    Ok(issue.links)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::domain::usecases::issue::tests::{feature, FakeIssueRepository, IssueFixture};

    fn scenario() -> Scenario {
        Scenario {
            repo: FakeIssueRepository::new(),
        }
    }

    struct Scenario {
        repo: FakeIssueRepository,
    }

    impl Scenario {
        fn given(mut self, fixture: IssueFixture) -> Self {
            let raw = fixture
                .id
                .as_deref()
                .expect("given() requires an explicit id — use .with_id()")
                .to_string();
            let numeric =
                IssueRef::new(&raw).unwrap_or_else(|_| panic!("given(): invalid id {raw:?}"));
            self.repo.push_issue(fixture.build(numeric));
            self
        }

        fn when_list_links(self, id: &str) -> Outcome {
            let id_ref = IssueRef::new(id).unwrap_or_else(|_| panic!("invalid issue ref {id:?}"));
            let result = list_links(&self.repo, &id_ref);
            Outcome { result }
        }
    }

    struct Outcome {
        result: anyhow::Result<IssueLinks>,
    }

    impl Outcome {
        fn then_link_count(self, expected: usize) -> Self {
            let links = self.result.as_ref().expect("expected Ok, got Err");
            assert_eq!(
                links.len(),
                expected,
                "expected {expected} links, got {}",
                links.len()
            );
            self
        }

        fn then_link_target(self, index: usize, expected: &str) -> Self {
            let links = self.result.as_ref().expect("expected Ok, got Err");
            assert_eq!(
                links[index].target.as_str(),
                expected,
                "expected link[{index}].target = {expected:?}"
            );
            self
        }

        fn then_err_contains(self, substring: &str) {
            let msg = self.result.expect_err("expected Err, got Ok").to_string();
            assert!(
                msg.contains(substring),
                "expected error containing {substring:?}, got {msg:?}"
            );
        }
    }

    #[test]
    fn listing_links_returns_the_links_of_an_issue() {
        scenario()
            .given(
                feature("Add login")
                    .with_id("ISSUE-0001")
                    .with_link("ISSUE-0002", "blocked-by"),
            )
            .when_list_links("ISSUE-0001")
            .then_link_count(1)
            .then_link_target(0, "ISSUE-0002");
    }

    #[test]
    fn listing_links_of_an_unknown_issue_returns_an_error() {
        scenario()
            .when_list_links("ISSUE-0099")
            .then_err_contains("not found");
    }

    #[test]
    fn listing_links_returns_empty_when_the_issue_has_none() {
        scenario()
            .given(feature("Add login").with_id("ISSUE-0001"))
            .when_list_links("ISSUE-0001")
            .then_link_count(0);
    }
}