Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

cursorSdk.test.ts

193 lines6.4 KB

1// @effect-diagnostics nodeBuiltinImport:off globalTimers:off -- The probe is a plain Node child; this deadline kills it if it hangs.
2import * as NodeChildProcess from "node:child_process";
3import { describe, expect, it } from "vite-plus/test";
4​
5const cursorSdkUrl = new URL("./cursorSdk.ts", import.meta.url).href;
6​
7// Loads cursorSdk.ts in a fresh process. @cursor/sdk is stubbed so the guard
8// can be tested without the real package. The vitest worker already has its
9// own unhandledRejection listener, which would hide the process-exit behavior.
10const probeProgram = `
11import { mkdtempSync, writeFileSync } from "node:fs";
12import { tmpdir } from "node:os";
13import { join } from "node:path";
14import { registerHooks } from "node:module";
15import { pathToFileURL } from "node:url";
16import { spawn } from "node:child_process";
17​
18const dir = mkdtempSync(join(tmpdir(), "cursor-sdk-stub-"));
19const stub = join(dir, "stub.cjs");
20writeFileSync(
21 stub,
22 "module.exports = { Agent: {}, AuthenticationError: class {}, createAgentPlatform: () => ({}), Cursor: {}, CursorSdkError: class {}, InMemoryCredentialStore: class {} };",
23);
24registerHooks({
25 resolve(specifier, context, next) {
26 if (specifier === "@cursor/sdk") {
27 return { url: pathToFileURL(stub).href, shortCircuit: true };
28 }
29 return next(specifier, context);
30 },
31});
32​
33const { isCursorShellSpawnFailure } = await import(${JSON.stringify(cursorSdkUrl)});
34const mode = process.argv[1];
35const missingCwd = join(tmpdir(), "t3-missing-cwd-" + process.pid);
36​
37if (mode === "predicate") {
38 const cursorShell = Object.assign(new Error("spawn /bin/zsh ENOENT"), {
39 code: "ENOENT",
40 syscall: "spawn /bin/zsh",
41 path: "/bin/zsh",
42 spawnargs: ["-c", "dump_zsh_state >&4", "--", "true"],
43 });
44 const bashShell = Object.assign(new Error("spawn bash ENOENT"), {
45 code: "ENOENT",
46 syscall: "spawn bash",
47 spawnargs: ["-c", "dump_bash_state >&4"],
48 });
49 const sandboxRestore = Object.assign(new Error("spawn /bin/zsh ENOENT"), {
50 code: "ENOENT",
51 syscall: "spawn /bin/zsh",
52 spawnargs: ["-c", "builtin eval \\"\${__CURSOR_SANDBOX_ENV_RESTORE:-}\\""],
53 });
54 const gitSpawn = Object.assign(new Error("spawn git ENOENT"), {
55 code: "ENOENT",
56 syscall: "spawn git",
57 path: "git",
58 spawnargs: ["status"],
59 });
60 const plainZsh = Object.assign(new Error("spawn /bin/zsh ENOENT"), {
61 code: "ENOENT",
62 syscall: "spawn /bin/zsh",
63 path: "/bin/zsh",
64 spawnargs: ["-lc", "true"],
65 });
66 console.log(
67 JSON.stringify({
68 cursorShell: isCursorShellSpawnFailure(cursorShell),
69 bashShell: isCursorShellSpawnFailure(bashShell),
70 sandboxRestore: isCursorShellSpawnFailure(sandboxRestore),
71 gitSpawn: isCursorShellSpawnFailure(gitSpawn),
72 plainZsh: isCursorShellSpawnFailure(plainZsh),
73 open: isCursorShellSpawnFailure(
74 Object.assign(new Error("open failed"), { code: "ENOENT", syscall: "open" }),
75 ),
76 plain: isCursorShellSpawnFailure(new Error("boom")),
77 string: isCursorShellSpawnFailure("spawn ENOENT"),
78 }),
79 );
80 process.exit(0);
82​
83if (mode === "cursor-shell") {
84 const child = spawn("/bin/zsh", ["-c", "dump_zsh_state >&4", "--", "true"], {
85 cwd: missingCwd,
86 });
87 child.on("error", (error) => {
88 Promise.reject(error);
89 });
90 setTimeout(() => process.exit(0), 500);
91} else if (mode === "other-spawn") {
92 Promise.reject(
93 Object.assign(new Error("spawn git ENOENT"), {
94 code: "ENOENT",
95 syscall: "spawn git",
96 path: "git",
97 spawnargs: ["status"],
98 }),
99 );
100 setTimeout(() => process.exit(0), 500);
101} else if (mode === "other-rejection") {
102 Promise.reject(new Error("boom"));
103 setTimeout(() => process.exit(0), 500);
104} else if (mode === "spawn-without-listener") {
105 spawn(process.execPath, ["-e", "process.exit(0)"], { cwd: missingCwd });
106 setTimeout(() => process.exit(0), 500);
107} else {
108 console.error("Unknown cursor shell spawn guard probe: " + mode);
109 process.exit(2);
111`;
112​
113function runProbe(mode: string): Promise<{ code: number | null; stdout: string; stderr: string }> {
114 return new Promise((resolve, reject) => {
115 const child = NodeChildProcess.spawn(
116 process.execPath,
117 [
118 "--experimental-strip-types",
119 "--disable-warning=ExperimentalWarning",
120 "--input-type=module",
121 "-e",
122 probeProgram,
123 mode,
124 ],
125 { stdio: ["ignore", "pipe", "pipe"] },
126 );
127 let stdout = "";
128 let stderr = "";
129 child.stdout.setEncoding("utf8");
130 child.stderr.setEncoding("utf8");
131 child.stdout.on("data", (chunk: string) => {
132 stdout += chunk;
133 });
134 child.stderr.on("data", (chunk: string) => {
135 stderr += chunk;
136 });
137 const timer = setTimeout(() => {
138 child.kill();
139 reject(new Error(`probe ${mode} timed out`));
140 }, 5_000);
141 child.on("error", reject);
142 child.on("exit", (code) => {
143 clearTimeout(timer);
144 resolve({ code, stdout, stderr });
145 });
146 });
148​
149describe("isCursorShellSpawnFailure", () => {
150 it("matches only Cursor's shell wrapper", async () => {
151 const result = await runProbe("predicate");
152 expect(result.code).toBe(0);
153 expect(JSON.parse(result.stdout)).toEqual({
154 cursorShell: true,
155 bashShell: true,
156 sandboxRestore: true,
157 gitSpawn: false,
158 plainZsh: false,
159 open: false,
160 plain: false,
161 string: false,
162 });
163 });
164});
165​
166describe("Cursor shell spawn guard", () => {
167 it("keeps the process alive when Cursor's shell spawn rejects", async () => {
168 const result = await runProbe("cursor-shell");
169 expect(result.code).toBe(0);
170 expect(result.stderr).toContain("The server will keep running.");
171 expect(result.stderr).toContain("ENOENT");
172 });
173​
174 it("still exits when a different spawn failure is unhandled", async () => {
175 const result = await runProbe("other-spawn");
176 expect(result.code).toBe(1);
177 expect(result.stderr).toContain("spawn git ENOENT");
178 expect(result.stderr).not.toContain("The server will keep running.");
179 });
180​
181 it("still exits on unrelated unhandled rejections", async () => {
182 const result = await runProbe("other-rejection");
183 expect(result.code).toBe(1);
184 expect(result.stderr).toContain("boom");
185 });
186​
187 it("leaves a spawn with no error listener fatal", async () => {
188 const result = await runProbe("spawn-without-listener");
189 expect(result.code).toBe(1);
190 expect(result.stderr).toContain("Unhandled");
191 expect(result.stderr).not.toContain("The server will keep running.");
192 });
193});