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

CursorAgentSdk.test.ts

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