diff --git a/src/client/auth.ts b/src/client/auth.ts index 80747b2..f9a0535 100644 --- a/src/client/auth.ts +++ b/src/client/auth.ts @@ -83,15 +83,23 @@ async function writeCredential(credential: StoredCredential): Promise { await chmod(path, 0o600); } +/** The `t3` CLI: the desktop app's copy, or the one on the PATH, or where the Windows installer puts it. A .cmd only runs through cmd. */ +export function t3Command(exists: (path: string) => boolean = existsSync, which: (program: string) => string | null = Bun.which): string[] { + const t3 = + process.env.T3TUI_T3_BIN ?? + [join(t3Home(), "bin", "t3"), which("t3"), join(homedir(), ".local", "bin", "t3.cmd")].find((path): path is string => path !== null && exists(path)) ?? + "t3"; + return /\.(?:cmd|bat)$/i.test(t3) ? ["cmd.exe", "/c", "call", t3] : [t3]; +} + /** * Issues a scoped bearer session through the local `t3` CLI, which can mint one * because it shares the server's data directory. Remote servers need T3TUI_TOKEN. */ export async function issueLocalCredential(target: ServerTarget): Promise { - const t3 = process.env.T3TUI_T3_BIN ?? join(t3Home(), "bin", "t3"); const args = ["auth", "session", "issue", "--label", "t3tui", "--token-only"]; for (const scope of T3TUI_SCOPES) args.push("--scope", scope); - const proc = Bun.spawn([t3, ...args], { stdout: "pipe", stderr: "pipe" }); + const proc = Bun.spawn([...t3Command(), ...args], { stdout: "pipe", stderr: "pipe" }); const [stdout, stderr, code] = await Promise.all([ new Response(proc.stdout).text(), new Response(proc.stderr).text(), diff --git a/test/auth.test.ts b/test/auth.test.ts new file mode 100644 index 0000000..1157c74 --- /dev/null +++ b/test/auth.test.ts @@ -0,0 +1,46 @@ +import { afterEach, beforeEach, describe, expect, test } from "bun:test"; +import { homedir } from "node:os"; +import { join } from "node:path"; + +import { t3Command } from "../src/client/auth.ts"; + +const appCopy = join(homedir(), ".t3", "bin", "t3"); +const installerCopy = join(homedir(), ".local", "bin", "t3.cmd"); + +describe("t3Command", () => { + const saved = { bin: process.env.T3TUI_T3_BIN, home: process.env.T3CODE_HOME }; + + beforeEach(() => { + delete process.env.T3TUI_T3_BIN; + delete process.env.T3CODE_HOME; + }); + + afterEach(() => { + delete process.env.T3TUI_T3_BIN; + if (saved.bin !== undefined) process.env.T3TUI_T3_BIN = saved.bin; + if (saved.home !== undefined) process.env.T3CODE_HOME = saved.home; + }); + + test("prefers the desktop app's copy, then the PATH", () => { + expect(t3Command((path) => path === appCopy, () => "/usr/bin/t3")).toEqual([appCopy]); + expect(t3Command((path) => path === "/usr/bin/t3", () => "/usr/bin/t3")).toEqual(["/usr/bin/t3"]); + }); + + test("runs where the Windows installer puts it, which needs cmd", () => { + expect(t3Command((path) => path === installerCopy, () => null)).toEqual(["cmd.exe", "/c", "call", installerCopy]); + }); + + test("runs a .cmd on the PATH through cmd too", () => { + const shim = "C:\\npm\\t3.CMD"; + expect(t3Command((path) => path === shim, () => shim)).toEqual(["cmd.exe", "/c", "call", shim]); + }); + + test("falls back to the bare name so the error names it", () => { + expect(t3Command(() => false, () => null)).toEqual(["t3"]); + }); + + test("T3TUI_T3_BIN wins", () => { + process.env.T3TUI_T3_BIN = "/opt/t3"; + expect(t3Command(() => true, () => "/usr/bin/t3")).toEqual(["/opt/t3"]); + }); +});