The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | |
| 3 | const fixture = vi.hoisted(() => { |
| 4 | // @effect-diagnostics-next-line extendsNativeError:off - stands in for the Cursor SDK's native Error classes. |
| 5 | class SdkError extends Error {} |
| 6 | const sdk = { |
| 7 | CursorSdkError: SdkError, |
| 8 | AuthenticationError: SdkError, |
| 9 | ConfigurationError: SdkError, |
| 10 | }; |
| 11 | const sqlite = { SqliteLocalAgentStore: {} }; |
| 12 | const requireSdk = vi.fn((id: string) => (id === "@cursor/sdk" ? sdk : sqlite)); |
| 13 | return { sdk, sqlite, requireSdk }; |
| 14 | }); |
| 15 | |
| 16 | vi.mock("node:module", () => ({ createRequire: () => fixture.requireSdk })); |
| 17 | |
| 18 | describe("Cursor disk modules", () => { |
| 19 | it("loads on demand, shares exports across callers, and leaves SQLite unloaded", async () => { |
| 20 | vi.resetModules(); |
| 21 | fixture.requireSdk.mockClear(); |
| 22 | const sdk = await import("./sdk.ts"); |
| 23 | expect(fixture.requireSdk).not.toHaveBeenCalled(); |
| 24 | expect(sdk.isCursorSdkError(new Error("unloaded"), "CursorSdkError")).toBe(false); |
| 25 | const modules = await Promise.all([ |
| 26 | Promise.resolve().then(sdk.loadCursorSdk), |
| 27 | Promise.resolve().then(sdk.loadCursorSdk), |
| 28 | ]); |
| 29 | expect(modules).toEqual([fixture.sdk, fixture.sdk]); |
| 30 | expect(modules[0]).toBe(modules[1]); |
| 31 | expect(fixture.requireSdk.mock.calls).toEqual([["@cursor/sdk"]]); |
| 32 | expect( |
| 33 | sdk.isCursorSdkError(new fixture.sdk.CursorSdkError("same class"), "CursorSdkError"), |
| 34 | ).toBe(true); |
| 35 | expect(sdk.loadCursorSqlite()).toBe(fixture.sqlite); |
| 36 | expect(sdk.loadCursorSqlite()).toBe(fixture.sqlite); |
| 37 | expect(fixture.requireSdk.mock.calls).toEqual([["@cursor/sdk"], ["@cursor/sdk/sqlite"]]); |
| 38 | }); |
| 39 | |
| 40 | it("surfaces failed loads and permits a later retry without caching partial exports", async () => { |
| 41 | vi.resetModules(); |
| 42 | fixture.requireSdk.mockClear(); |
| 43 | const sdk = await import("./sdk.ts"); |
| 44 | const failure = new Error("missing native chunk"); |
| 45 | fixture.requireSdk.mockImplementationOnce(() => { |
| 46 | throw failure; |
| 47 | }); |
| 48 | expect(sdk.loadCursorSdk).toThrow(failure); |
| 49 | expect(sdk.isCursorSdkError(failure, "CursorSdkError")).toBe(false); |
| 50 | expect(sdk.loadCursorSdk()).toBe(fixture.sdk); |
| 51 | fixture.requireSdk.mockImplementationOnce(() => { |
| 52 | throw failure; |
| 53 | }); |
| 54 | expect(sdk.loadCursorSqlite).toThrow(failure); |
| 55 | expect(sdk.loadCursorSqlite()).toBe(fixture.sqlite); |
| 56 | expect(fixture.requireSdk).toHaveBeenCalledTimes(4); |
| 57 | }); |
| 58 | }); |