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

CursorAgentSdk.test.ts

214 lines6.4 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3import { ProviderSessionId, ThreadId } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Exit from "effect/Exit";
7import * as Cause from "effect/Cause";
8import { vi } from "vite-plus/test";
9​
10import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEventLoggers";
11import * as CursorAgentSdk from "./CursorAgentSdk.ts";
12import * as CursorSdk from "./CursorSdk.ts";
13​
14const cursorSdkMock = vi.hoisted(() => {
15 const closeExecutionOrder: Array<string> = [];
16 const runOperations: Array<string> = [];
17 let agentCloseFailure: unknown;
18 let staleRunActive = false;
19 const runWait = vi.fn(async () => ({
20 id: "run-cursor-agent-sdk-test",
21 requestId: "request-cursor-agent-sdk-test",
22 status: "finished",
23 result: "ok",
24 model: { id: "default" },
25 durationMs: 1,
26 }));
27 const runCancel = vi.fn(async () => {});
28 const agentClose = vi.fn(() => {
29 closeExecutionOrder.push("sdk:agent.close");
30 if (agentCloseFailure !== undefined) {
31 throw agentCloseFailure;
32 }
33 });
34 const send = vi.fn(
35 async (
36 _message: unknown,
37 options: { readonly onDelta?: (input: { readonly update: unknown }) => Promise<void> },
38 ) => {
39 runOperations.push("run.start");
40 if (staleRunActive) {
41 throw new Error("Agent agent-cursor-agent-sdk-test already has active run");
42 }
43 await options.onDelta?.({
44 update: {
45 type: "assistant-message-chunk",
46 text: "hello",
47 },
48 });
49 return {
50 id: "run-cursor-agent-sdk-test",
51 agentId: "agent-cursor-agent-sdk-test",
52 wait: runWait,
53 cancel: runCancel,
54 };
55 },
56 );
57 const create = vi.fn(async () => ({
58 agentId: "agent-cursor-agent-sdk-test",
59 send,
60 close: agentClose,
61 }));
62 const resume = vi.fn(async () => ({
63 agentId: "agent-cursor-agent-sdk-test",
64 send,
65 close: agentClose,
66 }));
67 const listRuns = vi.fn(async () => {
68 runOperations.push("agent.listRuns");
69 return {
70 items: staleRunActive ? [{ id: "run-stale", status: "running" }] : [],
71 };
72 });
73 const cancelRun = vi.fn(async () => {
74 runOperations.push("agent.cancelRun");
75 staleRunActive = false;
76 });
77​
78 return {
79 agentClose,
80 cancelRun,
81 closeExecutionOrder,
82 create,
83 listRuns,
84 resume,
85 runOperations,
86 runCancel,
87 runWait,
88 send,
89 setAgentCloseFailure: (failure: unknown) => {
90 agentCloseFailure = failure;
91 },
92 setStaleRunActive: (active: boolean) => {
93 staleRunActive = active;
94 },
95 };
96});
97​
98vi.mock("./sdk.ts", () => ({
99 loadCursorSdk: () => ({
100 Agent: {
101 create: cursorSdkMock.create,
102 resume: cursorSdkMock.resume,
103 listRuns: cursorSdkMock.listRuns,
104 cancelRun: cursorSdkMock.cancelRun,
105 messages: {
106 list: vi.fn(async () => []),
107 },
108 },
109 createAgentPlatform: vi.fn(),
110 }),
111 loadCursorSqlite: () => ({ SqliteLocalAgentStore: { open: vi.fn() } }),
112}));
113​
114const layerTest = CursorAgentSdk.layer.pipe(
115 Layer.provide(
116 Layer.succeed(ProviderEventLoggers.ProviderEventLoggers, {
117 native: {
118 filePath: "cursor-agent-sdk-test.log",
119 write: (event: unknown) =>
120 Effect.sync(() => {
121 if (
122 typeof event === "object" &&
123 event !== null &&
124 Reflect.get(Reflect.get(event, "event"), "payload")?.type === "agent.close"
125 ) {
126 cursorSdkMock.closeExecutionOrder.push("protocol:agent.close");
127 }
128 }),
129 close: () => Effect.void,
130 },
131 canonical: undefined,
132 }),
133 ),
134 Layer.provide(CursorSdk.layer),
135 Layer.provide(NodeServices.layer),
136);
137​
138describe("CursorAgentSdkRunner", () => {
139 it.effect("constructs without loading and reports first-use load failures as runner errors", () =>
140 Effect.gen(function* () {
141 const failure = new Error("missing SDK chunk");
142 const load = vi.fn(() => {
143 throw failure;
144 });
145 const runner = yield* CursorAgentSdk.makeCursorAgentSdkRunner(() => undefined).pipe(
146 Effect.provideService(CursorSdk.CursorSdk, {
147 load,
148 loadSqlite: () => {
149 throw new Error("unused SQLite");
150 },
151 }),
152 );
153 assert.strictEqual(load.mock.calls.length, 0);
154 const result = yield* runner
155 .open({
156 operation: "create",
157 options: {},
158 threadId: ThreadId.make("lazy-load"),
159 providerSessionId: ProviderSessionId.make("lazy-load"),
160 })
161 .pipe(Effect.exit);
162 assert(Exit.isFailure(result));
163 assert.deepStrictEqual(
164 Cause.squash(result.cause),
165 new CursorAgentSdk.CursorAgentSdkRunnerError({
166 method: "agent.create",
167 cause: failure,
168 }),
169 );
170 assert.strictEqual(load.mock.calls.length, 1);
171 }),
172 );
173​
174 it.effect("retries a resumed send after cancelling its abandoned local run", () =>
175 Effect.gen(function* () {
176 cursorSdkMock.runOperations.length = 0;
177 cursorSdkMock.listRuns.mockClear();
178 cursorSdkMock.cancelRun.mockClear();
179 cursorSdkMock.setStaleRunActive(true);
180​
181 const runner = yield* CursorAgentSdk.CursorAgentSdkRunner;
182 const session = yield* runner.open({
183 operation: "resume",
184 agentId: "agent-cursor-agent-sdk-test",
185 options: { model: { id: "default" }, mode: "agent", local: { cwd: process.cwd() } },
186 threadId: ThreadId.make("thread-cursor-agent-sdk-stale-run-test"),
187 providerSessionId: ProviderSessionId.make(
188 "provider-session-cursor-agent-sdk-stale-run-test",
189 ),
190 });
191 assert.deepStrictEqual(cursorSdkMock.runOperations, []);
192 const run = yield* session.send({ message: "continue" });
193 yield* run.wait;
194​
195 assert.deepStrictEqual(cursorSdkMock.runOperations, [
196 "run.start",
197 "agent.listRuns",
198 "agent.cancelRun",
199 "run.start",
200 ]);
201 assert.deepStrictEqual(cursorSdkMock.listRuns.mock.calls[0], [
202 "agent-cursor-agent-sdk-test",
203 { runtime: "local", cwd: process.cwd(), limit: 1 },
204 ]);
205 assert.deepStrictEqual(cursorSdkMock.cancelRun.mock.calls[0], [
206 "run-stale",
207 {
208 runtime: "local",
209 cwd: process.cwd(),
210 },
211 ]);
212 }).pipe(Effect.provide(layerTest)),
213 );
214});