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.
This commit is contained in:
yuez committed 2026-10-23 10:08:15 +08:00
1 parent 64a1a98c1f
commit 0193230cd3
2 files changed
+94

No files matched your search

+30
View File
@@ -55,6 +55,7 @@ export interface DoctorContext {
nodeVersion: string; nodeVersion: string;
nvim: BinaryProbe; nvim: BinaryProbe;
pi: BinaryProbe; pi: BinaryProbe;
curl: BinaryProbe;
plugin: { dir: string | undefined; bridgeFound: boolean }; plugin: { dir: string | undefined; bridgeFound: boolean };
runtime: { path: string; exists: boolean; mode: number | undefined }; runtime: { path: string; exists: boolean; mode: number | undefined };
cliToken: { 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]; 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_NODE: readonly [number, number, number] = [22, 0, 0];
export const MIN_NVIM: readonly [number, number, number] = [0, 9, 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"})` }; 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 { export function checkPlugin(probe: DoctorContext["plugin"]): DoctorCheck {
if (probe.bridgeFound) { if (probe.bridgeFound) {
return { name: "plugin", status: "ok", detail: `${probe.dir ?? "pi2.nvim"} (lua/pi/web/bridge.lua)` }; 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: "node", run: () => checkNode(ctx.nodeVersion) },
{ name: "nvim", run: () => checkNvim(ctx.nvim) }, { name: "nvim", run: () => checkNvim(ctx.nvim) },
{ name: "pi", run: () => checkPi(ctx.pi) }, { name: "pi", run: () => checkPi(ctx.pi) },
{ name: "curl", run: () => checkCurl(ctx.curl) },
{ name: "plugin", run: () => checkPlugin(ctx.plugin) }, { name: "plugin", run: () => checkPlugin(ctx.plugin) },
{ name: "runtime-dir", run: () => checkRuntimeDir(ctx.runtime) }, { name: "runtime-dir", run: () => checkRuntimeDir(ctx.runtime) },
{ name: "cli-token", run: () => checkCliToken(ctx.cliToken) }, { name: "cli-token", run: () => checkCliToken(ctx.cliToken) },
@@ -383,6 +411,7 @@ export async function collectContext(options: DoctorOptions = {}): Promise<Docto
if (parsed !== undefined) nvim.version = `v${parsed}`; if (parsed !== undefined) nvim.version = `v${parsed}`;
} }
const pi = await binaryProbe("pi", ["--version"], options, env); const pi = await binaryProbe("pi", ["--version"], options, env);
const curl = await binaryProbe("curl", ["--version"], options, env);
const pluginDir = env.PI2_PLUGIN_DIR?.trim() || join(home, ".local", "share", "nvim", "lazy", "pi2.nvim"); const pluginDir = env.PI2_PLUGIN_DIR?.trim() || join(home, ".local", "share", "nvim", "lazy", "pi2.nvim");
const bridgeFound = existsSync(join(pluginDir, "lua", "pi", "web", "bridge.lua")); const bridgeFound = existsSync(join(pluginDir, "lua", "pi", "web", "bridge.lua"));
@@ -436,6 +465,7 @@ export async function collectContext(options: DoctorOptions = {}): Promise<Docto
nodeVersion: process.version, nodeVersion: process.version,
nvim, nvim,
pi, pi,
curl,
plugin: { dir: pluginDir, bridgeFound }, plugin: { dir: pluginDir, bridgeFound },
runtime: { path: runtime, exists: runtimeStat.exists, mode: runtimeStat.mode }, runtime: { path: runtime, exists: runtimeStat.exists, mode: runtimeStat.mode },
cliToken: { path: cliTokenPath, exists: tokenStat.exists, mode: tokenStat.mode }, cliToken: { path: cliTokenPath, exists: tokenStat.exists, mode: tokenStat.mode },
+64
View File
@@ -4,6 +4,7 @@ import { join } from "node:path";
import { import {
checkAccess, checkAccess,
checkCliToken, checkCliToken,
checkCurl,
checkLinger, checkLinger,
checkNvim, checkNvim,
checkNode, checkNode,
@@ -11,6 +12,7 @@ import {
collectContext, collectContext,
defaultProbes, defaultProbes,
formatReport, formatReport,
parseCurlVersion,
parseNvimVersion, parseNvimVersion,
parseVersion, parseVersion,
reportToJson, reportToJson,
@@ -26,6 +28,11 @@ function context(overrides: Partial<DoctorContext> = {}): DoctorContext {
nodeVersion: "v22.11.0", nodeVersion: "v22.11.0",
nvim: { found: true, path: "/usr/bin/nvim", version: "v0.10.2" }, nvim: { found: true, path: "/usr/bin/nvim", version: "v0.10.2" },
pi: { found: true, path: "/usr/local/bin/pi", version: "1.8.0" }, 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 }, plugin: { dir: "/home/u/.local/share/nvim/lazy/pi2.nvim", bridgeFound: true },
runtime: { path: "/run/user/1000/pi2", exists: true, mode: 0o700 }, runtime: { path: "/run/user/1000/pi2", exists: true, mode: 0o700 },
cliToken: { path: "/run/user/1000/pi2/cli.token", exists: true, mode: 0o600 }, cliToken: { path: "/run/user/1000/pi2/cli.token", exists: true, mode: 0o600 },
@@ -56,6 +63,7 @@ describe("doctor checks (all passing)", () => {
"node", "node",
"nvim", "nvim",
"pi", "pi",
"curl",
"plugin", "plugin",
"runtime-dir", "runtime-dir",
"cli-token", "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", () => { describe("doctor failure cases", () => {
it("fails on node < 22", async () => { it("fails on node < 22", async () => {
const report = await checks({ nodeVersion: "v20.11.0" }); const report = await checks({ nodeVersion: "v20.11.0" });
@@ -301,6 +361,9 @@ describe("collectContext", () => {
run: async (command, args) => { run: async (command, args) => {
if (command === "nvim") return { ok: true, stdout: "NVIM v0.10.2\nBuild type: Release\n", stderr: "" }; 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 === "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: "" }; if (command === "loginctl" && args[1] === "1000") return { ok: true, stdout: "Linger=yes\n", stderr: "" };
return { ok: false, stdout: "", stderr: "unexpected" }; 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.nvim).toEqual({ found: true, path: "/usr/bin/nvim", version: "v0.10.2" });
expect(ctx.pi.version).toBe("1.8.0"); 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.plugin.bridgeFound).toBe(true);
expect(ctx.runtime.exists).toBe(true); expect(ctx.runtime.exists).toBe(true);
expect(ctx.cliToken.mode).toBe(0o600); expect(ctx.cliToken.mode).toBe(0o600);