Open History to see the last commit
1import { CursorSettings, ProviderInstanceId } from "@t3tools/contracts";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Schema from "effect/Schema";
5import { createModelSelection } from "@t3tools/shared/model";
6import { beforeEach, vi } from "vite-plus/test";
7
8import { makeCursorTextGeneration } from "./CursorTextGeneration.ts";
9
10const cursorSdkMock = vi.hoisted(() => ({
11 prompt: vi.fn(async () => ({
12 id: "run-cursor-text-generation-test",
13 status: "finished" as const,
14 result:
15 '{"subject":"Add generated commit message","body":"- verify cursor sdk text generation"}',
16 })),
17}));
18
19vi.mock("@cursor/sdk", () => ({
20 Agent: {
21 prompt: cursorSdkMock.prompt,
22 },
23}));
24
25const decodeCursorSettings = Schema.decodeSync(CursorSettings);
26const cursorSettings = decodeCursorSettings({ enabled: true });
27
28beforeEach(() => {
29 cursorSdkMock.prompt.mockReset();
30 cursorSdkMock.prompt.mockResolvedValue({
31 id: "run-cursor-text-generation-test",
32 status: "finished",
33 result:
34 '{"subject":"Add generated commit message","body":"- verify cursor sdk text generation"}',
35 });
36});
37
38describe("CursorTextGeneration", () => {
39 it.effect("uses the Cursor SDK prompt API with model parameters and API key", () =>
40 Effect.gen(function* () {
41 const textGeneration = yield* makeCursorTextGeneration(cursorSettings, {
42 CURSOR_API_KEY: "test-cursor-key",
43 });
44
45 const generated = yield* textGeneration.generateCommitMessage({
46 cwd: process.cwd(),
47 branch: "feature/cursor-text-generation",
48 stagedSummary: "M apps/server/src/textGeneration/CursorTextGeneration.ts",
49 stagedPatch:
50 "diff --git a/apps/server/src/textGeneration/CursorTextGeneration.ts b/apps/server/src/textGeneration/CursorTextGeneration.ts",
51 modelSelection: createModelSelection(ProviderInstanceId.make("cursor"), "gpt-5.4", [
52 { id: "thinking", value: "high" },
53 { id: "contextWindow", value: "1m" },
54 { id: "fastMode", value: true },
55 ]),
56 });
57
58 expect(generated.subject).toBe("Add generated commit message");
59 expect(generated.body).toBe("- verify cursor sdk text generation");
60
61 expect(cursorSdkMock.prompt).toHaveBeenCalledTimes(1);
62 const [prompt, options] = (
63 cursorSdkMock.prompt.mock.calls as unknown as Array<[string, unknown]>
64 )[0]!;
65 expect(prompt).toContain("Staged patch:");
66 expect(options).toEqual({
67 apiKey: "test-cursor-key",
68 mode: "agent",
69 model: {
70 id: "gpt-5.4",
71 params: [
72 { id: "thinking", value: "high" },
73 { id: "context", value: "1m" },
74 { id: "fast", value: "true" },
75 ],
76 },
77 local: {
78 cwd: process.cwd(),
79 autoReview: false,
80 sandboxOptions: { enabled: false },
81 enableAgentRetries: true,
82 },
83 });
84 }),
85 );
86
87 it.effect("accepts json objects with extra assistant text around them", () =>
88 Effect.gen(function* () {
89 cursorSdkMock.prompt.mockResolvedValueOnce({
90 id: "run-cursor-text-generation-test",
91 status: "finished",
92 result:
93 'Sure, here is the JSON:\n```json\n{\n "subject": "Update README dummy comment with attribution and date",\n "body": ""\n}\n```\nDone.',
94 });
95 const textGeneration = yield* makeCursorTextGeneration(cursorSettings, {
96 CURSOR_API_KEY: "test-cursor-key",
97 });
98
99 const generated = yield* textGeneration.generateCommitMessage({
100 cwd: process.cwd(),
101 branch: "feature/cursor-noisy-json",
102 stagedSummary: "M README.md",
103 stagedPatch: "diff --git a/README.md b/README.md",
104 modelSelection: {
105 instanceId: ProviderInstanceId.make("cursor"),
106 model: "composer-2",
107 },
108 });
109
110 expect(generated.subject).toBe("Update README dummy comment with attribution and date");
111 expect(generated.body).toBe("");
112 }),
113 );
114
115 it.effect("generates thread titles through Cursor SDK text generation", () =>
116 Effect.gen(function* () {
117 cursorSdkMock.prompt.mockResolvedValueOnce({
118 id: "run-cursor-title-generation-test",
119 status: "finished",
120 result: '{"title":"\\"Trim reconnect spinner status after resume.\\""}',
121 });
122 const textGeneration = yield* makeCursorTextGeneration(cursorSettings, {
123 CURSOR_API_KEY: "test-cursor-key",
124 });
125
126 const generated = yield* textGeneration.generateThreadTitle({
127 cwd: process.cwd(),
128 message: "Fix the reconnect spinner after a resumed session.",
129 modelSelection: {
130 instanceId: ProviderInstanceId.make("cursor"),
131 model: "composer-2",
132 },
133 });
134
135 expect(generated.title).toBe("Trim reconnect spinner status after resume.");
136 }),
137 );
138
139 it.effect("requires CURSOR_API_KEY before calling the SDK", () =>
140 Effect.gen(function* () {
141 const textGeneration = yield* makeCursorTextGeneration(cursorSettings, {});
142
143 const error = yield* Effect.flip(
144 textGeneration.generateCommitMessage({
145 cwd: process.cwd(),
146 branch: "feature/cursor-api-key",
147 stagedSummary: "M README.md",
148 stagedPatch: "diff --git a/README.md b/README.md",
149 modelSelection: {
150 instanceId: ProviderInstanceId.make("cursor"),
151 model: "composer-2",
152 },
153 }),
154 );
155
156 expect(error.detail).toBe(
157 "Cursor API key is required. Add CURSOR_API_KEY in provider settings.",
158 );
159 expect(cursorSdkMock.prompt).not.toHaveBeenCalled();
160 }),
161 );
162});