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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
use std::process::Stdio;
use tempfile::TempDir;
use tokio::process::Command;
/// Build the binary path for the witryna executable.
fn witryna_bin() -> std::path::PathBuf {
let mut path = std::path::PathBuf::from(env!("CARGO_BIN_EXE_witryna"));
if !path.exists() {
path = std::path::PathBuf::from("target/debug/witryna");
}
path
}
/// Write a minimal witryna.toml config for status tests.
async fn write_status_config(
dir: &std::path::Path,
sites: &[&str],
log_dir: &std::path::Path,
) -> std::path::PathBuf {
let base_dir = dir.join("data");
tokio::fs::create_dir_all(&base_dir).await.unwrap();
let mut sites_toml = String::new();
for name in sites {
sites_toml.push_str(&format!(
r#"
[[sites]]
name = "{name}"
repo_url = "https://example.com/{name}.git"
branch = "main"
webhook_token = "unused"
"#
));
}
let config_path = dir.join("witryna.toml");
let config = format!(
r#"listen_address = "127.0.0.1:0"
container_runtime = "podman"
base_dir = "{base_dir}"
log_dir = "{log_dir}"
log_level = "info"
{sites_toml}"#,
base_dir = base_dir.display(),
log_dir = log_dir.display(),
);
tokio::fs::write(&config_path, config).await.unwrap();
config_path
}
/// Write a fake build log with a valid header.
async fn write_test_build_log(
log_dir: &std::path::Path,
site_name: &str,
timestamp: &str,
status: &str,
commit: &str,
image: &str,
duration: &str,
) {
let site_log_dir = log_dir.join(site_name);
tokio::fs::create_dir_all(&site_log_dir).await.unwrap();
let content = format!(
"=== BUILD LOG ===\n\
Site: {site_name}\n\
Timestamp: {timestamp}\n\
Git Commit: {commit}\n\
Image: {image}\n\
Duration: {duration}\n\
Status: {status}\n\
\n\
=== STDOUT ===\n\
build output\n\
\n\
=== STDERR ===\n"
);
let log_file = site_log_dir.join(format!("{timestamp}.log"));
tokio::fs::write(&log_file, content).await.unwrap();
}
/// Write a fake hook log with a valid header.
async fn write_test_hook_log(
log_dir: &std::path::Path,
site_name: &str,
timestamp: &str,
status: &str,
) {
let site_log_dir = log_dir.join(site_name);
tokio::fs::create_dir_all(&site_log_dir).await.unwrap();
let content = format!(
"=== HOOK LOG ===\n\
Site: {site_name}\n\
Timestamp: {timestamp}\n\
Command: hook-cmd\n\
Duration: 1s\n\
Status: {status}\n\
\n\
=== STDOUT ===\n\
\n\
=== STDERR ===\n"
);
let log_file = site_log_dir.join(format!("{timestamp}-hook.log"));
tokio::fs::write(&log_file, content).await.unwrap();
}
// ---------------------------------------------------------------------------
// Tier 1: no container runtime / git needed
// ---------------------------------------------------------------------------
#[tokio::test]
async fn cli_status_no_builds() {
let tempdir = TempDir::new().unwrap();
let log_dir = tempdir.path().join("logs");
tokio::fs::create_dir_all(&log_dir).await.unwrap();
let config_path = write_status_config(tempdir.path(), &["empty-site"], &log_dir).await;
let output = Command::new(witryna_bin())
.args(["--config", config_path.to_str().unwrap(), "status"])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.await
.unwrap();
assert!(output.status.success(), "should exit 0");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(stdout.contains("SITE"), "should have table header");
assert!(
stdout.contains("(no builds)"),
"should show (no builds), got: {stdout}"
);
}
#[tokio::test]
async fn cli_status_single_build() {
let tempdir = TempDir::new().unwrap();
let log_dir = tempdir.path().join("logs");
tokio::fs::create_dir_all(&log_dir).await.unwrap();
write_test_build_log(
&log_dir,
"my-site",
"20260126-143000-123456",
"success",
"abc123d",
"node:20-alpine",
"45s",
)
.await;
let config_path = write_status_config(tempdir.path(), &["my-site"], &log_dir).await;
let output = Command::new(witryna_bin())
.args(["--config", config_path.to_str().unwrap(), "status"])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.await
.unwrap();
assert!(output.status.success(), "should exit 0");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(stdout.contains("my-site"), "should show site name");
assert!(stdout.contains("success"), "should show status");
assert!(stdout.contains("abc123d"), "should show commit");
assert!(stdout.contains("45s"), "should show duration");
}
#[tokio::test]
async fn cli_status_json_output() {
let tempdir = TempDir::new().unwrap();
let log_dir = tempdir.path().join("logs");
tokio::fs::create_dir_all(&log_dir).await.unwrap();
write_test_build_log(
&log_dir,
"json-site",
"20260126-143000-123456",
"success",
"abc123d",
"node:20-alpine",
"45s",
)
.await;
let config_path = write_status_config(tempdir.path(), &["json-site"], &log_dir).await;
let output = Command::new(witryna_bin())
.args([
"--config",
config_path.to_str().unwrap(),
"status",
"--json",
])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.await
.unwrap();
assert!(output.status.success(), "should exit 0");
let stdout = String::from_utf8_lossy(&output.stdout);
let parsed: serde_json::Value = serde_json::from_str(&stdout).unwrap();
let arr = parsed.as_array().unwrap();
assert_eq!(arr.len(), 1);
assert_eq!(arr[0]["site_name"], "json-site");
assert_eq!(arr[0]["status"], "success");
assert_eq!(arr[0]["git_commit"], "abc123d");
assert_eq!(arr[0]["duration"], "45s");
}
#[tokio::test]
async fn cli_status_site_filter() {
let tempdir = TempDir::new().unwrap();
let log_dir = tempdir.path().join("logs");
tokio::fs::create_dir_all(&log_dir).await.unwrap();
// Create logs for two sites
write_test_build_log(
&log_dir,
"site-a",
"20260126-143000-000000",
"success",
"aaa1111",
"alpine:latest",
"10s",
)
.await;
write_test_build_log(
&log_dir,
"site-b",
"20260126-150000-000000",
"success",
"bbb2222",
"alpine:latest",
"20s",
)
.await;
let config_path = write_status_config(tempdir.path(), &["site-a", "site-b"], &log_dir).await;
let output = Command::new(witryna_bin())
.args([
"--config",
config_path.to_str().unwrap(),
"status",
"--site",
"site-a",
])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.await
.unwrap();
assert!(output.status.success(), "should exit 0");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(stdout.contains("site-a"), "should show filtered site");
assert!(
!stdout.contains("site-b"),
"should NOT show other site, got: {stdout}"
);
}
#[tokio::test]
async fn cli_status_hook_failed() {
let tempdir = TempDir::new().unwrap();
let log_dir = tempdir.path().join("logs");
tokio::fs::create_dir_all(&log_dir).await.unwrap();
// Build succeeded, but hook failed
write_test_build_log(
&log_dir,
"hook-site",
"20260126-143000-123456",
"success",
"abc123d",
"alpine:latest",
"12s",
)
.await;
write_test_hook_log(
&log_dir,
"hook-site",
"20260126-143000-123456",
"failed (exit code 1)",
)
.await;
let config_path = write_status_config(tempdir.path(), &["hook-site"], &log_dir).await;
let output = Command::new(witryna_bin())
.args(["--config", config_path.to_str().unwrap(), "status"])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.await
.unwrap();
assert!(output.status.success(), "should exit 0");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.contains("hook failed"),
"should show 'hook failed', got: {stdout}"
);
}
|