Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust
149 KB
Raw Blame
//! Packaging asset regression tests.

#[cfg(unix)]
use std::io::BufRead as _;
use std::path::{Path, PathBuf};
#[cfg(any(unix, windows))]
use std::process::Command;
#[cfg(unix)]
use std::process::Stdio;

#[cfg(unix)]
fn sha256_file(path: &Path) -> String {
    use sha2::{Digest as _, Sha256};

    format!("{:x}", Sha256::digest(std::fs::read(path).unwrap()))
}

fn repo_root() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .and_then(Path::parent)
        .expect("crate should live under crates/ai-memory-cli")
        .to_path_buf()
}

fn read_repo(path: &str) -> String {
    let path = repo_root().join(path);
    std::fs::read_to_string(&path)
        .unwrap_or_else(|e| panic!("failed to read {}: {e}", path.display()))
        .replace("\r\n", "\n")
}

// Unix-only alongside run_wrapper_on_fake_macos below — these helpers'
// former Git Bash arms existed to run the wrapper test on Windows, which
// the fake-uname executable-bit limitation rules out anyway.
#[cfg(unix)]
fn shell_script_command(script: &Path) -> Command {
    Command::new(script)
}

#[cfg(unix)]
// Preserve the script path as `$0` without directly execing a just-written file,
// which can transiently return ETXTBSY under parallel Linux test load.
fn freshly_written_shell_script_command(script: &Path) -> Command {
    let mut command = Command::new("bash");
    command.arg(script);
    command
}

#[cfg(unix)]
fn shell_path(path: &Path) -> String {
    path.display().to_string()
}

// Unix-only: the macOS simulation works by shadowing `uname` with a fake
// script earlier in PATH, which requires setting its executable bit. NTFS
// has no mode bits, so on a Windows host MSYS bash skips the non-executable
// fake and the real `uname.exe` reports MSYS_NT-* — the Darwin arm under
// test can never fire there.
#[cfg(unix)]
fn run_wrapper_on_fake_macos(args: &[&str]) -> String {
    let tmp = tempfile::tempdir().unwrap();
    let docker_args = tmp.path().join("docker-args.txt");
    let docker = tmp.path().join("docker");
    let uname = tmp.path().join("uname");
    std::fs::write(
        &docker,
        format!(
            "#!/usr/bin/env bash\nprintf '%s\\n' \"$@\" > {}\n",
            shell_path(&docker_args)
        ),
    )
    .unwrap();
    std::fs::write(&uname, "#!/usr/bin/env bash\nprintf 'Darwin\\n'\n").unwrap();
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(&docker, std::fs::Permissions::from_mode(0o755)).unwrap();
        std::fs::set_permissions(&uname, std::fs::Permissions::from_mode(0o755)).unwrap();
    }

    let path = format!(
        "{}:{}",
        shell_path(tmp.path()),
        std::env::var("PATH").unwrap_or_default()
    );
    let mut command = shell_script_command(&repo_root().join("bin/ai-memory"));
    let output = command
        .args(args)
        .env("PATH", path)
        .env("AI_MEMORY_DOCKER", shell_path(&docker))
        // This helper asserts container argv. `install-hooks` defaults to the
        // native host client now; opt into the documented script fallback so
        // that subcommand still exercises the container-routing contract.
        .env("AI_MEMORY_HOOK_PLATFORM", "posix")
        .env("AI_MEMORY_NO_VERSION_CHECK", "1")
        .env("AI_MEMORY_DATA_VOLUME", "test-ai-memory-data")
        .env("HOME", shell_path(tmp.path()))
        .env_remove("AI_MEMORY_SERVER_URL")
        .env_remove("CLAUDE_CONFIG_DIR")
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "wrapper failed: stdout={} stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    std::fs::read_to_string(docker_args).unwrap()
}

#[test]
fn systemd_units_use_explicit_native_paths() {
    let system = read_repo("packaging/systemd/ai-memory.service");
    assert!(system.contains("--data-dir /var/lib/ai-memory"));
    assert!(system.contains("--config /etc/ai-memory/config.toml"));
    assert!(system.contains("EnvironmentFile=-/etc/ai-memory/env"));
    assert!(system.contains("StateDirectory=ai-memory"));
    assert!(system.contains("ReadWritePaths=/var/lib/ai-memory"));
    assert!(!system.contains("/var/local"));

    let user = read_repo("packaging/systemd/ai-memory-user.service");
    assert!(user.contains("--data-dir %h/.local/share/ai-memory"));
    assert!(user.contains("--config %h/.config/ai-memory/config.toml"));
    assert!(user.contains("EnvironmentFile=-%h/.config/ai-memory/env"));
    assert!(!user.contains("/var/lib/ai-memory"));
}

const LAUNCHD_AGENT_PLIST: &str = "packaging/launchd/com.github.akitaonrails.ai-memory.plist";

#[test]
fn launchd_agent_plist_is_home_independent_and_unthrottled() {
    let agent = read_repo(LAUNCHD_AGENT_PLIST);

    // launchd expands nothing: no %h, no $HOME, no ~. Every path in a plist is
    // a literal, so the two the template cannot know stay placeholders.
    assert!(agent.contains("__AI_MEMORY_BIN__"));
    assert!(agent.contains("__HOME__/Library/Logs/ai-memory/"));
    for unexpandable in ["%h", "$HOME", "${HOME}", "~/"] {
        assert!(
            !agent.contains(unexpandable),
            "launchd would take {unexpandable} literally instead of expanding it"
        );
    }

    // The macOS data dir and the config file inside it are already the binary's
    // defaults, so passing them would only reintroduce a home-dependent path.
    assert!(!agent.contains("--data-dir"));
    assert!(!agent.contains("--config"));

    // An unset ProcessType makes launchd throttle CPU and I/O bandwidth, which
    // the hook ingress budget cannot absorb. Adaptive keys off XPC
    // transactions, which ai-memory never opens, so it decays to Background.
    assert!(agent.contains("<key>ProcessType</key>\n  <string>Interactive</string>"));

    assert!(agent.contains("<key>RunAtLoad</key>\n  <true/>"));
    assert!(agent.contains("<key>KeepAlive</key>\n  <true/>"));

    // launchctl addresses the job by Label, so a Label that disagrees with the
    // filename silently invalidates every bootout/kickstart command we publish.
    let label = Path::new(LAUNCHD_AGENT_PLIST)
        .file_stem()
        .and_then(|stem| stem.to_str())
        .expect("the plist path has a file stem");
    assert!(agent.contains(&format!("<key>Label</key>\n  <string>{label}</string>")));
}

// Substring assertions cannot see malformed XML, and a plist launchd refuses to
// parse fails at bootstrap time with no useful diagnostic.
#[cfg(target_os = "macos")]
#[test]
fn launchd_agent_plist_is_a_valid_property_list() {
    let path = repo_root().join(LAUNCHD_AGENT_PLIST);
    let output = Command::new("plutil")
        .arg("-lint")
        .arg(&path)
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "plutil -lint rejected {}: {}{}",
        path.display(),
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}

#[test]
fn macos_release_tarball_ships_the_launchd_agent_plist() {
    let release = read_repo(".github/workflows/release.yml");
    assert!(release.contains("cp -a packaging/launchd \"dist/$artifact/packaging/launchd\""));
    assert!(
        release.contains(LAUNCHD_AGENT_PLIST),
        "the macOS tarball smoke test must prove the plist actually shipped"
    );
}

/// Source lock: Unix release packing must disable macOS AppleDouble sidecars
/// (`._*`) so Linux runners and macOS producers emit the same archive layout
/// upgrade's allowlist expects (see `extracts_archive_built_like_release_yml`).
#[test]
fn unix_release_tarball_packing_disables_appledouble_sidecars() {
    let release = read_repo(".github/workflows/release.yml");
    let pack = "COPYFILE_DISABLE=1 tar -C \"dist/$artifact\" -czf \"$artifact.tar.gz\" .";
    assert!(
        release.matches(pack).count() >= 2,
        "Linux and macOS release pack steps must set COPYFILE_DISABLE=1"
    );
}

/// Drift lock for #1025: every top-level name staged into a release archive
/// by `release.yml` must be extractable or listed in upgrade's exact support
/// consts (`RELEASE_SUPPORT_*`). Direction is release → allowlist (Windows
/// correctly omits `packaging/`). Catching a new `dist/$artifact/share`
/// without an allowlist update keeps CI red before a live tarball upgrade
/// fails after checksum.
#[test]
fn release_yml_staged_top_levels_are_upgrade_allowlisted() {
    let release = read_repo(".github/workflows/release.yml");
    let staged = release_yml_staged_top_levels(&release);
    assert!(
        staged.contains("hooks") && staged.contains("docs") && staged.contains("crates"),
        "sanity: release.yml must still stage shared top-levels; got {staged:?}"
    );

    let upgrade = read_repo("crates/ai-memory-cli/src/commands/upgrade.rs");
    let dirs = const_str_slice_body(&upgrade, "RELEASE_SUPPORT_DIRS");
    let files = const_str_slice_body(&upgrade, "RELEASE_SUPPORT_FILES");
    for name in &staged {
        // Runtime extract targets live outside the support consts.
        if matches!(name.as_str(), "hooks" | "ai-memory" | "ai-memory.exe") {
            continue;
        }
        let quoted = format!("\"{name}\"");
        assert!(
            dirs.contains(&quoted) || files.contains(&quoted),
            "release.yml stages top-level `{name}` but it is missing from \
             RELEASE_SUPPORT_DIRS/FILES in upgrade.rs"
        );
    }
}

/// First path components under `dist/$artifact/…` and `$stage/…`, plus
/// README.md/LICENSE when the workflow copies them onto the stage root.
fn release_yml_staged_top_levels(release: &str) -> std::collections::BTreeSet<String> {
    let mut tops = std::collections::BTreeSet::new();
    for prefix in ["dist/$artifact/", "$stage/"] {
        let mut search = release;
        while let Some(idx) = search.find(prefix) {
            let after = &search[idx + prefix.len()..];
            let end = after
                .find(|c: char| c == '"' || c == '\'' || c == '`' || c == '\\' || c.is_whitespace())
                .unwrap_or(after.len());
            let rel = &after[..end];
            if let Some(top) = rel
                .split('/')
                .next()
                .filter(|s| is_release_top_level_name(s))
            {
                tops.insert(top.to_string());
            }
            // Always advance so an empty relative path cannot spin forever.
            search = &search[idx + 1..];
        }
    }
    if release.contains("cp README.md LICENSE \"dist/$artifact/\"")
        || release.contains("Copy-Item README.md, LICENSE $stage")
    {
        tops.insert("README.md".to_string());
        tops.insert("LICENSE".to_string());
    }
    tops
}

/// Reject globs / comment noise (e.g. `"$stage/*"` in release.yml prose).
fn is_release_top_level_name(name: &str) -> bool {
    !name.is_empty()
        && name
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.'))
}

fn const_str_slice_body<'a>(src: &'a str, name: &str) -> &'a str {
    let marker = format!("const {name}:");
    let start = src
        .find(&marker)
        .unwrap_or_else(|| panic!("upgrade.rs must declare {name}"));
    let from = &src[start..];
    let end = from
        .find(';')
        .unwrap_or_else(|| panic!("{name} const must end with ';'"));
    &from[..end]
}

#[test]
fn aur_packages_install_all_native_assets() {
    for path in ["packaging/aur/PKGBUILD", "packaging/aur/PKGBUILD-bin"] {
        let pkgbuild = read_repo(path);
        assert!(pkgbuild.contains("/usr/bin/ai-memory"), "{path}");
        assert!(pkgbuild.contains("/usr/share/ai-memory"), "{path}");
        assert!(
            pkgbuild.contains("/usr/lib/systemd/system/ai-memory.service"),
            "{path}"
        );
        assert!(
            pkgbuild.contains("/usr/lib/systemd/user/ai-memory.service"),
            "{path}"
        );
        assert!(
            pkgbuild.contains("/usr/lib/sysusers.d/ai-memory.conf"),
            "{path}"
        );
        assert!(
            pkgbuild.contains("/usr/lib/tmpfiles.d/ai-memory.conf"),
            "{path}"
        );
        assert!(pkgbuild.contains("etc/ai-memory/config.toml"), "{path}");
        assert!(pkgbuild.contains("etc/ai-memory/env"), "{path}");
        assert!(
            pkgbuild.contains("install -Dm0640 packaging/env/ai-memory.env"),
            "{path}"
        );
    }

    // The from-source PKGBUILD builds and runs `check()` on the AUR host.
    // Two things keep that green (see #677): `!lto` avoids the release-LTO
    // link step that OOM-killed the build on constrained AUR builders, and
    // pinning CARGO_HOME to the real registry before the HOME override lets
    // the `--frozen` check() resolve the packages build() already fetched.
    let src_pkgbuild = read_repo("packaging/aur/PKGBUILD");
    assert!(
        src_pkgbuild.contains("options=('!debug' '!lto')"),
        "from-source PKGBUILD must disable LTO to survive constrained AUR builders"
    );
    let cargo_home = src_pkgbuild
        .find("export CARGO_HOME=")
        .expect("check() must pin CARGO_HOME");
    let home_override = src_pkgbuild
        .find(r#"export HOME="$srcdir/test-home""#)
        .expect("check() must override HOME");
    assert!(
        cargo_home < home_override,
        "CARGO_HOME must be pinned to the real registry before HOME is repointed, \
         or --frozen check() cannot resolve the fetched packages"
    );

    let install = read_repo("packaging/aur/ai-memory.install");
    assert!(install.contains("sudo -u ai-memory ai-memory --data-dir /var/lib/ai-memory"));
    assert!(!install.contains("sudo ai-memory --data-dir /var/lib/ai-memory"));

    let bin_pkgbuild = read_repo("packaging/aur/PKGBUILD-bin");
    assert!(bin_pkgbuild.contains("source_x86_64"));
    assert!(bin_pkgbuild.contains("source_aarch64"));
    assert!(bin_pkgbuild.contains("linux-x86_64.tar.gz"));
    assert!(bin_pkgbuild.contains("linux-aarch64.tar.gz"));
}

#[test]
fn docker_source_build_uses_vendored_tailwind() {
    let dockerfile = read_repo("docker/Dockerfile");
    assert!(dockerfile.contains("cargo build --locked --release -p ai-memory-cli"));
    assert!(
        dockerfile.contains("COPY Cargo.toml Cargo.lock rust-toolchain.toml deny.toml LICENSE ./"),
        "the source builder must copy files embedded by ai-memory-cli"
    );
    assert!(
        dockerfile.contains("COPY CHANGELOG.md ./"),
        "the source builder must include bundled release notes"
    );
    assert!(
        !dockerfile.contains("TAILWIND"),
        "the vendored stylesheet is the default; the image build must not opt into a regeneration"
    );
}

#[test]
fn docker_context_excludes_operator_deployment_files() {
    let dockerignore = read_repo(".dockerignore");
    let protected_suffix = concat!(
        "# Operator-specific deployment files. Keep this block last so a later negation\n",
        "# cannot re-include credentials or host configuration in the build context.\n",
        "/bin/deploy.env\n",
        "/docker/.env.production\n",
        "/docker/docker-compose.prod.yml\n",
    );

    assert!(
        dockerignore.ends_with(protected_suffix),
        "operator deployment exclusions must remain the final Docker ignore rules"
    );
}

#[test]
fn docker_publish_jobs_use_prebuilt_binaries() {
    let dockerfile = read_repo("docker/Dockerfile");
    assert!(dockerfile.contains("FROM runtime-base AS runtime-prebuilt-amd64"));
    assert!(dockerfile.contains("FROM runtime-base AS runtime-prebuilt-arm64"));
    assert!(dockerfile.contains("dist/docker/ai-memory-linux-x86_64/ai-memory"));
    assert!(dockerfile.contains("dist/docker/ai-memory-linux-aarch64/ai-memory"));

    let release = read_repo(".github/workflows/release.yml");
    assert!(release.contains("artifact: ai-memory-linux-x86_64"));
    assert!(release.contains("artifact: ai-memory-linux-aarch64"));
    assert!(release.contains("artifact: ai-memory-macos-aarch64"));
    assert!(release.contains("artifact: ai-memory-macos-x86_64"));
    assert!(release.contains("needs: [binary, macos, windows, rpm, validate-version]"));
    assert!(release.contains("target: runtime-prebuilt-amd64"));
    assert!(release.contains("target: runtime-prebuilt-arm64"));

    let ci = read_repo(".github/workflows/ci.yml");
    assert!(ci.contains("ci-ai-memory-${{ matrix.artifact }}"));
    // The release-build matrix is a conditional expression since the
    // fast-CI split: Linux always, the macOS legs on full-ci/dispatch.
    // The full branch must keep every release artifact and runner.
    assert!(ci.contains(r#"{"artifact": "linux-x86_64", "runner": "ubuntu-22.04"}"#));
    assert!(ci.contains(r#"{"artifact": "macos-aarch64", "runner": "macos-15"}"#));
    assert!(ci.contains(r#"{"artifact": "macos-x86_64", "runner": "macos-15-intel"}"#));
    // and the reduced branch still builds the Linux artifact docker uses
    assert!(
        ci.matches(r#"{"artifact": "linux-x86_64", "runner": "ubuntu-22.04"}"#)
            .count()
            >= 2
    );
    assert!(ci.contains("--target runtime-prebuilt-amd64"));
}

// Like run_wrapper_on_fake_macos's fake docker, but the wrapper is spawned with
// stdin on a pipe: the shape every `cat page.md | ai-memory write-page --body -`
// (and every CI/cron) invocation actually has.
#[cfg(unix)]
fn run_wrapper_with_piped_stdin(args: &[&str], stdin_payload: &str) -> String {
    use std::io::Write as _;

    let tmp = tempfile::tempdir().unwrap();
    let docker_args = tmp.path().join("docker-args.txt");
    let docker = tmp.path().join("docker");
    std::fs::write(
        &docker,
        format!(
            "#!/usr/bin/env bash\nprintf '%s\\n' \"$@\" > {}\n",
            shell_path(&docker_args)
        ),
    )
    .unwrap();
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(&docker, std::fs::Permissions::from_mode(0o755)).unwrap();
    }

    let mut child = shell_script_command(&repo_root().join("bin/ai-memory"))
        .args(args)
        .env("AI_MEMORY_DOCKER", shell_path(&docker))
        .env("AI_MEMORY_NO_VERSION_CHECK", "1")
        .env("AI_MEMORY_DATA_VOLUME", "test-ai-memory-data")
        .env("HOME", shell_path(tmp.path()))
        .env_remove("AI_MEMORY_SERVER_URL")
        .env_remove("CLAUDE_CONFIG_DIR")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .unwrap();
    child
        .stdin
        .take()
        .expect("stdin was piped")
        .write_all(stdin_payload.as_bytes())
        .unwrap();
    let output = child.wait_with_output().unwrap();
    assert!(
        output.status.success(),
        "wrapper failed: stdout={} stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    std::fs::read_to_string(docker_args).unwrap()
}

#[test]
fn docker_wrappers_keep_stdin_attached_independently_of_tty_allocation() {
    let posix = read_repo("bin/ai-memory");
    assert!(
        posix.contains("TTY_ARGS=(-i)"),
        "POSIX wrapper must attach stdin before inspecting terminal state"
    );
    assert!(
        posix.contains("TTY_ARGS+=(-t)"),
        "POSIX wrapper must add TTY allocation separately"
    );
    assert!(
        !posix.contains("TTY_ARGS=(-it)"),
        "combined flags can drop stdin when only stdout is redirected"
    );
    assert!(
        posix.contains("CLAUDE_CODE_SESSION_ID"),
        "POSIX wrapper must forward Claude's session id into the bridge container"
    );
    assert!(
        posix.contains("  MUSE_SESSION_ID \\"),
        "POSIX wrapper must forward Muse's session id into the bridge container"
    );
    assert!(posix.contains("git -C \"${PWD}\" rev-parse --show-toplevel"));
    assert!(posix.contains("AI_MEMORY_SCOPE_CWD=/scope${SCOPE_REL}"));
    assert!(posix.contains("${SCOPE_ROOT}:/scope:ro"));

    let powershell = read_repo("bin/ai-memory.ps1");
    assert!(
        powershell.contains("$DockerArgs = @(\"run\", \"--rm\", \"-i\")"),
        "PowerShell wrapper must attach stdin before inspecting console state"
    );
    assert!(
        powershell.contains("$DockerArgs += \"-t\""),
        "PowerShell wrapper must add TTY allocation separately"
    );
    assert!(
        !powershell.contains("$DockerArgs += \"-it\""),
        "PowerShell must not couple stdin attachment to TTY allocation"
    );
    assert!(
        powershell.contains("\"CLAUDE_CODE_SESSION_ID\""),
        "PowerShell wrapper must forward Claude's session id into the bridge container"
    );
    assert!(
        powershell.contains("\"MUSE_SESSION_ID\""),
        "PowerShell wrapper must forward Muse's session id into the bridge container"
    );
    assert!(powershell.contains("AI_MEMORY_SCOPE_CWD=$ScopeCwd"));
    assert!(powershell.contains("${ScopeRoot}:/scope:ro"));
}

#[cfg(unix)]
#[test]
fn posix_wrapper_auto_selects_podman_when_docker_is_unavailable() {
    let tmp = tempfile::tempdir().unwrap();
    let podman_args = tmp.path().join("podman-args.txt");
    let scripts = [
        (
            "podman",
            format!(
                "#!/bin/bash\n\
                 if [ \"${{1:-}}\" = info ]; then\n\
                 \x20 printf '[name=seccomp]\\n'\n\
                 \x20 exit 0\n\
                 fi\n\
                 printf '%s\\n' \"$@\" > {}\n",
                shell_path(&podman_args)
            ),
        ),
        (
            "id",
            "#!/bin/bash\ncase \"$1\" in -u) printf '1000\\n' ;; -g) printf '1000\\n' ;; esac\n"
                .to_owned(),
        ),
        ("uname", "#!/bin/bash\nprintf 'Linux\\n'\n".to_owned()),
        (
            "getenforce",
            "#!/bin/bash\nprintf 'Disabled\\n'\n".to_owned(),
        ),
        (
            "grep",
            "#!/bin/bash\n\
             pattern=\"${@: -1}\"\n\
             input=\"\"\n\
             while IFS= read -r line; do input=\"${input}${line}\"; done\n\
             case \"${input}\" in *\"${pattern}\"*) exit 0 ;; *) exit 1 ;; esac\n"
                .to_owned(),
        ),
    ];
    for (name, body) in scripts {
        let path = tmp.path().join(name);
        std::fs::write(&path, body).unwrap();
        use std::os::unix::fs::PermissionsExt as _;
        std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).unwrap();
    }

    let output = Command::new("/bin/bash")
        .arg(repo_root().join("bin/ai-memory"))
        .arg("status")
        .env("PATH", tmp.path())
        .env("HOME", tmp.path())
        .env("AI_MEMORY_NO_TTY", "1")
        .env("AI_MEMORY_NO_VERSION_CHECK", "1")
        .env("AI_MEMORY_DATA_VOLUME", "test-ai-memory-data")
        .env_remove("AI_MEMORY_DOCKER")
        .env_remove("AI_MEMORY_IMAGE")
        .env_remove("AI_MEMORY_SERVER_URL")
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "wrapper failed: stdout={} stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    let args = std::fs::read_to_string(podman_args).unwrap();
    let flags: Vec<_> = args.lines().collect();
    assert_eq!(
        flags.first(),
        Some(&"run"),
        "Podman was not invoked: {args}"
    );
    assert!(
        flags.contains(&"docker.io/akitaonrails/ai-memory:latest"),
        "default image must not require Podman short-name resolution: {args}"
    );
}

const WRAPPER_FORWARDED_PROVIDER_KEYS: &[&str] = &[
    "ANTHROPIC_API_KEY",
    "ANTHROPIC_OAUTH_TOKEN",
    "OPENAI_API_KEY",
    "GEMINI_API_KEY",
    "GOOGLE_API_KEY",
    "VOYAGE_API_KEY",
    "COPILOT_GITHUB_TOKEN",
    "GITHUB_COPILOT_API_TOKEN",
    "COPILOT_API_URL",
    "LLM_API_KEY",
    "EMBEDDING_API_KEY",
    "OPENCODE_API_KEY",
];

#[test]
fn wrapper_forwards_every_supported_provider_api_key() {
    // The wrapper runs the server in a container, so any provider credential
    // the operator exports must be on the `-e` forwarding allowlist or it
    // never reaches the process and the provider reports "not configured".
    // Gemini/Google were missing while every other provider key was
    // forwarded (#698), so the guard names each key the config layer reads.
    let wrapper = read_repo("bin/ai-memory");
    for key in WRAPPER_FORWARDED_PROVIDER_KEYS {
        assert!(
            wrapper.contains(&format!("  {key} \\")),
            "wrapper must forward {key} into the container"
        );
    }
}

#[test]
fn powershell_wrapper_forwards_every_supported_provider_api_key() {
    // Same contract as the POSIX wrapper: Docker Desktop on Windows still
    // runs the CLI inside a Linux helper, so a host-exported Gemini /
    // Copilot / OpenCode key that is not on this allowlist never reaches
    // Config::load and the provider reports "not configured".
    let wrapper = read_repo("bin/ai-memory.ps1");
    let allowlist = wrapper
        .split_once("foreach ($Name in @(")
        .and_then(|(_, rest)| rest.split_once(")) {"))
        .map(|(allowlist, _)| allowlist)
        .expect("PowerShell wrapper env allowlist");
    for key in WRAPPER_FORWARDED_PROVIDER_KEYS {
        assert!(
            allowlist
                .lines()
                .any(|line| line.trim() == format!("\"{key}\",")
                    || line.trim() == format!("\"{key}\"")),
            "PowerShell wrapper must list {key} in the helper env allowlist"
        );
    }
    for key in [
        "AI_MEMORY_COPILOT_CLIENT_ID",
        "AI_MEMORY_WORKSTREAM_ID",
        "CLAUDE_CONFIG_DIR",
    ] {
        assert!(
            allowlist
                .lines()
                .any(|line| line.trim() == format!("\"{key}\",")
                    || line.trim() == format!("\"{key}\"")),
            "PowerShell wrapper must list {key} in the helper env allowlist"
        );
    }
}

#[test]
fn wrapper_updates_and_install_docs_use_verified_release_assets() {
    let wrapper = read_repo("bin/ai-memory");
    assert!(wrapper.contains("releases/latest/download/ai-memory-wrapper"));
    assert!(wrapper.contains("WRAPPER_SHA256_URL"));
    assert!(wrapper.contains("wrapper checksum mismatch; refusing update"));
    assert!(!wrapper.contains("raw.githubusercontent.com/akitaonrails/ai-memory/main"));

    let release = read_repo(".github/workflows/release.yml");
    for asset in [
        "ai-memory-wrapper",
        "ai-memory-wrapper.ps1",
        "ai-memory-wrapper.cmd",
        "ai-memory-install-hooks",
        "ai-memory-hooks.tar.gz",
        "install.sh",
        "install.ps1",
    ] {
        assert!(release.contains(asset), "release must publish {asset}");
        assert!(
            release.contains(&format!("{asset}.sha256")),
            "release must publish a checksum for {asset}"
        );
    }
    assert!(release.contains("permissions:\n  contents: read"));
    assert!(release.contains("github-release:"));
    assert!(release.contains("contents: write"));

    for path in ["README.md", "docs/install.md", "docs/windows.md"] {
        let docs = read_repo(path);
        assert!(
            !docs.contains("raw.githubusercontent.com/akitaonrails/ai-memory/main/bin/ai-memory"),
            "{path} must not install an executable from mutable main"
        );
    }
    let install_docs = read_repo("docs/install.md");
    assert!(
        !install_docs.contains("raw.githubusercontent.com/akitaonrails/ai-memory/main/scripts"),
        "hook installation docs must not execute mutable main"
    );
    let hook_installer = read_repo("scripts/install-hooks.sh");
    assert!(hook_installer.contains("ARCHIVE=\"ai-memory-hooks.tar.gz\""));
    assert!(hook_installer.contains("$BASE_URL/$ARCHIVE.sha256"));
    assert!(hook_installer.contains("hook bundle checksum mismatch; refusing installation"));
    assert!(hook_installer.contains("tar -xOf"));
    assert!(!hook_installer.contains("tar -xzf"));
    assert!(!hook_installer.contains("raw.githubusercontent.com"));
}

/// `install.ps1` runs only on Windows CI; these source checks keep its safety
/// properties visible to every platform's gate: the checksum is compared
/// before anything is unpacked, archive entries escaping the install dir are
/// refused, it never `exit`s (which would close the shell under `iex`), and
/// it runs from one function called on the last line.
#[test]
fn install_ps1_keeps_its_safety_checks() {
    let script = read_repo("install.ps1");
    let hash = script.find("Get-FileHash").expect("checksum computed");
    let mismatch = script.find("checksum mismatch").expect("mismatch refused");
    let unsafe_entry = script.find("unsafe path").expect("unsafe entries refused");
    let expand = script.find("Expand-Archive").expect("archive unpacked");
    assert!(hash < expand && mismatch < expand && unsafe_entry < expand);
    assert!(script.contains("$parts -contains '..'"));
    assert!(script.contains("ai-memory-windows-x86_64.zip"));
    assert!(script.contains("AI_MEMORY_NO_MODIFY_PATH"));
    assert!(
        !script
            .lines()
            .any(|line| line.trim_start().starts_with("exit")),
        "install.ps1 must throw, not exit"
    );
    assert_eq!(script.trim_end().lines().last(), Some("Install-AiMemory"));
    assert!(!script.contains("raw.githubusercontent.com"));

    // A plain-http GitHub base must be refused, and the refusal must not be
    // nested inside a branch that already requires https. That nesting is what
    // made it unreachable: both arms of
    // `$base -eq $defaultBase -or StartsWith('https://github.com/')` imply
    // `https://`, so the inner `-not StartsWith('https://')` could never fire,
    // and an `http://github.com/...` base matched neither arm and skipped the
    // check entirely.
    assert!(script.contains("[Uri]::TryCreate($base"));
    assert!(script.contains("$baseUri.Scheme -eq 'http'"));
    assert!(script.contains("$baseUri.Host.TrimEnd('.') -eq 'github.com'"));
    assert!(
        !script.contains("StartsWith('https://github.com/'"),
        "the GitHub refusal must not be gated on an https-only branch again"
    );
    assert!(
        script.contains("refusing a non-https GitHub URL"),
        "the refusal keeps its operator-facing message"
    );
    // Scoped to GitHub on purpose: `tests/install_ps1.ps1` serves its fixture
    // releases over plain http from loopback, and the header documents a
    // non-GitHub base as the operator's own trust decision.
    assert!(
        !script.contains("-match '(?i)^http://'"),
        "refusing every plain-http base would break the loopback test harness"
    );
}

#[test]
fn github_actions_are_pinned_to_full_commits() {
    for path in [".github/workflows/ci.yml", ".github/workflows/release.yml"] {
        let workflow = read_repo(path);
        for line in workflow.lines().filter(|line| line.contains("uses: ")) {
            let Some(reference) = line
                .split('@')
                .nth(1)
                .and_then(|value| value.split_whitespace().next())
            else {
                continue;
            };
            assert!(
                reference.len() == 40 && reference.chars().all(|c| c.is_ascii_hexdigit()),
                "{path} action is not pinned to a full commit: {line}"
            );
        }
    }
}

#[test]
fn workflows_keep_fixed_rust_jobs_on_the_fixed_toolchain() {
    for (path, expected_fixed_jobs) in [
        (".github/workflows/ci.yml", 2),
        (".github/workflows/release.yml", 3),
    ] {
        let workflow = read_repo(path);
        let lines = workflow.lines().collect::<Vec<_>>();
        let mut fixed_jobs = 0;
        for (index, line) in lines.iter().enumerate().filter(|(_, line)| {
            line.contains("uses: dtolnay/rust-toolchain@") && line.ends_with("# 1.95")
        }) {
            fixed_jobs += 1;
            assert_eq!(
                lines.get(index + 1).map(|line| line.trim()),
                Some("with:"),
                "{path} must configure the fixed toolchain after: {line}"
            );
            assert_eq!(
                lines.get(index + 2).map(|line| line.trim()),
                Some("toolchain: \"1.95\""),
                "{path} must keep its # 1.95 job on Rust 1.95"
            );
        }

        assert_eq!(
            fixed_jobs, expected_fixed_jobs,
            "{path} has an unexpected number of fixed Rust jobs"
        );
    }
}

#[test]
fn dev_loop_is_wired_consistently() {
    // One gate, written down in four places; keep them from drifting apart.
    let ci = read_repo(".github/workflows/ci.yml");
    assert!(ci.contains("cargo nextest run --workspace --all-targets --profile ci"));
    // nextest does not run doctests.
    assert!(ci.contains("cargo test --workspace --doc"));
    // Windows stays on plain `cargo test`: nextest's per-test processes cost
    // more there than its parallelism saves.
    let windows = read_repo(".github/workflows/windows.yml");
    assert!(windows.contains("cargo test --workspace --all-targets"));
    // The `ci` profile is what makes CI a full gate: every tier, nothing
    // turned green by a retry.
    let nextest = read_repo(".config/nextest.toml");
    let ci_profile = nextest
        .split("[profile.ci]")
        .nth(1)
        .expect("nextest.toml defines [profile.ci]");
    let ci_profile = ci_profile.split("\n[").next().unwrap_or(ci_profile);
    assert!(ci_profile.contains("default-filter = \"all()\""));
    assert!(ci_profile.contains("retries = 0"));
    assert!(
        ci.contains("TAILWIND_BUILD=1"),
        "CI must regenerate the stylesheet somewhere"
    );
    assert!(ci.contains("git diff --exit-code -- crates/ai-memory-web/static/tailwind.css"));

    let release = read_repo("bin/release");
    assert!(release.contains("git diff --check"));
    assert!(release.contains("cargo test --workspace --all-targets"));

    let pr_template = read_repo(".github/pull_request_template.md");
    assert!(pr_template.contains("git diff --check"));
    assert!(pr_template.contains("cargo tf"));

    let contributing = read_repo("CONTRIBUTING.md");
    assert!(contributing.contains("cargo tf"));
    assert!(contributing.contains("install-git-hooks.sh"));

    let installer = read_repo("scripts/install-git-hooks.sh");
    assert!(installer.contains("# >>> ai-memory pre-push >>>"));
    assert!(
        !installer.contains("cat > \"$hook\""),
        "the hook installer must not overwrite an existing user hook"
    );

    // `.cargo/config.toml` carries the tier aliases and nothing else. An env
    // table there applies to every cargo invocation on every platform.
    let cargo_config = read_repo(".cargo/config.toml");
    assert!(
        !cargo_config.contains("[env]"),
        "keep .cargo/config.toml to aliases"
    );
    for alias in ["t = ", "tf = "] {
        assert!(
            cargo_config.contains(alias),
            "missing cargo alias {alias:?}"
        );
    }

    // The build is self-contained: nothing should still ask for the old skip flag.
    for path in [
        ".github/workflows/ci.yml",
        ".github/workflows/release.yml",
        ".github/workflows/windows.yml",
        "docker/Dockerfile",
        "bin/release",
        "AGENTS.md",
        "CONTRIBUTING.md",
        ".github/pull_request_template.md",
    ] {
        assert!(
            !read_repo(path).contains("TAILWIND_SKIP"),
            "{path} still sets TAILWIND_SKIP"
        );
    }
}

#[cfg(unix)]
#[test]
fn hook_installer_normalizes_the_open_code_alias() {
    let tmp = tempfile::tempdir().unwrap();
    let output = shell_script_command(&repo_root().join("scripts/install-hooks.sh"))
        .args(["--agent", "open-code", "--to"])
        .arg(tmp.path().join("hooks"))
        .env("HOME", tmp.path())
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "installer failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("install-hooks --agent opencode --apply"),
        "{stdout}"
    );
    assert!(!stdout.contains("unsupported agent"), "{stdout}");
}

#[cfg(unix)]
fn installed_hook_names(agent_arg: &str, canonical_agent: &str, hooks: &[&str]) -> Vec<String> {
    let tmp = tempfile::tempdir().unwrap();
    let bundle = tmp.path().join("bundle/hooks").join(canonical_agent);
    std::fs::create_dir_all(&bundle).unwrap();
    for hook in hooks {
        std::fs::write(
            bundle.join(format!("{hook}.sh")),
            format!("#!/usr/bin/env bash\nprintf '{hook}\\n'\n"),
        )
        .unwrap();
    }
    std::fs::write(
        tmp.path().join("bundle/hooks/_lib.sh"),
        "# shared helper sourced by every event script\n",
    )
    .unwrap();
    let archive = tmp.path().join("ai-memory-hooks.tar.gz");
    let status = Command::new("tar")
        .arg("-czf")
        .arg(&archive)
        .arg("-C")
        .arg(tmp.path().join("bundle"))
        .arg("hooks")
        .status()
        .unwrap();
    assert!(status.success());

    let bin_dir = tmp.path().join("bin");
    std::fs::create_dir_all(&bin_dir).unwrap();
    let curl = bin_dir.join("curl");
    std::fs::write(
        &curl,
        "#!/usr/bin/env bash\n\
         set -euo pipefail\n\
         url=''\n\
         out=''\n\
         while [ \"$#\" -gt 0 ]; do\n\
           case \"$1\" in\n\
             -o) out=\"$2\"; shift 2 ;;\n\
             -*) shift ;;\n\
             *) url=\"$1\"; shift ;;\n\
           esac\n\
         done\n\
         case \"$url\" in\n\
           *.sha256) printf '%s  ai-memory-hooks.tar.gz\\n' \"$FAKE_HOOK_CHECKSUM\" > \"$out\" ;;\n\
           *) cp \"$FAKE_HOOK_ARCHIVE\" \"$out\" ;;\n\
         esac\n",
    )
    .unwrap();
    {
        use std::os::unix::fs::PermissionsExt as _;
        std::fs::set_permissions(&curl, std::fs::Permissions::from_mode(0o755)).unwrap();
    }
    let path = format!(
        "{}:{}",
        shell_path(&bin_dir),
        std::env::var("PATH").unwrap_or_default()
    );
    let destination = tmp.path().join("installed-hooks");
    let output = shell_script_command(&repo_root().join("scripts/install-hooks.sh"))
        .args(["--agent", agent_arg, "--to"])
        .arg(&destination)
        .env("PATH", path)
        .env("HOME", tmp.path())
        .env("FAKE_HOOK_ARCHIVE", &archive)
        .env("FAKE_HOOK_CHECKSUM", sha256_file(&archive))
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "installer failed: stdout={} stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        destination.join("_lib.sh").is_file(),
        "the shared helper must land beside the agent directories"
    );
    let installed = destination.join(canonical_agent);
    let mut names = std::fs::read_dir(&installed)
        .unwrap()
        .map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned())
        .collect::<Vec<_>>();
    names.sort();
    use std::os::unix::fs::PermissionsExt as _;
    for name in &names {
        assert_ne!(
            std::fs::metadata(installed.join(name))
                .unwrap()
                .permissions()
                .mode()
                & 0o111,
            0
        );
    }
    names
}

// Unlike run_wrapper_on_fake_macos's docker fake (which only ever sees one
// meaningful call — the final `docker run`), the rootless-Docker UID check
// calls `docker info` *before* `docker run`, so this fake must dispatch on
// $1: real stdout for `info` (read by the wrapper's `grep -q rootless`) vs.
// logging argv to a file for `run` (read back by the test).
// How the fake `docker` binary answers `docker info --format …`. The two
// engines expose the same two facts under incompatible keys, and the wrapper
// has to read both, so the fake has to be able to impersonate either one.
#[cfg(unix)]
enum FakeInfo<'a> {
    // Docker: `{{.SecurityOptions}}` prints the security-options list.
    Docker(&'a str),
    // Podman, including the podman-docker `docker` shim: `.SecurityOptions`
    // is not a field of its info report, so the template fails and the
    // command exits non-zero with nothing on stdout. The equivalent facts
    // live under `.Host.Security.*`.
    Podman { rootless: bool, selinux: bool },
}

#[cfg(unix)]
fn run_wrapper_with_fake_docker(args: &[&str], docker_info_stdout: &str) -> String {
    run_wrapper_with_fake_docker_and_uname(args, docker_info_stdout, None)
}

#[cfg(unix)]
fn run_wrapper_with_fake_docker_and_claude_config(
    args: &[&str],
    docker_info_stdout: &str,
    claude_config_dir: &str,
) -> String {
    run_wrapper_with_fake_docker_env(
        args,
        FakeInfo::Docker(docker_info_stdout),
        None,
        Some(claude_config_dir),
        None,
        &[],
    )
}

#[cfg(unix)]
fn run_wrapper_with_fake_docker_and_forwarded_env(
    args: &[&str],
    docker_info_stdout: &str,
    forwarded_env: &[(&str, &str)],
) -> String {
    run_wrapper_with_fake_docker_env(
        args,
        FakeInfo::Docker(docker_info_stdout),
        None,
        None,
        None,
        forwarded_env,
    )
}

// The wrapper also shells out to `id -u` / `id -g` when choosing its default
// Docker uid mapping. Arch container tests often run as root, which would make
// the default mapping `-u 0:0` and produce a false positive in the assertions
// below. Shadow `id` too so these tests exercise the rootless/rootful branch
// logic, not the uid of the test runner. This shadow is unconditional
// (unlike `uname`, which only matters for the macOS-simulation callers)
// because every caller of this helper is exposed to the flakiness.
#[cfg(unix)]
fn run_wrapper_with_fake_docker_and_uname(
    args: &[&str],
    docker_info_stdout: &str,
    uname_stdout: Option<&str>,
) -> String {
    run_wrapper_with_fake_docker_env(
        args,
        FakeInfo::Docker(docker_info_stdout),
        uname_stdout,
        None,
        None,
        &[],
    )
}

#[cfg(unix)]
fn run_wrapper_with_fake_selinux(
    args: &[&str],
    docker_info_stdout: &str,
    selinux_mode: &str,
) -> String {
    run_wrapper_with_fake_docker_env(
        args,
        FakeInfo::Docker(docker_info_stdout),
        Some("Linux"),
        None,
        Some(selinux_mode),
        &[],
    )
}

// Rootless podman on an SELinux-enforcing host — the combination Fedora and
// openSUSE ship by default, and the one where the Docker-only probe answers
// nothing at all.
#[cfg(unix)]
fn run_wrapper_with_fake_podman(
    args: &[&str],
    rootless: bool,
    selinux: bool,
    selinux_mode: &str,
) -> String {
    run_wrapper_with_fake_docker_env(
        args,
        FakeInfo::Podman { rootless, selinux },
        Some("Linux"),
        None,
        Some(selinux_mode),
        &[],
    )
}

#[cfg(unix)]
fn run_wrapper_with_fake_docker_env(
    args: &[&str],
    fake_info: FakeInfo<'_>,
    uname_stdout: Option<&str>,
    claude_config_dir: Option<&str>,
    selinux_mode: Option<&str>,
    forwarded_env: &[(&str, &str)],
) -> String {
    let tmp = tempfile::tempdir().unwrap();
    let docker_args = tmp.path().join("docker-args.txt");
    let docker = tmp.path().join("docker");
    let uname = tmp.path().join("uname");
    let id = tmp.path().join("id");
    let getenforce = tmp.path().join("getenforce");
    let info_branch = match fake_info {
        FakeInfo::Docker(stdout) => format!("  printf '%s\\n' '{stdout}'\n  exit 0\n"),
        FakeInfo::Podman { rootless, selinux } => format!(
            "  case \"$*\" in\n\
            \x20   *Host.Security.Rootless*) printf '{rootless}\\n' ; exit 0 ;;\n\
            \x20   *Host.Security.SELinuxEnabled*) printf '{selinux}\\n' ; exit 0 ;;\n\
            \x20 esac\n\
            \x20 printf '%s\\n' \"Error: template: info:1:2: executing \\\"info\\\" at \
             <.SecurityOptions>: can't evaluate field SecurityOptions in \
             type system.infoReport\" >&2\n\
            \x20 exit 125\n"
        ),
    };
    std::fs::write(
        &docker,
        format!(
            "#!/usr/bin/env bash\n\
             if [ \"$1\" = info ]; then\n{}fi\n\
             if [ \"$1\" = run ]; then\n  shift\n  printf '%s\\n' \"$@\" > {}\n  exit 0\nfi\n\
             exit 0\n",
            info_branch,
            shell_path(&docker_args)
        ),
    )
    .unwrap();
    if let Some(uname_stdout) = uname_stdout {
        std::fs::write(
            &uname,
            format!("#!/usr/bin/env bash\nprintf '{}\\n'\n", uname_stdout),
        )
        .unwrap();
    }
    std::fs::write(
        &id,
        "#!/usr/bin/env bash\n\
         case \"$1\" in\n\
           -u) printf '1000\\n' ;;\n\
           -g) printf '1000\\n' ;;\n\
           *) printf 'uid=1000 gid=1000 groups=1000\\n' ;;\n\
         esac\n",
    )
    .unwrap();
    std::fs::write(
        &getenforce,
        format!(
            "#!/usr/bin/env bash\nprintf '{}\\n'\n",
            selinux_mode.unwrap_or("Disabled")
        ),
    )
    .unwrap();
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(&docker, std::fs::Permissions::from_mode(0o755)).unwrap();
        if uname_stdout.is_some() {
            std::fs::set_permissions(&uname, std::fs::Permissions::from_mode(0o755)).unwrap();
        }
        std::fs::set_permissions(&id, std::fs::Permissions::from_mode(0o755)).unwrap();
        std::fs::set_permissions(&getenforce, std::fs::Permissions::from_mode(0o755)).unwrap();
    }

    // Always prepend the fake-binary dir to PATH: `id` is shadowed
    // unconditionally (see comment above), so PATH must always change, even
    // when `uname_stdout` is None and only `docker`/`id` are shadowed.
    let path = format!(
        "{}:{}",
        shell_path(tmp.path()),
        std::env::var("PATH").unwrap_or_default()
    );
    let mut command = shell_script_command(&repo_root().join("bin/ai-memory"));
    command
        .args(args)
        .env("PATH", path)
        .env("AI_MEMORY_DOCKER", shell_path(&docker))
        // Callers test Docker/Podman UID, SELinux, and env forwarding. Keep
        // `install-hooks` on that explicit compatibility path; native hook
        // routing has separate regression coverage below.
        .env("AI_MEMORY_HOOK_PLATFORM", "posix")
        .env("AI_MEMORY_NO_VERSION_CHECK", "1")
        .env("AI_MEMORY_DATA_VOLUME", "test-ai-memory-data")
        .env("HOME", shell_path(tmp.path()))
        .env_remove("AI_MEMORY_SERVER_URL")
        .env_remove("CLAUDE_CONFIG_DIR")
        // Guarantees the "unset" case in
        // `posix_wrapper_forwards_embedding_prefixes_by_presence_not_non_emptiness`
        // is actually unset rather than silently inheriting whatever the
        // test-runner's own ambient environment happens to hold; every
        // other case re-adds one of these via `forwarded_env` below, so
        // removing them unconditionally here is a no-op for every other
        // caller of this helper.
        .env_remove("AI_MEMORY_EMBEDDING_QUERY_PREFIX")
        .env_remove("AI_MEMORY_EMBEDDING_DOCUMENT_PREFIX");
    if let Some(claude_config_dir) = claude_config_dir {
        command.env("CLAUDE_CONFIG_DIR", claude_config_dir);
    }
    for (name, value) in forwarded_env {
        command.env(name, value);
    }
    let output = command.output().unwrap();
    assert!(
        output.status.success(),
        "wrapper failed: stdout={} stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    std::fs::read_to_string(docker_args).unwrap()
}

#[cfg(unix)]
fn run_wrapper_with_fake_rootless_docker_on_fake_macos(args: &[&str]) -> String {
    run_wrapper_with_fake_docker_and_uname(
        args,
        "[name=apparmor name=seccomp,profile=default name=rootless]",
        Some("Darwin"),
    )
}

#[test]
fn powershell_wrapper_lists_subscription_oauth_tokens_in_its_env_allowlist() {
    let wrapper = read_repo("bin/ai-memory.ps1");
    let allowlist = wrapper
        .split_once("foreach ($Name in @(")
        .and_then(|(_, rest)| rest.split_once(")) {"))
        .map(|(allowlist, _)| allowlist)
        .expect("PowerShell wrapper env allowlist");

    for name in ["ANTHROPIC_OAUTH_TOKEN", "CLAUDE_CODE_OAUTH_TOKEN"] {
        assert!(
            allowlist
                .lines()
                .any(|line| line.trim() == format!("\"{name}\",")),
            "PowerShell wrapper must list {name} in the helper env allowlist"
        );
    }
}

#[test]
fn powershell_wrapper_trims_paths_with_single_character_separators() {
    let wrapper = read_repo("bin/ai-memory.ps1");

    assert_eq!(
        wrapper.matches(r"TrimEnd([char[]]@('/', '\'))").count(),
        2,
        "PowerShell TrimEnd arguments must contain individual characters"
    );
    assert!(
        !wrapper.contains(r"TrimEnd([char[]]@('/', '\\'))"),
        "a multi-character backslash string cannot be converted to System.Char"
    );
}

// The helper container has no host harnesses, so both Docker wrappers
// intercept the managed launch/discovery commands and run them through a
// native client on the host (#1545). The two lists must stay identical, and
// every surface that refuses or explains a missing client must point at the
// same documented Windows installer.
#[test]
fn docker_wrappers_intercept_the_same_native_host_commands() {
    let expected = [
        "run",
        "show",
        "continue",
        "resume",
        "workstreams",
        "rename-workstream",
    ];

    let posix = read_repo("bin/ai-memory");
    let case_line = posix
        .lines()
        .find(|line| {
            line.starts_with("  run | show | continue | resume | workstreams | rename-workstream)")
        })
        .expect("POSIX wrapper native host command case arm");
    let posix_list: Vec<&str> = case_line
        .trim()
        .trim_end_matches(')')
        .split('|')
        .map(str::trim)
        .collect();
    assert_eq!(posix_list, expected, "POSIX wrapper intercept list drifted");

    let powershell = read_repo("bin/ai-memory.ps1");
    let (array_body, _) = powershell
        .split_once("$NativeHostCommands = @(")
        .expect("PowerShell wrapper native host command array")
        .1
        .split_once(')')
        .expect("PowerShell wrapper native host command array terminator");
    let ps1_list: Vec<&str> = array_body
        .lines()
        .filter_map(|line| {
            let item = line.trim().trim_end_matches(',').trim();
            item.strip_prefix('"')?.strip_suffix('"')
        })
        .collect();
    assert_eq!(
        ps1_list, expected,
        "PowerShell wrapper must intercept exactly the commands bin/ai-memory does"
    );

    let installer =
        "https://github.com/akitaonrails/ai-memory/releases/latest/download/install.ps1";
    assert!(
        powershell.contains(installer),
        "the PowerShell wrapper's missing-client refusal must name the documented installer"
    );
    assert!(
        posix.contains(installer),
        "the POSIX wrapper's native-Windows refusal must name the documented installer"
    );
    let run_cmd = read_repo("crates/ai-memory-cli/src/commands/run.rs");
    assert!(
        run_cmd.contains(installer),
        "the Windows missing-harness advice must name the documented installer"
    );
    assert!(
        run_cmd.contains("Docker users should run `ai-memory upgrade` to refresh the host wrapper"),
        "the non-Windows missing-harness advice must stay on `ai-memory upgrade`"
    );
}

// The PowerShell counterpart of run_wrapper_on_fake_macos: a fake `docker.cmd`
// records the argv the wrapper built. No `uname` shadowing is needed because
// the Windows arm under test is selected by running on Windows at all.
#[cfg(windows)]
fn run_powershell_wrapper(args: &[&str]) -> String {
    run_powershell_wrapper_with_server_url(args, None)
}

#[cfg(windows)]
fn run_powershell_wrapper_with_server_url(args: &[&str], server_url: Option<&str>) -> String {
    let tmp = tempfile::tempdir().unwrap();
    let docker_args = tmp.path().join("docker-args.txt");
    let docker = tmp.path().join("docker.cmd");
    std::fs::write(
        &docker,
        "@echo off\r\n>\"%AI_MEMORY_TEST_DOCKER_ARGS%\" echo %*\r\nexit /b 0\r\n",
    )
    .unwrap();

    let mut command = Command::new(ai_memory_test_support::powershell_exe());
    // See the execution-policy note on the OAuth-token test below.
    command
        .args([
            "-NoLogo",
            "-NoProfile",
            "-NonInteractive",
            "-ExecutionPolicy",
            "Bypass",
            "-File",
        ])
        .arg(repo_root().join("bin/ai-memory.ps1"))
        .args(args)
        .env("AI_MEMORY_DOCKER", &docker)
        .env("AI_MEMORY_TEST_DOCKER_ARGS", &docker_args)
        // This helper asserts container argv. `install-hooks` defaults to the
        // native host client now; opt into the documented script fallback so
        // that subcommand still exercises the container-routing contract.
        .env("AI_MEMORY_HOOK_PLATFORM", "windows")
        .env("AI_MEMORY_DATA_VOLUME", "test-ai-memory-data")
        .env_remove("AI_MEMORY_DATA_DIR");
    match server_url {
        Some(url) => command.env("AI_MEMORY_SERVER_URL", url),
        None => command.env_remove("AI_MEMORY_SERVER_URL"),
    };
    let output = command.output().unwrap();
    assert!(
        output.status.success(),
        "PowerShell wrapper failed for {args:?}: stdout={} stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    std::fs::read_to_string(docker_args).unwrap()
}

/// Runs `bin/ai-memory.ps1` with a fake native client (`AI_MEMORY_NATIVE_BIN`)
/// and a fake container engine, returning the wrapper's exit code plus what
/// each side saw. Native-host commands must reach only the native client, so
/// an empty engine log is itself an assertion input; the fake client exits
/// with a marker code so the wrapper's exit-code pass-through is visible.
#[cfg(windows)]
fn run_powershell_wrapper_with_fake_native(
    args: &[&str],
    extra_env: &[(&str, &str)],
) -> (Option<i32>, String, String) {
    let tmp = tempfile::tempdir().unwrap();
    let native_args = tmp.path().join("native-args.txt");
    let docker_args = tmp.path().join("docker-args.txt");
    let native = tmp.path().join("native.cmd");
    std::fs::write(
        &native,
        concat!(
            "@echo off\r\n",
            ">>\"%AI_MEMORY_TEST_NATIVE_ARGS%\" echo argv: %*\r\n",
            "if defined AI_MEMORY_SERVER_URL ",
            ">>\"%AI_MEMORY_TEST_NATIVE_ARGS%\" echo server-url: %AI_MEMORY_SERVER_URL%\r\n",
            "if not defined AI_MEMORY_SERVER_URL ",
            ">>\"%AI_MEMORY_TEST_NATIVE_ARGS%\" echo server-url: unset\r\n",
            "if defined AI_MEMORY_RUN_AUTOWIRE ",
            ">>\"%AI_MEMORY_TEST_NATIVE_ARGS%\" echo autowire: %AI_MEMORY_RUN_AUTOWIRE%\r\n",
            "if not defined AI_MEMORY_RUN_AUTOWIRE ",
            ">>\"%AI_MEMORY_TEST_NATIVE_ARGS%\" echo autowire: unset\r\n",
            "if defined AI_MEMORY_AUTH__BEARER_TOKEN_FILE ",
            ">>\"%AI_MEMORY_TEST_NATIVE_ARGS%\" echo bearer-file: %AI_MEMORY_AUTH__BEARER_TOKEN_FILE%\r\n",
            "exit /b %AI_MEMORY_TEST_NATIVE_EXIT%\r\n",
        ),
    )
    .unwrap();
    let docker = tmp.path().join("docker.cmd");
    std::fs::write(
        &docker,
        concat!(
            "@echo off\r\n",
            ">>\"%AI_MEMORY_TEST_DOCKER_ARGS%\" echo %*\r\n",
            "echo %* | findstr /c:\"auth-tokens/default\" >nul && echo test-volume-token\r\n",
            "exit /b 0\r\n",
        ),
    )
    .unwrap();

    let mut command = Command::new(ai_memory_test_support::powershell_exe());
    command
        // Same execution-policy note as run_powershell_wrapper above.
        .args([
            "-NoLogo",
            "-NoProfile",
            "-NonInteractive",
            "-ExecutionPolicy",
            "Bypass",
            "-File",
        ])
        .arg(repo_root().join("bin/ai-memory.ps1"))
        .args(args)
        .env("AI_MEMORY_DOCKER", &docker)
        .env("AI_MEMORY_NATIVE_BIN", &native)
        .env("AI_MEMORY_TEST_NATIVE_ARGS", &native_args)
        .env("AI_MEMORY_TEST_NATIVE_EXIT", "7")
        .env("AI_MEMORY_TEST_DOCKER_ARGS", &docker_args)
        .env("AI_MEMORY_DATA_VOLUME", "test-ai-memory-data")
        .env("LOCALAPPDATA", tmp.path())
        .env_remove("AI_MEMORY_DATA_DIR")
        .env_remove("AI_MEMORY_SERVER_URL")
        .env_remove("AI_MEMORY_RUN_AUTOWIRE");
    for (name, value) in extra_env {
        command.env(name, value);
    }
    let output = command.output().unwrap();
    (
        output.status.code(),
        std::fs::read_to_string(&native_args).unwrap_or_default(),
        std::fs::read_to_string(&docker_args).unwrap_or_default(),
    )
}

#[test]
fn macos_docs_use_valid_install_commands_and_release_body_points_to_them() {
    let docs = read_repo("docs/macos.md");
    assert!(docs.contains("install-hooks --agent claude-code --apply"));
    assert!(docs.contains("install-mcp --client claude-code --apply"));
    assert!(
        !docs.contains("setup-agent --agent claude-code --source ./hooks"),
        "setup-agent has no --apply path; use install-hooks for native macOS docs"
    );
    assert!(
        !docs.contains("init` configures the bearer token"),
        "init writes token_pepper, not a bearer token"
    );
    assert!(docs.contains("Host-side agent config should use"));
    assert!(docs.contains("Tagged releases publish a multi-arch manifest"));

    let release = read_repo(".github/workflows/release.yml");
    assert!(release.contains("follow the bundled docs/macos.md"));
}

// ---------------------------------------------------------------------------
// The generated pre-push hook and the Git environment it hands to Cargo.
//
// Git exports the repository-local environment to every hook it runs. The test
// suite builds throwaway Git repositories as fixtures, and a fixture that
// inherits GIT_DIR / GIT_WORK_TREE / GIT_INDEX_FILE runs against the checkout
// being pushed instead of its own temp directory. These tests pin the scrub.
//
// `cargo` here is a fake -- an exported Bash function that records its argv and
// the environment it was given and returns a chosen code. It is an observation
// boundary only: nothing below runs the real test suite. Git's own repository
// and config resolution is never mocked: both fixture repositories are real and
// every probe is a read-only `git` command.
//
// Unix and Windows both run these; on Windows via Git for Windows' bash.exe.
// Every give-up path goes through `skip_pre_push_tests`.
// ---------------------------------------------------------------------------

/// Every external command the installer, the generated hook, and the probes
/// below actually run. Shell builtins (`printf`, `echo`, `command`) are not
/// here; neither is `cargo`, which is an exported function.
#[cfg(any(unix, windows))]
const FIXTURE_TOOLS: &[&str] = &[
    "awk", "bash", "cat", "chmod", "env", "git", "grep", "mkdir", "mktemp", "mv", "sort", "uname",
];

/// The runner's last line, which hands over to the installed hook.
#[cfg(any(unix, windows))]
const RUNNER_EXEC: &str = "exec bash \"$AI_MEMORY_FIXTURE_HOOK\"\n";

/// Give up on the pre-push tests, or refuse to.
///
/// A contributor whose machine lacks Git Bash or a fixture tool gets a skip,
/// matching the convention in `install_hooks`' shell-contract test. On CI that
/// same skip would turn a missing prerequisite into a green Windows job that
/// proved nothing, so there it is a failure with the concrete reason.
#[cfg(any(unix, windows))]
fn skip_pre_push_tests<T>(reason: &str) -> Option<T> {
    let on_ci = ["GITHUB_ACTIONS", "CI"].iter().any(|key| {
        std::env::var_os(key).is_some_and(|value| !value.is_empty() && value != "false")
    });
    assert!(
        !on_ci,
        "the pre-push hook regressions must run on CI, but {reason}"
    );
    eprintln!("skipping pre-push hook tests: {reason}");
    None
}

/// Resolve `tool` against a `PATH`-shaped value, trying the bare name and the
/// `.exe` Windows carries.
#[cfg(any(unix, windows))]
fn resolve_in_path(path: &std::ffi::OsStr, tool: &str) -> Option<PathBuf> {
    std::env::split_paths(path).find_map(|dir| {
        let bare = dir.join(tool);
        if is_executable_candidate(&bare) {
            return Some(bare);
        }
        let exe = dir.join(format!("{tool}.exe"));
        is_executable_candidate(&exe).then_some(exe)
    })
}

#[cfg(any(unix, windows))]
fn is_executable_candidate(path: &Path) -> bool {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt as _;
        std::fs::metadata(path)
            .is_ok_and(|metadata| metadata.is_file() && metadata.permissions().mode() & 0o111 != 0)
    }
    #[cfg(windows)]
    {
        path.is_file()
    }
}

#[test]
#[cfg(unix)]
fn pre_push_fixture_path_skips_non_executable_shadows() {
    use std::os::unix::fs::PermissionsExt as _;

    let temp = tempfile::TempDir::new().unwrap();
    let shadow_dir = temp.path().join("shadow");
    let executable_dir = temp.path().join("executable");
    std::fs::create_dir(&shadow_dir).unwrap();
    std::fs::create_dir(&executable_dir).unwrap();
    let shadow = shadow_dir.join("env");
    std::fs::write(&shadow, "not executable").unwrap();
    std::fs::set_permissions(&shadow, std::fs::Permissions::from_mode(0o644)).unwrap();
    let executable = executable_dir.join("env");
    std::fs::write(&executable, "#!/bin/sh\nexit 0\n").unwrap();
    std::fs::set_permissions(&executable, std::fs::Permissions::from_mode(0o755)).unwrap();
    let path = std::env::join_paths([shadow_dir.as_os_str(), executable_dir.as_os_str()]).unwrap();

    assert_eq!(resolve_in_path(&path, "env"), Some(executable));
}

/// macOS hands back `/var/folders/...` while Git reports `/private/var/...`.
/// Windows canonicalization yields a `\\?\` verbatim path that Git Bash cannot
/// use, so leave it alone there.
#[cfg(unix)]
fn sandbox_root(path: PathBuf) -> PathBuf {
    std::fs::canonicalize(path).unwrap()
}

#[cfg(windows)]
fn sandbox_root(path: PathBuf) -> PathBuf {
    path
}

/// The Git for Windows installation root, derived from wherever `bash.exe`
/// lives. Mirrors the discovery in `install_hooks`' shell-contract test, plus
/// the root implied by a `git.exe` already on `PATH`.
#[cfg(windows)]
fn git_for_windows_root() -> Option<PathBuf> {
    let mut candidates = Vec::new();
    if let Some(root) = std::env::var_os("EXEPATH") {
        candidates.push(PathBuf::from(root));
    }
    for env_key in ["ProgramFiles", "ProgramW6432", "ProgramFiles(x86)"] {
        if let Some(root) = std::env::var_os(env_key) {
            candidates.push(PathBuf::from(root).join("Git"));
        }
    }
    // `<root>/cmd/git.exe` and `<root>/mingw64/bin/git.exe` both sit under the
    // installation root; walk up to it.
    for dir in std::env::split_paths(&std::env::var_os("PATH").unwrap_or_default()) {
        if !dir.join("git.exe").is_file() {
            continue;
        }
        let mut up = dir.clone();
        for _ in 0..2 {
            if !up.pop() {
                break;
            }
            candidates.push(up.clone());
        }
    }
    candidates.sort();
    candidates.dedup();
    candidates
        .into_iter()
        .find(|root| {
            root.join("usr").join("bin").join("bash.exe").is_file()
                || root.join("bin").join("bash.exe").is_file()
        })
        .or_else(|| skip_pre_push_tests("Git for Windows bash.exe was not found"))
}

/// The `PATH` the fixture hands the installer and the hook.
///
/// It must hold every tool those scripts run and must not hold `cargo-nextest`,
/// which the hook branches on. `cargo` is an exported Bash function, so it
/// never needs to be on `PATH` at all.
///
/// Unix: symlink each tool into the fixture's own `bin`. Dropping inherited
/// `PATH` directories instead would cost a contributor with `cargo-nextest`
/// next to `git` or `bash` those unrelated commands.
#[cfg(unix)]
fn fixture_path(bin: &Path) -> Option<std::ffi::OsString> {
    let dirs: Vec<PathBuf> =
        std::env::split_paths(&std::env::var_os("PATH").unwrap_or_default()).collect();
    for tool in FIXTURE_TOOLS {
        let Some(real) = dirs
            .iter()
            .map(|dir| dir.join(tool))
            .find(|candidate| is_executable_candidate(candidate))
        else {
            return skip_pre_push_tests(&format!("{tool} was not found on PATH"));
        };
        std::os::unix::fs::symlink(real, bin.join(tool)).unwrap();
    }
    Some(bin.as_os_str().to_owned())
}

/// Windows: NTFS has no mode bits and MSYS will not treat a plain fixture file
/// as executable, so a curated `bin` of shims is not available. Use the Git for
/// Windows tool directories instead -- they hold every tool in
/// [`FIXTURE_TOOLS`], and `cargo-nextest` (installed under `~/.cargo/bin`) is
/// excluded by construction rather than by filtering the inherited `PATH`.
#[cfg(windows)]
fn fixture_path(_bin: &Path) -> Option<std::ffi::OsString> {
    let root = git_for_windows_root()?;
    let dirs: Vec<PathBuf> = [
        Path::new("usr").join("bin"),
        Path::new("mingw64").join("bin"),
        Path::new("mingw32").join("bin"),
        PathBuf::from("bin"),
        PathBuf::from("cmd"),
    ]
    .iter()
    .map(|relative| root.join(relative))
    .filter(|dir| dir.is_dir())
    .collect();
    Some(std::env::join_paths(dirs).expect("Git for Windows paths must not contain ';'"))
}

#[cfg(any(unix, windows))]
fn log_field<'a>(log: &'a str, key: &str) -> &'a str {
    let prefix = format!("{key}:");
    log.lines()
        .find_map(|line| line.strip_prefix(prefix.as_str()))
        .unwrap_or_else(|| panic!("no `{key}:` line in:\n{log}"))
}

/// Windows temp paths can use an 8.3 alias while Git reports the long name.
/// Resolve both paths before comparing them; invalid paths still fail.
#[cfg(any(unix, windows))]
fn assert_same_directory(reported: &str, expected: &str, context: &str) {
    let resolve = |label: &str, value: &str| {
        std::fs::canonicalize(value).unwrap_or_else(|err| {
            panic!("{context}\nthe {label} path `{value}` is not a directory: {err}")
        })
    };
    assert_eq!(
        resolve("reported", reported),
        resolve("expected", expected),
        "{context}"
    );
}

#[cfg(any(unix, windows))]
fn log_section<'a>(log: &'a str, name: &str) -> &'a str {
    let begin = format!("{name}-begin\n");
    let end = format!("{name}-end\n");
    let start = log
        .find(begin.as_str())
        .unwrap_or_else(|| panic!("no `{name}-begin` in:\n{log}"))
        + begin.len();
    let rest = &log[start..];
    let stop = rest
        .find(end.as_str())
        .unwrap_or_else(|| panic!("no `{name}-end` in:\n{log}"));
    &rest[..stop]
}

/// A retained sandbox for the generated pre-push hook.
///
/// `hook_repo` stands in for the checkout being pushed; `fixture_repo` for the
/// throwaway repository a test would build. Both are real Git repositories in
/// the same temp root, so even the pre-fix hook cannot reach this checkout.
#[cfg(any(unix, windows))]
struct PrePushFixture {
    hook_repo: PathBuf,
    fixture_repo: PathBuf,
    home: PathBuf,
    hook: PathBuf,
    runner: PathBuf,
    cargo_log: PathBuf,
    caller_log: PathBuf,
    global_config: PathBuf,
    system_config: PathBuf,
    path: std::ffi::OsString,
    bash: PathBuf,
    git: PathBuf,
    /// `Some` under Git Bash, where scripts need MSYS paths. `None` on Unix.
    cygpath: Option<PathBuf>,
    hook_toplevel: String,
    fixture_toplevel: String,
    _guard: tempfile::TempDir,
}

#[cfg(any(unix, windows))]
impl PrePushFixture {
    fn new() -> Option<Self> {
        let guard = tempfile::tempdir().unwrap();
        let root = sandbox_root(guard.path().to_path_buf());
        assert!(
            !root.starts_with(repo_root()),
            "the sandbox must live outside this checkout, got {}",
            root.display()
        );

        // Every fixture path carries an apostrophe and a space, so a path
        // interpolated into a shell literal anywhere below would break.
        let work = root.join("it's a sandbox");
        let home = work.join("home");
        let bin = work.join("bin");
        let hook_repo = work.join("hook repo");
        let fixture_repo = work.join("fixture repo");
        for dir in [&home, &bin, &hook_repo, &fixture_repo] {
            std::fs::create_dir_all(dir).unwrap();
        }
        std::fs::create_dir_all(home.join(".config")).unwrap();

        // Synthetic machine config. The caller must keep seeing these; Cargo
        // must not.
        let global_config = work.join("global.gitconfig");
        let system_config = work.join("system.gitconfig");
        std::fs::write(&global_config, "[fixture]\n\tglobal = from-global-config\n").unwrap();
        std::fs::write(&system_config, "[fixture]\n\tsystem = from-system-config\n").unwrap();

        let path = fixture_path(&bin)?;
        for tool in FIXTURE_TOOLS {
            if resolve_in_path(&path, tool).is_none() {
                return skip_pre_push_tests(&format!(
                    "{tool} is missing from the fixture PATH ({})",
                    path.to_string_lossy()
                ));
            }
        }
        if let Some(found) = resolve_in_path(&path, "cargo-nextest") {
            return skip_pre_push_tests(&format!(
                "cargo-nextest is reachable at {}, so the fallback branch is not deterministic",
                found.display()
            ));
        }
        let cygpath = resolve_in_path(&path, "cygpath");
        if cfg!(windows) && cygpath.is_none() {
            return skip_pre_push_tests("cygpath is missing from the Git for Windows PATH");
        }

        let mut fixture = Self {
            bash: resolve_in_path(&path, "bash").expect("checked above"),
            git: resolve_in_path(&path, "git").expect("checked above"),
            hook: hook_repo.join(".git").join("hooks").join("pre-push"),
            runner: work.join("runner.sh"),
            cargo_log: work.join("cargo.log"),
            caller_log: work.join("caller.log"),
            cygpath,
            path,
            hook_repo,
            fixture_repo,
            home,
            global_config,
            system_config,
            hook_toplevel: String::new(),
            fixture_toplevel: String::new(),
            _guard: guard,
        };
        // Do not depend on installed Git templates: relocatable Git binaries
        // can lose their template path through the fixture's tool symlink.
        // Tests that seed a user hook need its directory before installation.
        // https://git-scm.com/docs/git-init#_template_directory
        fixture.git(&fixture.hook_repo, &["init", "-q", "--template="]);
        fixture.git(&fixture.fixture_repo, &["init", "-q", "--template="]);
        std::fs::create_dir_all(fixture.hook.parent().unwrap()).unwrap();
        // Compare Git's own answers rather than Rust path strings: Git Bash
        // reports `C:/...` where Rust reports `C:\...`.
        fixture.hook_toplevel = fixture.git(&fixture.hook_repo, &["rev-parse", "--show-toplevel"]);
        fixture.fixture_toplevel =
            fixture.git(&fixture.fixture_repo, &["rev-parse", "--show-toplevel"]);
        Some(fixture)
    }

    /// Drop the ambient Git environment and point HOME and the global/system
    /// config at the sandbox.
    fn isolate(&self, command: &mut Command) {
        for (key, _) in std::env::vars_os() {
            if key.to_string_lossy().starts_with("GIT_") {
                command.env_remove(&key);
            }
        }
        command
            .env("PATH", &self.path)
            .env("HOME", &self.home)
            .env("USERPROFILE", &self.home)
            .env("XDG_CONFIG_HOME", self.home.join(".config"))
            .env("GIT_CONFIG_GLOBAL", &self.global_config)
            .env("GIT_CONFIG_SYSTEM", &self.system_config);
    }

    /// The paths the generated scripts read. They go through the environment so
    /// no path is ever interpolated into a shell literal.
    fn shell_env(&self, command: &mut Command) {
        command
            .env("AI_MEMORY_FIXTURE_REPO", self.git_arg(&self.fixture_repo))
            .env(
                "AI_MEMORY_FIXTURE_CARGO_LOG",
                self.shell_arg(&self.cargo_log),
            )
            .env(
                "AI_MEMORY_FIXTURE_CALLER_LOG",
                self.shell_arg(&self.caller_log),
            )
            .env("AI_MEMORY_FIXTURE_HOOK", self.shell_arg(&self.hook));
    }

    /// Bash needs MSYS paths for scripts and redirections on Windows.
    fn shell_arg(&self, path: &Path) -> String {
        self.cygpath_arg("-u", path)
    }

    /// Native Git cannot resolve an MSYS `/tmp` mount. Use `C:/...` paths for
    /// its arguments so correctness does not depend on MSYS argument conversion.
    fn git_arg(&self, path: &Path) -> String {
        self.cygpath_arg("-m", path)
    }

    fn cygpath_arg(&self, mode: &str, path: &Path) -> String {
        let Some(cygpath) = &self.cygpath else {
            return path.display().to_string();
        };
        let output = Command::new(cygpath).arg(mode).arg(path).output().unwrap();
        assert!(
            output.status.success(),
            "cygpath {mode} {} failed: {}",
            path.display(),
            String::from_utf8_lossy(&output.stderr)
        );
        String::from_utf8(output.stdout).unwrap().trim().to_string()
    }

    fn git(&self, dir: &Path, args: &[&str]) -> String {
        let mut command = Command::new(&self.git);
        self.isolate(&mut command);
        let output = command.args(args).current_dir(dir).output().unwrap();
        assert!(
            output.status.success(),
            "git {args:?} in {} failed: {}",
            dir.display(),
            String::from_utf8_lossy(&output.stderr)
        );
        String::from_utf8(output.stdout).unwrap().trim().to_string()
    }

    /// A read-only probe block, reused by the fake Cargo and by the user hook.
    fn probe(&self, name: &str) -> String {
        let log = if name == "cargo" {
            "AI_MEMORY_FIXTURE_CARGO_LOG"
        } else {
            "AI_MEMORY_FIXTURE_CALLER_LOG"
        };
        format!(
            r#"{{
  printf '{name}-begin\n'
  env | grep '^GIT_' | LC_ALL=C sort
  printf '{name}-end\n'
  printf '{name}-ifs:[%s]\n' "${{IFS-<unset>}}"
  printf '{name}-opts:[%s]\n' "$-"
  printf '{name}-pipefail:[%s]\n' "$([[ -o pipefail ]] && echo on || echo off)"
  printf '{name}-ssl:[%s]\n' "${{SSL_CERT_FILE-<unset>}}"
  printf '{name}-toplevel:%s\n' "$(git -C "$AI_MEMORY_FIXTURE_REPO" rev-parse --show-toplevel 2>&1)"
  printf '{name}-injected:%s\n' "$(git -C "$AI_MEMORY_FIXTURE_REPO" config --get fixture.injected 2>&1)"
  printf '{name}-counted:%s\n' "$(git -C "$AI_MEMORY_FIXTURE_REPO" config --get fixture.counted 2>&1)"
  printf '{name}-global:%s\n' "$(git -C "$AI_MEMORY_FIXTURE_REPO" config --get fixture.global 2>&1)"
  printf '{name}-system:%s\n' "$(git -C "$AI_MEMORY_FIXTURE_REPO" config --get fixture.system 2>&1)"
}} >> "${log}" 2>&1
"#
        )
    }

    /// The runner that stands in for Cargo and then execs the installed hook.
    ///
    /// Both fakes are exported Bash functions: a function needs no execute bit,
    /// which NTFS cannot give a fixture file under Git Bash, and it lets the
    /// hook's `command -v cargo-nextest` probe answer what the test asked for.
    fn write_runner(&self, exit_code: i32, with_nextest: bool) {
        let mut runner = format!(
            "#!/usr/bin/env bash\n\
             cargo() {{\n\
             \x20 printf 'cargo-argv:%s\\n' \"$*\" >> \"$AI_MEMORY_FIXTURE_CARGO_LOG\"\n\
             {probe}\
             \x20 return {exit_code}\n\
             }}\n\
             export -f cargo\n",
            probe = self.probe("cargo"),
        );
        if with_nextest {
            runner.push_str("cargo-nextest() { return 0; }\nexport -f cargo-nextest\n");
        }
        runner.push_str(RUNNER_EXEC);
        std::fs::write(&self.runner, runner).unwrap();
    }

    /// Make the hook's `git rev-parse --local-env-vars` fail with `code`. The
    /// exported function shadows `git` for that one call and defers to the real
    /// binary for everything else, including the probes.
    fn fail_local_env_vars(&self, code: i32) {
        let runner = std::fs::read_to_string(&self.runner).unwrap();
        let prelude = runner
            .strip_suffix(RUNNER_EXEC)
            .expect("write_runner must run first");
        let git = format!(
            "git() {{\n\
             \x20 if [ \"$1\" = rev-parse ] && [ \"$2\" = --local-env-vars ]; then return {code}; fi\n\
             \x20 command git \"$@\"\n\
             }}\n\
             export -f git\n"
        );
        std::fs::write(&self.runner, format!("{prelude}{git}{RUNNER_EXEC}")).unwrap();
    }

    /// Seed a user hook whose own content precedes the managed block.
    fn write_user_hook(&self, prelude: &str) {
        std::fs::write(
            &self.hook,
            format!(
                "#!/usr/bin/env bash\n# A user hook that predates the managed block.\n{prelude}{}",
                self.probe("before"),
            ),
        )
        .unwrap();
    }

    /// Append the user's trailing content after the managed block's end marker.
    fn append_after_managed_block(&self) {
        let installed = std::fs::read_to_string(&self.hook).unwrap();
        assert!(
            installed
                .trim_end()
                .ends_with("# <<< ai-memory pre-push <<<"),
            "the managed block should close the installed hook:\n{installed}"
        );
        std::fs::write(&self.hook, format!("{installed}{}", self.probe("after"))).unwrap();
    }

    fn install_hook(&self) {
        self.install_hook_from(&self.hook_repo);
    }

    fn install_hook_from(&self, cwd: &Path) {
        let mut command = Command::new(&self.bash);
        command.arg(self.shell_arg(&repo_root().join("scripts/install-git-hooks.sh")));
        self.isolate(&mut command);
        let output = command.current_dir(cwd).output().unwrap();
        assert!(
            output.status.success(),
            "installer failed: stdout={} stderr={}",
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );
    }

    /// Run the installed hook with exactly the repository-local environment Git
    /// exports to a pre-push hook. Every injected path is inside the sandbox.
    fn run_hook(&self) -> std::process::Output {
        let mut command = Command::new(&self.bash);
        command.arg(self.shell_arg(&self.runner));
        self.isolate(&mut command);
        self.shell_env(&mut command);
        command
            .current_dir(&self.hook_repo)
            .env("GIT_DIR", self.hook_repo.join(".git"))
            .env("GIT_COMMON_DIR", self.hook_repo.join(".git"))
            .env("GIT_WORK_TREE", &self.hook_repo)
            .env("GIT_INDEX_FILE", self.hook_repo.join(".git").join("index"))
            .env("GIT_PREFIX", "")
            .env(
                "GIT_CONFIG_PARAMETERS",
                "'fixture.injected=from-parameters'",
            )
            .env("GIT_CONFIG_COUNT", "1")
            .env("GIT_CONFIG_KEY_0", "fixture.counted")
            .env("GIT_CONFIG_VALUE_0", "from-count");
        command.output().unwrap()
    }

    fn cargo_log(&self) -> String {
        std::fs::read_to_string(&self.cargo_log).unwrap_or_default()
    }

    fn caller_log(&self) -> String {
        std::fs::read_to_string(&self.caller_log).unwrap_or_default()
    }
}

#[test]
#[cfg(any(unix, windows))]
fn pre_push_hook_scrubs_the_repository_git_environment_before_cargo() {
    let Some(fixture) = PrePushFixture::new() else {
        return;
    };
    fixture.write_runner(0, false);
    fixture.install_hook();

    let output = fixture.run_hook();
    assert!(
        output.status.success(),
        "hook failed: stdout={} stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );

    let log = fixture.cargo_log();
    let git_env = log_section(&log, "cargo");
    for leaked in [
        "GIT_DIR=",
        "GIT_COMMON_DIR=",
        "GIT_WORK_TREE=",
        "GIT_INDEX_FILE=",
        "GIT_CONFIG_PARAMETERS=",
        "GIT_CONFIG_COUNT=",
        "GIT_PREFIX=",
    ] {
        assert!(
            !git_env.contains(leaked),
            "{leaked} reached cargo; a fixture repo would act on the pushed checkout:\n{git_env}"
        );
    }
    for expected in [
        "GIT_CONFIG_NOSYSTEM=1",
        "GIT_CONFIG_GLOBAL=/dev/null",
        "GIT_CONFIG_SYSTEM=/dev/null",
    ] {
        assert!(
            git_env.contains(expected),
            "cargo should run with {expected}:\n{git_env}"
        );
    }

    // The decisive check: a child `git` aimed at the fixture repository must
    // land there, not in the repository whose hook is running.
    assert_same_directory(
        log_field(&log, "cargo-toplevel"),
        &fixture.fixture_toplevel,
        &format!("child git resolved the wrong repository:\n{log}"),
    );
    for (key, source) in [
        ("cargo-injected", "GIT_CONFIG_PARAMETERS"),
        ("cargo-counted", "GIT_CONFIG_COUNT"),
        ("cargo-global", "the global config"),
        ("cargo-system", "the system config"),
    ] {
        assert_eq!(
            log_field(&log, key),
            "",
            "{source} still reached the fixture repository:\n{log}"
        );
    }
}

#[test]
#[cfg(any(unix, windows))]
fn pre_push_hook_leaves_the_user_hook_environment_intact_around_the_block() {
    let Some(fixture) = PrePushFixture::new() else {
        return;
    };
    fixture.write_runner(0, false);
    fixture.write_user_hook("");
    fixture.install_hook();
    fixture.append_after_managed_block();

    let output = fixture.run_hook();
    assert!(
        output.status.success(),
        "hook failed: stdout={} stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );

    let log = fixture.caller_log();
    for stage in ["before", "after"] {
        let git_env = log_section(&log, stage);
        for kept in ["GIT_DIR=", "GIT_INDEX_FILE=", "GIT_CONFIG_COUNT=1"] {
            assert!(
                git_env.contains(kept),
                "the scrub escaped the managed block and took {kept} from the {stage} content:\n{git_env}"
            );
        }
        // The scrub's own settings must not leak outward either: the caller
        // keeps its synthetic global/system config and never sees NOSYSTEM.
        assert!(
            !git_env.contains("GIT_CONFIG_GLOBAL=/dev/null")
                && !git_env.contains("GIT_CONFIG_SYSTEM=/dev/null"),
            "the {stage} content must keep the caller's own config paths:\n{git_env}"
        );
        assert!(
            !git_env.contains("GIT_CONFIG_NOSYSTEM"),
            "the {stage} content must not inherit the scrub's GIT_CONFIG_NOSYSTEM:\n{git_env}"
        );
        // The caller's Git still resolves through the inherited GIT_DIR, which
        // points at the hook's repository rather than the fixture repository
        // the command names.
        assert_same_directory(
            log_field(&log, &format!("{stage}-toplevel")),
            &fixture.hook_toplevel,
            &format!("the {stage} content lost the repository Git gave it:\n{log}"),
        );
        for (key, expected, source) in [
            ("global", "from-global-config", "the global config"),
            ("system", "from-system-config", "the system config"),
            ("injected", "from-parameters", "GIT_CONFIG_PARAMETERS"),
            ("counted", "from-count", "GIT_CONFIG_COUNT"),
        ] {
            assert_eq!(
                log_field(&log, &format!("{stage}-{key}")),
                expected,
                "the {stage} content lost {source}:\n{log}"
            );
        }
    }
    // The block's own shell options and exports stay inside it too: the block
    // is replaced in place, so user commands after it run with whatever it
    // leaves behind.
    for key in ["opts", "pipefail", "ssl"] {
        assert_eq!(
            log_field(&log, &format!("after-{key}")),
            log_field(&log, &format!("before-{key}")),
            "the managed block's {key} leaked into the trailing user content:\n{log}"
        );
    }
}

#[test]
#[cfg(any(unix, windows))]
fn pre_push_hook_reinstall_keeps_user_content_and_one_managed_block() {
    let Some(fixture) = PrePushFixture::new() else {
        return;
    };
    fixture.write_runner(0, false);
    fixture.write_user_hook("printf 'order-before\\n'\n");
    fixture.install_hook();
    fixture.append_after_managed_block();
    let original = std::fs::read_to_string(&fixture.hook).unwrap();
    let original = format!("{original}printf 'order-after\\n'\n");
    std::fs::write(&fixture.hook, &original).unwrap();
    fixture.install_hook();

    let installed = std::fs::read_to_string(&fixture.hook).unwrap();
    assert_eq!(
        installed, original,
        "reinstalling changed the hook's layout"
    );
    assert_eq!(
        installed.matches("# >>> ai-memory pre-push >>>").count(),
        1,
        "reinstalling duplicated the managed block:\n{installed}"
    );
    assert_eq!(
        installed.matches("printf 'before-begin\\n'").count(),
        1,
        "reinstalling lost or duplicated the user's leading content:\n{installed}"
    );
    assert_eq!(
        installed.matches("printf 'after-begin\\n'").count(),
        1,
        "reinstalling lost or duplicated the user's trailing content:\n{installed}"
    );

    let output = fixture.run_hook();
    assert!(
        output.status.success(),
        "reinstalled hook failed: stdout={} stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    let log = fixture.cargo_log();
    assert_same_directory(
        log_field(&log, "cargo-toplevel"),
        &fixture.fixture_toplevel,
        "the scrub did not survive a reinstall",
    );
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.find("order-before").unwrap() < stdout.find("pre-push: cargo").unwrap());
    assert!(stdout.find("pre-push: cargo").unwrap() < stdout.find("order-after").unwrap());
    fixture.write_runner(29, false);
    let failed = fixture.run_hook();
    assert_eq!(failed.status.code(), Some(29));
    assert!(!String::from_utf8_lossy(&failed.stdout).contains("order-after"));
}

#[test]
#[cfg(any(unix, windows))]
fn pre_push_hook_stops_before_cargo_and_user_content_when_the_scrub_fails() {
    let Some(fixture) = PrePushFixture::new() else {
        return;
    };
    fixture.write_runner(0, false);
    fixture.fail_local_env_vars(23);
    // No `set -e` in the user hook: the block alone has to stop the push.
    fixture.write_user_hook("");
    fixture.install_hook();
    fixture.append_after_managed_block();

    let output = fixture.run_hook();
    assert_eq!(
        output.status.code(),
        Some(23),
        "a failed scrub must fail the hook: stdout={} stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        !fixture.cargo_log().contains("cargo-argv:"),
        "Cargo ran without the scrub:\n{}",
        fixture.cargo_log()
    );
    let caller = fixture.caller_log();
    assert!(
        caller.contains("before-begin") && !caller.contains("after-begin"),
        "the user's trailing content ran after the block failed:\n{caller}"
    );
}

#[test]
#[cfg(any(unix, windows))]
fn pre_push_hook_installs_from_a_linked_worktree_subdirectory() {
    let Some(fixture) = PrePushFixture::new() else {
        return;
    };
    fixture.git(
        &fixture.hook_repo,
        &[
            "-c",
            "user.name=Fixture",
            "-c",
            "[email protected]",
            "commit",
            "--allow-empty",
            "--no-gpg-sign",
            "-qm",
            "fixture",
        ],
    );
    let linked = fixture.hook_repo.parent().unwrap().join("linked checkout");
    fixture.git(
        &fixture.hook_repo,
        &["worktree", "add", "--detach", &fixture.git_arg(&linked)],
    );
    let nested = linked.join("nested");
    std::fs::create_dir(&nested).unwrap();
    assert!(linked.join(".git").is_file());
    fixture.install_hook_from(&nested);
    assert!(
        fixture.hook.is_file(),
        "linked worktrees must use the shared hook"
    );
    fixture.install_hook_from(&fixture.hook_repo);

    fixture.write_runner(0, false);
    let runner = std::fs::read_to_string(&fixture.runner).unwrap();
    std::fs::write(
        &fixture.runner,
        runner.replace(
            "exec bash \"$AI_MEMORY_FIXTURE_HOOK\"",
            "exec git hook run pre-push",
        ),
    )
    .unwrap();
    let mut command = Command::new(&fixture.bash);
    command.arg(fixture.shell_arg(&fixture.runner));
    fixture.isolate(&mut command);
    fixture.shell_env(&mut command);
    let output = command.current_dir(&nested).output().unwrap();
    assert!(
        output.status.success(),
        "Git failed to run the shared hook: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert_eq!(
        log_field(&fixture.cargo_log(), "cargo-argv"),
        "test --workspace --all-targets"
    );
    assert_same_directory(
        log_field(&fixture.cargo_log(), "cargo-toplevel"),
        &fixture.fixture_toplevel,
        "the shared hook must isolate Cargo's Git environment",
    );
}

#[test]
#[cfg(any(unix, windows))]
fn pre_push_hook_refuses_configured_hooks_paths_without_writing() {
    for (scope, value) in [
        ("--local", ""),
        ("--local", ".git/hooks"),
        ("--local", "/dev/null"),
        ("--local", "custom hooks"),
        ("--global", "absolute"),
    ] {
        let Some(fixture) = PrePushFixture::new() else {
            return;
        };
        let shared = fixture.home.join("shared hooks");
        std::fs::create_dir(&shared).unwrap();
        let shared_hook = shared.join("pre-push");
        std::fs::write(&shared_hook, "#!/bin/sh\necho user-owned\n").unwrap();
        let path = if value == "absolute" {
            fixture.git_arg(&shared)
        } else {
            value.to_owned()
        };
        fixture.git(
            &fixture.hook_repo,
            &["config", scope, "core.hooksPath", &path],
        );
        let mut command = Command::new(&fixture.bash);
        command.arg(fixture.shell_arg(&repo_root().join("scripts/install-git-hooks.sh")));
        fixture.isolate(&mut command);
        let output = command.current_dir(&fixture.hook_repo).output().unwrap();
        assert!(!output.status.success());
        assert!(String::from_utf8_lossy(&output.stderr).contains("core.hooksPath is set"));
        assert!(!fixture.hook.exists());
        assert!(!fixture.hook_repo.join("custom hooks").exists());
        assert_eq!(
            std::fs::read_to_string(shared_hook).unwrap(),
            "#!/bin/sh\necho user-owned\n"
        );
        assert_eq!(
            fixture.git(&fixture.hook_repo, &["config", "--get", "core.hooksPath"]),
            path
        );
    }
}

#[test]
#[cfg(any(unix, windows))]
fn pre_push_hook_refuses_ambiguous_markers_without_replacing_the_user_hook() {
    let begin = "# >>> ai-memory pre-push >>>";
    let end = "# <<< ai-memory pre-push <<<";
    for body in [
        format!("{begin}\necho user-owned\n"),
        format!("echo user-owned\n{end}\n"),
        format!("{begin}\n{end}\n{begin}\n{end}\n"),
    ] {
        let Some(fixture) = PrePushFixture::new() else {
            return;
        };
        std::fs::write(&fixture.hook, &body).unwrap();
        let mut command = Command::new(&fixture.bash);
        command.arg(fixture.shell_arg(&repo_root().join("scripts/install-git-hooks.sh")));
        fixture.isolate(&mut command);
        let output = command.current_dir(&fixture.hook_repo).output().unwrap();
        assert!(!output.status.success());
        assert!(String::from_utf8_lossy(&output.stderr).contains("invalid managed markers"));
        assert_eq!(std::fs::read_to_string(&fixture.hook).unwrap(), body);
    }
}

#[test]
#[cfg(any(unix, windows))]
fn pre_push_hook_replaces_crlf_markers_and_stale_block_in_place() {
    let Some(fixture) = PrePushFixture::new() else {
        return;
    };
    let before = "#!/usr/bin/env bash\r\nprintf 'before\\n'\r\n";
    let after = "printf 'after\\n'\r\n";
    std::fs::write(&fixture.hook, format!("{before}# >>> ai-memory pre-push >>>\r\necho stale-block\r\n# <<< ai-memory pre-push <<<\r\n{after}")).unwrap();
    fixture.install_hook();
    let installed = std::fs::read_to_string(&fixture.hook).unwrap();
    assert!(
        installed.starts_with(before),
        "changed prefix: {installed:?}"
    );
    assert!(installed.ends_with(after), "changed suffix: {installed:?}");
    assert!(!installed.contains("stale-block"));
    assert!(installed.contains("git rev-parse --local-env-vars"));
    fixture.install_hook();
    assert_eq!(std::fs::read_to_string(&fixture.hook).unwrap(), installed);
}

#[test]
#[cfg(unix)]
fn pre_push_hook_preserves_an_unreadable_existing_hook() {
    use std::os::unix::fs::PermissionsExt;
    let Some(fixture) = PrePushFixture::new() else {
        return;
    };
    let original = "#!/usr/bin/env bash\necho user-owned\n";
    std::fs::write(&fixture.hook, original).unwrap();
    std::fs::set_permissions(&fixture.hook, std::fs::Permissions::from_mode(0o200)).unwrap();
    if std::fs::File::open(&fixture.hook).is_ok() {
        skip_pre_push_tests::<()>("the process can read a write-only file");
        return;
    }
    let mut command = Command::new(&fixture.bash);
    command.arg(fixture.shell_arg(&repo_root().join("scripts/install-git-hooks.sh")));
    fixture.isolate(&mut command);
    let output = command.current_dir(&fixture.hook_repo).output().unwrap();
    std::fs::set_permissions(&fixture.hook, std::fs::Permissions::from_mode(0o600)).unwrap();
    assert!(
        !output.status.success(),
        "a read error must abort installation"
    );
    assert_eq!(std::fs::read_to_string(&fixture.hook).unwrap(), original);
}

/// A user hook that narrowed `IFS` before the managed block used to break the
/// split of `git rev-parse --local-env-vars`, leaving the whole newline-joined
/// list as one word.
#[test]
#[cfg(any(unix, windows))]
fn pre_push_hook_splits_the_local_env_var_list_under_a_narrowed_ifs() {
    let Some(fixture) = PrePushFixture::new() else {
        return;
    };
    fixture.write_runner(0, false);
    fixture.write_user_hook("IFS=:\n");
    fixture.install_hook();
    fixture.append_after_managed_block();

    let output = fixture.run_hook();
    assert!(
        output.status.success(),
        "the hook did not survive a narrowed IFS: stdout={} stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    let log = fixture.cargo_log();
    assert_same_directory(
        log_field(&log, "cargo-toplevel"),
        &fixture.fixture_toplevel,
        "the scrub did not survive a narrowed IFS",
    );

    let caller = fixture.caller_log();
    for stage in ["before", "after"] {
        assert_eq!(
            log_field(&caller, &format!("{stage}-ifs")),
            "[:]",
            "the managed block changed the user hook's IFS:\n{caller}"
        );
    }
}

#[test]
#[cfg(any(unix, windows))]
fn pre_push_hook_keeps_the_full_tier_and_propagates_a_cargo_failure() {
    for (with_nextest, expected_argv, expected_notice) in [
        (
            true,
            "nextest run --workspace -P full",
            "pre-push: cargo nextest run --workspace -P full",
        ),
        (
            false,
            "test --workspace --all-targets",
            "pre-push: cargo test --workspace --all-targets (nextest not installed)",
        ),
    ] {
        let Some(fixture) = PrePushFixture::new() else {
            return;
        };
        fixture.write_runner(0, with_nextest);
        fixture.install_hook();

        let output = fixture.run_hook();
        assert!(
            output.status.success(),
            "hook failed with {expected_argv:?}: stdout={} stderr={}",
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );
        assert_eq!(
            log_field(&fixture.cargo_log(), "cargo-argv"),
            expected_argv,
            "wrong cargo invocation for nextest={with_nextest}"
        );
        assert!(
            String::from_utf8_lossy(&output.stdout).contains(expected_notice),
            "the hook stopped announcing what it runs: {}",
            String::from_utf8_lossy(&output.stdout)
        );
    }

    // A failing test run must still stop the push.
    let Some(fixture) = PrePushFixture::new() else {
        return;
    };
    fixture.write_runner(3, true);
    fixture.install_hook();
    let output = fixture.run_hook();
    assert_eq!(
        output.status.code(),
        Some(3),
        "the subshell swallowed cargo's exit status: stdout={} stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}

/// The slow tier. These drive the real wrapper scripts, a fake `uname`, and
/// fake container engines, 10-20s each, and cannot break from an ordinary Rust
/// edit. `.config/nextest.toml` skips every `slow::` module in the everyday
/// profile; `-P full`, the pre-push hook, and CI run them.
mod slow {
    #![allow(unused_imports)]
    use super::*;

    #[cfg(unix)]
    #[test]
    fn macos_wrapper_routes_urls_by_real_subcommand() {
        for subcommand in ["install-mcp", "install-hooks", "setup-agent"] {
            let args = run_wrapper_on_fake_macos(&[subcommand]);
            assert!(
                !args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
                "{subcommand} renders host-side config and must keep loopback defaults; got {args}"
            );
        }

        let args = run_wrapper_on_fake_macos(&["status"]);
        assert!(
            args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
            "thin-client commands must reach the host server through Docker Desktop; got {args}"
        );

        let args = run_wrapper_on_fake_macos(&["search", "install-hooks"]);
        assert!(
            args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
            "only the actual subcommand should control URL routing; got {args}"
        );

        let args = run_wrapper_on_fake_macos(&["--config", "/tmp/config.toml", "install-hooks"]);
        assert!(
            !args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
            "global options before install-hooks must not hide the real subcommand; got {args}"
        );
    }

    #[cfg(unix)]
    #[test]
    fn wrapper_keeps_stdin_attached_when_it_is_a_pipe() {
        let args = run_wrapper_with_piped_stdin(
            &["write-page", "--path", "notes/x.md", "--body", "-"],
            "# body that must survive the container boundary\n",
        );
        let flags: Vec<&str> = args.lines().collect();

        // Without `-i` docker gives the container a closed stdin, so `--body -`
        // reads an empty string and the page is persisted with frontmatter only —
        // silently, because the CLI still reports a successful write.
        assert!(
            flags.contains(&"-i"),
            "piped stdin must stay attached for `--body -`; got {args}"
        );
        // A pipe is not a terminal: asking for a TTY here makes docker fail with
        // "the input device is not a TTY".
        assert!(
            !flags.contains(&"-t") && !flags.contains(&"-it"),
            "no TTY may be requested when stdin is a pipe; got {args}"
        );
        assert!(
            flags
                .iter()
                .any(|arg| arg.starts_with("AI_MEMORY_SCOPE_CWD=/scope")),
            "an outside-home checkout must expose its bounded marker path; got {args}"
        );
        assert!(
            flags.iter().any(|arg| arg.ends_with(":/scope:ro")),
            "the marker scope mount must be read-only; got {args}"
        );
    }

    #[cfg(unix)]
    #[test]
    fn wrapper_self_upgrade_rejects_a_checksum_mismatch() {
        let tmp = tempfile::tempdir().unwrap();
        let bin_dir = tmp.path().join("bin");
        std::fs::create_dir_all(&bin_dir).unwrap();
        let wrapper = bin_dir.join("ai-memory");
        let original = read_repo("bin/ai-memory");
        std::fs::write(&wrapper, &original).unwrap();

        let payload = tmp.path().join("hostile-wrapper");
        std::fs::write(
            &payload,
            "#!/usr/bin/env bash\nprintf 'hostile payload executed\\n' >&2\nexit 91\n",
        )
        .unwrap();
        let curl = bin_dir.join("curl");
        std::fs::write(
            &curl,
            "#!/usr/bin/env bash\n\
         set -euo pipefail\n\
         url=''\n\
         out=''\n\
         while [ \"$#\" -gt 0 ]; do\n\
           case \"$1\" in\n\
             -o) out=\"$2\"; shift 2 ;;\n\
             -*) shift ;;\n\
             *) url=\"$1\"; shift ;;\n\
           esac\n\
         done\n\
         case \"$url\" in\n\
           *.sha256) printf '%064d  ai-memory-wrapper\\n' 0 > \"$out\" ;;\n\
           *) cp \"$FAKE_WRAPPER_PAYLOAD\" \"$out\" ;;\n\
         esac\n",
        )
        .unwrap();
        let docker = bin_dir.join("docker");
        std::fs::write(&docker, "#!/usr/bin/env bash\nexit 0\n").unwrap();
        {
            use std::os::unix::fs::PermissionsExt as _;
            for path in [&wrapper, &payload, &curl, &docker] {
                std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).unwrap();
            }
        }

        let path = format!(
            "{}:{}",
            shell_path(&bin_dir),
            std::env::var("PATH").unwrap_or_default()
        );
        let output = freshly_written_shell_script_command(&wrapper)
            .arg("upgrade")
            .env("PATH", path)
            .env("HOME", tmp.path())
            .env("XDG_DATA_HOME", tmp.path().join("data"))
            .env("AI_MEMORY_DOCKER", &docker)
            .env(
                "AI_MEMORY_WRAPPER_URL",
                "https://example.invalid/ai-memory-wrapper",
            )
            .env("FAKE_WRAPPER_PAYLOAD", &payload)
            .output()
            .unwrap();
        assert!(
            output.status.success(),
            "upgrade failed: stdout={} stderr={}",
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );
        let stdout = String::from_utf8_lossy(&output.stdout);
        assert!(stdout.contains("wrapper checksum mismatch; refusing update"));
        assert!(!String::from_utf8_lossy(&output.stderr).contains("hostile payload executed"));
        assert_eq!(std::fs::read_to_string(&wrapper).unwrap(), original);
    }

    #[cfg(unix)]
    #[test]
    fn wrapper_self_upgrade_installs_and_runs_a_verified_payload() {
        let tmp = tempfile::tempdir().unwrap();
        let bin_dir = tmp.path().join("bin");
        std::fs::create_dir_all(&bin_dir).unwrap();
        let wrapper = bin_dir.join("ai-memory");
        std::fs::write(&wrapper, read_repo("bin/ai-memory")).unwrap();

        let payload = tmp.path().join("verified-wrapper");
        let payload_body = "#!/usr/bin/env bash\nprintf 'verified wrapper executed\\n'\n";
        std::fs::write(&payload, payload_body).unwrap();
        let curl = bin_dir.join("curl");
        std::fs::write(
            &curl,
            "#!/usr/bin/env bash\n\
         set -euo pipefail\n\
         url=''\n\
         out=''\n\
         while [ \"$#\" -gt 0 ]; do\n\
           case \"$1\" in\n\
             -o) out=\"$2\"; shift 2 ;;\n\
             -*) shift ;;\n\
             *) url=\"$1\"; shift ;;\n\
           esac\n\
         done\n\
         case \"$url\" in\n\
           *.sha256) printf '%s  ai-memory-wrapper\\n' \"$FAKE_WRAPPER_CHECKSUM\" > \"$out\" ;;\n\
           *) cp \"$FAKE_WRAPPER_PAYLOAD\" \"$out\" ;;\n\
         esac\n",
        )
        .unwrap();
        {
            use std::os::unix::fs::PermissionsExt as _;
            for path in [&wrapper, &payload, &curl] {
                std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).unwrap();
            }
        }

        let path = format!(
            "{}:{}",
            shell_path(&bin_dir),
            std::env::var("PATH").unwrap_or_default()
        );
        let output = freshly_written_shell_script_command(&wrapper)
            .arg("upgrade")
            .env("PATH", path)
            .env("HOME", tmp.path())
            .env(
                "AI_MEMORY_WRAPPER_URL",
                "https://example.invalid/ai-memory-wrapper",
            )
            .env("FAKE_WRAPPER_PAYLOAD", &payload)
            .env("FAKE_WRAPPER_CHECKSUM", sha256_file(&payload))
            .output()
            .unwrap();
        assert!(
            output.status.success(),
            "upgrade failed: stdout={} stderr={}",
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );
        assert!(String::from_utf8_lossy(&output.stdout).contains("verified wrapper executed"));
        assert_eq!(std::fs::read_to_string(&wrapper).unwrap(), payload_body);
    }

    #[cfg(unix)]
    #[test]
    fn hook_installer_rejects_a_checksum_mismatch_before_writing_scripts() {
        let tmp = tempfile::tempdir().unwrap();
        let bin_dir = tmp.path().join("bin");
        std::fs::create_dir_all(&bin_dir).unwrap();
        let curl = bin_dir.join("curl");
        std::fs::write(
            &curl,
            "#!/usr/bin/env bash\n\
         set -euo pipefail\n\
         url=''\n\
         out=''\n\
         while [ \"$#\" -gt 0 ]; do\n\
           case \"$1\" in\n\
             -o) out=\"$2\"; shift 2 ;;\n\
             -*) shift ;;\n\
             *) url=\"$1\"; shift ;;\n\
           esac\n\
         done\n\
         case \"$url\" in\n\
           *.sha256) printf '%064d  ai-memory-hooks.tar.gz\\n' 0 > \"$out\" ;;\n\
           *) printf 'not the expected archive' > \"$out\" ;;\n\
         esac\n",
        )
        .unwrap();
        {
            use std::os::unix::fs::PermissionsExt as _;
            std::fs::set_permissions(&curl, std::fs::Permissions::from_mode(0o755)).unwrap();
        }
        let path = format!(
            "{}:{}",
            shell_path(&bin_dir),
            std::env::var("PATH").unwrap_or_default()
        );
        let destination = tmp.path().join("hooks");
        let output = shell_script_command(&repo_root().join("scripts/install-hooks.sh"))
            .args(["--agent", "claude-code", "--to"])
            .arg(&destination)
            .env("PATH", path)
            .env("HOME", tmp.path())
            .output()
            .unwrap();
        assert!(
            !output.status.success(),
            "checksum mismatch must fail closed"
        );
        assert!(
            String::from_utf8_lossy(&output.stderr)
                .contains("hook bundle checksum mismatch; refusing installation")
        );
        let agent_dir = destination.join("claude-code");
        assert!(
            !agent_dir.exists() || std::fs::read_dir(agent_dir).unwrap().next().is_none(),
            "no hook script may be written before archive verification"
        );
    }

    #[cfg(unix)]
    #[test]
    fn hook_installer_writes_only_expected_files_from_a_verified_archive() {
        const HOOKS: &[&str] = &[
            "post-tool-use",
            "pre-compact",
            "pre-tool-use",
            "session-end",
            "session-start",
            "stop",
            "subagent-start",
            "subagent-stop",
            "user-prompt-submit",
        ];

        let names = installed_hook_names("claude-code", "claude-code", HOOKS);
        let expected = HOOKS
            .iter()
            .map(|hook| format!("{hook}.sh"))
            .collect::<Vec<_>>();
        assert_eq!(names, expected);
    }

    #[cfg(unix)]
    #[test]
    fn hook_installer_writes_only_command_code_stable_events() {
        const HOOKS: &[&str] = &["post-tool-use", "pre-tool-use", "session-start", "stop"];

        let names = installed_hook_names("cmdc", "command-code", HOOKS);
        let expected = HOOKS
            .iter()
            .map(|hook| format!("{hook}.sh"))
            .collect::<Vec<_>>();
        assert_eq!(names, expected);
    }

    // The installer lists each agent's scripts by hand, so a bundle that gains
    // or loses a script leaves the list stale: a script the list still asks for
    // aborts the install, and one it never asks for is silently not installed.
    // Feeding it the file names the repository bundles really ship catches both.
    #[cfg(unix)]
    #[test]
    fn hook_installer_installs_every_script_each_shipped_bundle_has() {
        for agent in [
            "claude-code",
            "codex",
            "cursor",
            "gemini-cli",
            "antigravity-cli",
            "grok",
            "kimi-code",
            "kiro-cli",
            "command-code",
        ] {
            let mut shipped = std::fs::read_dir(repo_root().join("hooks").join(agent))
                .unwrap()
                .filter_map(|entry| {
                    let name = entry.unwrap().file_name().into_string().unwrap();
                    name.strip_suffix(".sh").map(str::to_owned)
                })
                .collect::<Vec<_>>();
            shipped.sort();
            let hooks = shipped.iter().map(String::as_str).collect::<Vec<_>>();
            let expected = shipped
                .iter()
                .map(|hook| format!("{hook}.sh"))
                .collect::<Vec<_>>();

            let installed = installed_hook_names(agent, agent, &hooks);
            assert_eq!(installed, expected, "{agent}");
        }
    }

    #[cfg(unix)]
    #[test]
    fn managed_host_commands_use_native_path_and_remote_server_without_docker() {
        let tmp = tempfile::tempdir().unwrap();
        let native = tmp.path().join("native-ai-memory");
        let docker = tmp.path().join("docker");
        let record = tmp.path().join("native-record.txt");
        let docker_record = tmp.path().join("docker-record.txt");
        std::fs::write(
        &native,
        format!(
            "#!/usr/bin/env bash\n\
             printf 'server=%s\\nauth=%s\\npath=%s\\n' \"$AI_MEMORY_SERVER_URL\" \"$AI_MEMORY_AUTH_TOKEN\" \"$PATH\" > {}\n\
             printf 'arg=%s\\n' \"$@\" >> {}\n",
            shell_path(&record),
            shell_path(&record)
        ),
    )
    .unwrap();
        std::fs::write(
            &docker,
            format!(
                "#!/usr/bin/env bash\nprintf '%s\\n' \"$@\" > {}\nexit 99\n",
                shell_path(&docker_record)
            ),
        )
        .unwrap();
        {
            use std::os::unix::fs::PermissionsExt as _;
            std::fs::set_permissions(&native, std::fs::Permissions::from_mode(0o755)).unwrap();
            std::fs::set_permissions(&docker, std::fs::Permissions::from_mode(0o755)).unwrap();
        }
        let host_path = format!(
            "{}:{}",
            shell_path(tmp.path()),
            std::env::var("PATH").unwrap_or_default()
        );
        let commands: &[&[&str]] = &[
            &["run", "codex", "--yolo", "resume"],
            &["show", "--json", "--no-scan"],
            &["continue", "--workspace", "work", "--yolo"],
            &["resume", "--workspace", "work", "--limit", "5"],
            &["workstreams", "--limit", "5", "--json"],
            &["rename-workstream", "--from", "old", "--to", "new"],
        ];
        for args in commands {
            let output = shell_script_command(&repo_root().join("bin/ai-memory"))
                .args(args.iter().copied())
                .env("AI_MEMORY_NATIVE_BIN", &native)
                .env("AI_MEMORY_DOCKER", &docker)
                .env("AI_MEMORY_SERVER_URL", "http://192.168.0.90:49374")
                .env("AI_MEMORY_AUTH_TOKEN", "remote-test-token")
                .env("PATH", &host_path)
                .output()
                .unwrap();
            assert!(
                output.status.success(),
                "wrapper failed for {args:?}: {}",
                String::from_utf8_lossy(&output.stderr)
            );
            let mut expected = format!(
                "server=http://192.168.0.90:49374\n\
             auth=remote-test-token\n\
             path={host_path}\n"
            );
            for arg in *args {
                expected.push_str(&format!("arg={arg}\n"));
            }
            assert_eq!(std::fs::read_to_string(&record).unwrap(), expected);
        }
        assert!(
            !docker_record.exists(),
            "managed host command entered Docker"
        );
    }

    /// `run` auto-wires hooks whose command is the native client's own path,
    /// so the client must not live where a cache flush deletes it, and its
    /// release `hooks/` bundle must sit beside it for script-based harnesses.
    #[cfg(unix)]
    #[test]
    fn wrapper_installs_native_client_and_hook_bundle_outside_the_cache() {
        use std::os::unix::fs::PermissionsExt as _;

        let tmp = tempfile::tempdir().unwrap();
        let release = tmp.path().join("release");
        std::fs::create_dir_all(release.join("hooks/claude-code")).unwrap();
        let record = tmp.path().join("native-record.txt");
        std::fs::write(
            release.join("ai-memory"),
            format!(
                "#!/usr/bin/env bash\nprintf '%s\\n' \"$@\" > {}\n",
                shell_path(&record)
            ),
        )
        .unwrap();
        std::fs::set_permissions(
            release.join("ai-memory"),
            std::fs::Permissions::from_mode(0o755),
        )
        .unwrap();
        std::fs::write(
            release.join("hooks/claude-code/session-start.sh"),
            "#!/bin/sh\n",
        )
        .unwrap();
        let tarball = tmp.path().join("release.tar.gz");
        let tar = Command::new("tar")
            .arg("-czf")
            .arg(&tarball)
            .arg("-C")
            .arg(&release)
            .arg(".")
            .status()
            .unwrap();
        assert!(tar.success(), "building the fake release tarball failed");
        let sum = sha256_file(&tarball);

        // Serve the tarball and its checksum the way the GitHub release does;
        // the checksum names the asset the wrapper asked for.
        let bin = tmp.path().join("bin");
        std::fs::create_dir_all(&bin).unwrap();
        let curl = bin.join("curl");
        std::fs::write(
            &curl,
            format!(
                "#!/usr/bin/env bash\n\
                 url=''; out=''\n\
                 while [ \"$#\" -gt 0 ]; do\n\
                   case \"$1\" in\n\
                     -o) out=\"$2\"; shift 2 ;;\n\
                     -*) shift ;;\n\
                     *) url=\"$1\"; shift ;;\n\
                   esac\n\
                 done\n\
                 case \"$url\" in\n\
                   *.tar.gz.sha256) body=\"{sum}  $(basename \"${{url%.sha256}}\")\" ;;\n\
                   *.tar.gz) cp {tarball} \"$out\"; exit 0 ;;\n\
                   *) exit 22 ;;\n\
                 esac\n\
                 if [ -n \"$out\" ]; then printf '%s\\n' \"$body\" > \"$out\"; else printf '%s\\n' \"$body\"; fi\n",
                tarball = shell_path(&tarball),
            ),
        )
        .unwrap();
        let docker = bin.join("docker");
        let docker_record = tmp.path().join("docker-record.txt");
        std::fs::write(
            &docker,
            format!(
                "#!/usr/bin/env bash\nprintf '%s\\n' \"$@\" > {}\nexit 99\n",
                shell_path(&docker_record)
            ),
        )
        .unwrap();
        for script in [&curl, &docker] {
            std::fs::set_permissions(script, std::fs::Permissions::from_mode(0o755)).unwrap();
        }
        let path = format!(
            "{}:{}",
            shell_path(&bin),
            std::env::var("PATH").unwrap_or_default()
        );

        // Default data home, then an explicit XDG_DATA_HOME.
        for (case, xdg_data_home) in [("default", false), ("xdg", true)] {
            let home = tmp.path().join(format!("home-{case}"));
            let cache = tmp.path().join(format!("cache-{case}"));
            let data_home = if xdg_data_home {
                tmp.path().join(format!("data-{case}"))
            } else {
                home.join(".local/share")
            };
            std::fs::create_dir_all(&home).unwrap();
            let _ = std::fs::remove_file(&record);

            let mut command = shell_script_command(&repo_root().join("bin/ai-memory"));
            command
                .args(["workstreams", "--limit", "5"])
                .env("HOME", &home)
                .env("XDG_CACHE_HOME", &cache)
                .env("AI_MEMORY_DOCKER", &docker)
                .env("PATH", &path)
                .env_remove("AI_MEMORY_NATIVE_BIN");
            if xdg_data_home {
                command.env("XDG_DATA_HOME", &data_home);
            } else {
                command.env_remove("XDG_DATA_HOME");
            }
            let output = command.output().unwrap();
            assert!(
                output.status.success(),
                "{case}: wrapper failed: {}",
                String::from_utf8_lossy(&output.stderr)
            );

            let runner = data_home.join("ai-memory/native-runner");
            assert!(
                runner.join("ai-memory").is_file(),
                "{case}: native client missing from {}",
                runner.display()
            );
            assert!(
                runner.join("hooks/claude-code/session-start.sh").is_file(),
                "{case}: release hooks bundle not kept beside the client"
            );
            assert!(
                !cache.join("ai-memory/native-runner").exists(),
                "{case}: native client was installed under the cache"
            );
            assert_eq!(
                std::fs::read_to_string(&record).unwrap(),
                "workstreams\n--limit\n5\n",
                "{case}: wrapper did not exec the installed client"
            );
        }
        assert!(
            !docker_record.exists(),
            "managed host command entered Docker"
        );
    }

    #[cfg(unix)]
    #[test]
    fn wrapper_upgrade_does_not_claim_an_updated_remote_server_is_stale() {
        let tmp = tempfile::tempdir().unwrap();
        let docker = tmp.path().join("docker");
        std::fs::write(
            &docker,
            "#!/usr/bin/env bash\n\
         case \"$1\" in\n\
           pull | ps) exit 0 ;;\n\
           *) exit 1 ;;\n\
         esac\n",
        )
        .unwrap();
        {
            use std::os::unix::fs::PermissionsExt as _;
            std::fs::set_permissions(&docker, std::fs::Permissions::from_mode(0o755)).unwrap();
        }

        let output = shell_script_command(&repo_root().join("bin/ai-memory"))
            .arg("upgrade")
            .env("AI_MEMORY_DOCKER", &docker)
            .env("AI_MEMORY_SKIP_SELF_UPGRADE", "1")
            .env("AI_MEMORY_SERVER_URL", "http://192.168.0.90:49374")
            .env("HOME", tmp.path())
            .env("XDG_DATA_HOME", tmp.path().join("data"))
            .output()
            .unwrap();
        assert!(
            output.status.success(),
            "wrapper failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );
        let stdout = String::from_utf8_lossy(&output.stdout);
        assert!(stdout.contains("does not\n  inspect or redeploy the remote server"));
        assert!(stdout.contains("If that host is not already current"));
        assert!(!stdout.contains("remote server still\n  runs the previous version"));
    }

    #[cfg(unix)]
    #[test]
    fn docker_wrapper_completions_tolerate_an_early_reader_close() {
        let tmp = tempfile::tempdir().unwrap();
        let docker = tmp.path().join("docker");
        std::fs::write(
            &docker,
            "#!/usr/bin/env bash\n\
         if [ \"$1\" = info ]; then\n\
           printf '[name=seccomp,profile=default]\\n'\n\
           exit 0\n\
         fi\n\
         if [ \"$1\" = run ]; then\n\
           i=0\n\
           while [ \"$i\" -lt 20000 ]; do\n\
             printf 'complete -c ai-memory -n condition-%s\\n' \"$i\"\n\
             i=$((i + 1))\n\
           done\n\
           exit 0\n\
         fi\n\
         exit 1\n",
        )
        .unwrap();
        {
            use std::os::unix::fs::PermissionsExt as _;
            std::fs::set_permissions(&docker, std::fs::Permissions::from_mode(0o755)).unwrap();
        }

        let mut child = shell_script_command(&repo_root().join("bin/ai-memory"))
            .args(["completions", "fish"])
            .env("AI_MEMORY_DOCKER", &docker)
            .env("AI_MEMORY_NO_TTY", "1")
            .env("AI_MEMORY_NO_VERSION_CHECK", "1")
            .env("AI_MEMORY_DATA_VOLUME", "test-ai-memory-data")
            .env("HOME", tmp.path())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .unwrap();
        let mut stdout = std::io::BufReader::new(child.stdout.take().unwrap());
        let mut first_line = String::new();
        stdout.read_line(&mut first_line).unwrap();
        drop(stdout);

        let output = child.wait_with_output().unwrap();
        assert_eq!(first_line, "complete -c ai-memory -n condition-0\n");
        assert!(
            output.status.success(),
            "early close should stay quiet and successful: {}",
            String::from_utf8_lossy(&output.stderr)
        );
        assert!(
            !String::from_utf8_lossy(&output.stderr).contains("broken pipe"),
            "wrapper leaked Docker's broken-pipe diagnostic: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }

    #[cfg(unix)]
    #[test]
    fn docker_wrapper_completions_preserve_helper_failure_without_partial_output() {
        let tmp = tempfile::tempdir().unwrap();
        let docker = tmp.path().join("docker");
        std::fs::write(
            &docker,
            "#!/usr/bin/env bash\n\
         if [ \"$1\" = info ]; then\n\
           printf '[name=seccomp,profile=default]\\n'\n\
           exit 0\n\
         fi\n\
         if [ \"$1\" = run ]; then\n\
           printf 'partial completion output\\n'\n\
           printf 'helper failed\\n' >&2\n\
           exit 42\n\
         fi\n\
         exit 1\n",
        )
        .unwrap();
        {
            use std::os::unix::fs::PermissionsExt as _;
            std::fs::set_permissions(&docker, std::fs::Permissions::from_mode(0o755)).unwrap();
        }

        let output = shell_script_command(&repo_root().join("bin/ai-memory"))
            .args(["completions", "fish"])
            .env("AI_MEMORY_DOCKER", &docker)
            .env("AI_MEMORY_NO_TTY", "1")
            .env("AI_MEMORY_NO_VERSION_CHECK", "1")
            .env("AI_MEMORY_DATA_VOLUME", "test-ai-memory-data")
            .env("HOME", tmp.path())
            .output()
            .unwrap();
        assert_eq!(output.status.code(), Some(42));
        assert!(
            output.stdout.is_empty(),
            "failed helper leaked partial completions: {}",
            String::from_utf8_lossy(&output.stdout)
        );
        assert_eq!(String::from_utf8_lossy(&output.stderr), "helper failed\n");
    }

    #[cfg(unix)]
    #[test]
    fn wrapper_forwards_claude_config_dir_to_helper_container() {
        let args = run_wrapper_with_fake_docker_and_claude_config(
            &["install-hooks", "--agent", "claude-code", "--apply"],
            "[name=seccomp,profile=default]",
            "/home/alice/.config/claude",
        );
        assert!(
            args.contains("-e\nCLAUDE_CONFIG_DIR"),
            "wrapper must forward Claude's config root; got {args}"
        );
    }

    #[cfg(unix)]
    #[test]
    fn rootless_docker_uses_root_uid_only_for_host_config_commands() {
        let rootless_info = "[name=apparmor name=seccomp,profile=default name=rootless]";

        for subcommand in [
            "install-mcp",
            "install-hooks",
            "setup-agent",
            "install-instructions",
            "install-skills",
            // uninstall edits the same host agent-config files; backup writes
            // its tarball to a host path, and restore reads one — same bind
            // mounts, same UID rule.
            "uninstall",
            "backup",
            "restore",
        ] {
            let args = run_wrapper_with_fake_docker(&[subcommand], rootless_info);
            assert!(
                args.contains("-u\n0:0"),
                "{subcommand} writes host bind-mounted files and must run as root \
             under rootless Docker so the write lands as the real host user \
             (rootlesskit only maps container UID 0 back to it); got {args}"
            );
        }

        let args = run_wrapper_with_fake_docker(&["status"], rootless_info);
        assert!(
            !args.contains("-u\n0:0"),
            "thin-client commands only touch the /data named volume, which isn't \
         host-visible, so they must keep the host-UID mapping; got {args}"
        );
    }

    #[cfg(unix)]
    #[test]
    fn fake_macos_rootless_docker_keeps_root_uid_for_host_config_commands() {
        let args = run_wrapper_with_fake_rootless_docker_on_fake_macos(&["install-mcp"]);
        assert!(
            args.contains("-u\n0:0"),
            "macOS rootless Docker still needs uid 0 for host config writes; got {args}"
        );

        let args = run_wrapper_with_fake_rootless_docker_on_fake_macos(&["status"]);
        assert!(
            !args.contains("-u\n0:0"),
            "macOS thin-client commands should keep Docker Desktop's default uid; got {args}"
        );
    }

    #[cfg(unix)]
    #[test]
    fn fake_macos_rootful_docker_keeps_default_uid_for_host_config_commands() {
        let args = run_wrapper_with_fake_docker_and_uname(
            &["install-mcp"],
            "[name=seccomp,profile=default]",
            Some("Darwin"),
        );
        assert!(
            !args.contains("-u\n0:0") && !args.contains("-u\n"),
            "macOS rootful Docker should keep Docker Desktop's default uid; got {args}"
        );
    }

    #[cfg(unix)]
    #[test]
    fn rootful_docker_keeps_host_uid_for_host_config_commands() {
        let rootful_info = "[name=seccomp,profile=default]";

        let args = run_wrapper_with_fake_docker(&["install-hooks"], rootful_info);
        assert!(
            !args.contains("-u\n0:0"),
            "rootful Docker must not switch to root UID — that would write \
         ~/.local/share/ai-memory/hooks owned by root instead of the invoking \
         user; got {args}"
        );
    }

    #[cfg(unix)]
    #[test]
    fn selinux_enforcing_disables_labels_only_for_host_file_commands() {
        let selinux_info = "[name=seccomp,profile=default name=selinux name=cgroupns]";

        for subcommand in [
            "install-mcp",
            "install-hooks",
            "setup-agent",
            "install-instructions",
            "install-skills",
            "uninstall",
            "backup",
            "restore",
        ] {
            let args = run_wrapper_with_fake_selinux(&[subcommand], selinux_info, "Enforcing");
            assert!(
                args.contains("--security-opt\nlabel=disable"),
                "{subcommand} writes bind-mounted host files and needs the scoped \
             SELinux exception; got {args}"
            );
        }

        let args = run_wrapper_with_fake_selinux(&["status"], selinux_info, "Enforcing");
        assert!(
            !args.contains("label=disable"),
            "thin-client commands must retain SELinux label confinement; got {args}"
        );
    }

    #[cfg(unix)]
    #[test]
    fn selinux_label_exception_requires_enforcement_and_daemon_support() {
        let selinux_info = "[name=seccomp,profile=default name=selinux name=cgroupns]";
        let args = run_wrapper_with_fake_selinux(&["install-mcp"], selinux_info, "Permissive");
        assert!(
            !args.contains("label=disable"),
            "permissive hosts do not need a label exception; got {args}"
        );

        let args = run_wrapper_with_fake_selinux(
            &["install-mcp"],
            "[name=seccomp,profile=default name=cgroupns]",
            "Enforcing",
        );
        assert!(
            !args.contains("label=disable"),
            "a daemon without SELinux support must not receive SELinux options; got {args}"
        );
    }

    #[cfg(unix)]
    #[test]
    fn posix_wrapper_forwards_subscription_oauth_tokens_without_putting_values_in_argv() {
        let tokens = [
            ("ANTHROPIC_OAUTH_TOKEN", "oauth-canary-primary"),
            ("CLAUDE_CODE_OAUTH_TOKEN", "oauth-canary-fallback"),
        ];
        let args = run_wrapper_with_fake_docker_and_forwarded_env(
            &["llm-test", "--provider", "anthropic-oauth"],
            "[name=seccomp,profile=default]",
            &tokens,
        );
        let args: Vec<&str> = args.lines().collect();

        for (name, value) in tokens {
            assert!(
                args.windows(2).any(|pair| pair == ["-e", name]),
                "wrapper must forward {name} by name; got {args:?}"
            );
            assert!(
                !args.iter().any(|arg| arg.contains(value)),
                "wrapper must not put the value of {name} in Docker argv"
            );
        }
    }

    /// The two embedding-prefix env vars are forwarded on PRESENCE, not
    /// non-emptiness, unlike every other var in the loop: an operator sets
    /// one to the empty string to clear a `config.toml`-configured prefix
    /// without editing the file (see `Config::load`'s figment overlay in
    /// `ai-memory-cli/src/config.rs`), and that override only reaches the
    /// server if the wrapper forwards the (empty) variable rather than
    /// dropping it the way a plain `[ -n ]` check would.
    #[cfg(unix)]
    #[test]
    fn posix_wrapper_forwards_embedding_prefixes_by_presence_not_non_emptiness() {
        const QUERY: &str = "AI_MEMORY_EMBEDDING_QUERY_PREFIX";
        const DOC: &str = "AI_MEMORY_EMBEDDING_DOCUMENT_PREFIX";
        let has_e = |args: &[&str], name: &str| args.windows(2).any(|pair| pair == ["-e", name]);

        // Unset: neither var forwarded (the loop must not invent a value).
        let args =
            run_wrapper_with_fake_docker_and_forwarded_env(&["llm-test"], "[name=seccomp]", &[]);
        let lines: Vec<&str> = args.lines().collect();
        assert!(
            !has_e(&lines, QUERY),
            "unset must not be forwarded; got {lines:?}"
        );
        assert!(
            !has_e(&lines, DOC),
            "unset must not be forwarded; got {lines:?}"
        );

        // Empty: forwarded anyway — this is the override case.
        let args = run_wrapper_with_fake_docker_and_forwarded_env(
            &["llm-test"],
            "[name=seccomp]",
            &[(QUERY, ""), (DOC, "")],
        );
        let lines: Vec<&str> = args.lines().collect();
        assert!(
            has_e(&lines, QUERY),
            "an empty (but present) value must still be forwarded; got {lines:?}"
        );
        assert!(
            has_e(&lines, DOC),
            "an empty (but present) value must still be forwarded; got {lines:?}"
        );

        // Whitespace-only: also present, also forwarded — this loop must
        // not apply any trimming/emptiness judgement of its own.
        let args = run_wrapper_with_fake_docker_and_forwarded_env(
            &["llm-test"],
            "[name=seccomp]",
            &[(QUERY, "   "), (DOC, "   ")],
        );
        let lines: Vec<&str> = args.lines().collect();
        assert!(
            has_e(&lines, QUERY),
            "whitespace-only must still be forwarded; got {lines:?}"
        );
        assert!(
            has_e(&lines, DOC),
            "whitespace-only must still be forwarded; got {lines:?}"
        );

        // Non-empty: forwarded, same as every other var.
        let args = run_wrapper_with_fake_docker_and_forwarded_env(
            &["llm-test"],
            "[name=seccomp]",
            &[(QUERY, "query: "), (DOC, "passage: ")],
        );
        let lines: Vec<&str> = args.lines().collect();
        assert!(
            has_e(&lines, QUERY),
            "a non-empty value must be forwarded; got {lines:?}"
        );
        assert!(
            has_e(&lines, DOC),
            "a non-empty value must be forwarded; got {lines:?}"
        );
    }

    // The Windows mirror of macos_wrapper_routes_urls_by_real_subcommand: Docker
    // Desktop gives Linux containers no host networking on Windows either, so the
    // helper container cannot reach the host-published server over loopback.
    #[cfg(windows)]
    #[test]
    fn windows_wrapper_routes_urls_by_real_subcommand() {
        for subcommand in ["install-mcp", "install-hooks", "setup-agent"] {
            let args = run_powershell_wrapper(&[subcommand]);
            assert!(
                !args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
                "{subcommand} renders host-side config and must keep loopback defaults; got {args}"
            );
        }

        let args = run_powershell_wrapper(&["status"]);
        assert!(
            args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
            "thin-client commands must reach the host server through Docker Desktop; got {args}"
        );

        let args = run_powershell_wrapper(&["search", "install-hooks"]);
        assert!(
            args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
            "only the actual subcommand should control URL routing; got {args}"
        );

        let args = run_powershell_wrapper(&["--config", "C:\\tmp\\config.toml", "install-hooks"]);
        assert!(
            !args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
            "global options before install-hooks must not hide the real subcommand; got {args}"
        );

        // A homelab/remote server is configured through the environment, and the
        // helper container must not be redirected back at the local Docker host.
        let args =
            run_powershell_wrapper_with_server_url(&["status"], Some("http://192.168.1.50:49374"));
        assert!(
            !args.contains("AI_MEMORY_SERVER_URL=http://host.docker.internal:49374"),
            "an explicit AI_MEMORY_SERVER_URL must win over the Docker Desktop alias; got {args}"
        );
        assert!(
            args.contains("-e AI_MEMORY_SERVER_URL"),
            "an explicit AI_MEMORY_SERVER_URL must still be forwarded by name; got {args}"
        );
    }

    #[cfg(windows)]
    #[test]
    fn windows_wrapper_upgrades_after_valid_global_options_only() {
        for args in [
            &["upgrade"][..],
            &["--config", "C:\\tmp\\config with spaces.toml", "upgrade"],
            &["--config=C:\\tmp\\config.toml", "upgrade"],
            &["--data-dir", "C:\\tmp\\data", "upgrade"],
            &["--data-dir=C:\\tmp\\data", "upgrade"],
        ] {
            let docker = run_powershell_wrapper(args);
            assert!(
                docker.starts_with("pull "),
                "valid global options must not hide the wrapper-only upgrade; args={args:?}, docker={docker}"
            );
        }

        for args in [
            &["--unknown", "upgrade"][..],
            &["--help", "upgrade"],
            &["--config", "--help", "upgrade"],
            &["--data-dir=", "upgrade"],
            &["--CONFIG", "C:\\tmp\\config.toml", "upgrade"],
            &["--DATA-DIR=C:\\tmp\\data", "upgrade"],
            &["--config", "C:\\tmp\\config.toml", "UPGRADE"],
        ] {
            let docker = run_powershell_wrapper(args);
            assert!(
                docker.starts_with("run "),
                "invalid or help options must not trigger an image pull; args={args:?}, docker={docker}"
            );
        }

        // `powershell.exe -File` consumes a bare `-` before the script sees it,
        // so exercise this valid config value through a PowerShell command.
        let script = repo_root()
            .join("bin/ai-memory.ps1")
            .display()
            .to_string()
            .replace('\'', "''");
        let output = Command::new(ai_memory_test_support::powershell_exe())
            .args([
                "-NoLogo",
                "-NoProfile",
                "-NonInteractive",
                "-ExecutionPolicy",
                "Bypass",
                "-Command",
            ])
            .arg(format!("& '{script}' --config '-' upgrade"))
            .env("AI_MEMORY_DOCKER", "Write-Output")
            .output()
            .unwrap();
        assert!(
            output.status.success() && String::from_utf8_lossy(&output.stdout).starts_with("pull"),
            "a single dash is a valid config value: status={:?}, stdout={}, stderr={}",
            output.status.code(),
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );
    }

    // #1545: the helper container has no host harnesses, so the managed
    // launch/discovery commands must run through the native Windows client —
    // never the container engine — with the child's exit code preserved.
    #[cfg(windows)]
    #[test]
    fn windows_wrapper_routes_native_host_commands_through_the_native_client() {
        let cases: &[&[&str]] = &[
            &["run", "claude"],
            &["run", "--new", "docs", "codex"],
            &["show", "--json"],
            &["continue", "--yolo"],
            &["resume"],
            &["workstreams", "--limit", "5"],
            &[
                "rename-workstream",
                "--from",
                "typo-nmae",
                "--to",
                "refactor-db",
            ],
            // Global options precede the subcommand in real invocations too.
            &["--config", "C:\\tmp\\config.toml", "run", "codex"],
        ];
        for args in cases {
            let (code, native_log, docker_log) = run_powershell_wrapper_with_fake_native(args, &[]);
            assert_eq!(
                code,
                Some(7),
                "wrapper must exit with the native client's code for {args:?}"
            );
            assert!(
                native_log.contains(&format!("argv: {}", args.join(" "))),
                "native client must receive the full argv for {args:?}; got {native_log}"
            );
            assert!(
                docker_log.is_empty(),
                "native host command {args:?} must not reach the container engine; got {docker_log}"
            );
        }

        // The child inherits AI_MEMORY_SERVER_URL untouched: unset means the
        // client's own host-loopback default applies (never the container's
        // host.docker.internal alias), and an explicit value wins.
        let (_, native_log, _) = run_powershell_wrapper_with_fake_native(&["run", "claude"], &[]);
        assert!(
            native_log.contains("server-url: unset"),
            "the wrapper must not inject a server URL into native host commands; got {native_log}"
        );
        assert!(
            native_log.contains("autowire: false"),
            "the wrapper must keep container-rendered wiring authoritative unless the \
             operator set AI_MEMORY_RUN_AUTOWIRE; got {native_log}"
        );

        let (_, native_log, _) = run_powershell_wrapper_with_fake_native(
            &["run", "claude"],
            &[("AI_MEMORY_SERVER_URL", "http://192.168.1.50:49374")],
        );
        assert!(
            native_log.contains("server-url: http://192.168.1.50:49374"),
            "an explicit AI_MEMORY_SERVER_URL must reach the native client; got {native_log}"
        );

        let (_, native_log, _) = run_powershell_wrapper_with_fake_native(
            &["run", "claude"],
            &[("AI_MEMORY_RUN_AUTOWIRE", "true")],
        );
        assert!(
            native_log.contains("autowire: true"),
            "an explicit AI_MEMORY_RUN_AUTOWIRE must win; got {native_log}"
        );
    }

    #[cfg(windows)]
    #[test]
    fn windows_install_hooks_refreshes_the_volume_bearer_for_managed_launches() {
        let (code, native_log, docker_log) = run_powershell_wrapper_with_fake_native(
            &["install-hooks", "--agent", "claude-code"],
            &[],
        );
        assert_eq!(code, Some(7));
        assert!(
            native_log.contains("argv: install-hooks --agent claude-code"),
            "native install must receive the original argv; got {native_log}"
        );
        assert!(
            native_log.contains("bearer-file:"),
            "native install must receive the refreshed bearer path; got {native_log}"
        );
        assert!(
            docker_log.contains("auth-tokens/default"),
            "setup must read the configured bearer from the named volume; got {docker_log}"
        );
    }

    // Without the native client the wrapper must refuse clearly (pointing at
    // the documented installer) instead of silently falling back to the
    // helper container, where the command used to die with a misleading
    // "executable not found in the host PATH" (#1545).
    #[cfg(windows)]
    #[test]
    fn windows_wrapper_refuses_native_host_commands_without_the_native_client() {
        let tmp = tempfile::tempdir().unwrap();
        let docker_args = tmp.path().join("docker-args.txt");
        let docker = tmp.path().join("docker.cmd");
        std::fs::write(
            &docker,
            "@echo off\r\n>>\"%AI_MEMORY_TEST_DOCKER_ARGS%\" echo %*\r\nexit /b 0\r\n",
        )
        .unwrap();

        let mut command = Command::new(ai_memory_test_support::powershell_exe());
        command
            .args([
                "-NoLogo",
                "-NoProfile",
                "-NonInteractive",
                "-ExecutionPolicy",
                "Bypass",
                "-File",
            ])
            .arg(repo_root().join("bin/ai-memory.ps1"))
            .args(["run", "claude"])
            .env("AI_MEMORY_DOCKER", &docker)
            .env("AI_MEMORY_TEST_DOCKER_ARGS", &docker_args)
            // A temp LOCALAPPDATA holds no installed client, standing in for
            // "the installer never ran" regardless of the host machine.
            .env("LOCALAPPDATA", tmp.path())
            .env_remove("AI_MEMORY_NATIVE_BIN")
            .env_remove("AI_MEMORY_SERVER_URL");
        let output = command.output().unwrap();
        assert!(
            !output.status.success(),
            "a missing native client must fail instead of entering the container"
        );
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            stderr.contains("install.ps1"),
            "the refusal must name the documented installer; got {stderr}"
        );
        assert!(
            stderr.contains("AI_MEMORY_NATIVE_BIN"),
            "the refusal must name the override; got {stderr}"
        );
        let docker_log = std::fs::read_to_string(&docker_args).unwrap_or_default();
        assert!(
            docker_log.is_empty(),
            "a missing native client must not fall back to the container; got {docker_log}"
        );
    }

    #[cfg(windows)]
    #[test]
    fn powershell_wrapper_forwards_subscription_oauth_tokens_without_putting_values_in_argv() {
        let tmp = tempfile::tempdir().unwrap();
        let docker_args = tmp.path().join("docker-args.txt");
        let docker = tmp.path().join("docker.cmd");
        std::fs::write(
            &docker,
            "@echo off\r\n>\"%AI_MEMORY_TEST_DOCKER_ARGS%\" echo %*\r\nexit /b 0\r\n",
        )
        .unwrap();

        let tokens = [
            ("ANTHROPIC_OAUTH_TOKEN", "oauth-canary-primary"),
            ("CLAUDE_CODE_OAUTH_TOKEN", "oauth-canary-fallback"),
        ];
        let mut command = Command::new(ai_memory_test_support::powershell_exe());
        command
            // -ExecutionPolicy Bypass: `-File` loads a script from disk, and execution policy
            // governs script *files*, so on a machine left at the Windows client default of
            // `Restricted` the unsigned bin/ai-memory.ps1 is refused (`UnauthorizedAccess`) and
            // the wrapper never runs — failing this test for a reason unrelated to what it
            // checks. The other in-repo invocation (render_shared.rs) uses `-Command`, which is
            // not policy-gated and therefore needs no override; the generated hook commands pass
            // Bypass defensively. GitHub's runner is permissive enough that this passes there
            // today, so the fix is for running the suite on a stock Windows install.
            .args([
                "-NoLogo",
                "-NoProfile",
                "-NonInteractive",
                "-ExecutionPolicy",
                "Bypass",
                "-File",
            ])
            .arg(repo_root().join("bin/ai-memory.ps1"))
            .args(["llm-test", "--provider", "anthropic-oauth"])
            .env("AI_MEMORY_DOCKER", &docker)
            .env("AI_MEMORY_TEST_DOCKER_ARGS", &docker_args);
        for (name, value) in tokens {
            command.env(name, value);
        }
        let output = command.output().unwrap();
        assert!(
            output.status.success(),
            "PowerShell wrapper failed: stdout={} stderr={}",
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );

        let args = std::fs::read_to_string(docker_args).unwrap();
        for (name, value) in tokens {
            assert!(
                args.contains(&format!("-e {name}")),
                "PowerShell wrapper must forward {name} by name; got {args}"
            );
            assert!(
                !args.contains(value),
                "PowerShell wrapper must not put the value of {name} in Docker argv"
            );
        }
    }

    #[cfg(unix)]
    #[test]
    fn podman_rootless_and_selinux_are_detected_without_the_docker_only_field() {
        // Podman answers nothing for `{{.SecurityOptions}}`, so both gates used to
        // read as "rootful, no SELinux" and every host-file write died with
        // Permission denied. Both adjustments are required: neither alone makes
        // the write land.
        for subcommand in [
            "install-mcp",
            "install-hooks",
            "setup-agent",
            "install-instructions",
            "install-skills",
            "uninstall",
            "backup",
            "restore",
        ] {
            let args = run_wrapper_with_fake_podman(&[subcommand], true, true, "Enforcing");
            assert!(
                args.contains("-u\n0:0"),
                "{subcommand} needs the rootless UID remap under podman too; got {args}"
            );
            assert!(
                args.contains("--security-opt\nlabel=disable"),
                "{subcommand} needs the scoped SELinux exception under podman too; got {args}"
            );
        }

        let args = run_wrapper_with_fake_podman(&["status"], true, true, "Enforcing");
        assert!(
            !args.contains("-u\n0:0") && !args.contains("label=disable"),
            "thin-client commands touch only the named volume and must stay \
         confined under podman as well; got {args}"
        );
    }

    #[cfg(unix)]
    #[test]
    fn podman_gates_still_respect_engine_and_host_state() {
        // The fallback reports what podman reports; it must not hard-code "yes".
        let args = run_wrapper_with_fake_podman(&["install-mcp"], false, true, "Enforcing");
        assert!(
            !args.contains("-u\n0:0"),
            "rootful podman maps the host UID directly and must keep it; got {args}"
        );

        let args = run_wrapper_with_fake_podman(&["install-mcp"], true, false, "Enforcing");
        assert!(
            !args.contains("label=disable"),
            "an engine without SELinux support must not receive SELinux options; got {args}"
        );

        let args = run_wrapper_with_fake_podman(&["install-mcp"], true, true, "Permissive");
        assert!(
            !args.contains("label=disable"),
            "permissive hosts do not need a label exception; got {args}"
        );
    }

    #[cfg(unix)]
    #[test]
    fn bootstrap_gets_host_file_treatment_because_it_reads_the_repo() {
        // bootstrap only reads host files, but an unmapped UID blocks reads just
        // as hard: it degrades silently to "no .git found at /work" and then dies
        // with Permission denied. Same gates as the writers, on both engines.
        let args = run_wrapper_with_fake_podman(&["bootstrap"], true, true, "Enforcing");
        assert!(
            args.contains("-u\n0:0") && args.contains("--security-opt\nlabel=disable"),
            "bootstrap reads the repo bind-mounted at /work and needs both \
         adjustments; got {args}"
        );

        let args = run_wrapper_with_fake_selinux(
            &["bootstrap"],
            "[name=seccomp,profile=default name=selinux name=cgroupns]",
            "Enforcing",
        );
        assert!(
            args.contains("--security-opt\nlabel=disable"),
            "the same read applies under Docker on an SELinux host; got {args}"
        );
    }

    #[cfg(unix)]
    #[test]
    fn explicit_config_gets_host_file_treatment() {
        let args = run_wrapper_with_fake_podman(
            &["--config", "/tmp/config.toml", "status"],
            true,
            true,
            "Enforcing",
        );
        assert!(
            args.contains("-u\n0:0") && args.contains("--security-opt\nlabel=disable"),
            "an explicit config is read through a host bind and needs both adjustments; got {args}"
        );

        let args = run_wrapper_with_fake_podman(
            &["--config=/tmp/config.toml", "status"],
            true,
            true,
            "Enforcing",
        );
        assert!(
            args.contains("-u\n0:0") && args.contains("--security-opt\nlabel=disable"),
            "the equals form of --config must receive the same treatment; got {args}"
        );
    }

    #[cfg(unix)]
    #[test]
    fn custom_data_dir_makes_thin_commands_touch_host_files() {
        let args = run_wrapper_with_fake_docker_env(
            &["status"],
            FakeInfo::Podman {
                rootless: true,
                selinux: true,
            },
            Some("Linux"),
            None,
            Some("Enforcing"),
            &[("AI_MEMORY_DATA_DIR", "/tmp")],
        );
        assert!(
            args.contains("-u\n0:0") && args.contains("--security-opt\nlabel=disable"),
            "a thin command backed by a host data directory needs both adjustments; got {args}"
        );
        assert!(
            args.contains("/tmp:/data"),
            "the custom data directory must remain the /data bind; got {args}"
        );
    }
}