The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import { assert, describe, it } from "@effect/vitest"; |
| 3 | import type { CursorConfigureOptions } from "@cursor/sdk"; |
| 4 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 5 | import * as Context from "effect/Context"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as FileSystem from "effect/FileSystem"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import * as Logger from "effect/Logger"; |
| 10 | import * as Path from "effect/Path"; |
| 11 | import { vi } from "vite-plus/test"; |
| 12 | |
| 13 | import * as CursorSdk from "./CursorSdk.ts"; |
| 14 | |
| 15 | const sdkDefaults = vi.hoisted(() => ({ useHttp1ForAgent: undefined as boolean | undefined })); |
| 16 | |
| 17 | // Holds the SDK's process-wide default the way it does: only keys present change, null clears. |
| 18 | vi.mock("./sdk.ts", () => ({ |
| 19 | loadCursorSdk: () => ({ |
| 20 | Cursor: { |
| 21 | configure: ({ local }: CursorConfigureOptions) => { |
| 22 | if (local !== undefined && "useHttp1ForAgent" in local) { |
| 23 | sdkDefaults.useHttp1ForAgent = local.useHttp1ForAgent ?? undefined; |
| 24 | } |
| 25 | }, |
| 26 | }, |
| 27 | }), |
| 28 | loadCursorSqlite: () => ({}), |
| 29 | })); |
| 30 | |
| 31 | const withHost = (environment: NodeJS.ProcessEnv, home: string) => |
| 32 | Effect.provide( |
| 33 | Layer.mergeAll( |
| 34 | NodeServices.layer, |
| 35 | Layer.succeed(HostProcess.Environment, environment), |
| 36 | Layer.succeed(HostProcess.HomeDirectory, home), |
| 37 | ), |
| 38 | ); |
| 39 | |
| 40 | // Shaped like the SDK's minified class: the constructor is not named after the error. |
| 41 | // @effect-diagnostics-next-line extendsNativeError:off - mimics the Cursor SDK's own native Error class. |
| 42 | class o extends Error { |
| 43 | override name = "WriteIterableClosedError"; |
| 44 | } |
| 45 | |
| 46 | // Calls the guard directly, with Vitest's own listener detached so the guard does not defer to it. |
| 47 | const rejectThroughGuard = (reason: unknown) => |
| 48 | Effect.gen(function* () { |
| 49 | const others = process.listeners("unhandledRejection"); |
| 50 | yield* Layer.build(CursorSdk.layer); |
| 51 | const guard = process.listeners("unhandledRejection").find((l) => !others.includes(l)); |
| 52 | assert.isDefined(guard); |
| 53 | using exit = vi.spyOn(process, "exit").mockImplementation(() => undefined as never); |
| 54 | using _log = vi.spyOn(console, "error").mockImplementation(() => {}); |
| 55 | for (const listener of others) process.off("unhandledRejection", listener); |
| 56 | try { |
| 57 | guard(reason, Promise.resolve()); |
| 58 | } finally { |
| 59 | for (const listener of others) process.on("unhandledRejection", listener); |
| 60 | } |
| 61 | return exit.mock.calls; |
| 62 | }).pipe(Effect.scoped, withHost({}, "/nonexistent-home")); |
| 63 | |
| 64 | describe("CursorSdk unhandled rejection guard", () => { |
| 65 | it.effect("keeps the server running when the SDK writes to a closed stream", () => |
| 66 | Effect.gen(function* () { |
| 67 | assert.deepStrictEqual(yield* rejectThroughGuard(new o("WritableIterable is closed")), []); |
| 68 | }), |
| 69 | ); |
| 70 | |
| 71 | it.effect("keeps the server running when the SDK aborts a stalled request", () => |
| 72 | Effect.gen(function* () { |
| 73 | const stall = new DOMException("This operation was aborted", "AbortError"); |
| 74 | stall.stack = "AbortError: This operation was aborted\n at Is.onStall (689.js:1:111536)"; |
| 75 | assert.deepStrictEqual(yield* rejectThroughGuard(stall), []); |
| 76 | }), |
| 77 | ); |
| 78 | |
| 79 | it.effect("still exits on an abort that was not raised from onStall", () => |
| 80 | Effect.gen(function* () { |
| 81 | const abort = new DOMException("onStall was aborted", "AbortError"); |
| 82 | abort.stack = "AbortError: onStall was aborted\n at somewhereElse (app.js:1:1)"; |
| 83 | assert.deepStrictEqual(yield* rejectThroughGuard(abort), [[1]]); |
| 84 | }), |
| 85 | ); |
| 86 | |
| 87 | it.effect("keeps the server running when the SDK writes after its request stream ended", () => |
| 88 | Effect.gen(function* () { |
| 89 | // Shaped like `ConnectError.from(e, Code.Aborted)` from the SDK's connect-node body pump. |
| 90 | const cause = Object.assign(new Error("write after end"), { |
| 91 | code: "ERR_STREAM_WRITE_AFTER_END", |
| 92 | }); |
| 93 | const reason = Object.assign(new Error("[aborted] write after end", { cause }), { |
| 94 | name: "ConnectError", |
| 95 | code: 10, |
| 96 | rawMessage: "write after end", |
| 97 | }); |
| 98 | assert.deepStrictEqual(yield* rejectThroughGuard(reason), []); |
| 99 | }), |
| 100 | ); |
| 101 | |
| 102 | it.effect("still exits on other rejections", () => |
| 103 | Effect.gen(function* () { |
| 104 | assert.deepStrictEqual(yield* rejectThroughGuard(new Error("WritableIterable is closed")), [ |
| 105 | [1], |
| 106 | ]); |
| 107 | }), |
| 108 | ); |
| 109 | |
| 110 | it.effect("still exits on aborted requests other than a write after end", () => |
| 111 | Effect.gen(function* () { |
| 112 | const cause = Object.assign(new Error("socket hang up"), { code: "ECONNRESET" }); |
| 113 | const reason = Object.assign(new Error("[aborted] socket hang up", { cause }), { |
| 114 | name: "ConnectError", |
| 115 | code: 10, |
| 116 | }); |
| 117 | assert.deepStrictEqual(yield* rejectThroughGuard(reason), [[1]]); |
| 118 | }), |
| 119 | ); |
| 120 | }); |
| 121 | |
| 122 | const http1 = JSON.stringify({ network: { useHttp1ForAgent: true } }); |
| 123 | |
| 124 | describe("CursorSdk agent transport", () => { |
| 125 | it.effect.each([ |
| 126 | { |
| 127 | name: "when set in CURSOR_CONFIG_DIR over XDG_CONFIG_HOME", |
| 128 | env: { CURSOR_CONFIG_DIR: "config", XDG_CONFIG_HOME: "xdg" }, |
| 129 | file: "config/cli-config.json", |
| 130 | text: http1, |
| 131 | expected: true, |
| 132 | }, |
| 133 | { |
| 134 | name: "when set in XDG_CONFIG_HOME", |
| 135 | env: { XDG_CONFIG_HOME: "xdg" }, |
| 136 | file: "xdg/cursor/cli-config.json", |
| 137 | text: http1, |
| 138 | expected: true, |
| 139 | }, |
| 140 | { |
| 141 | name: "when set in the home directory", |
| 142 | env: {}, |
| 143 | file: "home/.cursor/cli-config.json", |
| 144 | text: http1, |
| 145 | expected: true, |
| 146 | }, |
| 147 | { |
| 148 | name: "when it is off", |
| 149 | env: {}, |
| 150 | file: "home/.cursor/cli-config.json", |
| 151 | text: JSON.stringify({ network: { useHttp1ForAgent: false } }), |
| 152 | expected: undefined, |
| 153 | }, |
| 154 | { |
| 155 | name: "when the file is damaged", |
| 156 | env: {}, |
| 157 | file: "home/.cursor/cli-config.json", |
| 158 | text: "{", |
| 159 | expected: undefined, |
| 160 | warns: true, |
| 161 | }, |
| 162 | { name: "when there is no file", env: {}, file: undefined, text: "", expected: undefined }, |
| 163 | ])("follows the Cursor CLI's HTTP/1.1 setting $name", ({ env, file, text, expected, warns }) => |
| 164 | Effect.gen(function* () { |
| 165 | const fileSystem = yield* FileSystem.FileSystem; |
| 166 | const path = yield* Path.Path; |
| 167 | const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-cursor-cli-config-" }); |
| 168 | if (file !== undefined) { |
| 169 | yield* fileSystem.makeDirectory(path.dirname(path.join(root, file)), { recursive: true }); |
| 170 | yield* fileSystem.writeFileString(path.join(root, file), text); |
| 171 | } |
| 172 | const environment = Object.fromEntries( |
| 173 | Object.entries(env).map(([key, directory]) => [key, path.join(root, directory)]), |
| 174 | ); |
| 175 | const warnings: Array<unknown> = []; |
| 176 | const captureWarnings = Logger.make(({ logLevel, message }) => { |
| 177 | if (logLevel === "Warn") warnings.push(message); |
| 178 | }); |
| 179 | yield* Effect.gen(function* () { |
| 180 | const sdk = Context.get(yield* Layer.build(CursorSdk.layer), CursorSdk.CursorSdk); |
| 181 | assert.isUndefined(sdkDefaults.useHttp1ForAgent); |
| 182 | sdk.load(); |
| 183 | assert.strictEqual(sdkDefaults.useHttp1ForAgent, expected); |
| 184 | }).pipe( |
| 185 | Effect.scoped, |
| 186 | withHost(environment, path.join(root, "home")), |
| 187 | Effect.provide(Logger.layer([captureWarnings], { mergeWithExisting: false })), |
| 188 | ); |
| 189 | assert.isUndefined(sdkDefaults.useHttp1ForAgent); |
| 190 | assert.lengthOf(warnings, warns ? 1 : 0); |
| 191 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 192 | ); |
| 193 | }); |