import { afterEach, describe, expect, test } from "bun:test"; import { chmodSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { prepareAgentSession, readAgentSessionRecord } from "@bbbug/shared/agent_session"; import { classifySecurityAgentResult } from "../src/security_run_policy"; const WRAPPER = path.resolve(import.meta.dir, "../../bin/claude-cursor.ts"); const dirs: string[] = []; afterEach(() => { for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); }); describe("Cursor security run session", () => { test("preserves structured API-key usage errors for the drain classifier", async () => { const root = mkdtempSync(path.join(tmpdir(), "streamline-cursor-api-quota-")); dirs.push(root); const fakeCursor = path.join(root, "cursor-agent"); writeFileSync(fakeCursor, [ "#!/bin/sh", "if [ \"${1:-}\" = create-chat ]; then echo 11111111-1111-4111-8111-111111111111; exit 0; fi", "echo '{\"type\":\"error\",\"message\":\"request rejected\",\"error\":{\"code\":\"usage_limit_exceeded\"}}'", "exit 1", ].join("\n")); chmodSync(fakeCursor, 0o755); const previous = process.env.STREAMLINE_CURSOR_AUTH_MODE; process.env.STREAMLINE_CURSOR_AUTH_MODE = "api-key"; try { const proc = Bun.spawn({ cmd: [process.execPath, WRAPPER, "-p", "test"], cwd: root, stdout: "pipe", stderr: "pipe", env: { ...process.env, STREAMLINE_AGENT_TASK_KIND: "security", CURSOR_BIN: fakeCursor }, }); const [stdout, , code] = await Promise.all([ new Response(proc.stdout).text(), new Response(proc.stderr).text(), proc.exited, ]); expect(code).not.toBe(0); expect(classifySecurityAgentResult(stdout, "cursor")?.code).toBe("AGENT_USAGE_LIMIT"); } finally { if (previous === undefined) delete process.env.STREAMLINE_CURSOR_AUTH_MODE; else process.env.STREAMLINE_CURSOR_AUTH_MODE = previous; } }); test("preserves stderr-only quota, keychain and gateway failures for evaluate and run", async () => { for (const [message, expected] of [ ["You've reached your monthly usage limit Request higher limits to continue using Cursor", "AGENT_USAGE_LIMIT"], ["Error: Your macOS login keychain is locked. Run security unlock-keychain and try again.", "AGENT_AUTH_FAILED"], ["Error: service unavailable HTTP 503", "AGENT_GATEWAY_ERROR"], ]) { for (const phase of ["evaluate", "run"]) { const root = mkdtempSync(path.join(tmpdir(), "streamline-cursor-stderr-")); dirs.push(root); const fakeCursor = path.join(root, "cursor-agent"); writeFileSync(fakeCursor, "#!/bin/sh\nprintf '%s\\n' \"$MOCK_CLI_ERROR\" >&2\nexit 1\n"); chmodSync(fakeCursor, 0o755); const session = phase === "run" ? prepareAgentSession({ workspaceDir: root, taskKind: "security", taskId: "stderr-case", provider: "cursor", }) : null; const proc = Bun.spawn({ cmd: [process.execPath, WRAPPER, "-p", "collect evidence", ...(session?.args || [])], cwd: root, stdout: "pipe", stderr: "pipe", env: { ...process.env, ...session?.env, STREAMLINE_AGENT_TASK_KIND: "security", CLAUDE_CURSOR_DEBUG: "0", CURSOR_BIN: fakeCursor, MOCK_CLI_ERROR: message }, }); const [stdout, stderr, code] = await Promise.all([ new Response(proc.stdout).text(), new Response(proc.stderr).text(), proc.exited, ]); expect(code).not.toBe(0); expect(stderr).toBe(""); expect(classifySecurityAgentResult(stdout, "cursor")).toMatchObject({ code: expected }); const terminal = JSON.parse(stdout.trim().split("\n").at(-1)!); expect(terminal).toMatchObject({ is_error: true, num_turns: 0, result: message }); } } }); test("leaves feature, bugfix and asking stderr/terminal behaviour unchanged", async () => { for (const kind of ["feature", "bug", ""]) { const root = mkdtempSync(path.join(tmpdir(), "streamline-cursor-legacy-")); dirs.push(root); const fakeCursor = path.join(root, "cursor-agent"); writeFileSync(fakeCursor, "#!/bin/sh\necho 'Your macOS login keychain is locked.' >&2\nexit 1\n"); chmodSync(fakeCursor, 0o755); const proc = Bun.spawn({ cmd: [process.execPath, WRAPPER, "-p", "legacy", ...(kind ? [] : ["--session-id", "asking-id"])], cwd: root, stdout: "pipe", stderr: "pipe", env: { ...process.env, STREAMLINE_AGENT_TASK_KIND: kind, CURSOR_BIN: fakeCursor, CLAUDE_CURSOR_DEBUG: "0", CURSOR_BRIDGE_DIR: path.join(root, "bridge") }, }); const [stdout, stderr, code] = await Promise.all([ new Response(proc.stdout).text(), new Response(proc.stderr).text(), proc.exited, ]); expect(code).not.toBe(0); expect(stderr).toBe(""); expect(JSON.parse(stdout.trim().split("\n").at(-1)!).result).toBe(""); } }); test("does not turn successful CLI stderr warnings into failures", async () => { const root = mkdtempSync(path.join(tmpdir(), "streamline-cursor-warning-")); dirs.push(root); const fakeCursor = path.join(root, "cursor-agent"); writeFileSync(fakeCursor, ["#!/bin/sh", "if [ \"${1:-}\" = create-chat ]; then echo 44444444-4444-4444-8444-444444444444; exit 0; fi", "echo 'Warning: keychain is locked for another application' >&2", "echo '{\"type\":\"assistant\",\"message\":{\"content\":[{\"type\":\"text\",\"text\":\"reports ready\"}]}}'", "exit 0"].join("\n")); chmodSync(fakeCursor, 0o755); const proc = Bun.spawn({ cmd: [process.execPath, WRAPPER, "-p", "test"], cwd: root, stdout: "pipe", stderr: "pipe", env: { ...process.env, STREAMLINE_AGENT_TASK_KIND: "security", CURSOR_BIN: fakeCursor }, }); const [stdout] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text(), proc.exited]); expect(classifySecurityAgentResult(stdout, "cursor")).toBeNull(); }); test("uses an ID from a stuck create-chat and resumes it after a satellite restart", async () => { const root = mkdtempSync(path.join(tmpdir(), "streamline-cursor-session-")); dirs.push(root); const fakeCursor = path.join(root, "cursor-agent"); const argsLog = path.join(root, "args.log"); writeFileSync(fakeCursor, [ "#!/bin/sh", "set -eu", "if [ \"${1:-}\" = \"create-chat\" ]; then printf '%s\\n' '22222222-2222-4222-8222-222222222222'; exec sleep 30; fi", "printf '%s\\n' \"$@\" > \"$CURSOR_ARGS_LOG\"", "printf '%s\\n' '{\"type\":\"system\",\"subtype\":\"init\",\"session_id\":\"22222222-2222-4222-8222-222222222222\",\"model\":\"Opus 4.8 300K Max\"}'", "printf '%s\\n' '{\"type\":\"assistant\",\"message\":{\"role\":\"assistant\",\"content\":[{\"type\":\"text\",\"text\":\"STATUS: IMPLEMENTED\"}]}}'", "printf '%s\\n' '{\"type\":\"result\",\"is_error\":false,\"result\":\"STATUS: IMPLEMENTED\",\"duration_ms\":1,\"usage\":{\"inputTokens\":2,\"outputTokens\":4,\"cacheReadTokens\":1,\"cacheWriteTokens\":3,\"thinkingTokens\":2}}'", ].join("\n"), "utf8"); chmodSync(fakeCursor, 0o755); const first = prepareAgentSession({ workspaceDir: root, taskKind: "security", taskId: "security-1", provider: "cursor" }); const firstRun = Bun.spawn({ cmd: [process.execPath, WRAPPER, "-p", "implement", ...first.args, "--output-format", "stream-json"], cwd: root, stdout: "pipe", stderr: "pipe", env: { ...process.env, ...first.env, CURSOR_BIN: fakeCursor, CURSOR_ARGS_LOG: argsLog }, }); const [firstStdout] = await Promise.all([new Response(firstRun.stdout).text(), new Response(firstRun.stderr).text()]); expect(await firstRun.exited).toBe(0); const firstEvents = firstStdout.split("\n").filter(Boolean).map((line) => JSON.parse(line) as Record); expect(firstEvents.some((event) => event.type === "system" && event.model === "Opus 4.8 300K Max")).toBe(true); expect(firstEvents.at(-1)?.usage).toMatchObject({ input_tokens: 2, output_tokens: 4, cache_read_input_tokens: 1, cache_creation_input_tokens: 3, thinking_tokens: 2, }); expect(readAgentSessionRecord(first.recordPath)?.agentSessionId).toBe("22222222-2222-4222-8222-222222222222"); const afterRestart = prepareAgentSession({ workspaceDir: root, taskKind: "security", taskId: "security-1", provider: "cursor" }); expect(afterRestart.resume).toBe(true); const resumed = Bun.spawn({ cmd: [process.execPath, WRAPPER, "-p", "continue", ...afterRestart.args, "--output-format", "stream-json"], cwd: root, stdout: "pipe", stderr: "pipe", env: { ...process.env, ...afterRestart.env, CURSOR_BIN: fakeCursor, CURSOR_ARGS_LOG: argsLog }, }); await Promise.all([new Response(resumed.stdout).text(), new Response(resumed.stderr).text()]); expect(await resumed.exited).toBe(0); const args = readFileSync(argsLog, "utf8").split("\n").filter(Boolean); expect(args).toContain("--resume"); expect(args).toContain("22222222-2222-4222-8222-222222222222"); }); test("reports a session-creation error instead of starting without an ID", async () => { const root = mkdtempSync(path.join(tmpdir(), "streamline-cursor-no-chat-")); dirs.push(root); const fakeCursor = path.join(root, "cursor-agent"); writeFileSync(fakeCursor, [ "#!/bin/sh", "if [ \"${1:-}\" = create-chat ]; then echo 'session creation unavailable' >&2; exit 1; fi", "touch \"$CURSOR_PRINT_MARKER\"", ].join("\n")); chmodSync(fakeCursor, 0o755); const marker = path.join(root, "print-started"); const session = prepareAgentSession({ workspaceDir: root, taskKind: "security", taskId: "no-chat", provider: "cursor" }); const proc = Bun.spawn({ cmd: [process.execPath, WRAPPER, "-p", "test", ...session.args], cwd: root, stdout: "pipe", stderr: "pipe", env: { ...process.env, ...session.env, CURSOR_BIN: fakeCursor, CURSOR_PRINT_MARKER: marker } }); const [stdout, , code] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text(), proc.exited]); expect(code).not.toBe(0); expect(JSON.parse(stdout.trim().split("\n").at(-1)!)).toMatchObject({ is_error: true, result: "session creation unavailable" }); expect(await Bun.file(marker).exists()).toBe(false); expect(readAgentSessionRecord(session.recordPath)?.agentSessionId).toBeNull(); }); test("surfaces Cursor's turn_ended usage exhaustion for the security retry classifier", async () => { const root = mkdtempSync(path.join(tmpdir(), "streamline-cursor-limit-")); dirs.push(root); const fakeCursor = path.join(root, "cursor-agent"); writeFileSync(fakeCursor, [ "#!/bin/sh", "set -eu", "if [ \"${1:-}\" = \"create-chat\" ]; then printf '%s\\n' '33333333-3333-4333-8333-333333333333'; exit 0; fi", "printf '%s\\n' '{\"type\":\"system\",\"subtype\":\"init\",\"session_id\":\"33333333-3333-4333-8333-333333333333\"}'", "printf '%s\\n' \"{\\\"type\\\":\\\"turn_ended\\\",\\\"status\\\":\\\"error\\\",\\\"error\\\":\\\"You've reached your monthly usage limit Request higher limits to continue using Cursor\\\"}\"", "exit 1", ].join("\n"), "utf8"); chmodSync(fakeCursor, 0o755); const session = prepareAgentSession({ workspaceDir: root, taskKind: "security", taskId: "security-limit", provider: "cursor", }); const proc = Bun.spawn({ cmd: [process.execPath, WRAPPER, "-p", "collect evidence", ...session.args, "--output-format", "stream-json"], cwd: root, stdout: "pipe", stderr: "pipe", env: { ...process.env, ...session.env, CURSOR_BIN: fakeCursor }, }); const [stdout, stderr, code] = await Promise.all([ new Response(proc.stdout).text(), new Response(proc.stderr).text(), proc.exited, ]); expect(stderr).toBe(""); expect(code).not.toBe(0); const events = stdout.split("\n").filter(Boolean).map((line) => JSON.parse(line) as Record); expect(events.some((event) => JSON.stringify(event).includes("monthly usage limit"))).toBe(true); expect(events.at(-1)).toMatchObject({ type: "result", is_error: true, result: expect.stringContaining("monthly usage limit"), usage: { thinking_tokens: 0 }, }); }); });