import { describe, expect, it, vi } from "vitest"; import { extractPathFromShellOutput, listLoginShellCandidates, mergePathEntries, readEnvironmentFromLoginShell, readPathFromLaunchctl, readPathFromLoginShell, } from "./shell"; describe("extractPathFromShellOutput", () => { it("extracts the path between capture markers", () => { expect( extractPathFromShellOutput( "__T3CODE_PATH_START__\n/opt/homebrew/bin:/usr/bin\n__T3CODE_PATH_END__\n", ), ).toBe("/opt/homebrew/bin:/usr/bin"); }); it("ignores shell startup noise around the capture markers", () => { expect( extractPathFromShellOutput( "Welcome to fish\n__T3CODE_PATH_START__\n/opt/homebrew/bin:/usr/bin\n__T3CODE_PATH_END__\nBye\n", ), ).toBe("/opt/homebrew/bin:/usr/bin"); }); it("returns null when the markers are missing", () => { expect(extractPathFromShellOutput("/opt/homebrew/bin /usr/bin")).toBeNull(); }); }); describe("readPathFromLoginShell", () => { it("uses a shell-agnostic printenv PATH probe", () => { const execFile = vi.fn< ( file: string, args: ReadonlyArray, options: { encoding: "utf8"; timeout: number }, ) => string >(() => "__T3CODE_ENV_PATH_START__\n/a:/b\n__T3CODE_ENV_PATH_END__\n"); expect(readPathFromLoginShell("/opt/homebrew/bin/fish", execFile)).toBe("/a:/b"); expect(execFile).toHaveBeenCalledTimes(1); const firstCall = execFile.mock.calls[0] as | [string, ReadonlyArray, { encoding: "utf8"; timeout: number }] | undefined; expect(firstCall).toBeDefined(); if (!firstCall) { throw new Error("Expected execFile to be called"); } const [shell, args, options] = firstCall; expect(shell).toBe("/opt/homebrew/bin/fish"); expect(args).toHaveLength(2); expect(args?.[0]).toBe("-ilc"); expect(args?.[1]).toContain("printenv PATH || true"); expect(args?.[1]).toContain("__T3CODE_ENV_PATH_START__"); expect(args?.[1]).toContain("__T3CODE_ENV_PATH_END__"); expect(options).toEqual({ encoding: "utf8", timeout: 5000 }); }); }); describe("readPathFromLaunchctl", () => { it("returns a trimmed PATH value from launchctl", () => { const execFile = vi.fn< ( file: string, args: ReadonlyArray, options: { encoding: "utf8"; timeout: number }, ) => string >(() => " /opt/homebrew/bin:/usr/bin \n"); expect(readPathFromLaunchctl(execFile)).toBe("/opt/homebrew/bin:/usr/bin"); expect(execFile).toHaveBeenCalledWith("/bin/launchctl", ["getenv", "PATH"], { encoding: "utf8", timeout: 2000, }); }); it("returns undefined when launchctl is unavailable", () => { const execFile = vi.fn< ( file: string, args: ReadonlyArray, options: { encoding: "utf8"; timeout: number }, ) => string >(() => { throw new Error("spawn /bin/launchctl ENOENT"); }); expect(readPathFromLaunchctl(execFile)).toBeUndefined(); }); }); describe("readEnvironmentFromLoginShell", () => { it("extracts multiple environment variables from a login shell command", () => { const execFile = vi.fn< ( file: string, args: ReadonlyArray, options: { encoding: "utf8"; timeout: number }, ) => string >(() => [ "__T3CODE_ENV_PATH_START__", "/a:/b", "__T3CODE_ENV_PATH_END__", "__T3CODE_ENV_SSH_AUTH_SOCK_START__", "/tmp/secretive.sock", "__T3CODE_ENV_SSH_AUTH_SOCK_END__", ].join("\n"), ); expect(readEnvironmentFromLoginShell("/bin/zsh", ["PATH", "SSH_AUTH_SOCK"], execFile)).toEqual({ PATH: "/a:/b", SSH_AUTH_SOCK: "/tmp/secretive.sock", }); expect(execFile).toHaveBeenCalledTimes(1); }); it("omits environment variables that are missing or empty", () => { const execFile = vi.fn< ( file: string, args: ReadonlyArray, options: { encoding: "utf8"; timeout: number }, ) => string >(() => [ "__T3CODE_ENV_PATH_START__", "/a:/b", "__T3CODE_ENV_PATH_END__", "__T3CODE_ENV_SSH_AUTH_SOCK_START__", "__T3CODE_ENV_SSH_AUTH_SOCK_END__", ].join("\n"), ); expect(readEnvironmentFromLoginShell("/bin/zsh", ["PATH", "SSH_AUTH_SOCK"], execFile)).toEqual({ PATH: "/a:/b", }); }); it("preserves surrounding whitespace in captured values", () => { const execFile = vi.fn< ( file: string, args: ReadonlyArray, options: { encoding: "utf8"; timeout: number }, ) => string >(() => ["__T3CODE_ENV_CUSTOM_VAR_START__", " padded value ", "__T3CODE_ENV_CUSTOM_VAR_END__"].join( "\n", ), ); expect(readEnvironmentFromLoginShell("/bin/zsh", ["CUSTOM_VAR"], execFile)).toEqual({ CUSTOM_VAR: " padded value ", }); }); }); describe("listLoginShellCandidates", () => { it("returns env shell, user shell, then the platform fallback without duplicates", () => { expect(listLoginShellCandidates("darwin", " /opt/homebrew/bin/nu ", "/bin/zsh")).toEqual([ "/opt/homebrew/bin/nu", "/bin/zsh", ]); }); it("falls back to the platform default when no shells are available", () => { expect(listLoginShellCandidates("linux", undefined, "")).toEqual(["/bin/bash"]); }); }); describe("mergePathEntries", () => { it("prefers login-shell PATH entries and keeps inherited extras", () => { expect( mergePathEntries("/opt/homebrew/bin:/usr/bin", "/Users/test/.local/bin:/usr/bin", "darwin"), ).toBe("/opt/homebrew/bin:/usr/bin:/Users/test/.local/bin"); }); it("uses the platform-specific delimiter", () => { expect(mergePathEntries("C:\\Tools;C:\\Windows", "C:\\Windows;C:\\Git", "win32")).toBe( "C:\\Tools;C:\\Windows;C:\\Git", ); }); });