|
| 1 | +import Testing |
| 2 | +import CoreData |
| 3 | +import Foundation |
| 4 | +@testable import FenixKanban |
| 5 | + |
| 6 | +// MARK: - File-scope fixtures (URLProtocol handler runs off-actor) |
| 7 | + |
| 8 | +private let parityColumnsJSON = """ |
| 9 | +[ |
| 10 | + {"id":"FC1","name":"Triage","color":{"name":"Slate","value":"x"},"created_at":"2026-05-25T00:00:00Z"}, |
| 11 | + {"id":"FC2","name":"Doing","color":{"name":"Lime","value":"y"},"created_at":"2026-05-25T00:00:00Z"} |
| 12 | +] |
| 13 | +""" |
| 14 | + |
| 15 | +private func parityCardJSON( |
| 16 | + id: String, number: Int, title: String, |
| 17 | + columnID: String = "FC1", columnName: String = "Triage", |
| 18 | + tags: [String] = [], assigneeIDs: [String] = [], |
| 19 | + lastActiveAt: String = "2026-05-25T00:00:00Z" |
| 20 | +) -> String { |
| 21 | + let tagsJSON = tags.map { "\"\($0)\"" }.joined(separator: ",") |
| 22 | + let assigneesJSON = assigneeIDs.map { |
| 23 | + """ |
| 24 | + {"id":"\($0)","name":"User \($0)","role":"member","active":true, |
| 25 | + "email_address":"\($0)@example.com","created_at":"2026-05-25T00:00:00Z", |
| 26 | + "url":null,"avatar_url":null} |
| 27 | + """ |
| 28 | + }.joined(separator: ",") |
| 29 | + return """ |
| 30 | + {"id":"\(id)","number":\(number),"title":"\(title)","status":"published", |
| 31 | + "description":null,"description_html":null,"image_url":null, |
| 32 | + "has_attachments":false,"tags":[\(tagsJSON)],"closed":false,"postponed":false, |
| 33 | + "golden":false,"last_active_at":"\(lastActiveAt)", |
| 34 | + "created_at":"2026-05-25T00:00:00Z", |
| 35 | + "url":"https://fizzy.bluefenix.net/ACCT/cards/\(number)", |
| 36 | + "column":{"id":"\(columnID)","name":"\(columnName)","color":"var(--color-card-4)","created_at":"2026-05-25T00:00:00Z"}, |
| 37 | + "assignees":[\(assigneesJSON)]} |
| 38 | + """ |
| 39 | +} |
| 40 | + |
| 41 | +/// Records mutation requests (method, path, body) for exact-diff assertions. |
| 42 | +private final class ParityBoard: @unchecked Sendable { |
| 43 | + let cardsJSON: String |
| 44 | + private let lock = NSLock() |
| 45 | + private var _mutations: [(method: String, path: String, body: String)] = [] |
| 46 | + var mutations: [(method: String, path: String, body: String)] { |
| 47 | + lock.lock(); defer { lock.unlock() }; return _mutations |
| 48 | + } |
| 49 | + |
| 50 | + init(cardsJSON: String) { self.cardsJSON = cardsJSON } |
| 51 | + |
| 52 | + func handler(_ req: URLRequest) throws -> (Data, HTTPURLResponse) { |
| 53 | + let path = req.url?.path ?? "" |
| 54 | + let method = req.httpMethod ?? "?" |
| 55 | + switch method { |
| 56 | + case "GET" where path.hasSuffix("/my/pins"): |
| 57 | + return (Data("[]".utf8), .ok(for: req)) |
| 58 | + case "GET" where path.hasSuffix("/columns"): |
| 59 | + return (Data(parityColumnsJSON.utf8), .ok(for: req)) |
| 60 | + case "GET" where path.contains("/columns/") && path.hasSuffix("/cards"): |
| 61 | + let columnID = path.components(separatedBy: "/columns/").last? |
| 62 | + .components(separatedBy: "/cards").first ?? "" |
| 63 | + return (Data((columnID == "FC1" ? cardsJSON : "[]").utf8), .ok(for: req)) |
| 64 | + case "PUT" where path.contains("/cards/"): |
| 65 | + record(method, path, req) |
| 66 | + // Echo the updated card so the push can record lastActiveAt. |
| 67 | + return (Data(parityCardJSON(id: "fz9", number: 9, title: "Edited", lastActiveAt: "2026-06-12T05:00:00Z").utf8), .ok(for: req)) |
| 68 | + case "POST" where path.contains("/taggings") || path.contains("/triage") || path.contains("/assignments"): |
| 69 | + record(method, path, req) |
| 70 | + return (Data(), .response(for: req, status: 204)) |
| 71 | + default: |
| 72 | + Issue.record("unexpected request: \(method) \(path)") |
| 73 | + return (Data(), .response(for: req, status: 500)) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private func record(_ method: String, _ path: String, _ req: URLRequest) { |
| 78 | + let body = req.httpBody ?? req.httpBodyStream.map { stream -> Data in |
| 79 | + stream.open(); defer { stream.close() } |
| 80 | + var data = Data(); let buf = UnsafeMutablePointer<UInt8>.allocate(capacity: 4096) |
| 81 | + defer { buf.deallocate() } |
| 82 | + while stream.hasBytesAvailable { |
| 83 | + let n = stream.read(buf, maxLength: 4096) |
| 84 | + if n <= 0 { break } |
| 85 | + data.append(buf, count: n) |
| 86 | + } |
| 87 | + return data |
| 88 | + } ?? Data() |
| 89 | + lock.lock(); defer { lock.unlock() } |
| 90 | + _mutations.append((method, path, String(data: body, encoding: .utf8) ?? "")) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// MARK: - Suite |
| 95 | + |
| 96 | +/// Task #69 — push parity (C4b/C5/C6): local card moves, tag edits, and |
| 97 | +/// assignee edits reach the server on the LWW local-newer branch. |
| 98 | +/// |
| 99 | +/// Wire facts (live-probed 2026-06-12, docs/fizzy-api-notes.md): moves push |
| 100 | +/// via `POST /cards/:n/triage {column_id}`; `tag_ids` on PUT is rejected → |
| 101 | +/// tags push as EXACT toggle diffs via `POST /taggings {tag_title}` (toggles |
| 102 | +/// are not idempotent); assignments toggle symmetrically via |
| 103 | +/// `POST /assignments {assignee_id}`. |
| 104 | +@Suite("FizzySyncEngine — push parity (move/tags/assignees)", .serialized) |
| 105 | +@MainActor |
| 106 | +struct FizzySyncEnginePushParityTests { |
| 107 | + |
| 108 | + private struct Harness { |
| 109 | + let mock = MockHTTPState() |
| 110 | + let persistence: PersistenceController |
| 111 | + let boardRepo: BoardRepository |
| 112 | + let cardRepo: CardRepository |
| 113 | + let board: Board |
| 114 | + let triage: Column |
| 115 | + let doing: Column |
| 116 | + let engine: FizzySyncEngine |
| 117 | + let suiteName: String |
| 118 | + let authState: FizzyAuthState |
| 119 | + let mappingDefaults: UserDefaults |
| 120 | + let pairingStore: FizzyCardPairingStore |
| 121 | + |
| 122 | + @MainActor |
| 123 | + init() { |
| 124 | + persistence = PersistenceController(inMemory: true, useCloudKit: false) |
| 125 | + boardRepo = BoardRepository(context: persistence.viewContext) |
| 126 | + pairingStore = FizzyCardPairingStore( |
| 127 | + fileURL: FileManager.default.temporaryDirectory |
| 128 | + .appendingPathComponent("fk-pairings-\(UUID().uuidString).json") |
| 129 | + ) |
| 130 | + cardRepo = CardRepository(context: persistence.viewContext, pairingStore: pairingStore) |
| 131 | + board = boardRepo.createBoard(name: "Roadmap") |
| 132 | + triage = boardRepo.createColumn(in: board, name: "Triage") |
| 133 | + doing = boardRepo.createColumn(in: board, name: "Doing") |
| 134 | + triage.fizzyColumnID = "FC1" |
| 135 | + doing.fizzyColumnID = "FC2" |
| 136 | + try! persistence.viewContext.save() |
| 137 | + |
| 138 | + let prefix = "test.fizzy.parity.\(UUID().uuidString)" |
| 139 | + authState = FizzyAuthState(keyPrefix: prefix) |
| 140 | + authState.setAccessToken("t") |
| 141 | + authState.setAccountSlug("ACCT") |
| 142 | + |
| 143 | + suiteName = "test.fizzy.parity.mapping.\(UUID().uuidString)" |
| 144 | + mappingDefaults = UserDefaults(suiteName: suiteName)! |
| 145 | + let mapping = FizzyBoardMapping(defaults: mappingDefaults) |
| 146 | + mapping.setPairing(localBoardID: board.id!, fizzyBoardID: "FB1") |
| 147 | + |
| 148 | + let session = mock.makeSession() |
| 149 | + let client = FizzyClient( |
| 150 | + baseURL: URL(string: "https://fizzy.bluefenix.net")!, |
| 151 | + accessToken: "t", |
| 152 | + accountSlug: "ACCT", |
| 153 | + urlSession: session, |
| 154 | + clock: ImmediateClock() |
| 155 | + ) |
| 156 | + |
| 157 | + engine = FizzySyncEngine( |
| 158 | + client: client, |
| 159 | + authState: authState, |
| 160 | + mapping: mapping, |
| 161 | + context: persistence.viewContext, |
| 162 | + pairingStore: pairingStore |
| 163 | + ) |
| 164 | + } |
| 165 | + |
| 166 | + func tearDown() { |
| 167 | + authState.clear() |
| 168 | + mappingDefaults.removePersistentDomain(forName: suiteName) |
| 169 | + try? FileManager.default.removeItem(at: pairingStore.fileURL) |
| 170 | + } |
| 171 | + |
| 172 | + /// Local card paired to remote fz9/#9 at t0, locally edited at t0+120. |
| 173 | + func seedLocallyNewerCard(in column: Column, title: String = "Edited") -> Card { |
| 174 | + let t0 = Date(timeIntervalSince1970: 1_750_000_000) |
| 175 | + let card = cardRepo.createCard(in: column, title: title) |
| 176 | + try! persistence.viewContext.save() |
| 177 | + pairingStore.setPairing( |
| 178 | + FizzyCardPairing(fizzyID: "fz9", fizzyNumber: 9, fizzyUpdatedAt: t0), |
| 179 | + for: card.id! |
| 180 | + ) |
| 181 | + card.modifiedAt = t0.addingTimeInterval(120) |
| 182 | + return card |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + @Test("local column move pushes POST /cards/:n/triage with the new column id") |
| 187 | + func localMovePushesTriage() async throws { |
| 188 | + let h = Harness() |
| 189 | + defer { h.tearDown() } |
| 190 | + // Local card sits in Doing (FC2); remote still lists it under FC1. |
| 191 | + _ = h.seedLocallyNewerCard(in: h.doing) |
| 192 | + let board = ParityBoard(cardsJSON: "[\(parityCardJSON(id: "fz9", number: 9, title: "Edited"))]") |
| 193 | + h.mock.handler = { try board.handler($0) } |
| 194 | + |
| 195 | + let result = try await h.engine.sync() |
| 196 | + #expect(result.errors.isEmpty) |
| 197 | + |
| 198 | + let triages = board.mutations.filter { $0.path.hasSuffix("/cards/9/triage") } |
| 199 | + #expect(triages.count == 1) |
| 200 | + #expect(triages.first?.body.contains("FC2") == true, |
| 201 | + "triage body must carry the local column id, got: \(triages.first?.body ?? "")") |
| 202 | + } |
| 203 | + |
| 204 | + @Test("local tag edits push exact toggle diffs (add missing, remove extra)") |
| 205 | + func localTagEditsPushToggleDiffs() async throws { |
| 206 | + let h = Harness() |
| 207 | + defer { h.tearDown() } |
| 208 | + let card = h.seedLocallyNewerCard(in: h.triage) |
| 209 | + // Local labels: alpha, beta. Remote tags: alpha, gamma. |
| 210 | + let labelRepo = LabelRepository(context: h.persistence.viewContext) |
| 211 | + let alpha = labelRepo.createLabel(name: "alpha", colorHex: "#808080") |
| 212 | + let beta = labelRepo.createLabel(name: "beta", colorHex: "#808080") |
| 213 | + card.labels = NSSet(array: [alpha, beta]) |
| 214 | + let board = ParityBoard(cardsJSON: "[\(parityCardJSON(id: "fz9", number: 9, title: "Edited", tags: ["alpha", "gamma"]))]") |
| 215 | + h.mock.handler = { try board.handler($0) } |
| 216 | + |
| 217 | + let result = try await h.engine.sync() |
| 218 | + #expect(result.errors.isEmpty) |
| 219 | + |
| 220 | + let toggles = board.mutations.filter { $0.path.hasSuffix("/cards/9/taggings") } |
| 221 | + let bodies = Set(toggles.map(\.body)) |
| 222 | + #expect(toggles.count == 2, "exactly two toggles (beta on, gamma off), got: \(toggles)") |
| 223 | + #expect(bodies.contains { $0.contains("beta") }) |
| 224 | + #expect(bodies.contains { $0.contains("gamma") }) |
| 225 | + #expect(!bodies.contains { $0.contains("alpha") }, "alpha matches both sides — toggling it would REMOVE it") |
| 226 | + } |
| 227 | + |
| 228 | + @Test("local assignee edits push exact toggle diffs") |
| 229 | + func localAssigneeEditsPushToggleDiffs() async throws { |
| 230 | + let h = Harness() |
| 231 | + defer { h.tearDown() } |
| 232 | + let card = h.seedLocallyNewerCard(in: h.triage) |
| 233 | + card.assignees = [CardAssignee(id: "u1", name: "User u1")] |
| 234 | + // Remote has u2 assigned. |
| 235 | + let board = ParityBoard(cardsJSON: "[\(parityCardJSON(id: "fz9", number: 9, title: "Edited", assigneeIDs: ["u2"]))]") |
| 236 | + h.mock.handler = { try board.handler($0) } |
| 237 | + |
| 238 | + let result = try await h.engine.sync() |
| 239 | + #expect(result.errors.isEmpty) |
| 240 | + |
| 241 | + let toggles = board.mutations.filter { $0.path.hasSuffix("/cards/9/assignments") } |
| 242 | + let bodies = Set(toggles.map(\.body)) |
| 243 | + #expect(toggles.count == 2, "exactly two toggles (u1 on, u2 off), got: \(toggles)") |
| 244 | + #expect(bodies.contains { $0.contains("u1") }) |
| 245 | + #expect(bodies.contains { $0.contains("u2") }) |
| 246 | + } |
| 247 | + |
| 248 | + @Test("no diffs → PUT only, zero triage/tagging/assignment calls (pin)") |
| 249 | + func noDiffsMeansNoToggles() async throws { |
| 250 | + let h = Harness() |
| 251 | + defer { h.tearDown() } |
| 252 | + _ = h.seedLocallyNewerCard(in: h.triage) |
| 253 | + let board = ParityBoard(cardsJSON: "[\(parityCardJSON(id: "fz9", number: 9, title: "Edited"))]") |
| 254 | + h.mock.handler = { try board.handler($0) } |
| 255 | + |
| 256 | + let result = try await h.engine.sync() |
| 257 | + #expect(result.errors.isEmpty) |
| 258 | + |
| 259 | + let extras = board.mutations.filter { !$0.path.hasSuffix("/cards/9") } |
| 260 | + #expect(extras.isEmpty, "no toggles expected, got: \(extras)") |
| 261 | + #expect(board.mutations.contains { $0.method == "PUT" }) |
| 262 | + } |
| 263 | +} |
0 commit comments