Native: a lost clear that landed is reconciled too; one doc comment on revertWrites
Review #72 delta, finding 4: a cleared cell is absent from the server's
cells, so comparing a failed clear ("") with cells[point]?.raw (nil) never
matched and the cell stayed marked until Revert. Compare with ?? "". The
fixture now deletes a cleared cell, as the server does. Also the nit: the
stale first doc comment on revertWrites is gone.
Test: aLostClearThatLandedIsSavedAfterAll (fails without this).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+24
−5
native/apple/Sources/SheetsKit/SheetWrites.swift+6 −4native/apple/Tests/SheetsKitTests/OptimisticEditTests.swift+16 −0native/apple/Tests/SheetsKitTests/SheetServerFixture.swift+2 −1
diff --git a/native/apple/Sources/SheetsKit/SheetWrites.swift b/native/apple/Sources/SheetsKit/SheetWrites.swift| index f4f6177..c3b1fd0 100644 |
| --- a/native/apple/Sources/SheetsKit/SheetWrites.swift |
| +++ b/native/apple/Sources/SheetsKit/SheetWrites.swift |
| @@ -109,11 +109,10 @@ struct PendingEdit: Sendable, Equatable { |
| error = "" |
| startSending() |
| } |
| - /// Drops failed edits and shows the server's value again. Edits still being |
| - /// sent are not touched: they may already have landed. |
| /// Drops failed edits and shows the server's value again. An edit whose |
| /// answer was lost may have landed after all, so the cells are read back |
| - /// from the server rather than trusted from before the edit. |
| + /// from the server rather than trusted from before the edit. Edits still |
| + /// being sent are not touched: they may land yet. |
| func revertWrites(_ points: [Coordinate]? = nil) { |
| pending = pending.filter { point, edit in |
| edit.state != .failed || !(points?.contains(point) ?? true) |
| @@ -126,7 +125,10 @@ struct PendingEdit: Sendable, Equatable { |
| /// A failed edit the server turns out to hold (its answer was lost, then |
| /// the live stream or a refetch brought the cell back) is saved after all. |
| func reconcileFailedEdits() { |
| - let landed = pending.filter { point, edit in edit.state == .failed && cells[point]?.raw == edit.write.raw } |
| + // A cleared cell is absent from the server's cells, not an empty one. |
| + let landed = pending.filter { point, edit in |
| + edit.state == .failed && (cells[point]?.raw ?? "") == edit.write.raw |
| + } |
| guard !landed.isEmpty else { return } |
| pending = pending.filter { landed[$0.key] == nil } |
| } |
diff --git a/native/apple/Tests/SheetsKitTests/OptimisticEditTests.swift b/native/apple/Tests/SheetsKitTests/OptimisticEditTests.swift| index 1c9cc16..12b5049 100644 |
| --- a/native/apple/Tests/SheetsKitTests/OptimisticEditTests.swift |
| +++ b/native/apple/Tests/SheetsKitTests/OptimisticEditTests.swift |
| @@ -341,3 +341,19 @@ private let b1 = Coordinate(r: 0, c: 1) |
| await model.settle() |
| #expect(model.saveStatus == "Saved") |
| } |
| + |
| +// Review #72 delta, finding 4: a clear whose answer was lost, and landed. |
| +@MainActor @Test func aLostClearThatLandedIsSavedAfterAll() async { |
| + let server = SheetServerFixture() |
| + let model = fixtureModel(server) |
| + type(model, a1, "x") |
| + await model.settle() |
| + server.loseNextResponse() |
| + model.select(a1) |
| + await model.clear() |
| + await model.settle() |
| + #expect(model.editState(a1) == .failed) |
| + #expect(server.value(0, 0) == nil, "the server cleared it; only the answer was lost") |
| + await model.refresh() |
| + #expect(model.pending.isEmpty, "the server no longer has the cell: the clear landed") |
| +} |
diff --git a/native/apple/Tests/SheetsKitTests/SheetServerFixture.swift b/native/apple/Tests/SheetsKitTests/SheetServerFixture.swift| index 110f2e3..8476bef 100644 |
| --- a/native/apple/Tests/SheetsKitTests/SheetServerFixture.swift |
| +++ b/native/apple/Tests/SheetsKitTests/SheetServerFixture.swift |
| @@ -118,7 +118,8 @@ final class SheetServerFixture: NativeTransport, @unchecked Sendable { |
| return NativeResponse(data: Data(), status: 503) |
| } |
| stamp += UInt64(batch.count) |
| - for cell in batch { cells[Coordinate(r: cell.r, c: cell.c)] = cell.raw } |
| + // A cleared cell is deleted, as the real server deletes it. |
| + for cell in batch { cells[Coordinate(r: cell.r, c: cell.c)] = cell.raw.isEmpty ? nil : cell.raw } |
| let changed = batch.map { |
| #"{"r":\#($0.r),"c":\#($0.c),"raw":"\#($0.raw)","display":"\#(Self.display($0.raw))"}"# |
| }.joined(separator: ",") |