|
1 | 1 | import { beforeEach, describe, expect, it } from "bun:test"; |
2 | 2 | import type { CreateLifecycleWorktreeInput } from "../services/lifecycle-service"; |
3 | | -import type { LinearIssue } from "../services/linear-service"; |
| 3 | +import type { FetchIssuesResult, LinearIssue } from "../services/linear-service"; |
4 | 4 | import { |
5 | 5 | filterAutoCreateIssues, |
6 | 6 | LINEAR_AUTO_CREATE_POLL_INTERVAL_MS, |
7 | 7 | resetProcessedIssues, |
| 8 | + runLinearAutoCreateOnce, |
8 | 9 | startLinearAutoCreateMonitor, |
9 | 10 | type LinearAutoCreateDependencies, |
10 | 11 | } from "../services/linear-auto-create-service"; |
@@ -45,15 +46,31 @@ function createIssue(overrides: Partial<LinearIssue> = {}): LinearIssue { |
45 | 46 | }; |
46 | 47 | } |
47 | 48 |
|
48 | | -async function flushPromises(): Promise<void> { |
49 | | - for (let i = 0; i < 5; i += 1) { |
50 | | - await Promise.resolve(); |
51 | | - } |
| 49 | +interface Deferred<T> { |
| 50 | + promise: Promise<T>; |
| 51 | + resolve(value: T): void; |
| 52 | +} |
| 53 | + |
| 54 | +function createDeferred<T>(): Deferred<T> { |
| 55 | + let resolveDeferred: ((value: T) => void) | null = null; |
| 56 | + const promise = new Promise<T>((resolve) => { |
| 57 | + resolveDeferred = resolve; |
| 58 | + }); |
| 59 | + |
| 60 | + return { |
| 61 | + promise, |
| 62 | + resolve(value) { |
| 63 | + if (!resolveDeferred) throw new Error("deferred resolver not initialized"); |
| 64 | + resolveDeferred(value); |
| 65 | + }, |
| 66 | + }; |
52 | 67 | } |
53 | 68 |
|
54 | 69 | function createDeps(input: { |
55 | 70 | issues?: LinearIssue[]; |
56 | 71 | existingBranches?: string[]; |
| 72 | + fetchResult?: FetchIssuesResult; |
| 73 | + onFetch?: (options: { skipCache?: boolean } | undefined) => void; |
57 | 74 | } = {}): { |
58 | 75 | deps: LinearAutoCreateDependencies; |
59 | 76 | created: CreateLifecycleWorktreeInput[]; |
@@ -90,7 +107,8 @@ function createDeps(input: { |
90 | 107 | projectRoot: "/repo", |
91 | 108 | fetchIssues: async (options) => { |
92 | 109 | fetchOptions.push(options); |
93 | | - return { |
| 110 | + input.onFetch?.(options); |
| 111 | + return input.fetchResult ?? { |
94 | 112 | ok: true, |
95 | 113 | data: issues, |
96 | 114 | }; |
@@ -132,53 +150,85 @@ describe("filterAutoCreateIssues", () => { |
132 | 150 | }); |
133 | 151 | }); |
134 | 152 |
|
135 | | -describe("startLinearAutoCreateMonitor", () => { |
| 153 | +describe("runLinearAutoCreateOnce", () => { |
136 | 154 | beforeEach(() => { |
137 | 155 | resetProcessedIssues(); |
138 | 156 | }); |
139 | 157 |
|
140 | | - it("uses a 30 second poll interval", async () => { |
141 | | - let scheduledInterval: number | null = null; |
142 | | - const { deps } = createDeps(); |
| 158 | + it("creates worktrees without requiring dashboard activity", async () => { |
| 159 | + const issue = createIssue(); |
| 160 | + const { deps, created, fetchOptions } = createDeps({ issues: [issue] }); |
143 | 161 |
|
144 | | - const stop = startLinearAutoCreateMonitor(deps, { |
145 | | - intervalDeps: { |
146 | | - scheduleEvery: (_handler, intervalMs) => { |
147 | | - scheduledInterval = intervalMs; |
148 | | - return 1; |
149 | | - }, |
150 | | - cancelSchedule: () => {}, |
| 162 | + await runLinearAutoCreateOnce(deps); |
| 163 | + |
| 164 | + expect(fetchOptions).toEqual([{ skipCache: true }]); |
| 165 | + expect(created).toEqual([ |
| 166 | + { |
| 167 | + mode: "new", |
| 168 | + branch: issue.branchName, |
| 169 | + prompt: `${issue.title}\n\n${issue.description}`, |
| 170 | + }, |
| 171 | + ]); |
| 172 | + }); |
| 173 | + |
| 174 | + it("does not create duplicate worktrees for processed issues", async () => { |
| 175 | + const issue = createIssue(); |
| 176 | + const { deps, created, fetchOptions } = createDeps({ issues: [issue] }); |
| 177 | + |
| 178 | + await runLinearAutoCreateOnce(deps); |
| 179 | + await runLinearAutoCreateOnce(deps); |
| 180 | + |
| 181 | + expect(fetchOptions).toEqual([{ skipCache: true }, { skipCache: true }]); |
| 182 | + expect(created).toEqual([ |
| 183 | + { |
| 184 | + mode: "new", |
| 185 | + branch: issue.branchName, |
| 186 | + prompt: `${issue.title}\n\n${issue.description}`, |
| 187 | + }, |
| 188 | + ]); |
| 189 | + }); |
| 190 | + |
| 191 | + it("does not create worktrees when the Linear fetch fails", async () => { |
| 192 | + const { deps, created, fetchOptions } = createDeps({ |
| 193 | + fetchResult: { |
| 194 | + ok: false, |
| 195 | + error: "Linear API 401: Unauthorized", |
151 | 196 | }, |
152 | 197 | }); |
153 | 198 |
|
154 | | - await flushPromises(); |
155 | | - stop(); |
| 199 | + await runLinearAutoCreateOnce(deps); |
156 | 200 |
|
157 | | - expect(scheduledInterval).toBe(LINEAR_AUTO_CREATE_POLL_INTERVAL_MS); |
158 | | - expect(scheduledInterval).toBe(30_000); |
| 201 | + expect(fetchOptions).toEqual([{ skipCache: true }]); |
| 202 | + expect(created).toEqual([]); |
159 | 203 | }); |
| 204 | +}); |
160 | 205 |
|
161 | | - it("creates worktrees without requiring dashboard activity", async () => { |
162 | | - const issue = createIssue(); |
163 | | - const { deps, created, fetchOptions } = createDeps({ issues: [issue] }); |
| 206 | +describe("startLinearAutoCreateMonitor", () => { |
| 207 | + beforeEach(() => { |
| 208 | + resetProcessedIssues(); |
| 209 | + }); |
| 210 | + |
| 211 | + it("uses a 60 second poll interval", async () => { |
| 212 | + let scheduledInterval = -1; |
| 213 | + const fetchStarted = createDeferred<void>(); |
| 214 | + const { deps } = createDeps({ |
| 215 | + onFetch: () => fetchStarted.resolve(undefined), |
| 216 | + }); |
164 | 217 |
|
165 | 218 | const stop = startLinearAutoCreateMonitor(deps, { |
166 | 219 | intervalDeps: { |
167 | | - scheduleEvery: () => 1, |
| 220 | + scheduleEvery: (_handler, intervalMs) => { |
| 221 | + scheduledInterval = intervalMs; |
| 222 | + return 1; |
| 223 | + }, |
168 | 224 | cancelSchedule: () => {}, |
169 | 225 | }, |
170 | 226 | }); |
171 | 227 |
|
172 | | - await flushPromises(); |
| 228 | + await fetchStarted.promise; |
173 | 229 | stop(); |
174 | 230 |
|
175 | | - expect(fetchOptions).toEqual([{ skipCache: true }]); |
176 | | - expect(created).toEqual([ |
177 | | - { |
178 | | - mode: "new", |
179 | | - branch: issue.branchName, |
180 | | - prompt: `${issue.title}\n\n${issue.description}`, |
181 | | - }, |
182 | | - ]); |
| 231 | + expect(scheduledInterval).toBe(LINEAR_AUTO_CREATE_POLL_INTERVAL_MS); |
| 232 | + expect(scheduledInterval).toBe(60_000); |
183 | 233 | }); |
184 | 234 | }); |
0 commit comments