sailfork / t3code-github / packages/ provider-cursor/ src/ server/ CursorSdk.test.ts

import { assert, describe, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import { vi } from "vite-plus/test";

import * as CursorSdk from "./CursorSdk.ts";

// Shaped like the SDK's minified class: the constructor is not named after the error.
// @effect-diagnostics-next-line extendsNativeError:off - mimics the Cursor SDK's own native Error class.
class o extends Error {
  override name = "WriteIterableClosedError";
}

// Calls the guard directly, with Vitest's own listener detached so the guard does not defer to it.
const rejectThroughGuard = (reason: unknown) =>
  Effect.gen(function* () {
    const others = process.listeners("unhandledRejection");
    yield* Layer.build(CursorSdk.layer);
    const guard = process.listeners("unhandledRejection").find((l) => !others.includes(l));
    assert.isDefined(guard);
    using exit = vi.spyOn(process, "exit").mockImplementation(() => undefined as never);
    using _log = vi.spyOn(console, "error").mockImplementation(() => {});
    for (const listener of others) process.off("unhandledRejection", listener);
    try {
      guard(reason, Promise.resolve());
    } finally {
      for (const listener of others) process.on("unhandledRejection", listener);
    }
    return exit.mock.calls;
  }).pipe(Effect.scoped);

describe("CursorSdk unhandled rejection guard", () => {
  it.effect("keeps the server running when the SDK writes to a closed stream", () =>
    Effect.gen(function* () {
      assert.deepStrictEqual(yield* rejectThroughGuard(new o("WritableIterable is closed")), []);
    }),
  );

  it.effect("keeps the server running when the SDK writes after its request stream ended", () =>
    Effect.gen(function* () {
      // Shaped like `ConnectError.from(e, Code.Aborted)` from the SDK's connect-node body pump.
      const cause = Object.assign(new Error("write after end"), {
        code: "ERR_STREAM_WRITE_AFTER_END",
      });
      const reason = Object.assign(new Error("[aborted] write after end", { cause }), {
        name: "ConnectError",
        code: 10,
        rawMessage: "write after end",
      });
      assert.deepStrictEqual(yield* rejectThroughGuard(reason), []);
    }),
  );

  it.effect("still exits on other rejections", () =>
    Effect.gen(function* () {
      assert.deepStrictEqual(yield* rejectThroughGuard(new Error("WritableIterable is closed")), [
        [1],
      ]);
    }),
  );

  it.effect("still exits on aborted requests other than a write after end", () =>
    Effect.gen(function* () {
      const cause = Object.assign(new Error("socket hang up"), { code: "ECONNRESET" });
      const reason = Object.assign(new Error("[aborted] socket hang up", { cause }), {
        name: "ConnectError",
        code: 10,
      });
      assert.deepStrictEqual(yield* rejectThroughGuard(reason), [[1]]);
    }),
  );
});