mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-23 16:42:19 +00:00
Both paths ran synchronous I/O on the Electron main process, which freezes
every window and the terminal keystroke relay while it runs:
- Agent discovery (opening the AI panel or Settings > AI) probed auth with
execFileSync: `security find-generic-password` (4 s timeout), `gh auth
status` (network, 5 s), `which cursor-agent` (4 s) and `cursor-agent status`
(8 s), one after another. The probes now use a non-rejecting async execFile
helper with the same result shape; discovery awaits them.
- Cursor SDK detection evaluated `import("@cursor/sdk")` (~4 MB of ESM plus
its transport stack) only to check that it is installed, and kept it
resident afterwards. It now checks for the ESM entry that the dynamic import
loads; the SDK itself still loads on first use. The package does not export
package.json and its CJS build is stripped from releases, so the check walks
require.resolve.paths() instead of using require.resolve.
- The AI chat backup wrote the whole history (often several MB) with
writeFileSync on every debounced change. Writes now use fs.promises and are
serialized so generations cannot interleave.
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
335 lines
11 KiB
JavaScript
335 lines
11 KiB
JavaScript
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const {
|
|
probeClaudeAuth, probeCopilotAuth, probeCodexAuth, probeCodebuddyAuth, probeCursorCliAuth, probeGrokAuth,
|
|
parseCursorStatusJson, resolveCursorCliSpawnSpec,
|
|
} = require("./agentAuthProbes.cjs");
|
|
const { prepareCommandForSpawn } = require("../ai/shellUtils.cjs");
|
|
const { resolveCursorCliSpawnSpec: resolveSharedCursorCliSpawnSpec } = require("./cursorCliSpawn.cjs");
|
|
|
|
test("probeClaudeAuth: env ANTHROPIC_API_KEY -> authenticated env", async () => {
|
|
const r = await probeClaudeAuth({
|
|
env: { ANTHROPIC_API_KEY: "sk-x" },
|
|
platform: "darwin",
|
|
runSecurity: () => { throw new Error("should not be called"); },
|
|
fileExists: () => false,
|
|
});
|
|
assert.equal(r.authenticated, true);
|
|
assert.equal(r.authSource, "env");
|
|
});
|
|
|
|
test("probeClaudeAuth: macOS keychain hit -> authenticated keychain", async () => {
|
|
const r = await probeClaudeAuth({
|
|
env: {},
|
|
platform: "darwin",
|
|
runSecurity: () => ({ exitCode: 0, stdout: '{"claudeAiOauth":{}}' }),
|
|
fileExists: () => false,
|
|
});
|
|
assert.equal(r.authenticated, true);
|
|
assert.equal(r.authSource, "keychain");
|
|
});
|
|
|
|
test("probeClaudeAuth: linux credentials file -> authenticated credentials-file", async () => {
|
|
const r = await probeClaudeAuth({
|
|
env: {},
|
|
platform: "linux",
|
|
runSecurity: () => { throw new Error("no keychain on linux"); },
|
|
fileExists: (p) => p.endsWith(".credentials.json"),
|
|
});
|
|
assert.equal(r.authenticated, true);
|
|
assert.equal(r.authSource, "credentials-file");
|
|
});
|
|
|
|
test("probeClaudeAuth: nothing -> not authenticated", async () => {
|
|
const r = await probeClaudeAuth({
|
|
env: {}, platform: "darwin",
|
|
runSecurity: () => ({ exitCode: 44, stdout: "" }),
|
|
fileExists: () => false,
|
|
});
|
|
assert.equal(r.authenticated, false);
|
|
assert.equal(r.authSource, null);
|
|
});
|
|
|
|
test("probeCopilotAuth: gh auth status exit 0 -> authenticated gh", async () => {
|
|
const r = await probeCopilotAuth({ runGhAuthStatus: () => ({ exitCode: 0, stderr: "Logged in to github.com" }) });
|
|
assert.equal(r.authenticated, true);
|
|
assert.equal(r.authSource, "gh");
|
|
});
|
|
|
|
test("probeCopilotAuth: gh auth status non-zero -> not authenticated", async () => {
|
|
const r = await probeCopilotAuth({ runGhAuthStatus: () => ({ exitCode: 1, stderr: "not logged in" }) });
|
|
assert.equal(r.authenticated, false);
|
|
});
|
|
|
|
test("probeCodexAuth: 'Logged in using ChatGPT' -> authenticated chatgpt", () => {
|
|
const r = probeCodexAuth({
|
|
runLoginStatus: () => ({ exitCode: 0, stdout: "Logged in using ChatGPT" }),
|
|
fileExists: () => false,
|
|
});
|
|
assert.equal(r.authenticated, true);
|
|
assert.equal(r.authSource, "chatgpt");
|
|
});
|
|
|
|
test("probeCodexAuth: auth.json fallback -> authenticated auth-file", () => {
|
|
const r = probeCodexAuth({
|
|
runLoginStatus: () => ({ exitCode: 1, stdout: "not logged in" }),
|
|
fileExists: (p) => p.endsWith("auth.json"),
|
|
});
|
|
assert.equal(r.authenticated, true);
|
|
assert.equal(r.authSource, "auth-file");
|
|
});
|
|
|
|
// ── CodeBuddy ──
|
|
test("probeCodebuddyAuth: CODEBUDDY_API_KEY env -> authenticated api-key", () => {
|
|
const r = probeCodebuddyAuth({
|
|
env: { CODEBUDDY_API_KEY: "cb-key-123" },
|
|
readFile: () => null,
|
|
});
|
|
assert.equal(r.authenticated, true);
|
|
assert.equal(r.authSource, "api-key");
|
|
});
|
|
|
|
test("probeCodebuddyAuth: CODEBUDDY_AUTH_TOKEN env -> authenticated auth-token", () => {
|
|
const r = probeCodebuddyAuth({
|
|
env: { CODEBUDDY_AUTH_TOKEN: "oauth-token-xyz" },
|
|
readFile: () => null,
|
|
});
|
|
assert.equal(r.authenticated, true);
|
|
assert.equal(r.authSource, "auth-token");
|
|
});
|
|
|
|
test("probeCodebuddyAuth: settings.json with authToken -> authenticated settings-file", () => {
|
|
const r = probeCodebuddyAuth({
|
|
env: {},
|
|
readFile: () => '{"authToken":"real-token"}',
|
|
});
|
|
assert.equal(r.authenticated, true);
|
|
assert.equal(r.authSource, "settings-file");
|
|
});
|
|
|
|
test("probeCodebuddyAuth: settings.json with apiKeyHelper -> authenticated settings-file", () => {
|
|
const r = probeCodebuddyAuth({
|
|
env: {},
|
|
readFile: () => '{"apiKeyHelper":"/usr/local/bin/helper"}',
|
|
});
|
|
assert.equal(r.authenticated, true);
|
|
assert.equal(r.authSource, "settings-file");
|
|
});
|
|
|
|
test("probeCodebuddyAuth: empty settings.json -> not authenticated", () => {
|
|
const r = probeCodebuddyAuth({
|
|
env: {},
|
|
readFile: () => "",
|
|
});
|
|
assert.equal(r.authenticated, false);
|
|
assert.equal(r.authSource, null);
|
|
});
|
|
|
|
test("probeCodebuddyAuth: malformed JSON in settings.json -> not authenticated", () => {
|
|
const r = probeCodebuddyAuth({
|
|
env: {},
|
|
readFile: () => "{not valid json",
|
|
});
|
|
assert.equal(r.authenticated, false);
|
|
assert.equal(r.authSource, null);
|
|
});
|
|
|
|
test("probeCodebuddyAuth: settings.json without auth fields -> not authenticated", () => {
|
|
const r = probeCodebuddyAuth({
|
|
env: {},
|
|
readFile: () => '{"theme":"dark","language":"en"}',
|
|
});
|
|
assert.equal(r.authenticated, false);
|
|
assert.equal(r.authSource, null);
|
|
});
|
|
|
|
test("probeCodebuddyAuth: no env, no settings file -> not authenticated", () => {
|
|
const r = probeCodebuddyAuth({
|
|
env: {},
|
|
readFile: () => null,
|
|
});
|
|
assert.equal(r.authenticated, false);
|
|
assert.equal(r.authSource, null);
|
|
});
|
|
|
|
test("probeCodebuddyAuth: CODEBUDDY_API_KEY takes precedence over settings.json", () => {
|
|
const r = probeCodebuddyAuth({
|
|
env: { CODEBUDDY_API_KEY: "cb-key" },
|
|
readFile: () => '{"authToken":"token"}',
|
|
});
|
|
assert.equal(r.authenticated, true);
|
|
assert.equal(r.authSource, "api-key");
|
|
});
|
|
|
|
// ── Cursor CLI login ──
|
|
test("probeCursorCliAuth: prefers cursor-agent and parses authenticated JSON", async () => {
|
|
const calls = [];
|
|
const r = await probeCursorCliAuth({
|
|
env: { CURSOR_API_KEY: "should-be-stripped" },
|
|
resolveBinary: (name) => {
|
|
calls.push(name);
|
|
return name === "cursor-agent" ? "/bin/cursor-agent" : null;
|
|
},
|
|
runStatus: (bin) => {
|
|
assert.equal(bin, "/bin/cursor-agent");
|
|
return {
|
|
exitCode: 0,
|
|
stdout: JSON.stringify({
|
|
status: "authenticated",
|
|
isAuthenticated: true,
|
|
userInfo: { email: "user@example.com" },
|
|
}),
|
|
};
|
|
},
|
|
});
|
|
assert.deepEqual(calls, ["cursor-agent"]);
|
|
assert.equal(r.authenticated, true);
|
|
assert.equal(r.authSource, "cli-login");
|
|
assert.equal(r.email, "user@example.com");
|
|
assert.equal(r.binPath, "/bin/cursor-agent");
|
|
});
|
|
|
|
test("probeCursorCliAuth: does not fall back to bare agent binary", async () => {
|
|
const statusCalls = [];
|
|
const resolveCalls = [];
|
|
const r = await probeCursorCliAuth({
|
|
resolveBinary: (name) => {
|
|
resolveCalls.push(name);
|
|
return name === "agent" ? "/bin/agent" : null;
|
|
},
|
|
runStatus: (bin) => {
|
|
statusCalls.push(bin);
|
|
return {
|
|
exitCode: 0,
|
|
stdout: JSON.stringify({ isAuthenticated: true, userInfo: { email: "a@b.c" } }),
|
|
};
|
|
},
|
|
});
|
|
assert.deepEqual(resolveCalls, ["cursor-agent"]);
|
|
assert.deepEqual(statusCalls, []);
|
|
assert.equal(r.authenticated, false);
|
|
assert.equal(r.binPath, null);
|
|
});
|
|
|
|
// ── Grok Build ──
|
|
test("probeGrokAuth: env XAI_API_KEY -> authenticated env", () => {
|
|
const r = probeGrokAuth({
|
|
env: { XAI_API_KEY: "xai-test" },
|
|
fileExists: () => false,
|
|
homeDir: "/home/user",
|
|
});
|
|
assert.equal(r.authenticated, true);
|
|
assert.equal(r.authSource, "env");
|
|
});
|
|
|
|
test("probeGrokAuth: auth.json fallback -> authenticated auth-file", () => {
|
|
const path = require("node:path");
|
|
const homeDir = path.join("home", "user");
|
|
const authPath = path.join(homeDir, ".grok", "auth.json");
|
|
const r = probeGrokAuth({
|
|
env: {},
|
|
homeDir,
|
|
fileExists: (p) => p === authPath,
|
|
});
|
|
assert.equal(r.authenticated, true);
|
|
assert.equal(r.authSource, "auth-file");
|
|
});
|
|
|
|
test("probeGrokAuth: nothing -> not authenticated", () => {
|
|
const r = probeGrokAuth({
|
|
env: {},
|
|
homeDir: "/home/user",
|
|
fileExists: () => false,
|
|
});
|
|
assert.equal(r.authenticated, false);
|
|
assert.equal(r.authSource, null);
|
|
});
|
|
|
|
test("probeCursorCliAuth: unauthenticated JSON -> not authenticated but keeps binPath", async () => {
|
|
const r = await probeCursorCliAuth({
|
|
resolveBinary: (name) => (name === "cursor-agent" ? "/bin/cursor-agent" : null),
|
|
runStatus: () => ({
|
|
exitCode: 0,
|
|
stdout: JSON.stringify({ isAuthenticated: false }),
|
|
}),
|
|
});
|
|
assert.equal(r.authenticated, false);
|
|
assert.equal(r.authSource, null);
|
|
assert.equal(r.binPath, "/bin/cursor-agent");
|
|
});
|
|
|
|
test("probeCursorCliAuth: missing binary -> not authenticated", async () => {
|
|
const r = await probeCursorCliAuth({
|
|
resolveBinary: () => null,
|
|
runStatus: () => { throw new Error("should not run"); },
|
|
});
|
|
assert.equal(r.authenticated, false);
|
|
assert.equal(r.binPath, null);
|
|
});
|
|
|
|
test("probeCursorCliAuth: status command failure -> not authenticated", async () => {
|
|
const r = await probeCursorCliAuth({
|
|
resolveBinary: () => "/bin/cursor-agent",
|
|
runStatus: () => ({ exitCode: 1, stdout: "", stderr: "boom" }),
|
|
});
|
|
assert.equal(r.authenticated, false);
|
|
assert.equal(r.binPath, "/bin/cursor-agent");
|
|
});
|
|
|
|
test("probeCursorCliAuth: unrecognized status stdout keeps resolved binPath", async () => {
|
|
const r = await probeCursorCliAuth({
|
|
resolveBinary: () => "/bin/cursor-agent",
|
|
runStatus: () => ({ exitCode: 0, stdout: "cursor-agent 1.2.3\nnot json" }),
|
|
});
|
|
assert.equal(r.authenticated, false);
|
|
assert.equal(r.authSource, null);
|
|
assert.equal(r.binPath, "/bin/cursor-agent");
|
|
});
|
|
|
|
test("probeCursorCliAuth: thrown status probe keeps resolved binPath", async () => {
|
|
const r = await probeCursorCliAuth({
|
|
resolveBinary: () => "/bin/cursor-agent",
|
|
runStatus: () => { throw new Error("timeout"); },
|
|
});
|
|
assert.equal(r.authenticated, false);
|
|
assert.equal(r.authSource, null);
|
|
assert.equal(r.binPath, "/bin/cursor-agent");
|
|
});
|
|
|
|
test("probeCursorCliAuth: extracts authenticated JSON wrapped in cmd noise", async () => {
|
|
const r = await probeCursorCliAuth({
|
|
resolveBinary: () => "C:\\Users\\me\\AppData\\Local\\cursor-agent\\cursor-agent.cmd",
|
|
runStatus: () => ({
|
|
exitCode: 0,
|
|
stdout: "Starting...\r\n{\"status\":\"authenticated\",\"isAuthenticated\":true,\"userInfo\":{\"email\":\"user@example.com\"}}\r\n",
|
|
}),
|
|
});
|
|
assert.equal(r.authenticated, true);
|
|
assert.equal(r.authSource, "cli-login");
|
|
assert.equal(r.email, "user@example.com");
|
|
});
|
|
|
|
test("parseCursorStatusJson accepts BOM and surrounding text", () => {
|
|
const parsed = parseCursorStatusJson(
|
|
"\uFEFFnoise\n{\"isAuthenticated\":true,\"status\":\"authenticated\"}\n",
|
|
);
|
|
assert.equal(parsed.isAuthenticated, true);
|
|
assert.equal(parsed.status, "authenticated");
|
|
});
|
|
|
|
test("resolveCursorCliSpawnSpec re-exports the shared native launch helper", () => {
|
|
const shim = "C:\\Users\\me\\AppData\\Local\\cursor-agent\\cursor-agent.cmd";
|
|
const args = ["status", "--format", "json"];
|
|
assert.deepEqual(
|
|
resolveCursorCliSpawnSpec(shim, args),
|
|
resolveSharedCursorCliSpawnSpec(shim, args),
|
|
);
|
|
assert.deepEqual(
|
|
resolveSharedCursorCliSpawnSpec(shim, args, {
|
|
exists: () => false,
|
|
readFile: () => { throw new Error("missing"); },
|
|
}),
|
|
prepareCommandForSpawn(shim, args, { unwrapNativeExe: false }),
|
|
);
|
|
});
|