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

CursorSdk.test.ts

45 lines1.7 KB

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