| @@ -146,6 +146,11 @@ private let emptyPage = """ |
| 146 | private let noLabels = """ |
146 | private let noLabels = """ |
| 147 | {"protocol_version":1,"data":[],"exit_code":0} |
147 | {"protocol_version":1,"data":[],"exit_code":0} |
| 148 | """ |
148 | """ |
| |
149 | private let pageWithCursor = """ |
| |
150 | {"protocol_version":1,"data":{"items":[\ |
| |
151 | {"number":7,"title":"a bug","state":"open","author":"cmc",\ |
| |
152 | "created_at":"2026-09-01T00:00:00Z"}],"next":"c1"},"exit_code":0} |
| |
153 | """ |
| 149 | |
154 | |
| 150 | /// Every `argv=` value from a request URL, in order. |
155 | /// Every `argv=` value from a request URL, in order. |
| 151 | private func argvFrom(_ url: URL) -> [String] { |
156 | private func argvFrom(_ url: URL) -> [String] { |
| @@ -198,17 +203,106 @@ struct IssueListFilterWiringTests { |
| 198 | await model.load() |
203 | await model.load() |
| 199 | |
204 | |
| 200 | stub.enqueue(.init(status: 200, json: emptyPage, match: "argv=issue")) |
205 | stub.enqueue(.init(status: 200, json: emptyPage, match: "argv=issue")) |
| |
206 | // No sleeps between keystrokes: `Task.sleep` guarantees a minimum |
| |
207 | // wait, not a maximum, so any inter-keystroke delay is a window |
| |
208 | // for the debounce timer to fire early on a loaded machine. Typing |
| |
209 | // the whole string with no `await` between assignments removes |
| |
210 | // that window; only the final wait — comfortably past the 300ms |
| |
211 | // debounce — needs headroom. |
| 201 | for text in ["c", "cr", "cra", "cras", "crash"] { |
212 | for text in ["c", "cr", "cra", "cras", "crash"] { |
| 202 | model.filter.search = text |
213 | model.filter.search = text |
| 203 | try await Task.sleep(for: .milliseconds(20)) |
| |
| 204 | } |
214 | } |
| 205 | try await Task.sleep(for: .milliseconds(500)) |
215 | try await Task.sleep(for: .milliseconds(600)) |
| 206 | |
216 | |
| 207 | let reads = stub.seen.filter { $0.url.absoluteString.contains("argv=issue") } |
217 | let reads = stub.seen.filter { $0.url.absoluteString.contains("argv=issue") } |
| 208 | #expect(reads.count == 2) |
218 | #expect(reads.count == 2) |
| 209 | #expect(argvFrom(try #require(reads.last).url).contains("crash")) |
219 | #expect(argvFrom(try #require(reads.last).url).contains("crash")) |
| 210 | } |
220 | } |
| 211 | |
221 | |
| |
222 | /// Typing into `author` must debounce too — it is free text, exactly |
| |
223 | /// like `search`, and firing a request per keystroke would hit the |
| |
224 | /// rate limit on an 8-character username. |
| |
225 | @Test func authorDebouncesIntoOneRead() async throws { |
| |
226 | let (client, stub) = try makeClient() |
| |
227 | stub.enqueue(.init(status: 200, json: emptyPage, match: "argv=issue")) |
| |
228 | stub.enqueue(.init(status: 200, json: noLabels, match: "argv=label")) |
| |
229 | let model = IssueListViewModel(client: client, repoPath: "krz/gitbay") |
| |
230 | await model.load() |
| |
231 | |
| |
232 | stub.enqueue(.init(status: 200, json: emptyPage, match: "argv=issue")) |
| |
233 | for text in ["c", "cm", "cmc"] { |
| |
234 | model.filter.author = text |
| |
235 | } |
| |
236 | try await Task.sleep(for: .milliseconds(600)) |
| |
237 | |
| |
238 | let reads = stub.seen.filter { $0.url.absoluteString.contains("argv=issue") } |
| |
239 | #expect(reads.count == 2) |
| |
240 | let argv = argvFrom(try #require(reads.last).url) |
| |
241 | #expect(argv.contains("--author")) |
| |
242 | #expect(argv.contains("cmc")) |
| |
243 | } |
| |
244 | |
| |
245 | /// Same for `assignee`, issue-list-only. |
| |
246 | @Test func assigneeDebouncesIntoOneRead() async throws { |
| |
247 | let (client, stub) = try makeClient() |
| |
248 | stub.enqueue(.init(status: 200, json: emptyPage, match: "argv=issue")) |
| |
249 | stub.enqueue(.init(status: 200, json: noLabels, match: "argv=label")) |
| |
250 | let model = IssueListViewModel(client: client, repoPath: "krz/gitbay") |
| |
251 | await model.load() |
| |
252 | |
| |
253 | stub.enqueue(.init(status: 200, json: emptyPage, match: "argv=issue")) |
| |
254 | for text in ["c", "cm", "cmc"] { |
| |
255 | model.filter.assignee = text |
| |
256 | } |
| |
257 | try await Task.sleep(for: .milliseconds(600)) |
| |
258 | |
| |
259 | let reads = stub.seen.filter { $0.url.absoluteString.contains("argv=issue") } |
| |
260 | #expect(reads.count == 2) |
| |
261 | let argv = argvFrom(try #require(reads.last).url) |
| |
262 | #expect(argv.contains("--assignee")) |
| |
263 | #expect(argv.contains("cmc")) |
| |
264 | } |
| |
265 | |
| |
266 | /// A filter change reloads rather than pages, so it must not carry |
| |
267 | /// forward a cursor from whatever page was on screen. |
| |
268 | @Test func aFilterChangeDoesNotCarryAStaleCursor() async throws { |
| |
269 | let (client, stub) = try makeClient() |
| |
270 | stub.enqueue(.init(status: 200, json: pageWithCursor, match: "argv=issue")) |
| |
271 | stub.enqueue(.init(status: 200, json: noLabels, match: "argv=label")) |
| |
272 | let model = IssueListViewModel(client: client, repoPath: "krz/gitbay") |
| |
273 | await model.load() |
| |
274 | #expect(model.list.hasMore) |
| |
275 | |
| |
276 | stub.enqueue(.init(status: 200, json: emptyPage, match: "argv=issue")) |
| |
277 | model.filter.label = "bug" |
| |
278 | try await Task.sleep(for: .milliseconds(150)) |
| |
279 | |
| |
280 | let reads = stub.seen.filter { $0.url.absoluteString.contains("argv=issue") } |
| |
281 | let last = try #require(reads.last) |
| |
282 | #expect(!argvFrom(last.url).contains("--cursor")) |
| |
283 | } |
| |
284 | |
| |
285 | /// Two rapid picker changes cancel the first reload's task. The |
| |
286 | /// cancelled fetch must not paint a bogus "check your connection" |
| |
287 | /// failure over the correct state the second reload lands. |
| |
288 | @Test func rapidFilterChangesDoNotLeaveTheScreenFailed() async throws { |
| |
289 | let (client, stub) = try makeClient() |
| |
290 | stub.enqueue(.init(status: 200, json: emptyPage, match: "argv=issue")) |
| |
291 | stub.enqueue(.init(status: 200, json: noLabels, match: "argv=label")) |
| |
292 | let model = IssueListViewModel(client: client, repoPath: "krz/gitbay") |
| |
293 | await model.load() |
| |
294 | |
| |
295 | stub.enqueue(.init(status: 200, json: emptyPage, match: "argv=issue")) |
| |
296 | stub.enqueue(.init(status: 200, json: emptyPage, match: "argv=issue")) |
| |
297 | model.filter.label = "bug" |
| |
298 | model.filter.label = "feature" |
| |
299 | try await Task.sleep(for: .milliseconds(200)) |
| |
300 | |
| |
301 | if case .failed(let message) = model.state { |
| |
302 | Testing.Issue.record("rapid filter changes left the screen failed: \(message)") |
| |
303 | } |
| |
304 | } |
| |
305 | |
| 212 | @Test func theEmptyMessageComesFromTheFilter() async throws { |
306 | @Test func theEmptyMessageComesFromTheFilter() async throws { |
| 213 | let (client, stub) = try makeClient() |
307 | let (client, stub) = try makeClient() |
| 214 | stub.enqueue(.init(status: 200, json: emptyPage, match: "argv=issue")) |
308 | stub.enqueue(.init(status: 200, json: emptyPage, match: "argv=issue")) |
| @@ -243,6 +337,27 @@ struct MRListFilterWiringTests { |
| 243 | #expect(argv.contains("retarget")) |
337 | #expect(argv.contains("retarget")) |
| 244 | } |
338 | } |
| 245 | |
339 | |
| |
340 | /// Typing into `author` must debounce into one read, same as `search` |
| |
341 | /// — and, unlike the other tests in this struct, actually count the |
| |
342 | /// requests rather than just inspecting the last one. |
| |
343 | @Test func authorDebouncesIntoOneRead() async throws { |
| |
344 | let (client, stub) = try makeClient() |
| |
345 | stub.enqueue(.init(status: 200, json: emptyPage)) |
| |
346 | let model = MRListViewModel(client: client, repoPath: "krz/gitbay") |
| |
347 | await model.load() |
| |
348 | |
| |
349 | stub.enqueue(.init(status: 200, json: emptyPage)) |
| |
350 | for text in ["c", "cm", "cmc"] { |
| |
351 | model.filter.author = text |
| |
352 | } |
| |
353 | try await Task.sleep(for: .milliseconds(600)) |
| |
354 | |
| |
355 | #expect(stub.seen.count == 2) |
| |
356 | let argv = argvFrom(try #require(stub.seen.last).url) |
| |
357 | #expect(argv.contains("--author")) |
| |
358 | #expect(argv.contains("cmc")) |
| |
359 | } |
| |
360 | |
| 246 | @Test func sourceGoneReachesTheCommandInItsWireSpelling() async throws { |
361 | @Test func sourceGoneReachesTheCommandInItsWireSpelling() async throws { |
| 247 | let (client, stub) = try makeClient() |
362 | let (client, stub) = try makeClient() |
| 248 | stub.enqueue(.init(status: 200, json: emptyPage)) |
363 | stub.enqueue(.init(status: 200, json: emptyPage)) |