Skip to main content

max / makenotwork

3.8 KB · 121 lines History Blame Raw
1 //! MCP tool parameters and handlers for the test-run tools.
2
3 use schemars::JsonSchema;
4 use serde::Deserialize;
5 use tracing::instrument;
6
7 use crate::checks::ssh;
8 use crate::db;
9
10 use super::PomServer;
11
12 #[derive(Debug, Deserialize, JsonSchema)]
13 pub struct RunTestsParams {
14 /// Target name to run tests on
15 pub target: String,
16 /// Optional filter to run specific tests
17 pub filter: Option<String>,
18 }
19
20 #[derive(Debug, Deserialize, JsonSchema)]
21 pub struct TestHistoryParams {
22 /// Filter by target name
23 pub target: Option<String>,
24 /// Number of results to return (default 10)
25 pub limit: Option<i64>,
26 }
27
28 #[derive(Debug, Deserialize, JsonSchema)]
29 pub struct LastTestOutputParams {
30 /// Target name to get output for
31 pub target: String,
32 }
33
34 impl PomServer {
35 #[instrument(skip_all)]
36 pub async fn run_tests_impl(&self, params: RunTestsParams) -> crate::error::Result<String> {
37 let target = self.config.get_target(&params.target).ok_or_else(|| {
38 crate::error::PomError::Config(format!("Unknown target: {}", params.target))
39 })?;
40
41 let tests_config = target.tests.as_ref().ok_or_else(|| {
42 crate::error::PomError::Config(format!(
43 "Target '{}' has no test configuration",
44 params.target
45 ))
46 })?;
47
48 let run = ssh::run_tests(&params.target, tests_config, params.filter.as_deref()).await;
49 let run_id = db::insert_test_run(&self.pool, &run).await?;
50
51 // Store per-test details and detect regressions
52 if !run.summary.details.is_empty() {
53 db::insert_test_details(&self.pool, run_id, &run.summary.details).await?;
54 }
55 let regressions = db::get_test_regressions(&self.pool, &params.target, run_id)
56 .await
57 .unwrap_or_default();
58
59 // Return summary without raw_output (it can be huge)
60 let summary = serde_json::json!({
61 "target": run.target,
62 "passed": run.passed,
63 "exit_code": run.exit_code,
64 "duration_secs": run.duration_secs,
65 "started_at": run.started_at,
66 "finished_at": run.finished_at,
67 "filter": run.filter,
68 "summary": run.summary,
69 "regressions": regressions,
70 });
71
72 Ok(serde_json::to_string_pretty(&summary)?)
73 }
74
75 #[instrument(skip_all)]
76 pub async fn test_history_impl(
77 &self,
78 params: TestHistoryParams,
79 ) -> crate::error::Result<String> {
80 let limit = params.limit.unwrap_or(10);
81 let history = db::get_test_history(&self.pool, params.target.as_deref(), limit).await?;
82
83 if history.is_empty() {
84 return Ok("No test run history.".to_string());
85 }
86
87 // Return without raw_output
88 let summaries: Vec<serde_json::Value> = history
89 .iter()
90 .map(|run| {
91 serde_json::json!({
92 "id": run.id,
93 "target": run.target,
94 "passed": run.passed,
95 "exit_code": run.exit_code,
96 "duration_secs": run.duration_secs,
97 "started_at": run.started_at,
98 "finished_at": run.finished_at,
99 "filter": run.filter,
100 "summary": run.summary,
101 })
102 })
103 .collect();
104
105 Ok(serde_json::to_string_pretty(&summaries)?)
106 }
107
108 #[instrument(skip_all)]
109 pub async fn last_test_output_impl(
110 &self,
111 params: LastTestOutputParams,
112 ) -> crate::error::Result<String> {
113 let run = db::get_latest_test_run(&self.pool, &params.target).await?;
114
115 match run {
116 Some(r) => Ok(r.raw_output),
117 None => Ok(format!("No test runs found for target '{}'", params.target)),
118 }
119 }
120 }
121