|
| 1 | +import { EventEmitter } from "node:events"; |
| 2 | +import type { spawn as nodeSpawn } from "node:child_process"; |
| 3 | + |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | + |
| 6 | +import { installLatestPackage } from "../src/platform/update/install.js"; |
| 7 | + |
| 8 | +type PlatformSpawnFn = typeof nodeSpawn; |
| 9 | + |
| 10 | +function fakeSpawn(exitCode: number | null): { |
| 11 | + spawnFn: PlatformSpawnFn; |
| 12 | + calls: Array<{ |
| 13 | + command: string; |
| 14 | + args: readonly string[]; |
| 15 | + options: unknown; |
| 16 | + }>; |
| 17 | +} { |
| 18 | + const calls: Array<{ |
| 19 | + command: string; |
| 20 | + args: readonly string[]; |
| 21 | + options: unknown; |
| 22 | + }> = []; |
| 23 | + const spawnFn = ((command, args, options) => { |
| 24 | + calls.push({ |
| 25 | + command, |
| 26 | + args: args ?? [], |
| 27 | + options, |
| 28 | + }); |
| 29 | + const emitter = new EventEmitter(); |
| 30 | + queueMicrotask(() => { |
| 31 | + emitter.emit("exit", exitCode, null); |
| 32 | + }); |
| 33 | + return emitter as ReturnType<PlatformSpawnFn>; |
| 34 | + }) as PlatformSpawnFn; |
| 35 | + return { spawnFn, calls }; |
| 36 | +} |
| 37 | + |
| 38 | +function fakeSpawnError(code: string): PlatformSpawnFn { |
| 39 | + return (() => { |
| 40 | + const emitter = new EventEmitter(); |
| 41 | + queueMicrotask(() => { |
| 42 | + const err = new Error(`spawn npm ${code}`) as NodeJS.ErrnoException; |
| 43 | + err.code = code; |
| 44 | + emitter.emit("error", err); |
| 45 | + }); |
| 46 | + return emitter as ReturnType<PlatformSpawnFn>; |
| 47 | + }) as PlatformSpawnFn; |
| 48 | +} |
| 49 | + |
| 50 | +describe("installLatestPackage", () => { |
| 51 | + it("spawns npm with the global codealmanac latest install command", async () => { |
| 52 | + const { spawnFn, calls } = fakeSpawn(0); |
| 53 | + |
| 54 | + const result = await installLatestPackage({ spawnFn }); |
| 55 | + |
| 56 | + expect(result).toEqual({ |
| 57 | + stdout: "almanac: updated.\n", |
| 58 | + stderr: "", |
| 59 | + exitCode: 0, |
| 60 | + }); |
| 61 | + expect(calls).toEqual([ |
| 62 | + { |
| 63 | + command: "npm", |
| 64 | + args: ["i", "-g", "codealmanac@latest"], |
| 65 | + options: { stdio: "inherit" }, |
| 66 | + }, |
| 67 | + ]); |
| 68 | + }); |
| 69 | + |
| 70 | + it("returns a clear message when npm is not on PATH", async () => { |
| 71 | + const result = await installLatestPackage({ |
| 72 | + spawnFn: fakeSpawnError("ENOENT"), |
| 73 | + }); |
| 74 | + |
| 75 | + expect(result.exitCode).toBe(1); |
| 76 | + expect(result.stderr).toContain("`npm` not found on PATH"); |
| 77 | + }); |
| 78 | + |
| 79 | + it("returns the npm exit code and permission hint on install failure", async () => { |
| 80 | + const { spawnFn } = fakeSpawn(243); |
| 81 | + |
| 82 | + const result = await installLatestPackage({ spawnFn }); |
| 83 | + |
| 84 | + expect(result.exitCode).toBe(243); |
| 85 | + expect(result.stderr).toContain("EACCES"); |
| 86 | + expect(result.stderr).toContain("sudo npm i -g codealmanac@latest"); |
| 87 | + }); |
| 88 | +}); |
0 commit comments