| new file mode 100644 |
| index 0000000..8a8f11c |
| --- /dev/null |
| +++ b/native/apple/Tests/SheetsKitTests/VersionGapTests.swift |
| @@ -0,0 +1,78 @@ |
| +import Foundation |
| +import RastrilloApple |
| +import Testing |
| + |
| +@testable import SheetsKit |
| + |
| +// B12: the server stamps each typed cell of a batch with its own version, |
| +// so an N-cell batch moves the version by N. accept() used to treat any |
| +// jump over one as a missed event and refetch the whole grid, so every |
| +// multi-cell paste, fill or clear (native or a peer's, over the stream) |
| +// cost a full chunked refresh. A contiguous batch now applies in place; |
| +// only a jump wider than the batch's typed cells is a gap. |
| +// |
| +// The client points at a closed port, so an attempted refresh shows as a |
| +// connection error and leaves the incremental cells unapplied. |
| +@MainActor private func gapModel(version: UInt64) -> SheetModel { |
| + let model = SheetModel(client: NativeHTTP(origin: URL(string: "http://127.0.0.1:1")!)) |
| + model.page = SheetPage( |
| + Sheet: SheetSummary(ID: 1, Title: "Fixture", TabID: 1, TabName: "Sheet 1"), Rows: 10, Cols: 5, |
| + Truncated: false, MayWrite: true, MayComment: true, MayShare: false, FillSwatches: [], |
| + InkSwatches: [], NumberFormats: [], Warned: nil) |
| + model.version = version |
| + model.error = "" |
| + return model |
| +} |
| + |
| +@MainActor @Test func contiguousBatchAppliesWithoutRefetch() async { |
| + let model = gapModel(version: 5) |
| + // Three typed cells (versions 6, 7, 8) plus one formula they changed. |
| + await model.received( |
| + SheetEvent( |
| + name: "cells", |
| + data: #"{"version":8,"cells":[{"r":0,"c":0,"raw":"1","display":"1"},{"r":1,"c":0,"raw":"2","display":"2"},{"r":2,"c":0,"raw":"3","display":"3"},{"r":3,"c":0,"raw":"=SUM(A1:A3)","display":"6","derived":true}]}"# |
| + ), source: model.grid) |
| + #expect(model.error == "", "a contiguous batch attempted a refetch: \(model.error)") |
| + #expect(model.version == 8) |
| + #expect(model.cells[Coordinate(r: 2, c: 0)]?.raw == "3") |
| + #expect(model.cells[Coordinate(r: 3, c: 0)]?.display == "6") |
| +} |
| + |
| +@MainActor @Test func versionGapBeyondTheBatchStillRefetches() async { |
| + let model = gapModel(version: 5) |
| + // Two typed cells end at version 9, so versions 6 and 7 were missed. |
| + await model.received( |
| + SheetEvent( |
| + name: "cells", |
| + data: #"{"version":9,"cells":[{"r":0,"c":0,"raw":"1","display":"1"},{"r":1,"c":0,"raw":"2","display":"2"},{"r":2,"c":0,"raw":"=A1+A2","display":"3","derived":true}]}"# |
| + ), source: model.grid) |
| + #expect(model.error != "", "a real gap was applied incrementally instead of refetched") |
| + #expect(model.version == 5) |
| + #expect(model.cells[Coordinate(r: 0, c: 0)] == nil) |
| +} |
| + |
| +@MainActor @Test func derivedCellsDoNotWidenTheAcceptedJump() async { |
| + let model = gapModel(version: 5) |
| + // One typed cell and three derived: the batch accounts for one version, |
| + // not four, so version 8 means two edits were missed. |
| + await model.accept( |
| + GridResponse( |
| + version: 8, |
| + cells: [ |
| + Cell(r: 0, c: 0, raw: "1", display: "1"), |
| + Cell(r: 1, c: 0, raw: "=A1", display: "1", derived: true), |
| + Cell(r: 2, c: 0, raw: "=A1", display: "1", derived: true), |
| + Cell(r: 3, c: 0, raw: "=A1", display: "1", derived: true), |
| + ])) |
| + #expect(model.error != "") |
| + #expect(model.version == 5) |
| +} |
| + |
| +@MainActor @Test func singleStepAndFormatOnlyResponsesStillApply() async { |
| + let model = gapModel(version: 5) |
| + await model.accept(GridResponse(version: 6, cells: [Cell(r: 0, c: 0, raw: "x", display: "x")])) |
| + await model.accept(GridResponse(version: 6, cells: [])) |
| + #expect(model.error == "") |
| + #expect(model.version == 6) |
| + #expect(model.cells[Coordinate(r: 0, c: 0)]?.raw == "x") |
| +} |