forked from pingdotgg/t3code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopilotTextGeneration.test.ts
More file actions
238 lines (216 loc) · 7.15 KB
/
Copy pathCopilotTextGeneration.test.ts
File metadata and controls
238 lines (216 loc) · 7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import * as NodeServices from "@effect/platform-node/NodeServices";
import { it } from "@effect/vitest";
import type { ModelInfo, SessionEvent } from "@github/copilot-sdk";
import { Effect, Layer } from "effect";
import { expect, vi } from "vitest";
import { ServerConfig } from "../../config.ts";
import { ServerSettingsService } from "../../serverSettings.ts";
import { TextGeneration } from "../Services/TextGeneration.ts";
import { makeCopilotTextGenerationLive } from "./CopilotTextGeneration.ts";
class FakeCopilotSession {
public readonly sendImpl = vi.fn(
async (_input: {
prompt: string;
attachments?: Array<{ type: "file"; path: string; displayName?: string }>;
mode?: "enqueue" | "immediate";
}) => {
this.onEvent?.({
id: "turn-start",
timestamp: new Date().toISOString(),
parentId: null,
type: "assistant.turn_start",
data: { turnId: "turn-1" },
});
this.onEvent?.({
id: "assistant-message",
timestamp: new Date().toISOString(),
parentId: "turn-start",
type: "assistant.message",
data: {
messageId: "message-1",
content: this.messageContent,
},
});
this.onEvent?.({
id: "turn-end",
timestamp: new Date().toISOString(),
parentId: "assistant-message",
type: "assistant.turn_end",
data: { turnId: "turn-1" },
});
return "message-1";
},
);
public readonly getMessagesImpl = vi.fn(
async (): Promise<ReadonlyArray<SessionEvent>> => [
{
id: "assistant-message",
timestamp: new Date().toISOString(),
parentId: null,
type: "assistant.message",
data: {
messageId: "message-1",
content: this.messageContent,
},
},
],
);
public readonly destroyImpl = vi.fn(async () => undefined);
public onEvent: ((event: SessionEvent) => void) | undefined;
constructor(public messageContent: string) {}
send(input: {
prompt: string;
attachments?: Array<{ type: "file"; path: string; displayName?: string }>;
mode?: "enqueue" | "immediate";
}) {
return this.sendImpl(input);
}
getMessages() {
return this.getMessagesImpl();
}
destroy() {
return this.destroyImpl();
}
}
class FakeCopilotClient {
public readonly startImpl = vi.fn(async () => undefined);
public readonly stopImpl = vi.fn(async () => [] as Error[]);
public readonly listModelsImpl = vi.fn(async (): Promise<ReadonlyArray<ModelInfo>> => []);
public readonly createSessionImpl = vi.fn(
async (
config: { onEvent?: ((event: SessionEvent) => void) | undefined } & Record<string, unknown>,
) => {
this.session.onEvent = config.onEvent;
return this.session;
},
);
constructor(public readonly session: FakeCopilotSession) {}
start() {
return this.startImpl();
}
stop() {
return this.stopImpl();
}
listModels() {
return this.listModelsImpl();
}
createSession(
config: { onEvent?: ((event: SessionEvent) => void) | undefined } & Record<string, unknown>,
) {
return this.createSessionImpl(config);
}
}
function makeModelInfo(input: {
id: string;
name: string;
supportedReasoningEfforts?: ReadonlyArray<"low" | "medium" | "high" | "xhigh">;
}) {
return input as unknown as import("@github/copilot-sdk").ModelInfo;
}
const session = new FakeCopilotSession(
JSON.stringify({
subject: " Add Copilot text generation. ",
body: "- updated settings\n- added routing",
}),
);
const client = new FakeCopilotClient(session);
let lastClientFactoryOptions: unknown;
const CopilotTextGenerationTestLayer = makeCopilotTextGenerationLive({
clientFactory: (options) => {
lastClientFactoryOptions = options;
return client;
},
}).pipe(
Layer.provideMerge(ServerSettingsService.layerTest()),
Layer.provideMerge(
ServerConfig.layerTest(process.cwd(), {
prefix: "t3code-copilot-text-generation-test-",
}),
),
Layer.provideMerge(NodeServices.layer),
);
it.layer(CopilotTextGenerationTestLayer)("CopilotTextGenerationLive", (it) => {
it.effect("generates and sanitizes commit messages", () =>
Effect.gen(function* () {
client.listModelsImpl.mockReset();
client.createSessionImpl.mockClear();
session.sendImpl.mockClear();
lastClientFactoryOptions = undefined;
client.listModelsImpl.mockResolvedValue([
makeModelInfo({
id: "gpt-5.4-mini",
name: "GPT-5.4 Mini",
supportedReasoningEfforts: ["low", "medium", "high", "xhigh"],
}),
]);
const textGeneration = yield* TextGeneration;
const generated = yield* textGeneration.generateCommitMessage({
cwd: process.cwd(),
branch: "feature/copilot-text-generation",
stagedSummary: "M apps/server/src/git/Layers/CopilotTextGeneration.ts",
stagedPatch: "diff --git a/file b/file",
modelSelection: {
provider: "copilot",
model: "gpt-5.4-mini",
},
});
expect(generated.subject).toBe("Add Copilot text generation");
expect(generated.body).toBe("- updated settings\n- added routing");
const sessionConfig = client.createSessionImpl.mock.calls[0]?.[0] as Record<string, unknown>;
expect(sessionConfig.model).toBe("gpt-5.4-mini");
expect(sessionConfig.reasoningEffort).toBe("low");
expect(sessionConfig.workingDirectory).toBe(process.cwd());
expect(lastClientFactoryOptions).toMatchObject({
cwd: process.cwd(),
logLevel: "error",
});
expect(session.sendImpl.mock.calls[0]?.[0]).toMatchObject({
mode: "immediate",
});
}),
);
it.effect("uses configured binary path and config dir for Copilot text generation", () =>
Effect.gen(function* () {
client.listModelsImpl.mockReset();
client.createSessionImpl.mockClear();
client.startImpl.mockClear();
lastClientFactoryOptions = undefined;
client.listModelsImpl.mockResolvedValue([
makeModelInfo({
id: "gpt-5.4",
name: "GPT-5.4",
supportedReasoningEfforts: ["low", "medium", "high", "xhigh"],
}),
]);
const serverSettings = yield* ServerSettingsService;
yield* serverSettings.updateSettings({
providers: {
copilot: {
binaryPath: "/tmp/copilot",
configDir: "/tmp/copilot-config",
},
},
});
const textGeneration = yield* TextGeneration;
yield* textGeneration.generateCommitMessage({
cwd: process.cwd(),
branch: null,
stagedSummary: "M README.md",
stagedPatch: "diff --git a/README.md b/README.md",
modelSelection: {
provider: "copilot",
model: "gpt-5.4",
options: { reasoningEffort: "high" },
},
});
const sessionConfig = client.createSessionImpl.mock.calls[0]?.[0] as Record<string, unknown>;
expect(lastClientFactoryOptions).toMatchObject({
cliPath: "/tmp/copilot",
cwd: process.cwd(),
logLevel: "error",
});
expect(sessionConfig.configDir).toBe("/tmp/copilot-config");
expect(sessionConfig.reasoningEffort).toBe("high");
}),
);
});