import Foundation import Testing import RastrilloApple @testable import SheetsKit @Test func backgroundBatchesRespectSizeAndRetainEveryCell() async throws { let worker = SheetWireWorker() let writes = (0..<620).map { CellWrite(r: $0, c: 0, raw: String(repeating: "é", count: 4096)) } var offset = 0 while offset < writes.count { let batch = try await worker.encode(writes, startingAt: offset) #expect(batch.count > 0 && batch.count <= 500) #expect(batch.data.count <= 1 << 20) let object = try #require(JSONSerialization.jsonObject(with: batch.data) as? [String: Any]) let cells = try #require(object["cells"] as? [[String: Any]]) #expect(cells.count == batch.count) for (index, cell) in cells.enumerated() { #expect(cell["r"] as? Int == offset + index) #expect(cell["raw"] as? String == writes[offset + index].raw) } offset += batch.count } let response = try await worker.decode(Data(#"{"version":42,"cells":[{"r":3,"c":2,"raw":"=1+1","display":"2"}]}"#.utf8)) #expect(response.version == 42) #expect(response.cells?.first?.raw == "=1+1") await #expect(throws: (any Error).self) { try await worker.decode(Data("invalid".utf8)) } } @MainActor @Test func oldStreamCannotClearOrOverwriteCurrentSheet() async { let model = SheetModel(client: NativeHTTP(origin: URL(string: "http://localhost")!)) let point = Coordinate(r: 0, c: 0) model.cells[point] = Cell(r: 0, c: 0, raw: "current", display: "current") model.live = "Live" await model.received(SheetEvent(name: "bye", data: "access-changed"), source: "/api/sheets/old/tabs/old") await model.received(SheetEvent(name: "cells", data: #"{"version":99,"cells":[{"r":0,"c":0,"raw":"old","display":"old"}]}"#), source: "/api/sheets/old/tabs/old") #expect(model.cells[point]?.raw == "current") #expect(model.version == 0) #expect(model.live == "Live") }