| 1 | import Foundation |
| 2 | import Testing |
| 3 | @testable import EquipoKit |
| 4 | |
| 5 | @MainActor @Suite struct ChangeFilesRefreshTests { |
| 6 | static func detail() throws -> ThinTask { |
| 7 | var task = try TaskDetailTests.task() |
| 8 | task.candidateSha = ChangeFilesTests.sha |
| 9 | var step = task.steps![0] |
| 10 | step.specialist = "engineering"; step.state = "done" |
| 11 | step.baseSha = ChangeFilesTests.base; step.candidateSha = task.candidateSha |
| 12 | step.changeFiles = ChangeFilesTests.facts(); task.steps = [step] |
| 13 | return task |
| 14 | } |
| 15 | @Test func sameCandidateKeepsFactsDuringReadAndReplacesThemOnCompletion() async throws { |
| 16 | let server = ChangeFilesReadServer(), model = try TaskDetailModelTests.model(server) |
| 17 | let detail = try Self.detail(); model.loaded = detail |
| 18 | var listed = detail; listed.revision += 1; listed.steps?[0].changeFiles = nil |
| 19 | await server.list(listed); await model.session.refresh() |
| 20 | #expect(model.task?.revision == listed.revision) |
| 21 | #expect(model.changeFiles == ChangeFilesTests.facts()) |
| 22 | #expect(model.rules.available.isEmpty) |
| 23 | let read = Task { await model.load() } |
| 24 | await server.waitForRead() |
| 25 | #expect(model.changeFiles == ChangeFilesTests.facts()) |
| 26 | var fresh = listed |
| 27 | var facts = ChangeFilesTests.facts(); facts.entries[0].path = "updated.txt" |
| 28 | fresh.steps?[0].changeFiles = facts |
| 29 | try await server.finish(fresh) |
| 30 | #expect(await read.value == nil) |
| 31 | #expect(model.changeFiles == facts) |
| 32 | // A completed detail read without facts restores the old-runner fallback. |
| 33 | model.loaded = listed |
| 34 | #expect(model.changeFiles == nil) |
| 35 | } |
| 36 | @Test func changedCandidateDropsFactsAndFailedReadDoesNotEraseSameCandidate() async throws { |
| 37 | let server = ChangeFilesReadServer(), model = try TaskDetailModelTests.model(server) |
| 38 | let detail = try Self.detail(); model.loaded = detail |
| 39 | var listed = detail; listed.revision += 1; listed.steps?[0].changeFiles = nil |
| 40 | await server.list(listed); await model.session.refresh() |
| 41 | let read = Task { await model.load() } |
| 42 | await server.waitForRead(); await server.fail(); #expect(await read.value != nil) |
| 43 | #expect(model.readFailure != nil) |
| 44 | #expect(model.changeFiles == ChangeFilesTests.facts()) |
| 45 | listed.candidateSha = ChangeFilesTests.base |
| 46 | await server.list(listed); await model.session.refresh() |
| 47 | #expect(model.changeFiles == nil) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | private actor ChangeFilesReadServer: EquipoTransport { |
| 52 | nonisolated let origin = URL(string: "https://facts.invalid")! |
| 53 | private var listed: ThinTask? |
| 54 | func list(_ task: ThinTask) { listed = task } |
| 55 | private var pending: CheckedContinuation<APIResponse, any Error>? |
| 56 | private var waiting: CheckedContinuation<Void, Never>? |
| 57 | func waitForRead() async { |
| 58 | if pending != nil { return } |
| 59 | await withCheckedContinuation { waiting = $0 } |
| 60 | } |
| 61 | func send(_ request: APIRequest) async throws -> APIResponse { |
| 62 | if request.path == "/snapshot" { return APIResponse(status: 200, data: Fixture.data("thin/snapshot")) } |
| 63 | if request.path == "/time-zone" { return APIResponse(status: 200, data: Data(#"{"time_zone":"Europe/Dublin"}"#.utf8)) } |
| 64 | if ["/tasks", "/needs-you"].contains(request.path) { |
| 65 | let encoder = EquipoJSON.encoder; encoder.dateEncodingStrategy = .iso8601 |
| 66 | return APIResponse(status: 200, data: try encoder.encode(ThinList(items: listed.map { [$0] } ?? []))) |
| 67 | } |
| 68 | return try await withCheckedThrowingContinuation { continuation in |
| 69 | pending = continuation; waiting?.resume(); waiting = nil |
| 70 | } |
| 71 | } |
| 72 | func finish(_ task: ThinTask) throws { |
| 73 | let encoder = EquipoJSON.encoder; encoder.dateEncodingStrategy = .iso8601 |
| 74 | pending?.resume(returning: APIResponse(status: 200, data: try encoder.encode(task))); pending = nil |
| 75 | } |
| 76 | func fail() { pending?.resume(throwing: URLError(.notConnectedToInternet)); pending = nil } |
| 77 | func lines(_ request: APIRequest) async throws -> AsyncThrowingStream<String, Error> { .init { $0.finish() } } |
| 78 | } |
| 79 | |