Skip to content

Incremental engine for long horizon agents 🌟 Star if you like it!

26.5 KB
Raw Blame
//! End-to-end: the real `cocoindex` binary builds and drives
//! `tests/fixture_app` (a workspace member, so the nested `cargo build` shares
//! this workspace's target directory).
//!
//! Each test has its own state db, input and output directories, and runs the
//! CLI from a temporary working directory (no `.env` above it). A few calls go
//! through the build (`FIXTURE`); the rest run the built binary (`exe_target`)
//! to skip cargo's up-to-date check.

use std::path::{Path, PathBuf};
use std::process::{Command, Output};
use std::sync::OnceLock;

const FIXTURE: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/fixture_app");

/// The fixture binary, built once per test run through the CLI itself.
fn fixture_exe() -> &'static Path {
    static EXE: OnceLock<PathBuf> = OnceLock::new();
    EXE.get_or_init(|| {
        let sb = Sandbox::new(&[]);
        let output = sb.cli(&["ls", FIXTURE]);
        assert!(
            output.status.success(),
            "building the fixture through the CLI"
        );
        let exe = Path::new(env!("CARGO_BIN_EXE_cocoindex")).with_file_name(format!(
            "cocoindex_cli_fixture_app{}",
            std::env::consts::EXE_SUFFIX
        ));
        assert!(
            exe.is_file(),
            "the fixture was built next to the CLI: {}",
            exe.display()
        );
        exe
    })
}

/// `<built fixture binary>:<app>`.
fn exe_target(app: &str) -> String {
    format!("{}:{app}", fixture_exe().display())
}

struct Sandbox {
    dir: tempfile::TempDir,
}

impl Sandbox {
    fn new(files: &[(&str, &str)]) -> Self {
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(dir.path().join("src")).unwrap();
        for (name, content) in files {
            std::fs::write(dir.path().join("src").join(name), content).unwrap();
        }
        Self { dir }
    }

    fn path(&self, rel: &str) -> PathBuf {
        self.dir.path().join(rel)
    }

    fn db(&self) -> String {
        self.path("db").display().to_string()
    }

    fn cli(&self, args: &[&str]) -> Output {
        self.cli_with_input(args, "")
    }

    /// Run the CLI with `input` on its stdin (answers to its prompts).
    fn cli_with_input(&self, args: &[&str], input: &str) -> Output {
        use std::io::Write as _;
        let started = std::time::Instant::now();
        let mut child = Command::new(env!("CARGO_BIN_EXE_cocoindex"))
            .args(args)
            .current_dir(self.dir.path())
            .env("COCOINDEX_DB", self.path("db"))
            .env("FIXTURE_SRC", self.path("src"))
            .env("FIXTURE_OUT", self.path("out"))
            .env_remove("RUST_LOG")
            .stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::piped())
            .spawn()
            .expect("run the cocoindex binary");
        child
            .stdin
            .take()
            .expect("stdin is piped")
            .write_all(input.as_bytes())
            .expect("write the CLI's stdin");
        let output = child
            .wait_with_output()
            .expect("wait for the cocoindex binary");
        eprintln!(
            "[{:.1}s] $ cocoindex {}\n--- stdout\n{}--- stderr\n{}",
            started.elapsed().as_secs_f64(),
            args.join(" "),
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );
        output
    }

    fn out_files(&self) -> Vec<String> {
        let mut names: Vec<String> = std::fs::read_dir(self.path("out"))
            .map(|d| {
                d.map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
                    .collect()
            })
            .unwrap_or_default();
        names.sort();
        names
    }
}

fn stdout(output: &Output) -> String {
    String::from_utf8_lossy(&output.stdout).into_owned()
}

fn stderr(output: &Output) -> String {
    String::from_utf8_lossy(&output.stderr).into_owned()
}

fn target(app: &str) -> String {
    format!("{FIXTURE}:{app}")
}

#[track_caller]
fn assert_ok(output: &Output) {
    assert!(output.status.success(), "exit {:?}", output.status.code());
}

#[test]
fn update_preview_ls_show_drop() {
    let sb = Sandbox::new(&[("a.txt", "alpha"), ("b.txt", "beta")]);

    let ls = sb.cli(&["ls", FIXTURE]);
    assert_ok(&ls);
    let text = stdout(&ls);
    assert!(text.contains("  FixtureApp [+]\n"), "{text}");
    assert!(text.contains("  never_run [+]\n"));
    assert!(text.contains("[+]: Apps present in the binary, but not yet run"));

    // First update writes both files; the app's own stdout passes through.
    let update = sb.cli(&["update", &target("FixtureApp")]); // through the build
    assert_ok(&update);
    let text = stdout(&update);
    assert!(text.contains("Running app 'FixtureApp'"), "{text}");
    assert!(text.contains("fixture: processed 2"), "{text}");
    assert!(text.contains("βœ“ Done in "), "{text}");
    assert!(
        text.lines()
            .any(|l| l.trim_start().starts_with("βœ“ mount:upper") && l.ends_with("  2 added")),
        "{text}"
    );
    assert!(
        text.contains("[Stats] processed 3, wrote 3, skipped 0, deleted 0"),
        "{text}"
    );
    assert!(
        stderr(&update).contains("fixture: shutdown"),
        "on_shutdown ran"
    );
    assert!(
        !stderr(&update).contains("INFO"),
        "engine logs default to warnings"
    );
    assert_eq!(sb.out_files(), ["a.txt", "b.txt"]);
    assert_eq!(
        std::fs::read_to_string(sb.path("out/a.txt")).unwrap(),
        "ALPHA"
    );

    // Unchanged inputs are skipped.
    let again = sb.cli(&["update", &exe_target("FixtureApp")]);
    assert_ok(&again);
    assert!(stdout(&again).contains("2 unchanged"), "{}", stdout(&again));

    // Quiet mode prints only the app's own output.
    let quiet = sb.cli(&["update", "-q", &exe_target("FixtureApp")]);
    assert_ok(&quiet);
    assert_eq!(stdout(&quiet), "fixture: processed 2\n");

    // Preview plans the change without applying it.
    std::fs::remove_file(sb.path("src/b.txt")).unwrap();
    let preview = sb.cli(&["update", "--preview", &exe_target("FixtureApp")]);
    assert_ok(&preview);
    let text = stdout(&preview);
    assert!(text.contains("Preview: planned target actions\n"), "{text}");
    assert!(text.contains("b.txt"), "{text}");
    assert_eq!(
        sb.out_files(),
        ["a.txt", "b.txt"],
        "preview applies nothing"
    );

    let ls = sb.cli(&["ls", &exe_target("FixtureApp")]);
    assert!(stdout(&ls).contains("  FixtureApp\n"), "{}", stdout(&ls));

    let ls_db = sb.cli(&["ls", "--db", &sb.db()]);
    assert_ok(&ls_db);
    // Inside the working directory, the path shows relative, as in Python.
    assert_eq!(stdout(&ls_db), "./db:\n  FixtureApp\n");

    // show, straight from the db.
    let show = sb.cli(&["show", "--db", &sb.db(), "--app-name", "FixtureApp"]);
    assert_ok(&show);
    assert_eq!(
        stdout(&show),
        "Stable paths:\n  /\n  /\"upper\"\n  /\"upper\"/\"a.txt\"\n  /\"upper\"/\"b.txt\"\n"
    );
    let tree = sb.cli(&[
        "show",
        "--db",
        &sb.db(),
        "--app-name",
        "FixtureApp",
        "--tree",
    ]);
    assert!(
        stdout(&tree).contains("- upper\n  - a.txt [component]"),
        "{}",
        stdout(&tree)
    );
    let long = sb.cli(&["show", "--db", &sb.db(), "--app-name", "FixtureApp", "-l"]);
    assert!(
        stdout(&long).contains("type:component version:"),
        "{}",
        stdout(&long)
    );
    assert!(stdout(&long).contains("processor:app:FixtureApp"));
    // The whole block of each path, in the Python CLI's format. Target states
    // come in stored (fingerprint) order, which depends on the output path.
    // A readable path escapes its string keys (the `\` of a Windows path).
    let out_dir = sb
        .path("out")
        .display()
        .to_string()
        .escape_default()
        .to_string();
    let either_order = |text: String, render: &dyn Fn(&str, &str) -> String| {
        assert!(
            text == render("a.txt", "b.txt") || text == render("b.txt", "a.txt"),
            "{text}"
        );
    };
    either_order(stdout(&long), &|first, second| {
        format!(
            "Stable paths:\n  /\n    type:component version:1 processor:app:FixtureApp\n    \
             has_memoization:false target_state_count:2\n    Target states:\n      \
             - path:/@cocoindex/localfs/file/[\"{out_dir}\",\"{first}\"]\n        \
             states:1:Existing schema_version:0 generation:None\n      \
             - path:/@cocoindex/localfs/file/[\"{out_dir}\",\"{second}\"]\n        \
             states:1:Existing schema_version:0 generation:None\n\n  \
             /\"upper\"\n    type:directory version:0 processor:-\n    \
             has_memoization:false target_state_count:0\n\n  \
             /\"upper\"/\"a.txt\"\n    type:component version:1 processor:mount:upper\n    \
             has_memoization:true target_state_count:0\n\n  \
             /\"upper\"/\"b.txt\"\n    type:component version:1 processor:mount:upper\n    \
             has_memoization:true target_state_count:0\n\n"
        )
    });
    let one = sb.cli(&[
        "show",
        "--db",
        &sb.db(),
        "--app-name",
        "FixtureApp",
        "-p",
        "/upper/a.txt",
    ]);
    assert_ok(&one);
    let states = sb.cli(&[
        "show",
        "--db",
        &sb.db(),
        "--app-name",
        "FixtureApp",
        "--target-states",
    ]);
    either_order(stdout(&states), &|first, second| {
        format!(
            "Target states:\n  /@cocoindex/localfs/file/[\"{out_dir}\",\"{first}\"]\n    \
             owner:/\n  /@cocoindex/localfs/file/[\"{out_dir}\",\"{second}\"]\n    owner:/\n"
        )
    });
    let states_tree = sb.cli(&[
        "show",
        "--db",
        &sb.db(),
        "--app-name",
        "FixtureApp",
        "--target-states",
        "--tree",
    ]);
    either_order(stdout(&states_tree), &|first, second| {
        format!(
            "Target states:\n- @cocoindex/localfs/file\n  - [\"{out_dir}\",\"{first}\"] owner:/\n  \
             - [\"{out_dir}\",\"{second}\"] owner:/\n"
        )
    });

    // show through the binary (resolves the db path and app over the protocol).
    let show_target = sb.cli(&["show", &target("FixtureApp")]);
    assert_ok(&show_target);
    assert_eq!(stdout(&show_target), stdout(&show));

    // drop
    let never_run = sb.cli(&["drop", "-f", &exe_target("never_run")]);
    assert_ok(&never_run);
    assert!(
        stdout(&never_run).contains("App 'never_run' has no persisted state. Nothing to drop.")
    );
    let drop = sb.cli(&["drop", "-f", &exe_target("FixtureApp")]);
    assert_ok(&drop);
    assert!(
        stdout(&drop).contains("Dropped app 'FixtureApp'."),
        "{}",
        stdout(&drop)
    );
    assert_eq!(
        sb.out_files(),
        Vec::<String>::new(),
        "drop reverted the files"
    );
    let ls_db = sb.cli(&["ls", "--db", &sb.db()]);
    assert_eq!(stdout(&ls_db), "No persisted apps found in the database.\n");
}

#[test]
fn reset_db_only_and_prompts() {
    let sb = Sandbox::new(&[("a.txt", "alpha")]);
    assert_ok(&sb.cli(&["update", "-q", &exe_target("FixtureApp")]));

    // A declined prompt (stdin is empty) aborts with success.
    let declined = sb.cli(&["update", "--reset", &exe_target("FixtureApp")]);
    assert_ok(&declined);
    assert!(
        stdout(&declined).contains("Type 'yes' to reset app 'FixtureApp' (drop existing state)")
    );
    assert!(stdout(&declined).contains("Update operation aborted."));

    let reset = sb.cli(&["update", "-f", "--reset", &exe_target("FixtureApp")]);
    assert_ok(&reset);
    assert!(
        stdout(&reset).contains("1 added"),
        "state was dropped first: {}",
        stdout(&reset)
    );

    let declined = sb.cli(&["drop", "--db-only", &exe_target("FixtureApp")]);
    assert_ok(&declined);
    assert!(stdout(&declined).contains("its target states are NOT reverted"));
    assert!(stdout(&declined).contains("Drop operation aborted."));

    // Answering the prompt: the lifespan runs once (one shutdown line).
    let reset = sb.cli_with_input(&["update", "--reset", &exe_target("FixtureApp")], "yes\n");
    assert_ok(&reset);
    assert!(stdout(&reset).contains("1 added"), "{}", stdout(&reset));
    assert_eq!(stderr(&reset).matches("fixture: shutdown").count(), 1);

    let cleared = sb.cli_with_input(&["drop", "--db-only", &exe_target("FixtureApp")], "YES\n");
    assert_ok(&cleared);
    assert!(stdout(&cleared).contains("Cleared the state of app 'FixtureApp'."));
    assert_eq!(stderr(&cleared).matches("fixture: shutdown").count(), 1);
    assert_ok(&sb.cli(&["update", "-q", &exe_target("FixtureApp")]));

    let cleared = sb.cli(&["drop", "-f", "--db-only", &exe_target("FixtureApp")]);
    assert_ok(&cleared);
    assert!(stdout(&cleared).contains("Cleared the state of app 'FixtureApp'."));
    assert_eq!(sb.out_files(), ["a.txt"], "--db-only leaves targets alone");
    let ls = sb.cli(&["ls", &exe_target("FixtureApp")]);
    assert!(
        stdout(&ls).contains("  FixtureApp [+]\n"),
        "{}",
        stdout(&ls)
    );
}

#[test]
fn executable_target_skips_the_build() {
    let sb = Sandbox::new(&[("a.txt", "alpha")]);
    let update = sb.cli(&["update", &exe_target("FixtureApp")]);
    assert_ok(&update);
    assert!(!stderr(&update).contains("Compiling"), "no build step");
    assert!(!stderr(&update).contains("Finished"), "cargo did not run");
    assert_eq!(sb.out_files(), ["a.txt"]);
}

#[test]
fn usage_errors() {
    let sb = Sandbox::new(&[]);
    let both = sb.cli(&["update", "--preview", "--reset", FIXTURE]);
    assert_eq!(both.status.code(), Some(2));
    assert!(stderr(&both).contains("Error: --preview and --reset cannot be used together."));

    let select_reset = sb.cli(&["update", "--select", "/1", "--reset", FIXTURE]);
    assert_eq!(select_reset.status.code(), Some(2));
    assert!(stderr(&select_reset).contains("Error: --select and --reset cannot be used together."));
    let bad_select = sb.cli(&["update", "--select", "process", FIXTURE]);
    assert_eq!(bad_select.status.code(), Some(2));
    assert!(stderr(&bad_select).contains("cannot parse component path"));

    let missing = sb.cli(&["update", "/definitely/not/here"]);
    assert_eq!(missing.status.code(), Some(2));
    assert!(stderr(&missing).contains("does not exist"));

    let no_db = sb.cli(&["ls", "--db", "/definitely/not/here"]);
    assert_eq!(no_db.status.code(), Some(1));
    assert!(stderr(&no_db).contains("Database path does not exist"));

    // Checks after parsing, and an app that is not found, are failures (exit
    // 1), as the Python CLI's `click.ClickException`s are.
    let recursive = sb.cli(&["show", "-r", "--db", "x", "--app-name", "y"]);
    assert_eq!(recursive.status.code(), Some(1));
    assert!(stderr(&recursive).contains("require a stable_path"));

    let half = sb.cli(&["show", "--db", "x"]);
    assert_eq!(half.status.code(), Some(1));
    assert!(stderr(&half).contains("Both --db and --app-name are required"));

    // Two apps and no name.
    let ambiguous = sb.cli(&["update", FIXTURE]);
    assert_eq!(ambiguous.status.code(), Some(1));
    assert!(
        stderr(&ambiguous).contains("FixtureApp, never_run"),
        "{}",
        stderr(&ambiguous)
    );

    let unknown = sb.cli(&["update", &exe_target("Nope")]);
    assert_eq!(unknown.status.code(), Some(1));
    assert!(stderr(&unknown).contains("Nope"));
}

#[cfg(unix)]
#[test]
fn a_binary_without_the_protocol_is_refused() {
    let sb = Sandbox::new(&[]);
    let output = sb.cli(&["ls", "/bin/echo"]);
    assert_eq!(output.status.code(), Some(2));
    assert!(stderr(&output).contains("did not answer the cocoindex protocol"));
}

#[test]
fn env_file_is_loaded() {
    let sb = Sandbox::new(&[("a.txt", "alpha")]);
    let env_file = sb.path("custom.env");
    // Single quotes keep the value literal: a Windows path has backslashes,
    // which an unquoted value reads as escapes.
    std::fs::write(
        &env_file,
        format!("FIXTURE_OUT='{}'\n", sb.path("elsewhere").display()),
    )
    .unwrap();
    // FIXTURE_OUT is already set by the sandbox: .env values never override.
    let output = sb.cli(&[
        "-e",
        env_file.to_str().unwrap(),
        "update",
        "-q",
        &exe_target("FixtureApp"),
    ]);
    assert_ok(&output);
    assert!(stderr(&output).contains("Loaded environment variables from:"));
    assert_eq!(sb.out_files(), ["a.txt"]);
    assert!(!sb.path("elsewhere").exists());
}

/// Ctrl-C while the lifespan is still running cancels cleanly: the CLI prints
/// `Stopping...`, the child reports a cancel, and both exit 0 right away.
#[cfg(unix)]
#[test]
fn ctrl_c_during_the_lifespan_is_a_cancel() {
    for verb in [&["update"][..], &["drop", "-f"][..]] {
        interrupt_slow_lifespan(verb);
    }
}

#[cfg(unix)]
fn interrupt_slow_lifespan(verb: &[&str]) {
    use std::io::{BufRead, BufReader};
    use std::time::{Duration, Instant};

    let sb = Sandbox::new(&[("a.txt", "alpha")]);
    let target = exe_target("FixtureApp");
    let mut args = verb.to_vec();
    args.push(&target);
    let mut child = Command::new(env!("CARGO_BIN_EXE_cocoindex"))
        .args(&args)
        .current_dir(sb.dir.path())
        .env("COCOINDEX_DB", sb.path("db"))
        .env("FIXTURE_SRC", sb.path("src"))
        .env("FIXTURE_OUT", sb.path("out"))
        .env("FIXTURE_SLOW_LIFESPAN", "1")
        .stdin(std::process::Stdio::null())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .expect("run the cocoindex binary");
    let mut stderr = BufReader::new(child.stderr.take().unwrap());
    let mut seen = String::new();
    let mut line = String::new();
    while stderr.read_line(&mut line).unwrap() > 0 {
        seen.push_str(&line);
        if line.contains("slow lifespan started") {
            break;
        }
        line.clear();
    }
    assert!(seen.contains("slow lifespan started"), "{verb:?}: {seen}");

    let started = Instant::now();
    let status = Command::new("kill")
        .args(["-INT", &child.id().to_string()])
        .status()
        .unwrap();
    assert!(status.success());
    let mut rest = String::new();
    std::io::Read::read_to_string(&mut stderr, &mut rest).unwrap();
    let status = child.wait().unwrap();
    eprintln!("{verb:?}\n{seen}{rest}");
    assert!(
        started.elapsed() < Duration::from_secs(20),
        "{verb:?}: did not wait for the lifespan"
    );
    assert_eq!(status.code(), Some(0), "{verb:?}: a cancel is a clean exit");
    assert!(rest.contains("Stopping..."), "{verb:?}: {rest}");
    assert!(
        !rest.contains("without reporting a result"),
        "{verb:?}: {rest}"
    );
    assert_eq!(
        sb.out_files(),
        Vec::<String>::new(),
        "{verb:?}: nothing ran"
    );
}

/// A failure inside a stats group reaches the final status, though the group's
/// rows stay out of the report, and its engine log comes through on stderr.
#[test]
fn a_failure_in_a_stats_group_fails_the_final_status() {
    let sb = Sandbox::new(&[("a.txt", "alpha")]);
    let output = Command::new(env!("CARGO_BIN_EXE_cocoindex"))
        .args(["update", &exe_target("FixtureApp")])
        .current_dir(sb.dir.path())
        .env("COCOINDEX_DB", sb.path("db"))
        .env("FIXTURE_SRC", sb.path("src"))
        .env("FIXTURE_OUT", sb.path("out"))
        .env("FIXTURE_FAIL_IN_GROUP", "1")
        .env_remove("RUST_LOG")
        .output()
        .unwrap();
    assert_ok(&output);
    let text = stdout(&output);
    assert!(text.contains("βœ— Done in"), "{text}");
    assert!(text.contains("1 failed"), "{text}");
    assert!(stderr(&output).contains("fixture: boom"));
}

/// The binary's own `main` still runs, and its error (a `Debug`-only type, as
/// a plain `main` allows) becomes exit status 1.
#[test]
fn the_binarys_own_main_runs() {
    let ok = Command::new(fixture_exe()).output().unwrap();
    assert!(ok.status.success());
    assert_eq!(String::from_utf8_lossy(&ok.stdout), "fixture: own main\n");
    let failed = Command::new(fixture_exe())
        .env("FIXTURE_MAIN_FAILS", "1")
        .output()
        .unwrap();
    assert_eq!(failed.status.code(), Some(1));
    assert!(String::from_utf8_lossy(&failed.stderr).contains("Error: FixtureError"));
}

/// `init` writes a project that builds and runs: `update` turns its sample
/// markdown into `out/hello.txt`.
#[test]
fn init_scaffold_builds_and_runs() {
    // Inside the target dir: the repository's cargo config applies, and the
    // workspace lockfile and target dir are reused, so only the scaffold
    // itself compiles.
    let target_dir = Path::new(env!("CARGO_BIN_EXE_cocoindex"))
        .parent()
        .and_then(Path::parent)
        .expect("the CLI is built under <target>/<profile>/");
    let tmp = tempfile::tempdir_in(env!("CARGO_TARGET_TMPDIR")).unwrap();
    let project = tmp.path().join("scaffold_app");
    let sdk = concat!(env!("CARGO_MANIFEST_DIR"), "/../sdk/cocoindex");
    let init = Command::new(env!("CARGO_BIN_EXE_cocoindex"))
        .args([
            "init",
            "--dir",
            project.to_str().unwrap(),
            "--sdk-path",
            sdk,
        ])
        .current_dir(tmp.path())
        .output()
        .unwrap();
    assert!(
        init.status.success(),
        "{}",
        String::from_utf8_lossy(&init.stderr)
    );
    std::fs::copy(
        concat!(env!("CARGO_MANIFEST_DIR"), "/../../Cargo.lock"),
        project.join("Cargo.lock"),
    )
    .unwrap();

    let update = Command::new(env!("CARGO_BIN_EXE_cocoindex"))
        .args(["update", "-q", project.to_str().unwrap()])
        .current_dir(tmp.path())
        .env("CARGO_TARGET_DIR", target_dir)
        .env_remove("COCOINDEX_DB")
        .output()
        .unwrap();
    eprintln!("{}", String::from_utf8_lossy(&update.stderr));
    assert!(update.status.success());
    let written = std::fs::read_to_string(project.join("out/hello.txt")).unwrap();
    assert!(written.starts_with("# HELLO"), "{written}");
    assert!(
        project.join(".cocoindex_db").is_dir(),
        "the lifespan's db path"
    );
}

/// `auth sign-in --help` and `auth sign-out --help` list the providers.
/// `auth sign-out google-drive` without a stored sign-in says so;
/// `auth sign-in google-drive` with a missing client file is a usage error,
/// and without one this build (which has no built-in OAuth client) says how
/// to sign in. No browser opens: both fail before the consent.
#[test]
fn google_drive_sign_in_and_sign_out() {
    let sb = Sandbox::new(&[]);
    let token = sb.path("google_drive_token.json");
    let token_arg = token.display().to_string();

    for action in ["sign-in", "sign-out"] {
        let help = sb.cli(&["auth", action, "--help"]);
        assert_ok(&help);
        assert!(stdout(&help).contains("google-drive"), "{}", stdout(&help));
    }

    let out = sb.cli(&[
        "auth",
        "sign-out",
        "google-drive",
        "--token-path",
        &token_arg,
    ]);
    assert_ok(&out);
    assert!(
        stdout(&out).contains("No sign-in was stored."),
        "{}",
        stdout(&out)
    );

    let missing = sb.cli(&[
        "auth",
        "sign-in",
        "google-drive",
        "/definitely/not/here.json",
        "--token-path",
        &token_arg,
    ]);
    assert_eq!(missing.status.code(), Some(2));
    assert!(
        stderr(&missing).contains("does not exist"),
        "{}",
        stderr(&missing)
    );

    let no_builtin = sb.cli(&[
        "auth",
        "sign-in",
        "google-drive",
        "--token-path",
        &token_arg,
    ]);
    assert_eq!(no_builtin.status.code(), Some(1));
    assert!(
        stderr(&no_builtin).contains("no built-in OAuth client"),
        "{}",
        stderr(&no_builtin)
    );
    assert!(!token.exists());

    // A stored sign-in is deleted even though Google cannot be reached at the
    // address in the file: the sign-out never leaves tokens behind.
    std::fs::write(
        &token,
        r#"{"version":1,"client_id":"c","client_secret":"s","refresh_token":"r","access_token":"a","expires_at":0.0,"email":"[email protected]"}"#,
    )
    .unwrap();
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(&token, std::fs::Permissions::from_mode(0o600)).unwrap();
    }
    let out = sb.cli(&[
        "auth",
        "sign-out",
        "google-drive",
        "--token-path",
        &token_arg,
    ]);
    assert_ok(&out);
    assert!(stdout(&out).contains("Signed out."), "{}", stdout(&out));
    assert!(!token.exists());
}

/// `--full-reprocess` bypasses the memo, `--fingerprints` shows stored
/// target-state paths raw, and `--preview` refuses `--live`.
#[test]
fn full_reprocess_fingerprints_and_preview_live() {
    let sb = Sandbox::new(&[("a.txt", "alpha"), ("b.txt", "beta")]);
    assert_ok(&sb.cli(&["update", &exe_target("FixtureApp")]));
    let again = sb.cli(&["update", &exe_target("FixtureApp")]);
    assert!(stdout(&again).contains("2 unchanged"), "{}", stdout(&again));

    let full = sb.cli(&["update", "--full-reprocess", &exe_target("FixtureApp")]);
    assert_ok(&full);
    assert!(
        !stdout(&full).contains("unchanged"),
        "a full reprocess skips nothing: {}",
        stdout(&full)
    );
    assert_eq!(sb.out_files(), ["a.txt", "b.txt"]);

    let raw = sb.cli(&[
        "show",
        "--db",
        &sb.db(),
        "--app-name",
        "FixtureApp",
        "--target-states",
        "--fingerprints",
    ]);
    assert_ok(&raw);
    let text = stdout(&raw);
    assert!(text.starts_with("Target states:\n"), "{text}");
    assert!(
        !text.contains("localfs/file/["),
        "raw paths, not readable keys: {text}"
    );
    assert_eq!(text.matches("owner:/").count(), 2, "{text}");

    let both = sb.cli(&["update", "--preview", "-L", &exe_target("FixtureApp")]);
    assert_eq!(both.status.code(), Some(2));
    assert!(stderr(&both).contains("--preview and --live cannot be used together."));
}

/// A crate that does not compile fails with exit 1 (as the Python CLI does
/// for a module that does not import), with cargo's diagnostics on stderr.
#[test]
fn a_crate_that_does_not_build_fails() {
    let sb = Sandbox::new(&[("main.rs", "fn main() { let x: u32 = \"not a number\"; }\n")]);
    std::fs::write(
        sb.path("Cargo.toml"),
        "[package]\nname = \"broken_crate\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[workspace]\n",
    )
    .unwrap();
    let crate_dir = sb.path("").display().to_string();
    let output = sb.cli(&["update", &crate_dir]);
    assert_eq!(output.status.code(), Some(1));
    let err = stderr(&output);
    assert!(err.contains("mismatched types"), "{err}");
    assert!(err.contains("failed"), "{err}");
}