mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-12-05 08:15:41 -08:00
Tell a failed macOS folder picker from a cancel, and start Browse at a folder that exists
AI Assisted
This commit is contained in:
1 parent
91479b21a1
commit
ddbc85f73d
5 files changed
+202
-42
No files matched your search
@@ -948,6 +948,34 @@ test("launcher app: modelFrom reads --model, --model= and Codex's -m", () => {
|
||||
assert.equal(modelFrom(["--verbose"], "claude"), null);
|
||||
});
|
||||
|
||||
// osascript exits 1 both on a cancel and on a failure; only the cancel has -128.
|
||||
test("launcher picker: on macOS an exit 1 is a cancel only with osascript's (-128); elsewhere an exit 1 with nothing picked stays a cancel", () => {
|
||||
const { pickerCancelled } = require(LAUNCHER);
|
||||
const cancel = "0:17: execution error: User canceled. (-128)\n";
|
||||
const failure = "0:4: execution error: The variable this is not defined. (-2753)\n";
|
||||
assert.equal(pickerCancelled(1, "", cancel, "darwin"), true);
|
||||
assert.equal(pickerCancelled(1, "", failure, "darwin"), false, "a failure falls through to the typed prompt");
|
||||
assert.equal(pickerCancelled(1, "", "", "darwin"), false);
|
||||
assert.equal(pickerCancelled(1, null, null, "darwin"), false);
|
||||
for (const platform of ["linux", "win32"]) {
|
||||
assert.equal(pickerCancelled(1, "", "", platform), true, platform);
|
||||
assert.equal(pickerCancelled(1, "", "Gtk-WARNING\n", platform), true, platform);
|
||||
assert.equal(pickerCancelled(3, "", "", platform), false, platform);
|
||||
}
|
||||
for (const platform of ["darwin", "linux", "win32"]) {
|
||||
assert.equal(pickerCancelled(0, "", "", platform), true, `${platform}: exit 0 with nothing picked`);
|
||||
assert.equal(pickerCancelled(0, "/some/folder\n", "", platform), false, `${platform}: a pick`);
|
||||
assert.equal(pickerCancelled(1, "/some/folder\n", cancel, platform), false, `${platform}: something printed is no cancel`);
|
||||
}
|
||||
});
|
||||
|
||||
test("launcher picker: pickFolder decides a cancel through pickerCancelled", () => {
|
||||
const source = fs.readFileSync(require.resolve(LAUNCHER), "utf8");
|
||||
const body = source.slice(source.indexOf("async function pickFolder"), source.indexOf("async function projectCwd"));
|
||||
assert.match(body, /pickerCancelled\(result\.status, result\.stdout, result\.stderr\)/);
|
||||
assert.doesNotMatch(body, /result\.status === 1/, "no cancel rule left beside it");
|
||||
});
|
||||
|
||||
test("launcher app: an app that starts and then exits with a failure falls back to the terminal flow", (t) => {
|
||||
const box = sandbox(["claude"]);
|
||||
t.after(box.cleanup);
|
||||
|
||||
@@ -9,7 +9,8 @@ import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { execFileSync, spawnSync } from "node:child_process";
|
||||
import { execFileSync, spawn, spawnSync } from "node:child_process";
|
||||
import { createRequire } from "node:module";
|
||||
import { EventEmitter } from "node:events";
|
||||
import { PassThrough } from "node:stream";
|
||||
// The page's pure answer-shaping logic. app.js itself cannot be imported here
|
||||
@@ -166,6 +167,7 @@ import {
|
||||
import { applyMention, matchNames, mentionAt } from "../src/web-console/public/mentions.js";
|
||||
import {
|
||||
pickerCommand,
|
||||
pickerOutcome,
|
||||
pickFolder,
|
||||
WINDOWS_PICKER_SOURCE,
|
||||
WORKSPACE_PICKER_PROMPT,
|
||||
@@ -187,6 +189,7 @@ import { chatReplyLines } from "../src/web-console/inbox.mjs";
|
||||
import {
|
||||
initialWorkspace,
|
||||
pendingWorkspaceChanges,
|
||||
pickerStart,
|
||||
readWorkspaceCursor,
|
||||
WORKSPACE_FILE,
|
||||
writeWorkspaceCursor,
|
||||
@@ -2351,31 +2354,106 @@ test("picker: the workspace picker cannot drift from the launcher's", () => {
|
||||
assert.equal(WORKSPACE_PICKER_PROMPT, "Choose a folder to add to the workspace");
|
||||
});
|
||||
|
||||
function fakeSpawn(code, output) {
|
||||
test("picker: the launcher and the workspace read a picker's exit the same way", () => {
|
||||
// Two copies of one rule (macOS: only osascript's (-128) is a cancel), each
|
||||
// with its own tests: this keeps an edit to one from leaving the other behind.
|
||||
const { pickerCancelled } = createRequire(import.meta.url)(path.join(ROOT, "src", "launcher", "plan2code.js"));
|
||||
const errors = ["", "0:17: execution error: User canceled. (-128)", "execution error: Not authorized to send Apple events. (-1743)", "(-1280)", "Gtk-WARNING\n"];
|
||||
for (const platform of ["darwin", "linux", "win32"]) {
|
||||
for (const code of [0, 1, 2]) {
|
||||
for (const stdout of ["", "/some/folder\n"]) {
|
||||
for (const stderr of errors) {
|
||||
const workspace = "cancelled" in pickerOutcome(code, stdout, stderr, platform);
|
||||
assert.equal(pickerCancelled(code, stdout, stderr, platform), workspace, JSON.stringify({ platform, code, stdout, stderr }));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("picker: stderr is read only on macOS, where it tells a cancel from a failure", { skip: process.platform === "win32" ? "pickerCommand needs a POSIX picker here" : false }, async () => {
|
||||
for (const [platform, expected] of [["darwin", "pipe"], ["linux", "ignore"], ["win32", "ignore"]]) {
|
||||
// Linux's picker is zenity or kdialog, found on PATH: none on this machine, nothing to start.
|
||||
if (!pickerCommand(os.tmpdir(), "p", { platform })) continue;
|
||||
let stdio = null;
|
||||
const spy = (command, args, options) => {
|
||||
stdio = options.stdio;
|
||||
return fakeSpawn(0, "", "", platform)(command, args, options);
|
||||
};
|
||||
await pickFolder(os.tmpdir(), "p", { platform, spawnImpl: spy });
|
||||
assert.ok(stdio, `${platform}: the picker was started`);
|
||||
assert.equal(stdio[2], expected, platform);
|
||||
}
|
||||
});
|
||||
|
||||
function fakeSpawn(code, output, errorText = "", platform = process.platform) {
|
||||
return (command, args, options) => {
|
||||
assert.equal(options.windowsHide, true);
|
||||
assert.equal(options.stdio[2], platform === "darwin" ? "pipe" : "ignore", "stderr is piped on macOS only, where a cancel is told from a failure");
|
||||
const child = new EventEmitter();
|
||||
child.stdout = new PassThrough();
|
||||
child.stderr = new PassThrough();
|
||||
setImmediate(() => {
|
||||
if (output) child.stdout.write(output);
|
||||
if (errorText) child.stderr.write(errorText);
|
||||
child.stdout.end();
|
||||
child.stderr.end();
|
||||
setImmediate(() => child.emit("close", code));
|
||||
});
|
||||
return child;
|
||||
};
|
||||
}
|
||||
|
||||
// What osascript prints on a cancel, and on a failure (a bad script here; a
|
||||
// missing default folder or a refused permission end the same way, exit 1).
|
||||
const MAC_CANCEL = "0:17: execution error: User canceled. (-128)\n";
|
||||
const MAC_FAILURE = "0:4: execution error: The variable this is not defined. (-2753)\n";
|
||||
|
||||
test(
|
||||
"picker: pickFolder reads a pick, a cancel and a failure off the picker's exit",
|
||||
{ skip: pickerCommand(os.tmpdir(), WORKSPACE_PICKER_PROMPT) ? false : "no folder picker on this machine" },
|
||||
async () => {
|
||||
const picked = path.join(os.tmpdir(), "picked");
|
||||
assert.deepEqual(await pickFolder(os.tmpdir(), "p", { spawnImpl: fakeSpawn(0, picked + "\n") }), { path: path.resolve(picked) });
|
||||
assert.deepEqual(await pickFolder(os.tmpdir(), "p", { spawnImpl: fakeSpawn(1, "") }), { cancelled: true });
|
||||
assert.deepEqual(await pickFolder(os.tmpdir(), "p", { spawnImpl: fakeSpawn(1, "", MAC_CANCEL) }), { cancelled: true });
|
||||
assert.deepEqual(await pickFolder(os.tmpdir(), "p", { spawnImpl: fakeSpawn(2, "") }), { error: "picker-failed" });
|
||||
}
|
||||
);
|
||||
|
||||
test("picker: on macOS only osascript's (-128) makes an exit 1 a cancel; any other exit 1 with no folder is a failure", async () => {
|
||||
const pick = (code, output, errorText) => pickFolder(os.tmpdir(), "p", { platform: "darwin", spawnImpl: fakeSpawn(code, output, errorText, "darwin") });
|
||||
assert.equal(pickerCommand(os.tmpdir(), "p", { platform: "darwin" }).command, "osascript");
|
||||
assert.deepEqual(await pick(1, "", MAC_CANCEL), { cancelled: true });
|
||||
assert.deepEqual(await pick(1, "", MAC_FAILURE), { error: "picker-failed" });
|
||||
assert.deepEqual(await pick(1, "", ""), { error: "picker-failed" }, "no stderr at all is no proof of a cancel");
|
||||
const picked = path.join(os.tmpdir(), "picked");
|
||||
assert.deepEqual(await pick(0, picked + "\n", ""), { path: path.resolve(picked) });
|
||||
});
|
||||
|
||||
test("picker: on Windows and Linux an exit 1 with no folder stays a cancel, whatever stderr says", async () => {
|
||||
assert.equal(pickerCommand(os.tmpdir(), "p", { platform: "win32" }).command, "powershell.exe");
|
||||
for (const errorText of ["", "Gtk-WARNING: some noise\n"]) {
|
||||
assert.deepEqual(await pickFolder(os.tmpdir(), "p", { platform: "win32", spawnImpl: fakeSpawn(1, "", errorText, "win32") }), { cancelled: true });
|
||||
}
|
||||
// Linux's picker needs a display and zenity or kdialog on this host, so its
|
||||
// rule is checked on the outcome itself.
|
||||
for (const platform of ["linux", "win32"]) {
|
||||
assert.deepEqual(pickerOutcome(1, "", "", platform), { cancelled: true }, platform);
|
||||
assert.deepEqual(pickerOutcome(1, "", "(-2753)", platform), { cancelled: true }, platform);
|
||||
assert.deepEqual(pickerOutcome(0, "", "", platform), { cancelled: true }, platform);
|
||||
assert.deepEqual(pickerOutcome(3, "", "", platform), { error: "picker-failed" }, platform);
|
||||
}
|
||||
assert.deepEqual(pickerOutcome(1, "", MAC_CANCEL, "darwin"), { cancelled: true });
|
||||
assert.deepEqual(pickerOutcome(1, "", MAC_FAILURE, "darwin"), { error: "picker-failed" });
|
||||
});
|
||||
|
||||
test("picker: a picker that writes a lot to stderr still finishes, and its last words decide", async () => {
|
||||
// A real child writing far past a pipe's buffer: unread, it would block before exiting.
|
||||
const chatty = (command, args, options) =>
|
||||
spawn(process.execPath, ["-e", "process.stderr.write('Gtk-WARNING\\n'.repeat(100000)); process.stderr.write('User canceled. (-128)'); process.exitCode = 1"], options);
|
||||
assert.deepEqual(await pickFolder(os.tmpdir(), "p", { platform: "darwin", spawnImpl: chatty }), { cancelled: true });
|
||||
});
|
||||
|
||||
test("ledger: a torn trailing line is skipped, then read once completed; an append after a fragment starts a new line", () => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "p2c-ledger-"));
|
||||
try {
|
||||
@@ -2708,6 +2786,26 @@ test("initialWorkspace names the original folder after itself and marks it origi
|
||||
assert.deepEqual([ws.missing, ws.changes, ws.remote], [[], [], ""]);
|
||||
});
|
||||
|
||||
// Browse... opens beside the last folder added. When that folder is gone, a missing
|
||||
// start must not read as "Browse is not available here".
|
||||
test("pickerStart opens beside the last folder added, or at its nearest folder still on disk", () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "p2c-picker-start-"));
|
||||
try {
|
||||
const project = path.join(root, "project");
|
||||
const kept = path.join(root, "kept");
|
||||
fs.mkdirSync(project);
|
||||
fs.mkdirSync(path.join(kept, "inner"), { recursive: true });
|
||||
assert.equal(pickerStart(initialWorkspace(project), project), project, "the original folder when nothing was added");
|
||||
const ws = initialWorkspace(project);
|
||||
ws.folders.push({ id: "f1", path: path.join(kept, "inner"), name: "inner", description: "" });
|
||||
assert.equal(pickerStart(ws, project), kept, "beside the last folder added");
|
||||
fs.rmSync(kept, { recursive: true, force: true });
|
||||
assert.equal(pickerStart(ws, project), root, "the nearest folder still on disk");
|
||||
} finally {
|
||||
fs.rmSync(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
// The meter's tables are built from skills.js plus the events that are not a
|
||||
// skill's own. Held to their values when they were written by hand.
|
||||
test("meter: the weights and labels built from the skill list are the ones written by hand before", () => {
|
||||
|
||||
Reference in new issue
Block a user