trusty_code/serve/rest/projects.rs
1//! `GET /projects` REST route over the `fs.list_projects` JSON-RPC method
2//! (issue #3365).
3//!
4//! Why: the GUI's workstream-first project-picker modal needs a small,
5//! read-only, loopback-only roster of candidate projects (ADR-0011 — this
6//! daemon binds loopback only, matching every other route in this gateway).
7//! Mirrors `super::fs`'s shape exactly: a thin handler over
8//! `crate::fs_browse::protocol::list_projects` via [`super::respond`], never
9//! duplicating the scan logic (`crate::fs_browse::roster::list_projects`) as
10//! a bespoke axum handler — see `super` module docs' "forking the two
11//! surfaces" rationale.
12//! What: [`routes`] builds a standalone `axum::Router<()>` (its own
13//! `ProjectsState` carrying just the `Arc<Router>`, `with_state`-erased
14//! before return, mirroring `super::fs::FsState`) mapping `GET /projects` ->
15//! `fs.list_projects`. No query params — the roster takes none.
16//! `crate::serve::http::build_axum_router` merges this into the daemon's
17//! main router alongside `POST /rpc`, `GET /health`, `GET /fs`, and the
18//! other REST resource groups — `/projects` collides with none of them.
19//! Test: `tests::*`.
20
21use std::sync::Arc;
22
23use axum::Router as AxumRouter;
24use axum::extract::State;
25use axum::routing::get;
26use serde_json::Value;
27
28use crate::jsonrpc::Router;
29
30use super::{RestResult, respond};
31
32/// Shared axum state for the route in this module: just the JSON-RPC
33/// router, mirroring `super::fs::FsState` — the handler goes through
34/// [`super::respond`] rather than touching the filesystem directly.
35#[derive(Clone)]
36struct ProjectsState {
37 router: Arc<Router>,
38}
39
40/// Build the `GET /projects` route group.
41///
42/// Why: kept separate from `crate::serve::http::build_axum_router` so this
43/// resource group is unit-testable via `tower::util::ServiceExt::oneshot` on
44/// its own, exactly like every other `rest::*` group.
45/// What: one `GET` route, `ProjectsState { router }`, `with_state`-erased to
46/// `axum::Router<()>` so the caller can `.merge()` it alongside the other
47/// resource groups into the daemon's main router.
48/// Test: `tests::list_projects_returns_200_with_entries_array`.
49pub fn routes(router: Arc<Router>) -> AxumRouter {
50 AxumRouter::new()
51 .route("/projects", get(list_projects))
52 .with_state(ProjectsState { router })
53}
54
55/// `GET /projects` -> `fs.list_projects`.
56///
57/// Why: the REST entry point for the project-picker modal's roster fetch.
58/// What: `200` with the `ProjectRoster` JSON (`{"entries": [...]}`) — the
59/// underlying RPC method never returns a caller-facing error (see
60/// `fs_browse::protocol::list_projects`'s docs), so this route has no
61/// documented error path of its own beyond a lower-level dispatch failure.
62/// Test: `tests::list_projects_returns_200_with_entries_array`.
63async fn list_projects(State(state): State<ProjectsState>) -> RestResult {
64 respond(&state.router, "fs.list_projects", Value::Null).await
65}
66
67#[cfg(test)]
68mod tests {
69 use super::*;
70 use axum::body::{Body, to_bytes};
71 use axum::http::{Request, StatusCode};
72 use serde_json::Value as JsonValue;
73 use tower::util::ServiceExt;
74
75 fn app() -> AxumRouter {
76 let mut router = Router::new();
77 crate::fs_browse::protocol::register(&mut router);
78 routes(Arc::new(router))
79 }
80
81 async fn body_json(response: axum::response::Response) -> JsonValue {
82 let bytes = to_bytes(response.into_body(), 1024 * 1024).await.unwrap();
83 serde_json::from_slice(&bytes).unwrap()
84 }
85
86 /// `GET /projects` must be reachable and return the `entries` array
87 /// shape the picker modal dereferences. Runs against the real
88 /// developer/CI-runner home directory (no injectable seam at the REST
89 /// layer — `fs_browse::roster::tests` carries the real scan-behaviour
90 /// coverage), so this only pins the route + wire shape.
91 #[tokio::test]
92 async fn list_projects_returns_200_with_entries_array() {
93 let resp = app()
94 .oneshot(
95 Request::builder()
96 .method("GET")
97 .uri("/projects")
98 .body(Body::empty())
99 .unwrap(),
100 )
101 .await
102 .unwrap();
103
104 assert_eq!(resp.status(), StatusCode::OK);
105 let v = body_json(resp).await;
106 assert!(v["entries"].is_array());
107 }
108}