| 1 | import Foundation |
| 2 | import Testing |
| 3 | import RastrilloApple |
| 4 | @testable import SheetsKit |
| 5 | |
| 6 | @Test func backgroundBatchesRespectSizeAndRetainEveryCell() async throws { |
| 7 | let worker = SheetWireWorker() |
| 8 | let writes = (0..<620).map { CellWrite(r: $0, c: 0, raw: String(repeating: "é", count: 4096)) } |
| 9 | var offset = 0 |
| 10 | while offset < writes.count { |
| 11 | let batch = try await worker.encode(writes, startingAt: offset) |
| 12 | #expect(batch.count > 0 && batch.count <= 500) |
| 13 | #expect(batch.data.count <= 1 << 20) |
| 14 | let object = try #require(JSONSerialization.jsonObject(with: batch.data) as? [String: Any]) |
| 15 | let cells = try #require(object["cells"] as? [[String: Any]]) |
| 16 | #expect(cells.count == batch.count) |
| 17 | for (index, cell) in cells.enumerated() { |
| 18 | #expect(cell["r"] as? Int == offset + index) |
| 19 | #expect(cell["raw"] as? String == writes[offset + index].raw) |
| 20 | } |
| 21 | offset += batch.count |
| 22 | } |
| 23 | let response = try await worker.decode(Data(#"{"version":42,"cells":[{"r":3,"c":2,"raw":"=1+1","display":"2"}]}"#.utf8)) |
| 24 | #expect(response.version == 42) |
| 25 | #expect(response.cells?.first?.raw == "=1+1") |
| 26 | await #expect(throws: (any Error).self) { try await worker.decode(Data("invalid".utf8)) } |
| 27 | } |
| 28 | |
| 29 | @MainActor @Test func oldStreamCannotClearOrOverwriteCurrentSheet() async { |
| 30 | let model = SheetModel(client: NativeHTTP(origin: URL(string: "http://localhost")!)) |
| 31 | let point = Coordinate(r: 0, c: 0) |
| 32 | model.cells[point] = Cell(r: 0, c: 0, raw: "current", display: "current") |
| 33 | model.live = "Live" |
| 34 | await model.received(SheetEvent(name: "bye", data: "access-changed"), source: "/api/sheets/old/tabs/old") |
| 35 | await model.received(SheetEvent(name: "cells", data: #"{"version":99,"cells":[{"r":0,"c":0,"raw":"old","display":"old"}]}"#), source: "/api/sheets/old/tabs/old") |
| 36 | #expect(model.cells[point]?.raw == "current") |
| 37 | #expect(model.version == 0) |
| 38 | #expect(model.live == "Live") |
| 39 | } |
| 40 | |