Skip to main content

soothfast_spec/
specified.rs

1//! Whether each registered route or tool is in the committed spec and
2//! exercised by a probe.
3
4use crate::reconcile::Reconciliation;
5use crate::{DeclaredOp, RouteDecl};
6
7/// One registered route or tool and where it is accounted for.
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct Specified {
10    /// Spec file the route is registered against.
11    pub spec: String,
12    /// Operation identity: operationId, GraphQL field, MCP tool name or channel.
13    pub operation: String,
14    /// Verb the code serves, or `TOOL`/`QUERY`/... for non-HTTP dialects.
15    pub method: String,
16    /// Path or template the code serves.
17    pub path: String,
18    /// The handler's public path, as the registry records it.
19    pub handler: String,
20    /// The committed spec declares this operation with the method and path
21    /// the code serves.
22    pub in_spec: bool,
23    /// How the spec's declaration of this operation disagrees with the code.
24    pub mismatch: Option<String>,
25    /// Some probe exercises this method and path. `None` for a `TOOL`, which
26    /// no HTTP probe can reach.
27    pub probed: Option<bool>,
28}
29
30impl Specified {
31    /// In the spec and, where a probe can reach it, probed.
32    pub fn is_specified(&self) -> bool {
33        self.in_spec && self.probed.unwrap_or(true)
34    }
35}
36
37/// One entry per route, in registry order. `rec` is the reconciliation of
38/// `routes` against the spec file's declared operations, and `covers`
39/// decides whether a probe exercises a route's operation.
40pub fn specified(
41    spec: &str,
42    routes: &[RouteDecl],
43    rec: &Reconciliation,
44    covers: impl Fn(&DeclaredOp) -> bool,
45) -> Vec<Specified> {
46    routes
47        .iter()
48        .map(|route| {
49            let op = DeclaredOp {
50                operation: route.operation.clone(),
51                method: route.method.clone(),
52                path: route.path.clone(),
53            };
54            let mismatch = rec
55                .mismatched
56                .iter()
57                .find(|(m, _, _)| m.id == route.id && m.operation == route.operation)
58                .map(|(_, _, what)| what.clone());
59            let absent = rec
60                .missing_spec
61                .iter()
62                .any(|m| m.id == route.id && m.operation == route.operation);
63            Specified {
64                spec: spec.to_string(),
65                in_spec: !absent && mismatch.is_none(),
66                mismatch,
67                probed: (route.method != "TOOL").then(|| covers(&op)),
68                operation: route.operation.clone(),
69                method: route.method.clone(),
70                path: route.path.clone(),
71                handler: route.id.clone(),
72            }
73        })
74        .collect()
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80    use crate::reconcile::reconcile;
81
82    fn op(operation: &str, method: &str, path: &str) -> DeclaredOp {
83        DeclaredOp {
84            operation: operation.into(),
85            method: method.into(),
86            path: path.into(),
87        }
88    }
89
90    fn route(operation: &str, method: &str, path: &str) -> RouteDecl {
91        RouteDecl {
92            id: format!("app::{operation}"),
93            operation: operation.into(),
94            method: method.into(),
95            path: path.into(),
96        }
97    }
98
99    #[test]
100    fn reports_spec_and_probe_membership_per_route() {
101        let declared = vec![op("getItem", "GET", "/items/{id}")];
102        let routes = vec![
103            route("getItem", "GET", "/items/{id}"),
104            route("ghost", "GET", "/ghost"),
105        ];
106        let rec = reconcile(&declared, &routes);
107        let items = specified("api.yaml", &routes, &rec, |op| op.operation == "getItem");
108        assert_eq!(items.len(), 2);
109        assert!(items[0].in_spec && items[0].probed == Some(true) && items[0].is_specified());
110        assert!(!items[1].in_spec && items[1].probed == Some(false) && !items[1].is_specified());
111        assert_eq!(items[0].handler, "app::getItem");
112        assert_eq!(items[0].spec, "api.yaml");
113    }
114
115    #[test]
116    fn an_mcp_tool_is_specified_by_being_in_the_spec_alone() {
117        let declared = vec![op("search", "TOOL", "search")];
118        let routes = vec![route("search", "TOOL", "search")];
119        let rec = reconcile(&declared, &routes);
120        let items = specified("tools.json", &routes, &rec, |_| false);
121        assert!(items[0].in_spec);
122        assert_eq!(items[0].probed, None);
123        assert!(items[0].is_specified());
124    }
125
126    #[test]
127    fn a_mismatched_route_is_not_in_the_spec() {
128        let declared = vec![op("getItem", "GET", "/items/{id}")];
129        let routes = vec![route("getItem", "POST", "/items/{id}")];
130        let rec = reconcile(&declared, &routes);
131        let items = specified("api.yaml", &routes, &rec, |_| true);
132        assert!(!items[0].in_spec);
133        assert_eq!(items[0].probed, Some(true));
134        assert_eq!(
135            items[0].mismatch.as_deref(),
136            Some("method POST != spec GET")
137        );
138        assert!(!items[0].is_specified());
139    }
140}