import { mkdir, mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; import { describe, expect, it, vi } from "vitest"; import { ShellInitSnapshotManager, buildShellInitSnapshotCreationScript, detectShellInitConfigPath, supportsShellInitSnapshot, } from "../src/exec/shell-init-snapshot.js"; describe("shell init snapshot", () => { it("supports only POSIX and Git Bash dialects", () => { expect(supportsShellInitSnapshot("posix")).toBe(true); expect(supportsShellInitSnapshot("git-bash")).toBe(true); expect(supportsShellInitSnapshot("cmd")).toBe(false); expect(supportsShellInitSnapshot("legacy-shell")).toBe(false); }); it("detects rc file path from shell kind", () => { expect( detectShellInitConfigPath({ env: { HOME: "/home/user" }, shellPath: "/bin/zsh", }), ).toBe("/home/user/.zshrc"); expect( detectShellInitConfigPath({ env: { HOME: "/home/user" }, shellPath: "/bin/bash", }), ).toBe("/home/user/.bashrc"); expect( detectShellInitConfigPath({ env: { HOME: "/home/user" }, shellPath: "/bin/sh", }), ).toBe("/home/user/.profile"); }); it("detects Git Bash rc file path from USERPROFILE when HOME is unavailable", () => { expect( detectShellInitConfigPath({ env: { USERPROFILE: "C:\\Users\\me" }, shellPath: "C:\\Program Files\\Git\\bin\\bash.exe", }), ).toBe(join("C:\\Users\\me", ".bashrc")); }); it("builds a snapshot creation script that sources config and writes exports", () => { const script = buildShellInitSnapshotCreationScript({ configPath: "/home/user/.bashrc", configExists: true, shellKind: "bash", snapshotPath: "/tmp/zcode-snapshot.sh", }); expect(script).toContain("SNAPSHOT_FILE='/tmp/zcode-snapshot.sh'"); expect(script).toContain('source "/home/user/.bashrc" < /dev/null'); expect(script).toContain('echo "# Snapshot file" >| "$SNAPSHOT_FILE"'); expect(script).toContain('echo "shopt -s expand_aliases" >> "$SNAPSHOT_FILE"'); expect(script).toContain("declare -F | cut -d' ' -f3 | grep -vE '^_[^_]'"); expect(script).toContain("encoded_func=$(declare -f \"$func\" | base64 )"); expect(script).toContain("base64 -d"); expect(script).toContain('shopt -p | head -n 1000'); expect(script).toContain('set -o | grep "on"'); expect(script).toContain("alias | grep -v \"='winpty \""); expect(script).toContain("sed 's/^/alias -- /'"); expect(script).toContain("head -n 1000"); expect(script).toContain("export PATH="); }); it("builds zsh snapshot creation scripts with zsh-specific exports", () => { const script = buildShellInitSnapshotCreationScript({ configPath: "/home/user/.zshrc", configExists: true, shellKind: "zsh", snapshotPath: "/tmp/zcode-zsh-snapshot.sh", }); expect(script).toContain("SNAPSHOT_FILE='/tmp/zcode-zsh-snapshot.sh'"); expect(script).toContain('source "/home/user/.zshrc" < /dev/null'); expect(script).toContain("typeset +f | grep -vE '^_[^_]'"); expect(script).toContain("typeset -f \"$func\""); expect(script).toContain("setopt | sed 's/^/setopt /' | head -n 1000"); expect(script).toContain("alias | grep -v \"='winpty \""); expect(script).not.toContain("declare -f"); expect(script).not.toContain("shopt -s expand_aliases"); }); it("omits rc sourcing when the selected shell has no config file", () => { const script = buildShellInitSnapshotCreationScript({ configPath: "/home/user/.bashrc", configExists: false, shellKind: "bash", snapshotPath: "/tmp/zcode-snapshot.sh", }); expect(script).toContain("SNAPSHOT_FILE='/tmp/zcode-snapshot.sh'"); expect(script).not.toContain('source "/home/user/.bashrc" < /dev/null'); expect(script).toContain('echo "shopt -s expand_aliases" >> "$SNAPSHOT_FILE"'); expect(script).not.toContain("declare -F"); expect(script).not.toContain("alias |"); }); it("caches successful snapshot creation with snapshot--