forked from 1jehuang/jcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_testers.rs
More file actions
236 lines (211 loc) · 7.94 KB
/
Copy pathdebug_testers.rs
File metadata and controls
236 lines (211 loc) · 7.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
use anyhow::Result;
use std::path::PathBuf;
/// Execute tester commands
pub(super) async fn execute_tester_command(command: &str) -> Result<String> {
let trimmed = command.trim();
if trimmed == "list" {
let testers = load_testers()?;
if testers.is_empty() {
return Ok("No active testers.".to_string());
}
return Ok(serde_json::to_string_pretty(&testers)?);
}
if trimmed == "spawn" || trimmed.starts_with("spawn ") {
let opts: serde_json::Value = if trimmed == "spawn" {
serde_json::json!({})
} else {
serde_json::from_str(trimmed.strip_prefix("spawn ").unwrap_or("{}"))?
};
return spawn_tester(opts).await;
}
let parts: Vec<&str> = trimmed.splitn(3, ':').collect();
if parts.len() >= 2 {
let tester_id = parts[0];
let cmd = parts[1];
let arg = parts.get(2).copied();
return execute_tester_subcommand(tester_id, cmd, arg).await;
}
Err(anyhow::anyhow!(
"Unknown tester command: {}. Use tester:help for usage.",
trimmed
))
}
fn load_testers() -> Result<Vec<serde_json::Value>> {
let path = crate::storage::jcode_dir()?.join("testers.json");
if path.exists() {
let content = std::fs::read_to_string(&path)?;
if content.trim().is_empty() {
return Ok(vec![]);
}
Ok(serde_json::from_str(&content)?)
} else {
Ok(vec![])
}
}
fn save_testers(testers: &[serde_json::Value]) -> Result<()> {
let path = crate::storage::jcode_dir()?.join("testers.json");
std::fs::write(&path, serde_json::to_string_pretty(testers)?)?;
Ok(())
}
async fn spawn_tester(opts: serde_json::Value) -> Result<String> {
use std::process::Stdio;
let id = format!("tester_{}", crate::id::new_id("tui"));
let cwd = opts.get("cwd").and_then(|v| v.as_str()).unwrap_or(".");
let binary = opts.get("binary").and_then(|v| v.as_str());
let binary_path = if let Some(b) = binary {
PathBuf::from(b)
} else if let Ok(current) = crate::build::current_binary_path() {
if current.exists() {
current
} else if let Ok(canary) = crate::build::canary_binary_path() {
if canary.exists() {
canary
} else {
std::env::current_exe()?
}
} else {
std::env::current_exe()?
}
} else if let Ok(canary) = crate::build::canary_binary_path() {
if canary.exists() {
canary
} else {
std::env::current_exe()?
}
} else {
std::env::current_exe()?
};
if !binary_path.exists() {
return Err(anyhow::anyhow!(
"Binary not found: {}",
binary_path.display()
));
}
let debug_cmd = std::env::temp_dir().join(format!("jcode_debug_cmd_{}", id));
let debug_resp = std::env::temp_dir().join(format!("jcode_debug_response_{}", id));
let stdout_path = std::env::temp_dir().join(format!("jcode_tester_stdout_{}", id));
let stderr_path = std::env::temp_dir().join(format!("jcode_tester_stderr_{}", id));
let stdout_file = std::fs::File::create(&stdout_path)?;
let stderr_file = std::fs::File::create(&stderr_path)?;
let _ = crate::platform::set_permissions_owner_only(&stdout_path);
let _ = crate::platform::set_permissions_owner_only(&stderr_path);
let _ = std::fs::File::create(&debug_cmd)
.and_then(|_| crate::platform::set_permissions_owner_only(&debug_cmd));
let _ = std::fs::File::create(&debug_resp)
.and_then(|_| crate::platform::set_permissions_owner_only(&debug_resp));
let mut cmd = tokio::process::Command::new(&binary_path);
cmd.current_dir(cwd);
cmd.env(crate::cli::selfdev::CLIENT_SELFDEV_ENV, "1");
cmd.env(
"JCODE_DEBUG_CMD_PATH",
debug_cmd.to_string_lossy().to_string(),
);
cmd.env(
"JCODE_DEBUG_RESPONSE_PATH",
debug_resp.to_string_lossy().to_string(),
);
cmd.arg("--debug-socket");
cmd.stdout(Stdio::from(stdout_file));
cmd.stderr(Stdio::from(stderr_file));
let child = cmd.spawn()?;
let pid = child.id().unwrap_or(0);
let info = serde_json::json!({
"id": id,
"pid": pid,
"binary": binary_path.to_string_lossy(),
"cwd": cwd,
"debug_cmd_path": debug_cmd.to_string_lossy(),
"debug_response_path": debug_resp.to_string_lossy(),
"stdout_path": stdout_path.to_string_lossy(),
"stderr_path": stderr_path.to_string_lossy(),
"started_at": chrono::Utc::now().to_rfc3339(),
});
let mut testers = load_testers()?;
testers.push(info);
save_testers(&testers)?;
Ok(serde_json::json!({
"id": id,
"pid": pid,
"message": format!("Spawned tester {} (pid {})", id, pid)
})
.to_string())
}
async fn execute_tester_subcommand(
tester_id: &str,
cmd: &str,
arg: Option<&str>,
) -> Result<String> {
let testers = load_testers()?;
let tester = testers
.iter()
.find(|t| t.get("id").and_then(|v| v.as_str()) == Some(tester_id))
.ok_or_else(|| anyhow::anyhow!("Tester not found: {}", tester_id))?;
let debug_cmd_path = tester
.get("debug_cmd_path")
.and_then(|v| v.as_str())
.ok_or_else(|| anyhow::anyhow!("Invalid tester config"))?;
let debug_resp_path = tester
.get("debug_response_path")
.and_then(|v| v.as_str())
.ok_or_else(|| anyhow::anyhow!("Invalid tester config"))?;
let file_cmd = match cmd {
"frame" => "screen-json".to_string(),
"frame-normalized" => "screen-json-normalized".to_string(),
"state" => "state".to_string(),
"history" => "history".to_string(),
"wait" => "wait".to_string(),
"input" => "input".to_string(),
"message" => format!("message:{}", arg.unwrap_or("")),
"inject" => format!("inject:{}", arg.unwrap_or("")),
"keys" => format!("keys:{}", arg.unwrap_or("")),
"set_input" => format!("set_input:{}", arg.unwrap_or("")),
"scroll" => format!("scroll:{}", arg.unwrap_or("down")),
"scroll-test" => match arg {
Some(raw) => format!("scroll-test:{}", raw),
None => "scroll-test".to_string(),
},
"scroll-suite" => match arg {
Some(raw) => format!("scroll-suite:{}", raw),
None => "scroll-suite".to_string(),
},
"side-panel-latency" => match arg {
Some(raw) => format!("side-panel-latency:{}", raw),
None => "side-panel-latency".to_string(),
},
"mermaid-ui-bench" => match arg {
Some(raw) => format!("mermaid:ui-bench:{}", raw),
None => "mermaid:ui-bench".to_string(),
},
"stop" => {
if let Some(pid) = tester.get("pid").and_then(|v| v.as_u64()) {
let _ = std::process::Command::new("kill")
.arg("-TERM")
.arg(pid.to_string())
.output();
}
let mut testers = load_testers()?;
testers.retain(|t| t.get("id").and_then(|v| v.as_str()) != Some(tester_id));
save_testers(&testers)?;
return Ok("Stopped tester.".to_string());
}
_ => return Err(anyhow::anyhow!("Unknown tester command: {}", cmd)),
};
std::fs::write(debug_cmd_path, &file_cmd)?;
let timeout = std::time::Duration::from_secs(10);
let start = std::time::Instant::now();
loop {
if start.elapsed() > timeout {
return Err(anyhow::anyhow!("Timeout waiting for tester response"));
}
if let Ok(response) = std::fs::read_to_string(debug_resp_path)
&& !response.is_empty()
{
let _ = std::fs::remove_file(debug_resp_path);
return Ok(response);
}
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
}
}
#[cfg(test)]
#[path = "debug_testers_tests.rs"]
mod debug_testers_tests;