config.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "fs";
  2. import { tmpdir } from "os";
  3. import { delimiter, join } from "path";
  4. import { afterEach, describe, expect, test } from "vitest";
  5. import {
  6. detectInstallMethod,
  7. getSelfUpdateCommand,
  8. getSelfUpdateUnavailableInstruction,
  9. getUpdateInstruction,
  10. } from "../src/config.ts";
  11. const execPathDescriptor = Object.getOwnPropertyDescriptor(process, "execPath");
  12. const originalPath = process.env.PATH;
  13. const originalPiPackageDir = process.env.PI_PACKAGE_DIR;
  14. const originalArgv1 = process.argv[1];
  15. let tempDir: string | undefined;
  16. function setExecPath(value: string): void {
  17. Object.defineProperty(process, "execPath", {
  18. value,
  19. configurable: true,
  20. });
  21. }
  22. afterEach(() => {
  23. if (execPathDescriptor) {
  24. Object.defineProperty(process, "execPath", execPathDescriptor);
  25. }
  26. if (originalPath === undefined) {
  27. delete process.env.PATH;
  28. } else {
  29. process.env.PATH = originalPath;
  30. }
  31. if (originalPiPackageDir === undefined) {
  32. delete process.env.PI_PACKAGE_DIR;
  33. } else {
  34. process.env.PI_PACKAGE_DIR = originalPiPackageDir;
  35. }
  36. if (originalArgv1 === undefined) {
  37. process.argv.splice(1, 1);
  38. } else {
  39. process.argv[1] = originalArgv1;
  40. }
  41. if (tempDir) {
  42. chmodSync(tempDir, 0o700);
  43. rmSync(tempDir, { recursive: true, force: true });
  44. tempDir = undefined;
  45. }
  46. });
  47. function createNpmPrefixInstall(template = "pi-prefix-"): { prefix: string; packageDir: string } {
  48. const prefix = mkdtempSync(join(tmpdir(), template));
  49. const root = join(prefix, "lib", "node_modules");
  50. const scopeDir = join(root, "@earendil-works");
  51. const packageDir = join(scopeDir, "pi-coding-agent");
  52. mkdirSync(packageDir, { recursive: true });
  53. tempDir = prefix;
  54. process.env.PI_PACKAGE_DIR = packageDir;
  55. setExecPath(join(packageDir, "dist", "cli.js"));
  56. return { prefix, packageDir };
  57. }
  58. function createPnpmGlobalInstall(): { root: string; packageDir: string } {
  59. const temp = mkdtempSync(join(tmpdir(), "pi-pnpm-"));
  60. const binDir = join(temp, "bin");
  61. const root = join(temp, "pnpm", "global", "5", "node_modules");
  62. const packageDir = join(root, "@mariozechner", "pi-coding-agent");
  63. mkdirSync(packageDir, { recursive: true });
  64. mkdirSync(binDir, { recursive: true });
  65. writeFileSync(join(binDir, process.platform === "win32" ? "pnpm.cmd" : "pnpm"), createFakePnpmScript(root));
  66. chmodSync(join(binDir, process.platform === "win32" ? "pnpm.cmd" : "pnpm"), 0o755);
  67. tempDir = temp;
  68. process.env.PATH = `${binDir}${delimiter}${originalPath ?? ""}`;
  69. process.env.PI_PACKAGE_DIR = packageDir;
  70. setExecPath(
  71. join(
  72. root,
  73. ".pnpm",
  74. "@mariozechner+pi-coding-agent@0.0.0",
  75. "node_modules",
  76. "@mariozechner",
  77. "pi-coding-agent",
  78. "dist",
  79. "cli.js",
  80. ),
  81. );
  82. return { root, packageDir };
  83. }
  84. function createYarnGlobalInstall(): { globalDir: string; packageDir: string } {
  85. const temp = mkdtempSync(join(tmpdir(), "pi-yarn-"));
  86. const binDir = join(temp, "bin");
  87. const globalDir = join(temp, "yarn", "global");
  88. const packageDir = join(globalDir, "node_modules", "@mariozechner", "pi-coding-agent");
  89. mkdirSync(packageDir, { recursive: true });
  90. mkdirSync(binDir, { recursive: true });
  91. writeFileSync(join(binDir, process.platform === "win32" ? "yarn.cmd" : "yarn"), createFakeYarnScript(globalDir));
  92. chmodSync(join(binDir, process.platform === "win32" ? "yarn.cmd" : "yarn"), 0o755);
  93. tempDir = temp;
  94. process.env.PATH = `${binDir}${delimiter}${originalPath ?? ""}`;
  95. process.env.PI_PACKAGE_DIR = packageDir;
  96. setExecPath(join(globalDir, ".yarn", "@mariozechner", "pi-coding-agent", "dist", "cli.js"));
  97. return { globalDir, packageDir };
  98. }
  99. function createBunGlobalInstall(): { packageDir: string } {
  100. const temp = mkdtempSync(join(tmpdir(), "pi-bun-"));
  101. const prefix = join(temp, ".bun");
  102. const bunBin = join(prefix, "bin");
  103. const root = join(prefix, "install", "global", "node_modules");
  104. const scopeDir = join(root, "@earendil-works");
  105. const packageDir = join(scopeDir, "pi-coding-agent");
  106. mkdirSync(packageDir, { recursive: true });
  107. mkdirSync(bunBin, { recursive: true });
  108. writeFileSync(join(bunBin, process.platform === "win32" ? "bun.cmd" : "bun"), createFakeBunScript(bunBin));
  109. chmodSync(join(bunBin, process.platform === "win32" ? "bun.cmd" : "bun"), 0o755);
  110. tempDir = temp;
  111. process.env.PATH = `${bunBin}${delimiter}${originalPath ?? ""}`;
  112. process.env.PI_PACKAGE_DIR = packageDir;
  113. setExecPath(join(packageDir, "dist", "cli.js"));
  114. return { packageDir };
  115. }
  116. function createFakePnpmScript(root: string): string {
  117. if (process.platform === "win32") {
  118. return `@echo off\r\nif "%1"=="root" if "%2"=="-g" echo ${root}\r\n`;
  119. }
  120. const escapedRoot = root.replaceAll("'", "'\\''");
  121. return `#!/bin/sh\nif [ "$1" = "root" ] && [ "$2" = "-g" ]; then\n\tprintf '%s\\n' '${escapedRoot}'\n\texit 0\nfi\nexit 1\n`;
  122. }
  123. function createFakeYarnScript(globalDir: string): string {
  124. if (process.platform === "win32") {
  125. return `@echo off\r\nif "%1"=="global" if "%2"=="dir" echo ${globalDir}\r\n`;
  126. }
  127. const escapedGlobalDir = globalDir.replaceAll("'", "'\\''");
  128. return `#!/bin/sh\nif [ "$1" = "global" ] && [ "$2" = "dir" ]; then\n\tprintf '%s\\n' '${escapedGlobalDir}'\n\texit 0\nfi\nexit 1\n`;
  129. }
  130. function createFakeBunScript(bunBin: string): string {
  131. if (process.platform === "win32") {
  132. return `@echo off\r\nif "%1"=="pm" if "%2"=="bin" if "%3"=="-g" echo ${bunBin}\r\n`;
  133. }
  134. const escapedBunBin = bunBin.replaceAll("'", "'\\''");
  135. return `#!/bin/sh\nif [ "$1" = "pm" ] && [ "$2" = "bin" ] && [ "$3" = "-g" ]; then\n\tprintf '%s\\n' '${escapedBunBin}'\n\texit 0\nfi\nexit 1\n`;
  136. }
  137. describe("detectInstallMethod", () => {
  138. test("detects pnpm from Windows .pnpm install paths", () => {
  139. setExecPath(
  140. "C:\\Users\\Admin\\Documents\\pnpm-repository\\global\\5\\.pnpm\\@earendil-works+pi-coding-agent@0.67.68\\node_modules\\@earendil-works\\pi-coding-agent\\dist\\cli.js",
  141. );
  142. expect(detectInstallMethod()).toBe("pnpm");
  143. expect(getUpdateInstruction("@earendil-works/pi-coding-agent")).toBe(
  144. "Run: pnpm install -g --ignore-scripts --config.minimumReleaseAge=0 @earendil-works/pi-coding-agent",
  145. );
  146. });
  147. test("does not self-update unknown wrapper installs", () => {
  148. setExecPath("/usr/local/bin/node");
  149. expect(detectInstallMethod()).toBe("unknown");
  150. expect(getSelfUpdateCommand("@earendil-works/pi-coding-agent")).toBeUndefined();
  151. expect(getUpdateInstruction("@earendil-works/pi-coding-agent")).toBe(
  152. "Update @earendil-works/pi-coding-agent using the package manager, wrapper, or source checkout that provides this installation.",
  153. );
  154. });
  155. test("self-updates npm installs from custom prefixes", () => {
  156. const { prefix } = createNpmPrefixInstall();
  157. const command = getSelfUpdateCommand("@earendil-works/pi-coding-agent");
  158. expect(detectInstallMethod()).toBe("npm");
  159. expect(command).toEqual({
  160. command: "npm",
  161. args: [
  162. "--prefix",
  163. prefix,
  164. "install",
  165. "-g",
  166. "--ignore-scripts",
  167. "--min-release-age=0",
  168. "@earendil-works/pi-coding-agent",
  169. ],
  170. display: `npm --prefix ${prefix} install -g --ignore-scripts --min-release-age=0 @earendil-works/pi-coding-agent`,
  171. });
  172. });
  173. test("self-updates renamed packages from the current install prefix", () => {
  174. const { prefix } = createNpmPrefixInstall();
  175. const command = getSelfUpdateCommand("@mariozechner/pi-coding-agent", undefined, "@new-scope/pi");
  176. expect(command).toEqual({
  177. command: "npm",
  178. args: ["--prefix", prefix, "install", "-g", "--ignore-scripts", "--min-release-age=0", "@new-scope/pi"],
  179. display: `npm --prefix ${prefix} uninstall -g @mariozechner/pi-coding-agent && npm --prefix ${prefix} install -g --ignore-scripts --min-release-age=0 @new-scope/pi`,
  180. steps: [
  181. {
  182. command: "npm",
  183. args: ["--prefix", prefix, "uninstall", "-g", "@mariozechner/pi-coding-agent"],
  184. display: `npm --prefix ${prefix} uninstall -g @mariozechner/pi-coding-agent`,
  185. },
  186. {
  187. command: "npm",
  188. args: ["--prefix", prefix, "install", "-g", "--ignore-scripts", "--min-release-age=0", "@new-scope/pi"],
  189. display: `npm --prefix ${prefix} install -g --ignore-scripts --min-release-age=0 @new-scope/pi`,
  190. },
  191. ],
  192. });
  193. });
  194. test("self-update respects configured npmCommand", () => {
  195. const { prefix } = createNpmPrefixInstall();
  196. const command = getSelfUpdateCommand("@earendil-works/pi-coding-agent", ["npm", "--prefix", prefix]);
  197. expect(command).toEqual({
  198. command: "npm",
  199. args: [
  200. "--prefix",
  201. prefix,
  202. "install",
  203. "-g",
  204. "--ignore-scripts",
  205. "--min-release-age=0",
  206. "@earendil-works/pi-coding-agent",
  207. ],
  208. display: `npm --prefix ${prefix} install -g --ignore-scripts --min-release-age=0 @earendil-works/pi-coding-agent`,
  209. });
  210. });
  211. test("self-update treats empty npmCommand as unset", () => {
  212. const { prefix } = createNpmPrefixInstall();
  213. const command = getSelfUpdateCommand("@earendil-works/pi-coding-agent", []);
  214. expect(command?.args).toEqual([
  215. "--prefix",
  216. prefix,
  217. "install",
  218. "-g",
  219. "--ignore-scripts",
  220. "--min-release-age=0",
  221. "@earendil-works/pi-coding-agent",
  222. ]);
  223. });
  224. test("quotes npm self-update display paths", () => {
  225. const { prefix } = createNpmPrefixInstall("pi prefix ");
  226. const command = getSelfUpdateCommand("@earendil-works/pi-coding-agent");
  227. expect(command?.display).toBe(
  228. `npm --prefix "${prefix}" install -g --ignore-scripts --min-release-age=0 @earendil-works/pi-coding-agent`,
  229. );
  230. });
  231. test("does not infer Windows npm custom prefixes from package paths", () => {
  232. const packageDir = "C:\\Users\\Admin\\npm prefix\\node_modules\\@earendil-works\\pi-coding-agent";
  233. process.env.PI_PACKAGE_DIR = packageDir;
  234. setExecPath(`${packageDir}\\dist\\cli.js`);
  235. expect(detectInstallMethod()).toBe("npm");
  236. expect(getUpdateInstruction("@earendil-works/pi-coding-agent")).toBe(
  237. "Run: npm install -g --ignore-scripts --min-release-age=0 @earendil-works/pi-coding-agent",
  238. );
  239. });
  240. test("self-updates bun global installs from bun pm bin", () => {
  241. createBunGlobalInstall();
  242. const command = getSelfUpdateCommand("@earendil-works/pi-coding-agent");
  243. expect(detectInstallMethod()).toBe("bun");
  244. expect(command).toEqual({
  245. command: "bun",
  246. args: ["install", "-g", "--ignore-scripts", "--minimum-release-age=0", "@earendil-works/pi-coding-agent"],
  247. display: "bun install -g --ignore-scripts --minimum-release-age=0 @earendil-works/pi-coding-agent",
  248. });
  249. });
  250. test("self-updates renamed pnpm global installs by removing the old package first", () => {
  251. createPnpmGlobalInstall();
  252. const command = getSelfUpdateCommand("@mariozechner/pi-coding-agent", undefined, "@new-scope/pi");
  253. expect(detectInstallMethod()).toBe("pnpm");
  254. expect(command).toEqual({
  255. command: "pnpm",
  256. args: ["install", "-g", "--ignore-scripts", "--config.minimumReleaseAge=0", "@new-scope/pi"],
  257. display:
  258. "pnpm remove -g @mariozechner/pi-coding-agent && pnpm install -g --ignore-scripts --config.minimumReleaseAge=0 @new-scope/pi",
  259. steps: [
  260. {
  261. command: "pnpm",
  262. args: ["remove", "-g", "@mariozechner/pi-coding-agent"],
  263. display: "pnpm remove -g @mariozechner/pi-coding-agent",
  264. },
  265. {
  266. command: "pnpm",
  267. args: ["install", "-g", "--ignore-scripts", "--config.minimumReleaseAge=0", "@new-scope/pi"],
  268. display: "pnpm install -g --ignore-scripts --config.minimumReleaseAge=0 @new-scope/pi",
  269. },
  270. ],
  271. });
  272. });
  273. test("self-updates pnpm v11 global installs resolved through the store", () => {
  274. const temp = mkdtempSync(join(tmpdir(), "pi-pnpm11-"));
  275. const binDir = join(temp, "bin");
  276. const root = join(temp, "Library", "pnpm", "global", "v11");
  277. const packageName = "@earendil-works/pi-coding-agent";
  278. const globalPackageDir = join(root, "11e9a", "node_modules", "@earendil-works", "pi-coding-agent");
  279. const storePackageDir = join(
  280. temp,
  281. "Library",
  282. "pnpm",
  283. "store",
  284. "v11",
  285. "links",
  286. "@earendil-works",
  287. "pi-coding-agent",
  288. "0.75.0",
  289. "hash",
  290. "node_modules",
  291. "@earendil-works",
  292. "pi-coding-agent",
  293. );
  294. mkdirSync(globalPackageDir, { recursive: true });
  295. mkdirSync(storePackageDir, { recursive: true });
  296. mkdirSync(binDir, { recursive: true });
  297. writeFileSync(join(globalPackageDir, "package.json"), "{}");
  298. writeFileSync(join(binDir, process.platform === "win32" ? "pnpm.cmd" : "pnpm"), createFakePnpmScript(root));
  299. chmodSync(join(binDir, process.platform === "win32" ? "pnpm.cmd" : "pnpm"), 0o755);
  300. tempDir = temp;
  301. process.env.PATH = `${binDir}${delimiter}${originalPath ?? ""}`;
  302. process.env.PI_PACKAGE_DIR = storePackageDir;
  303. process.argv[1] = join(globalPackageDir, "dist", "cli.js");
  304. setExecPath(join(storePackageDir, "dist", "cli.js"));
  305. const command = getSelfUpdateCommand(packageName);
  306. expect(detectInstallMethod()).toBe("pnpm");
  307. expect(command).toEqual({
  308. command: "pnpm",
  309. args: ["install", "-g", "--ignore-scripts", "--config.minimumReleaseAge=0", packageName],
  310. display: `pnpm install -g --ignore-scripts --config.minimumReleaseAge=0 ${packageName}`,
  311. });
  312. });
  313. test("self-updates renamed yarn global installs by removing the old package first", () => {
  314. createYarnGlobalInstall();
  315. const command = getSelfUpdateCommand("@mariozechner/pi-coding-agent", undefined, "@new-scope/pi");
  316. expect(detectInstallMethod()).toBe("yarn");
  317. expect(command).toEqual({
  318. command: "yarn",
  319. args: ["global", "add", "--ignore-scripts", "@new-scope/pi"],
  320. display: "yarn global remove @mariozechner/pi-coding-agent && yarn global add --ignore-scripts @new-scope/pi",
  321. steps: [
  322. {
  323. command: "yarn",
  324. args: ["global", "remove", "@mariozechner/pi-coding-agent"],
  325. display: "yarn global remove @mariozechner/pi-coding-agent",
  326. },
  327. {
  328. command: "yarn",
  329. args: ["global", "add", "--ignore-scripts", "@new-scope/pi"],
  330. display: "yarn global add --ignore-scripts @new-scope/pi",
  331. },
  332. ],
  333. });
  334. });
  335. test("self-updates renamed bun global installs by removing the old package first", () => {
  336. createBunGlobalInstall();
  337. const command = getSelfUpdateCommand("@mariozechner/pi-coding-agent", undefined, "@new-scope/pi");
  338. expect(detectInstallMethod()).toBe("bun");
  339. expect(command).toEqual({
  340. command: "bun",
  341. args: ["install", "-g", "--ignore-scripts", "--minimum-release-age=0", "@new-scope/pi"],
  342. display:
  343. "bun uninstall -g @mariozechner/pi-coding-agent && bun install -g --ignore-scripts --minimum-release-age=0 @new-scope/pi",
  344. steps: [
  345. {
  346. command: "bun",
  347. args: ["uninstall", "-g", "@mariozechner/pi-coding-agent"],
  348. display: "bun uninstall -g @mariozechner/pi-coding-agent",
  349. },
  350. {
  351. command: "bun",
  352. args: ["install", "-g", "--ignore-scripts", "--minimum-release-age=0", "@new-scope/pi"],
  353. display: "bun install -g --ignore-scripts --minimum-release-age=0 @new-scope/pi",
  354. },
  355. ],
  356. });
  357. });
  358. test("does not self-update when npm install path is not writable", () => {
  359. const { packageDir } = createNpmPrefixInstall();
  360. chmodSync(packageDir, 0o500);
  361. expect(getSelfUpdateCommand("@earendil-works/pi-coding-agent")).toBeUndefined();
  362. expect(getSelfUpdateUnavailableInstruction("@earendil-works/pi-coding-agent")).toContain(
  363. "the install path is not writable",
  364. );
  365. });
  366. });