-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsyncRemoteCommandService.test.ts
More file actions
284 lines (240 loc) · 9.29 KB
/
Copy pathsyncRemoteCommandService.test.ts
File metadata and controls
284 lines (240 loc) · 9.29 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import { describe, expect, it, vi } from "vitest";
import type { SyncCommandPayload } from "../../../../desktop/src/shared/types";
import { deriveDeterministicLaneNameFromPrompt } from "../../../../desktop/src/shared/laneNameFallback";
import { createSyncRemoteCommandService } from "./syncRemoteCommandService";
function makePayload(action: string, args: Record<string, unknown> = {}): SyncCommandPayload {
return { commandId: "cmd-1", action, args };
}
function createService(options?: {
agentChatService?: Record<string, unknown>;
prService?: Record<string, unknown>;
}) {
const ptyService = {
resumeSession: vi.fn().mockResolvedValue({
sessionId: "session-1",
ptyId: "pty-1",
session: { id: "session-1", status: "running" },
}),
};
const logger = { debug: vi.fn(), warn: vi.fn(), error: vi.fn(), info: vi.fn() };
const service = createSyncRemoteCommandService({
laneService: {},
prService: options?.prService ?? {},
ptyService,
sessionService: {},
fileService: {},
...(options?.agentChatService ? { agentChatService: options.agentChatService } : {}),
logger,
} as any);
return { service, ptyService, logger };
}
describe("createSyncRemoteCommandService", () => {
it("routes work.resumeCliSession through the durable PTY resume path", async () => {
const { service, ptyService } = createService();
expect(service.getDescriptor("work.resumeCliSession")).toEqual({
action: "work.resumeCliSession",
scope: "project",
policy: { viewerAllowed: true, queueable: true },
});
const result = await service.execute(makePayload("work.resumeCliSession", {
sessionId: "session-1",
cols: 999,
rows: 1,
}));
expect(ptyService.resumeSession).toHaveBeenCalledWith({
sessionId: "session-1",
cols: 400,
rows: 4,
});
expect(result).toEqual({
sessionId: "session-1",
ptyId: "pty-1",
session: { id: "session-1", status: "running" },
});
});
it("rejects work.resumeCliSession without a session id", async () => {
const { service, ptyService } = createService();
await expect(service.execute(makePayload("work.resumeCliSession"))).rejects.toThrow(
"work.resumeCliSession requires sessionId.",
);
expect(ptyService.resumeSession).not.toHaveBeenCalled();
});
it("omits non-finite work.resumeCliSession dimensions", async () => {
const { service, ptyService } = createService();
await service.execute(makePayload("work.resumeCliSession", {
sessionId: "session-1",
cols: Number.NaN,
rows: Number.POSITIVE_INFINITY,
}));
expect(ptyService.resumeSession).toHaveBeenCalledWith({
sessionId: "session-1",
});
});
it("routes the canonical chat history page command to the chat service", async () => {
const getChatEventHistoryPage = vi.fn().mockReturnValue({
sessionId: "chat-1",
events: [],
startOffset: 128,
hasMore: true,
sessionFound: true,
});
const { service } = createService({
agentChatService: { getChatEventHistoryPage },
});
expect(service.getDescriptor("chat.getChatEventHistoryPage")).toEqual({
action: "chat.getChatEventHistoryPage",
scope: "project",
policy: { viewerAllowed: true },
});
const result = await service.execute(makePayload("chat.getChatEventHistoryPage", {
sessionId: "chat-1",
beforeOffset: 4096,
maxBytes: 65_536,
}));
expect(getChatEventHistoryPage).toHaveBeenCalledWith("chat-1", {
beforeOffset: 4096,
maxBytes: 65_536,
});
expect(result).toEqual({
sessionId: "chat-1",
events: [],
startOffset: 128,
hasMore: true,
sessionFound: true,
});
});
});
describe("prs.land", () => {
it("forwards bypass + editable commit message to prService.land", async () => {
const land = vi.fn().mockResolvedValue({ prId: "pr-1", success: true });
const { service } = createService({ prService: { land } });
const result = await service.execute(makePayload("prs.land", {
prId: "pr-1",
method: "squash",
bypassRules: true,
commitTitle: "Land it",
commitBody: "Body text",
expectedHeadSha: "abc123",
}));
expect(land).toHaveBeenCalledWith({
prId: "pr-1",
method: "squash",
bypassRules: true,
commitTitle: "Land it",
commitBody: "Body text",
expectedHeadSha: "abc123",
});
expect(result).toEqual({ prId: "pr-1", success: true });
});
it("omits optional fields that are absent or blank", async () => {
const land = vi.fn().mockResolvedValue({ prId: "pr-1", success: true });
const { service } = createService({ prService: { land } });
await service.execute(makePayload("prs.land", {
prId: "pr-1",
method: "merge",
commitTitle: " ",
}));
expect(land).toHaveBeenCalledWith({ prId: "pr-1", method: "merge" });
});
it("rejects an invalid method", async () => {
const land = vi.fn();
const { service } = createService({ prService: { land } });
await expect(
service.execute(makePayload("prs.land", { prId: "pr-1", method: "fast-forward" })),
).rejects.toThrow("prs.land requires method to be merge, squash, or rebase.");
expect(land).not.toHaveBeenCalled();
});
});
describe("prs.updateBranch", () => {
it("forwards strategy + expected head sha to prService.updateBranch", async () => {
const updateBranch = vi.fn().mockResolvedValue({ prId: "pr-1", success: true, hasConflicts: false });
const { service } = createService({ prService: { updateBranch } });
const result = await service.execute(makePayload("prs.updateBranch", {
prId: "pr-1",
strategy: "rebase",
expectedHeadSha: "abc123",
}));
expect(updateBranch).toHaveBeenCalledWith({
prId: "pr-1",
strategy: "rebase",
expectedHeadSha: "abc123",
});
expect(result).toEqual({ prId: "pr-1", success: true, hasConflicts: false });
});
it("rejects an invalid strategy", async () => {
const updateBranch = vi.fn();
const { service } = createService({ prService: { updateBranch } });
await expect(
service.execute(makePayload("prs.updateBranch", { prId: "pr-1", strategy: "squash" })),
).rejects.toThrow("prs.updateBranch requires strategy to be merge or rebase.");
expect(updateBranch).not.toHaveBeenCalled();
});
});
describe("lanes.suggestName", () => {
it("exposes a non-queueable, viewer-allowed descriptor", () => {
const { service } = createService();
expect(service.getDescriptor("lanes.suggestName")).toEqual({
action: "lanes.suggestName",
scope: "project",
policy: { viewerAllowed: true },
});
});
it("returns the model-suggested name on success", async () => {
const suggestLaneNameFromPrompt = vi.fn().mockResolvedValue("refactor-auth-flow");
const { service } = createService({ agentChatService: { suggestLaneNameFromPrompt } });
const result = await service.execute(makePayload("lanes.suggestName", {
laneId: "lane-1",
prompt: "please refactor the auth flow",
modelId: "anthropic/claude-haiku-4-5",
}));
expect(result).toEqual({ name: "refactor-auth-flow" });
expect(suggestLaneNameFromPrompt).toHaveBeenCalledWith({
laneId: "lane-1",
prompt: "please refactor the auth flow",
modelId: "anthropic/claude-haiku-4-5",
});
});
it("falls back to the client's deterministic name and logs when the naming service throws", async () => {
const suggestLaneNameFromPrompt = vi.fn().mockRejectedValue(new Error("boom"));
const { service, logger } = createService({ agentChatService: { suggestLaneNameFromPrompt } });
const result = await service.execute(makePayload("lanes.suggestName", {
laneId: "lane-1",
prompt: "fix the flaky login test",
modelId: "m",
fallbackName: "fix the flaky login test",
}));
expect(result).toEqual({ name: "fix the flaky login test" });
expect(logger.warn).toHaveBeenCalledWith(
"sync.lanes_suggest_name_failed",
expect.objectContaining({ laneId: "lane-1", modelId: "m" }),
);
});
it("falls back when the naming service is unavailable, deriving from the prompt without a client fallback", async () => {
const { service } = createService();
const result = await service.execute(makePayload("lanes.suggestName", {
laneId: "lane-1",
prompt: "Please help me fix the login bug",
modelId: "m",
}));
expect(result).toEqual({
name: deriveDeterministicLaneNameFromPrompt("Please help me fix the login bug"),
});
});
it("falls back when the naming service returns an empty string", async () => {
const suggestLaneNameFromPrompt = vi.fn().mockResolvedValue(" ");
const { service } = createService({ agentChatService: { suggestLaneNameFromPrompt } });
const result = await service.execute(makePayload("lanes.suggestName", {
laneId: "lane-1",
prompt: "build the dashboard",
modelId: "m",
fallbackName: "build the dashboard",
}));
expect(result).toEqual({ name: "build the dashboard" });
});
it("rejects when prompt is missing", async () => {
const { service } = createService();
await expect(
service.execute(makePayload("lanes.suggestName", { laneId: "lane-1", modelId: "m" })),
).rejects.toThrow("lanes.suggestName requires prompt.");
});
});