anything-anything 0.3.0

anything-anything. Literally anything. Describe a tool, app or game in a line: your coding agent builds it in any stack and its own tests prove it works.
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Runs the examples: each in a fresh empty folder with its files, against the CLI, a shell line,
//! or the server. Whatever the stack, this is what "it works" means.

use crate::spec::{Example, Run};
use crate::util;
use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::BTreeMap;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::process::{Child, Stdio};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{Duration, Instant};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExampleResult {
    pub name: String,
    pub ok: bool,
    pub secs: f64,
    /// What was wrong, with the output that shows it. Empty when it passed.
    pub problem: String,
}

pub struct Captured {
    pub code: Option<i32>,
    pub stdout: String,
    pub stderr: String,
    pub timed_out: bool,
}

/// Text as compared: one kind of newline, one kind of slash.
fn norm(s: &str) -> String {
    s.replace("\r\n", "\n").replace('\\', "/")
}

fn env_of(run: &Run, root: &Path, port: Option<u16>) -> BTreeMap<String, String> {
    let mut env: BTreeMap<String, String> = run.env.iter().map(|(k, v)| (k.clone(), expand(v, root, port))).collect();
    env.insert("ANYTHING_ROOT".into(), util::slashes(root));
    env.insert("NO_COLOR".into(), "1".into());
    if let Some(p) = port {
        env.insert("PORT".into(), p.to_string());
    }
    env
}

fn expand(s: &str, root: &Path, port: Option<u16>) -> String {
    let s = util::expand(s, root);
    match port {
        Some(p) => s.replace("{port}", &p.to_string()),
        None => s,
    }
}

/// Runs argv in dir, feeding stdin, killing it after timeout.
pub fn exec(
    argv: &[String],
    dir: &Path,
    env: &BTreeMap<String, String>,
    stdin: Option<&str>,
    timeout: Duration,
) -> Result<Captured> {
    let mut child = util::command(argv)
        .current_dir(dir)
        .envs(env)
        .env_remove("CLAUDECODE")
        .stdin(if stdin.is_some() { Stdio::piped() } else { Stdio::null() })
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .with_context(|| format!("couldn't start {}", argv[0]))?;
    if let (Some(text), Some(mut pipe)) = (stdin, child.stdin.take()) {
        let text = text.to_string();
        std::thread::spawn(move || {
            let _ = pipe.write_all(text.as_bytes());
        });
    }
    let reader = |pipe: Option<Box<dyn Read + Send>>| {
        std::thread::spawn(move || {
            let mut buf = Vec::new();
            if let Some(mut p) = pipe {
                let _ = p.read_to_end(&mut buf);
            }
            String::from_utf8_lossy(&buf).into_owned()
        })
    };
    let out = reader(child.stdout.take().map(|p| Box::new(p) as Box<dyn Read + Send>));
    let err = reader(child.stderr.take().map(|p| Box::new(p) as Box<dyn Read + Send>));
    let start = Instant::now();
    let (code, timed_out) = loop {
        if let Some(status) = child.try_wait()? {
            break (status.code(), false);
        }
        if start.elapsed() > timeout {
            util::kill_tree(&mut child);
            break (None, true);
        }
        std::thread::sleep(Duration::from_millis(10));
    };
    Ok(Captured { code, stdout: out.join().unwrap_or_default(), stderr: err.join().unwrap_or_default(), timed_out })
}

struct Sandbox(PathBuf);

impl Sandbox {
    fn new(e: &Example) -> Result<Sandbox> {
        static N: AtomicUsize = AtomicUsize::new(0);
        let dir = std::env::temp_dir().join(format!(
            "anything-example-{}-{}",
            std::process::id(),
            N.fetch_add(1, Ordering::Relaxed)
        ));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir)?;
        for (rel, text) in &e.files {
            let p = dir.join(rel);
            if let Some(parent) = p.parent() {
                std::fs::create_dir_all(parent)?;
            }
            std::fs::write(p, text)?;
        }
        Ok(Sandbox(dir))
    }
}

impl Drop for Sandbox {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.0);
    }
}

fn show(c: &Captured) -> String {
    let cut = |s: &str| util::tail(s, 30);
    format!(
        "exit: {}\nstdout:\n{}\nstderr:\n{}",
        c.code.map_or("none (killed)".into(), |c| c.to_string()),
        cut(&c.stdout),
        cut(&c.stderr)
    )
}

fn check_text(e: &Example, text: &str, problems: &mut Vec<String>) {
    let seen = norm(text);
    for want in &e.stdout {
        if !seen.contains(&norm(want)) {
            problems.push(format!("the output should contain {want:?}"));
        }
    }
    for not in &e.not_stdout {
        if seen.contains(&norm(not)) {
            problems.push(format!("the output should not contain {not:?}"));
        }
    }
}

fn check_json(e: &Example, text: &str, problems: &mut Vec<String>) {
    if e.json.is_empty() {
        return;
    }
    let value: Value = match serde_json::from_str(text.trim()) {
        Ok(v) => v,
        Err(err) => {
            problems.push(format!("the --json output isn't JSON ({err}):\n{}", util::tail(text, 15)));
            return;
        }
    };
    for (pointer, want) in &e.json {
        match value.pointer(pointer) {
            Some(got) if same(got, want) => {}
            Some(got) => problems.push(format!("json {pointer:?} is {got}, should be {want}")),
            None => problems.push(format!("json {pointer:?} is missing")),
        }
    }
}

/// Equal, with 3 == 3.0 and slashes normalized in strings.
fn same(got: &Value, want: &Value) -> bool {
    match (got, want) {
        (Value::Number(a), Value::Number(b)) => a.as_f64() == b.as_f64(),
        (Value::String(a), Value::String(b)) => norm(a) == norm(b),
        (Value::Array(a), Value::Array(b)) => a.len() == b.len() && a.iter().zip(b).all(|(x, y)| same(x, y)),
        (Value::Object(a), Value::Object(b)) => {
            a.len() == b.len() && a.iter().all(|(k, v)| b.get(k).is_some_and(|w| same(v, w)))
        }
        _ => got == want,
    }
}

fn check_files(e: &Example, dir: &Path, problems: &mut Vec<String>) {
    for (rel, want) in &e.files_after {
        match std::fs::read_to_string(dir.join(rel)) {
            Ok(text) if norm(&text).contains(&norm(want)) => {}
            Ok(text) => problems.push(format!("{rel} should contain {want:?}, it has:\n{}", util::tail(&text, 10))),
            Err(_) => problems.push(format!("{rel} should exist afterwards")),
        }
    }
}

fn timeout(e: &Example) -> Duration {
    Duration::from_secs(if e.timeout == 0 { 60 } else { e.timeout })
}

/// One run of an example (args or sh), checked. with_json: append --json and check the json keys.
fn process(root: &Path, run: &Run, e: &Example, with_json: bool) -> Result<Vec<String>> {
    let sandbox = Sandbox::new(e)?;
    let argv: Vec<String> = if e.mode() == "sh" {
        vec![expand(&e.sh, root, None)]
    } else {
        let mut a: Vec<String> = run.cmd.iter().map(|s| expand(s, root, None)).collect();
        a.extend(e.args.iter().cloned());
        if with_json {
            a.push("--json".into());
        }
        a
    };
    let c = exec(&argv, &sandbox.0, &env_of(run, root, None), e.stdin.as_deref(), timeout(e))?;
    let mut problems = Vec::new();
    if c.timed_out {
        problems.push(format!("it didn't finish in {}s", timeout(e).as_secs()));
    } else if c.code != Some(e.exit) {
        problems.push(format!("it exited {}, should be {}", c.code.map_or("?".into(), |c| c.to_string()), e.exit));
    }
    if with_json {
        check_json(e, &c.stdout, &mut problems);
    } else {
        // a shell line's stderr counts too, on its own line: a match can't straddle the two
        let text = if e.mode() == "sh" { format!("{}\n{}", c.stdout, c.stderr) } else { c.stdout.clone() };
        check_text(e, &text, &mut problems);
        if e.mode() == "sh" {
            check_json(e, &c.stdout, &mut problems);
        }
        check_files(e, &sandbox.0, &mut problems);
    }
    if !problems.is_empty() {
        problems.push(format!("{}{}", if with_json { "with --json, " } else { "" }, show(&c)));
    }
    Ok(problems)
}

/// A running server for http examples, killed when dropped.
pub struct Server {
    child: Child,
    pub port: u16,
    log: PathBuf,
}

impl Drop for Server {
    fn drop(&mut self) {
        util::kill_tree(&mut self.child);
    }
}

fn agent(timeout: Duration) -> ureq::Agent {
    ureq::Agent::config_builder().http_status_as_error(false).timeout_global(Some(timeout)).build().into()
}

pub fn start(root: &Path, run: &Run, log: &Path) -> Result<Server> {
    let port = util::free_port();
    let argv: Vec<String> = run.serve.iter().map(|s| expand(s, root, Some(port))).collect();
    if let Some(parent) = log.parent() {
        std::fs::create_dir_all(parent)?;
    }
    let file = std::fs::File::create(log)?;
    let child = util::command(&argv)
        .current_dir(root)
        .envs(env_of(run, root, Some(port)))
        .stdin(Stdio::null())
        .stdout(file.try_clone()?)
        .stderr(file)
        .spawn()
        .with_context(|| format!("couldn't start the server ({})", argv.join(" ")))?;
    let mut server = Server { child, port, log: log.to_path_buf() };
    let ready = if run.ready.is_empty() { "/" } else { run.ready.as_str() };
    let url = format!("http://127.0.0.1:{port}{ready}");
    let start = Instant::now();
    let http = ureq::Agent::config_builder()
        .http_status_as_error(false)
        .timeout_global(Some(Duration::from_secs(2)))
        .build()
        .new_agent();
    loop {
        if let Ok(r) = http.get(&url).call()
            && r.status().as_u16() < 500
        {
            return Ok(server);
        }
        if let Some(status) = server.child.try_wait()? {
            let out = std::fs::read_to_string(&server.log).unwrap_or_default();
            bail!("the server exited ({status}) before answering {url}:\n{}", util::tail(&out, 20));
        }
        if start.elapsed() > Duration::from_secs(60) {
            let out = std::fs::read_to_string(&server.log).unwrap_or_default();
            bail!("the server didn't answer {url} within 60s:\n{}", util::tail(&out, 20));
        }
        std::thread::sleep(Duration::from_millis(200));
    }
}

fn request(server: &Server, e: &Example) -> Result<Vec<String>> {
    let mut parts = e.http.split_whitespace();
    let method = parts.next().unwrap_or("GET");
    let path = parts.next().unwrap_or("/");
    let url = format!("http://127.0.0.1:{}{path}", server.port);
    let http = agent(timeout(e));
    let response = match method {
        "GET" => http.get(&url).call(),
        "HEAD" => http.head(&url).call(),
        "DELETE" => http.delete(&url).call(),
        "POST" => http.post(&url).content_type(content_type(&e.body)).send(e.body.as_bytes()),
        "PUT" => http.put(&url).content_type(content_type(&e.body)).send(e.body.as_bytes()),
        _ => http.patch(&url).content_type(content_type(&e.body)).send(e.body.as_bytes()),
    };
    let mut problems = Vec::new();
    let mut response = match response {
        Ok(r) => r,
        Err(err) => {
            problems.push(format!("{method} {path} failed: {err}"));
            return Ok(problems);
        }
    };
    let status = response.status().as_u16();
    let body = response.body_mut().read_to_string().unwrap_or_default();
    let want = e.status.unwrap_or(200);
    if status != want {
        problems.push(format!("{method} {path} answered {status}, should be {want}"));
    }
    check_text(e, &body, &mut problems);
    check_json(e, &body, &mut problems);
    if !problems.is_empty() {
        problems.push(format!("body:\n{}", util::tail(&body, 25)));
    }
    Ok(problems)
}

fn content_type(body: &str) -> &'static str {
    if body.trim_start().starts_with(['{', '[']) { "application/json" } else { "text/plain" }
}

/// These examples, in order. A server is started once for the http ones.
pub fn run_all(root: &Path, examples: &[&Example], run: &Run, log_dir: &Path) -> Vec<ExampleResult> {
    let mut server: Option<Result<Server, String>> = None;
    let mut out = Vec::new();
    for &e in examples {
        let start = Instant::now();
        let problems = match e.mode() {
            "http" => {
                let s = server.get_or_insert_with(|| start_server(root, run, log_dir));
                match s {
                    Ok(s) => request(s, e).unwrap_or_else(|err| vec![format!("{err:#}")]),
                    Err(err) => vec![err.clone()],
                }
            }
            _ => {
                let mut p = process(root, run, e, false).unwrap_or_else(|err| vec![format!("{err:#}")]);
                if p.is_empty() && e.mode() == "cli" && !e.json.is_empty() {
                    p = process(root, run, e, true).unwrap_or_else(|err| vec![format!("{err:#}")]);
                }
                p
            }
        };
        out.push(ExampleResult {
            name: e.name.clone(),
            ok: problems.is_empty(),
            secs: start.elapsed().as_secs_f64(),
            problem: problems.join("\n"),
        });
    }
    out
}

fn start_server(root: &Path, run: &Run, log_dir: &Path) -> Result<Server, String> {
    start(root, run, &log_dir.join("server.log")).map_err(|e| format!("{e:#}"))
}

#[cfg(test)]
mod tests {
    use super::*;

    fn example(sh: &str) -> Example {
        Example { name: "t".into(), sh: sh.into(), ..Example::default() }
    }

    #[test]
    fn shell_examples_check_output_files_and_json() {
        let root = std::env::temp_dir();
        let run = Run::default();
        let mut e = example(if cfg!(windows) { "echo {\"n\": 3}" } else { "echo '{\"n\": 3}'" });
        e.files.insert("in.txt".into(), "hello".into());
        e.stdout = vec!["\"n\"".into()];
        e.json.insert("/n".into(), serde_json::json!(3.0));
        assert_eq!(process(&root, &run, &e, false).unwrap(), Vec::<String>::new());
        e.stdout = vec!["missing".into()];
        e.files_after.insert("in.txt".into(), "nope".into());
        let p = process(&root, &run, &e, false).unwrap().join("\n");
        assert!(p.contains("should contain \"missing\"") && p.contains("in.txt should contain"), "{p}");
        let mut code = example("exit 3");
        code.exit = 3;
        code.not_stdout = vec!["boom".into()];
        assert!(process(&root, &run, &code, false).unwrap().is_empty());
    }

    #[test]
    fn json_compares_like_a_person_would() {
        assert!(same(&serde_json::json!(3), &serde_json::json!(3.0)));
        assert!(same(&serde_json::json!("src\\a.py"), &serde_json::json!("src/a.py")));
        assert!(!same(&serde_json::json!([1]), &serde_json::json!([1, 2])));
    }
}