-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlinear-auto-create-service.test.ts
More file actions
512 lines (450 loc) · 16.9 KB
/
Copy pathlinear-auto-create-service.test.ts
File metadata and controls
512 lines (450 loc) · 16.9 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
import { beforeEach, describe, expect, it } from "bun:test";
import type { CreateLifecycleWorktreeInput } from "../services/lifecycle-service";
import type { FetchIssuesResult, LinearIssue } from "../services/linear-service";
import {
filterAutoCreateIssues,
filterAutoOneshotIssues,
LINEAR_AUTO_CREATE_POLL_INTERVAL_MS,
resetProcessedIssues,
runLinearAutoCreateOnce,
startLinearAutoCreateMonitor,
type LinearAutoCreateDependencies,
} from "../services/linear-auto-create-service";
function createIssue(overrides: Partial<LinearIssue> = {}): LinearIssue {
const issue: LinearIssue = {
id: "issue-1",
identifier: "ENG-123",
title: "Auto create this",
description: "Details",
priority: 0,
priorityLabel: "No priority",
url: "https://linear.app/acme/issue/ENG-123",
branchName: "eng-123-auto-create",
dueDate: null,
updatedAt: "2026-05-13T10:00:00.000Z",
state: {
name: "Todo",
color: "#999999",
type: "unstarted",
},
team: {
name: "Engineering",
key: "ENG",
},
labels: [
{
name: "webmux",
color: "#2563eb",
},
],
project: null,
};
return {
...issue,
...overrides,
};
}
interface Deferred<T> {
promise: Promise<T>;
resolve(value: T): void;
}
function createDeferred<T>(): Deferred<T> {
let resolveDeferred: ((value: T) => void) | null = null;
const promise = new Promise<T>((resolve) => {
resolveDeferred = resolve;
});
return {
promise,
resolve(value) {
if (!resolveDeferred) throw new Error("deferred resolver not initialized");
resolveDeferred(value);
},
};
}
function createDeps(input: {
issues?: LinearIssue[] | (() => LinearIssue[]);
existingBranches?: string[];
fetchResult?: FetchIssuesResult;
onFetch?: (options: { skipCache?: boolean } | undefined) => void;
runOneshotForIssue?: (issueId: string) => Promise<{ branch: string }>;
onOneshotPickedUp?: (input: { issue: LinearIssue; branch: string }) => Promise<void>;
} = {}): {
deps: LinearAutoCreateDependencies;
created: CreateLifecycleWorktreeInput[];
fetchOptions: Array<{ skipCache?: boolean } | undefined>;
} {
const created: CreateLifecycleWorktreeInput[] = [];
const fetchOptions: Array<{ skipCache?: boolean } | undefined> = [];
const issues: LinearIssue[] | (() => LinearIssue[]) = input.issues ?? [];
const existingBranches = input.existingBranches ?? [];
return {
created,
fetchOptions,
deps: {
lifecycleService: {
async createWorktree(worktreeInput): Promise<{ branch: string; worktreeId: string }> {
created.push(worktreeInput);
return {
branch: worktreeInput.branch ?? "generated-branch",
worktreeId: `wt-${created.length}`,
};
},
},
git: {
listWorktrees: () =>
existingBranches.map((branch) => ({
path: `/repo/__worktrees/${branch}`,
branch,
head: "abc123",
detached: false,
bare: false,
})),
},
projectRoot: "/repo",
fetchIssues: async (options) => {
fetchOptions.push(options);
input.onFetch?.(options);
return input.fetchResult ?? {
ok: true,
data: typeof issues === "function" ? issues() : issues,
};
},
...(input.runOneshotForIssue ? { runOneshotForIssue: input.runOneshotForIssue } : {}),
...(input.onOneshotPickedUp ? { onOneshotPickedUp: input.onOneshotPickedUp } : {}),
},
};
}
describe("filterAutoCreateIssues", () => {
beforeEach(() => {
resetProcessedIssues();
});
it("keeps Todo issues with the webmux label that do not already have a worktree", () => {
const issue = createIssue();
const inProgress = createIssue({
id: "issue-2",
identifier: "ENG-124",
branchName: "eng-124-started",
state: {
name: "In Progress",
color: "#f59e0b",
type: "started",
},
});
const missingLabel = createIssue({
id: "issue-3",
identifier: "ENG-125",
branchName: "eng-125-no-label",
labels: [],
});
const existing = createIssue({
id: "issue-4",
identifier: "ENG-126",
branchName: "eng-126-existing",
});
expect(filterAutoCreateIssues([issue, inProgress, missingLabel, existing], ["eng-126-existing"])).toEqual([issue]);
});
});
describe("watchTeamKeys filter", () => {
beforeEach(() => {
resetProcessedIssues();
});
it("keeps every team when watchTeamKeys is undefined or empty", () => {
const eng = createIssue({ id: "eng", identifier: "ENG-1", branchName: "eng-1", team: { name: "Engineering", key: "ENG" } });
const ops = createIssue({ id: "ops", identifier: "OPS-1", branchName: "ops-1", team: { name: "Ops", key: "OPS" } });
expect(filterAutoCreateIssues([eng, ops], []).map((i) => i.identifier)).toEqual(["ENG-1", "OPS-1"]);
expect(filterAutoCreateIssues([eng, ops], [], []).map((i) => i.identifier)).toEqual(["ENG-1", "OPS-1"]);
});
it("drops issues whose team key is not in the (uppercase, normalized) allowlist", () => {
const eng = createIssue({ id: "eng", identifier: "ENG-1", branchName: "eng-1", team: { name: "Engineering", key: "ENG" } });
const ops = createIssue({ id: "ops", identifier: "OPS-1", branchName: "ops-1", team: { name: "Ops", key: "OPS" } });
const design = createIssue({ id: "des", identifier: "DES-1", branchName: "des-1", team: { name: "Design", key: "DES" } });
expect(filterAutoCreateIssues([eng, ops, design], [], ["ENG", "OPS"]).map((i) => i.identifier))
.toEqual(["ENG-1", "OPS-1"]);
});
it("applies the same filter to the oneshot variant", () => {
const ops = createIssue({
id: "ops", identifier: "OPS-1", branchName: "ops-1",
labels: [{ name: "webmux_oneshot", color: "#fff" }],
team: { name: "Ops", key: "OPS" },
});
expect(filterAutoOneshotIssues([ops], [], ["ENG"])).toEqual([]);
expect(filterAutoOneshotIssues([ops], [], ["OPS"]).map((i) => i.identifier)).toEqual(["OPS-1"]);
});
});
describe("filterAutoOneshotIssues", () => {
beforeEach(() => {
resetProcessedIssues();
});
it("matches the webmux_oneshot label case-insensitively", () => {
const issues = [
createIssue({ id: "a", identifier: "ENG-1", branchName: "eng-1", labels: [{ name: "webmux_oneshot", color: "#fff" }] }),
createIssue({ id: "b", identifier: "ENG-2", branchName: "eng-2", labels: [{ name: "WEBMUX_ONESHOT", color: "#fff" }] }),
createIssue({ id: "c", identifier: "ENG-3", branchName: "eng-3", labels: [{ name: "webmux", color: "#fff" }] }),
];
const matches = filterAutoOneshotIssues(issues, []);
expect(matches.map((i) => i.identifier)).toEqual(["ENG-1", "ENG-2"]);
});
it("excludes issues that already have a worktree", () => {
const issues = [
createIssue({
id: "a", identifier: "ENG-1", branchName: "eng-1",
labels: [{ name: "webmux_oneshot", color: "#fff" }],
}),
];
expect(filterAutoOneshotIssues(issues, ["eng-1"])).toEqual([]);
});
it("routes issues tagged with both webmux and webmux_oneshot to the oneshot filter only", () => {
const issues = [
createIssue({
id: "a", identifier: "ENG-1", branchName: "eng-1",
labels: [{ name: "webmux", color: "#fff" }, { name: "webmux_oneshot", color: "#fff" }],
}),
];
expect(filterAutoOneshotIssues(issues, []).map((i) => i.identifier)).toEqual(["ENG-1"]);
expect(filterAutoCreateIssues(issues, [])).toEqual([]);
});
});
describe("runLinearAutoCreateOnce", () => {
beforeEach(() => {
resetProcessedIssues();
});
it("creates worktrees without requiring dashboard activity", async () => {
const issue = createIssue();
const { deps, created, fetchOptions } = createDeps({ issues: [issue] });
await runLinearAutoCreateOnce(deps);
expect(fetchOptions).toEqual([{ skipCache: true }]);
expect(created).toEqual([
{
mode: "new",
branch: issue.branchName,
prompt: `${issue.title}\n\n${issue.description}`,
},
]);
});
it("does not create duplicate worktrees for processed issues", async () => {
const issue = createIssue();
const { deps, created, fetchOptions } = createDeps({ issues: [issue] });
await runLinearAutoCreateOnce(deps);
await runLinearAutoCreateOnce(deps);
expect(fetchOptions).toEqual([{ skipCache: true }, { skipCache: true }]);
expect(created).toEqual([
{
mode: "new",
branch: issue.branchName,
prompt: `${issue.title}\n\n${issue.description}`,
},
]);
});
it("dedupes permanent createWorktree failures so they don't retry every poll", async () => {
const issue = createIssue();
let createCalls = 0;
const deps: LinearAutoCreateDependencies = {
lifecycleService: {
async createWorktree(): Promise<{ branch: string; worktreeId: string }> {
createCalls += 1;
throw new Error("Branch already exists");
},
},
git: { listWorktrees: () => [] },
projectRoot: "/repo",
fetchIssues: async () => ({ ok: true, data: [issue] }),
};
await runLinearAutoCreateOnce(deps);
await runLinearAutoCreateOnce(deps);
await runLinearAutoCreateOnce(deps);
expect(createCalls).toBe(1);
});
it("dedupes permanent oneshot failures so they don't retry every poll", async () => {
const issue = createIssue({ labels: [{ name: "webmux_oneshot", color: "#fff" }] });
let oneshotCalls = 0;
const deps: LinearAutoCreateDependencies = {
lifecycleService: {
async createWorktree(): Promise<{ branch: string; worktreeId: string }> {
throw new Error("should not be called");
},
},
git: { listWorktrees: () => [] },
projectRoot: "/repo",
fetchIssues: async () => ({ ok: true, data: [issue] }),
runOneshotForIssue: async () => {
oneshotCalls += 1;
throw new Error("server unreachable");
},
};
await runLinearAutoCreateOnce(deps);
await runLinearAutoCreateOnce(deps);
await runLinearAutoCreateOnce(deps);
expect(oneshotCalls).toBe(1);
});
it("re-creates after the label is removed and re-added", async () => {
const labeled = createIssue();
const unlabeled = createIssue({ labels: [] });
let current: LinearIssue[] = [labeled];
// No existing worktrees — so once removed, the branch-existence filter
// won't be the thing blocking the second pass. processedIssueIds is.
const { deps, created } = createDeps({ issues: () => current });
await runLinearAutoCreateOnce(deps);
expect(created.length).toBe(1);
// Same issue still labeled → dedup blocks creation.
await runLinearAutoCreateOnce(deps);
expect(created.length).toBe(1);
// Label removed → dedup entry evicts on next poll.
current = [unlabeled];
await runLinearAutoCreateOnce(deps);
expect(created.length).toBe(1);
// Label re-added → dedup is no longer blocking, so we trigger again.
current = [labeled];
await runLinearAutoCreateOnce(deps);
expect(created.length).toBe(2);
});
it("does not create worktrees when the Linear fetch fails", async () => {
const { deps, created, fetchOptions } = createDeps({
fetchResult: {
ok: false,
error: "Linear API 401: Unauthorized",
},
});
await runLinearAutoCreateOnce(deps);
expect(fetchOptions).toEqual([{ skipCache: true }]);
expect(created).toEqual([]);
});
it("calls runOneshotForIssue for webmux_oneshot-labeled issues and skips webmux create for them", async () => {
const oneshotIssue = createIssue({
id: "issue-oneshot", identifier: "ENG-200", branchName: "eng-200-oneshot",
labels: [{ name: "webmux_oneshot", color: "#fff" }],
});
const createIssue1 = createIssue({
id: "issue-create", identifier: "ENG-201", branchName: "eng-201-create",
labels: [{ name: "webmux", color: "#fff" }],
});
const triggered: string[] = [];
const { deps, created } = createDeps({
issues: [oneshotIssue, createIssue1],
runOneshotForIssue: async (id) => {
triggered.push(id);
return { branch: "eng-200-oneshot" };
},
});
await runLinearAutoCreateOnce(deps);
expect(triggered).toEqual(["ENG-200"]);
expect(created.map((c) => c.branch)).toEqual(["eng-201-create"]);
});
it("does not notify onOneshotPickedUp for the regular `webmux` (create) path", async () => {
// Regular pickups are user-driven (the user added the label themselves);
// the comment would be noise. Only the autonomous oneshot path gets it.
const issue = createIssue();
const pickups: string[] = [];
const { deps } = createDeps({
issues: [issue],
onOneshotPickedUp: async ({ issue: picked }) => {
pickups.push(picked.identifier);
},
});
await runLinearAutoCreateOnce(deps);
expect(pickups).toEqual([]);
});
it("notifies onOneshotPickedUp with the branch resolved by runOneshotForIssue, not the issue's branchName", async () => {
// Regression guard: `buildSeedFromLinear` may resolve to an attachment-payload
// or PR branch instead of `issue.branchName`. The pickup-comment contract
// requires the *actual* working branch.
const oneshotIssue = createIssue({
id: "issue-oneshot", identifier: "ENG-200", branchName: "eng-200-original",
labels: [{ name: "webmux_oneshot", color: "#fff" }],
});
const pickups: Array<{ identifier: string; branch: string }> = [];
const { deps } = createDeps({
issues: [oneshotIssue],
runOneshotForIssue: async () => ({ branch: "eng-200-resumed-from-attachment" }),
onOneshotPickedUp: async ({ issue: picked, branch }) => {
pickups.push({ identifier: picked.identifier, branch });
},
});
await runLinearAutoCreateOnce(deps);
expect(pickups).toEqual([
{ identifier: "ENG-200", branch: "eng-200-resumed-from-attachment" },
]);
});
it("does not notify onOneshotPickedUp when the oneshot launch itself fails", async () => {
const oneshotIssue = createIssue({
id: "issue-oneshot", identifier: "ENG-200", branchName: "eng-200-oneshot",
labels: [{ name: "webmux_oneshot", color: "#fff" }],
});
const pickups: string[] = [];
const deps: LinearAutoCreateDependencies = {
lifecycleService: {
async createWorktree(): Promise<{ branch: string; worktreeId: string }> {
throw new Error("should not be called");
},
},
git: { listWorktrees: () => [] },
projectRoot: "/repo",
fetchIssues: async () => ({ ok: true, data: [oneshotIssue] }),
runOneshotForIssue: async () => { throw new Error("server unreachable"); },
onOneshotPickedUp: async ({ issue: picked }) => {
pickups.push(picked.identifier);
},
};
await runLinearAutoCreateOnce(deps);
expect(pickups).toEqual([]);
});
it("swallows onOneshotPickedUp failures so the pickup still completes", async () => {
const oneshotIssue = createIssue({
id: "issue-oneshot", identifier: "ENG-200", branchName: "eng-200-oneshot",
labels: [{ name: "webmux_oneshot", color: "#fff" }],
});
const oneshotCalls: string[] = [];
const { deps } = createDeps({
issues: [oneshotIssue],
runOneshotForIssue: async (id) => {
oneshotCalls.push(id);
return { branch: "eng-200-oneshot" };
},
onOneshotPickedUp: async () => {
throw new Error("Linear comment failed");
},
});
await runLinearAutoCreateOnce(deps);
// Pickup still happened and is deduped on the next pass.
await runLinearAutoCreateOnce(deps);
expect(oneshotCalls).toEqual(["ENG-200"]);
});
it("skips webmux_oneshot issues when no runOneshotForIssue dep is provided", async () => {
const oneshotIssue = createIssue({
id: "issue-oneshot", identifier: "ENG-200", branchName: "eng-200-oneshot",
labels: [{ name: "webmux_oneshot", color: "#fff" }],
});
const { deps, created } = createDeps({ issues: [oneshotIssue] });
await runLinearAutoCreateOnce(deps);
expect(created).toEqual([]);
});
});
describe("startLinearAutoCreateMonitor", () => {
beforeEach(() => {
resetProcessedIssues();
});
it("uses a 60 second poll interval", async () => {
let scheduledInterval = -1;
const fetchStarted = createDeferred<void>();
let fetchCount = 0;
const { deps } = createDeps({
onFetch: () => {
fetchCount += 1;
if (fetchCount === 1) fetchStarted.resolve();
},
});
const stop = startLinearAutoCreateMonitor(deps, {
intervalDeps: {
scheduleEvery: (handler: () => void, intervalMs: number) => {
scheduledInterval = intervalMs;
handler();
return 1;
},
cancelSchedule: () => {},
},
});
await fetchStarted.promise;
stop();
expect(scheduledInterval).toBe(LINEAR_AUTO_CREATE_POLL_INTERVAL_MS);
expect(LINEAR_AUTO_CREATE_POLL_INTERVAL_MS).toBe(60_000);
});
});