Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import { vi } from "vite-plus/test";
5
6import * as CursorSdk from "./CursorSdk.ts";
7
8vi.mock("./sdk.ts", () => ({ Agent: {}, createAgentPlatform: {}, SqliteLocalAgentStore: {} }));
9
10// Shaped like the SDK's minified class: the constructor is not named after the error.
11class o extends Error {
12 override name = "WriteIterableClosedError";
13}
14
15// Calls the guard directly, with Vitest's own listener detached so the guard does not defer to it.
16const rejectThroughGuard = (reason: unknown) =>
17 Effect.gen(function* () {
18 const others = process.listeners("unhandledRejection");
19 yield* Layer.build(CursorSdk.layer);
20 const guard = process.listeners("unhandledRejection").find((l) => !others.includes(l));
21 assert.isDefined(guard);
22 using exit = vi.spyOn(process, "exit").mockImplementation(() => undefined as never);
23 using _log = vi.spyOn(console, "error").mockImplementation(() => {});
24 for (const listener of others) process.off("unhandledRejection", listener);
25 try {
26 guard(reason, Promise.resolve());
27 } finally {
28 for (const listener of others) process.on("unhandledRejection", listener);
29 }
30 return exit.mock.calls;
31 }).pipe(Effect.scoped);
32
33describe("CursorSdk unhandled rejection guard", () => {
34 it.effect("keeps the server running when the SDK writes to a closed stream", () =>
35 Effect.gen(function* () {
36 assert.deepStrictEqual(yield* rejectThroughGuard(new o("WritableIterable is closed")), []);
37 }),
38 );
39
40 it.effect("still exits on other rejections", () =>
41 Effect.gen(function* () {
42 assert.deepStrictEqual(yield* rejectThroughGuard(new Error("WritableIterable is closed")), [
43 [1],
44 ]);
45 }),
46 );
47});