From 0193230cd3aa4a80cf6f2d6d26f02e7592235ef0 Mon Sep 17 00:00:00 2001 From: yuez Date: Fri, 23 Oct 2026 10:08:15 +0800 Subject: [PATCH] feat(cli): flag a missing curl in pi2 doctor `:PiWeb` signs the local browser in through curl (Neovim has no HTTP client), so a missing binary breaks the primary onboarding path. The new `curl` check sits with the other binary probes, warns instead of failing, and can never flip `doctor`'s exit code. --- src/cli/doctor.ts | 30 +++++++++++++++++++ test/cli/doctor.test.ts | 64 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/src/cli/doctor.ts b/src/cli/doctor.ts index dfd791f..1e98645 100644 --- a/src/cli/doctor.ts +++ b/src/cli/doctor.ts @@ -55,6 +55,7 @@ export interface DoctorContext { nodeVersion: string; nvim: BinaryProbe; pi: BinaryProbe; + curl: BinaryProbe; plugin: { dir: string | undefined; bridgeFound: boolean }; runtime: { path: string; exists: boolean; mode: number | undefined }; cliToken: { path: string; exists: boolean; mode: number | undefined }; @@ -90,6 +91,13 @@ export function parseNvimVersion(output: string): string | undefined { return match?.[1]; } +/** `curl 8.7.1 (x86_64-pc-linux-gnu) …` (first line of `curl --version`) → `8.7.1`. */ +export function parseCurlVersion(output: string | undefined): string | undefined { + if (output === undefined) return undefined; + const match = /^curl\s+v?(\d+\.\d+\.\d+)/.exec(output.trim()); + return match?.[1]; +} + export const MIN_NODE: readonly [number, number, number] = [22, 0, 0]; export const MIN_NVIM: readonly [number, number, number] = [0, 9, 0]; @@ -130,6 +138,25 @@ export function checkPi(probe: BinaryProbe): DoctorCheck { return { name: "pi", status: "ok", detail: `${probe.version?.trim() ?? "unknown version"} (${probe.path ?? "pi"})` }; } +/** + * `curl`: `:PiWeb`'s login handshake shells out to curl (Neovim has no HTTP + * client), so a missing binary only breaks local sign-in — warn, never fail. + */ +export function checkCurl(probe: BinaryProbe): DoctorCheck { + if (!probe.found) { + return { + name: "curl", + status: "warn", + detail: "not found on PATH (:PiWeb cannot sign the browser in automatically; pair instead)", + }; + } + const version = parseCurlVersion(probe.version); + if (version === undefined) { + return { name: "curl", status: "warn", detail: `could not parse version of ${probe.path ?? "curl"}` }; + } + return { name: "curl", status: "ok", detail: `${version} (${probe.path ?? "curl"})` }; +} + export function checkPlugin(probe: DoctorContext["plugin"]): DoctorCheck { if (probe.bridgeFound) { return { name: "plugin", status: "ok", detail: `${probe.dir ?? "pi2.nvim"} (lua/pi/web/bridge.lua)` }; @@ -252,6 +279,7 @@ export function defaultProbes(ctx: DoctorContext): DoctorProbe[] { { name: "node", run: () => checkNode(ctx.nodeVersion) }, { name: "nvim", run: () => checkNvim(ctx.nvim) }, { name: "pi", run: () => checkPi(ctx.pi) }, + { name: "curl", run: () => checkCurl(ctx.curl) }, { name: "plugin", run: () => checkPlugin(ctx.plugin) }, { name: "runtime-dir", run: () => checkRuntimeDir(ctx.runtime) }, { name: "cli-token", run: () => checkCliToken(ctx.cliToken) }, @@ -383,6 +411,7 @@ export async function collectContext(options: DoctorOptions = {}): Promise = {}): DoctorContext { nodeVersion: "v22.11.0", nvim: { found: true, path: "/usr/bin/nvim", version: "v0.10.2" }, pi: { found: true, path: "/usr/local/bin/pi", version: "1.8.0" }, + curl: { + found: true, + path: "/usr/bin/curl", + version: "curl 8.7.1 (x86_64-pc-linux-gnu) libcurl/8.7.1 OpenSSL/3.3.1", + }, plugin: { dir: "/home/u/.local/share/nvim/lazy/pi2.nvim", bridgeFound: true }, runtime: { path: "/run/user/1000/pi2", exists: true, mode: 0o700 }, cliToken: { path: "/run/user/1000/pi2/cli.token", exists: true, mode: 0o600 }, @@ -56,6 +63,7 @@ describe("doctor checks (all passing)", () => { "node", "nvim", "pi", + "curl", "plugin", "runtime-dir", "cli-token", @@ -133,6 +141,58 @@ describe("doctor access check", () => { }); }); +describe("doctor curl check", () => { + it("is ok and reports the parsed version with its path", () => { + const check = checkCurl({ found: true, path: "/usr/bin/curl", version: "curl 8.7.1 (x86_64-pc-linux-gnu) libcurl/8.7.1" }); + expect(check).toEqual({ name: "curl", status: "ok", detail: "8.7.1 (/usr/bin/curl)" }); + }); + + it("warns with an actionable hint when curl is missing", async () => { + const check = checkCurl({ found: false, error: "curl not found on PATH" }); + expect(check).toEqual({ + name: "curl", + status: "warn", + detail: "not found on PATH (:PiWeb cannot sign the browser in automatically; pair instead)", + }); + expect(check.detail).toContain("pair instead"); + + const report = await checks({ curl: { found: false } }); + expect(statusOf(report, "curl")).toBe("warn"); + // A warn keeps the overall report ok. + expect(report.ok).toBe(true); + }); + + it("warns when the version cannot be parsed", () => { + expect(checkCurl({ found: true, path: "/usr/bin/curl", version: "garbage" })).toEqual({ + name: "curl", + status: "warn", + detail: "could not parse version of /usr/bin/curl", + }); + expect(checkCurl({ found: true, path: "/usr/bin/curl" }).status).toBe("warn"); + expect(parseCurlVersion("curl 8.7.1 (x86_64-pc-linux-gnu) libcurl/8.7.1")).toBe("8.7.1"); + expect(parseCurlVersion("garbage")).toBeUndefined(); + expect(parseCurlVersion(undefined)).toBeUndefined(); + }); + + it("is wired into the report right after pi, in the table and in --json", async () => { + const report = await checks(); + const names = report.checks.map((check) => check.name); + expect(names.indexOf("curl")).toBe(names.indexOf("pi") + 1); + expect(names.indexOf("plugin")).toBe(names.indexOf("curl") + 1); + expect(report.checks.find((check) => check.name === "curl")).toEqual({ + name: "curl", + status: "ok", + detail: "8.7.1 (/usr/bin/curl)", + }); + expect(formatReport(report)).toMatch(/ok\s+curl\s+8\.7\.1 \(\/usr\/bin\/curl\)/); + + const parsed = JSON.parse(reportToJson(report)) as { ok: boolean; checks: { name: string; status: string; detail: string }[] }; + expect(parsed.ok).toBe(true); + expect(parsed.checks.map((check) => check.name).indexOf("curl")).toBe(names.indexOf("pi") + 1); + expect(parsed.checks.find((check) => check.name === "curl")?.detail).toBe("8.7.1 (/usr/bin/curl)"); + }); +}); + describe("doctor failure cases", () => { it("fails on node < 22", async () => { const report = await checks({ nodeVersion: "v20.11.0" }); @@ -301,6 +361,9 @@ describe("collectContext", () => { run: async (command, args) => { if (command === "nvim") return { ok: true, stdout: "NVIM v0.10.2\nBuild type: Release\n", stderr: "" }; if (command === "pi") return { ok: true, stdout: "1.8.0\n", stderr: "" }; + if (command === "curl") { + return { ok: true, stdout: "curl 8.7.1 (x86_64-pc-linux-gnu) libcurl/8.7.1\nRelease-Date: 2024-03-27\n", stderr: "" }; + } if (command === "loginctl" && args[1] === "1000") return { ok: true, stdout: "Linger=yes\n", stderr: "" }; return { ok: false, stdout: "", stderr: "unexpected" }; }, @@ -309,6 +372,7 @@ describe("collectContext", () => { expect(ctx.nvim).toEqual({ found: true, path: "/usr/bin/nvim", version: "v0.10.2" }); expect(ctx.pi.version).toBe("1.8.0"); + expect(ctx.curl).toEqual({ found: true, path: "/usr/bin/curl", version: "curl 8.7.1 (x86_64-pc-linux-gnu) libcurl/8.7.1" }); expect(ctx.plugin.bridgeFound).toBe(true); expect(ctx.runtime.exists).toBe(true); expect(ctx.cliToken.mode).toBe(0o600);