| 1 | import Foundation |
| 2 | import RastrilloApple |
| 3 | import RastrilloNative |
| 4 | import RastrilloTestSupport |
| 5 | import XCTest |
| 6 | @testable import DocsKit |
| 7 | |
| 8 | /// Spec §6 and plan S2a step 4: what `refreshAccess` does with each answer. |
| 9 | /// 401 pauses input and leaves the access alone; 403 and 404 are "No longer |
| 10 | /// available", left again on a later 200; a 200 without can_write is View |
| 11 | /// only, in place. |
| 12 | @MainActor final class DocsAccessTests: XCTestCase { |
| 13 | private func open() async throws -> (DocsModel, AccessServer) { |
| 14 | let server = AccessServer() |
| 15 | try await server.inner.seed([.object(["type": .string("paragraph"), "children": .array([ |
| 16 | .object(["type": .string("text"), "runs": .array([.object(["insert": .string("Hello"), "marks": .object([:])])])])])])]) |
| 17 | let connectivity = NativeConnectivity(source: ScriptedPathSource(.satisfied)) |
| 18 | let account = DocsOfflineAccount(store: EphemeralDocsStore(), accountID: UUID().uuidString, connectivity: connectivity, transport: { server }) |
| 19 | let model = DocsModel(transport: { server }, connectivity: connectivity, offlineAccount: account) |
| 20 | await model.open("doc") |
| 21 | XCTAssertNotNil(model.projection, "the document opened") |
| 22 | XCTAssertEqual(model.documentAccess, .writable) |
| 23 | return (model, server) |
| 24 | } |
| 25 | |
| 26 | /// Asks again, as the window does after a refused write or stream. |
| 27 | private func revalidate(_ model: DocsModel) async { |
| 28 | model.inputPaused = true |
| 29 | await model.retry() |
| 30 | } |
| 31 | |
| 32 | private func row(_ model: DocsModel) -> DocsSubtitleRow { |
| 33 | DocsSubtitleRow(access: model.documentAccess, status: .saved, savingShown: false, loading: model.loading) |
| 34 | } |
| 35 | |
| 36 | func testA403IsNoLongerAvailableAndALater200LeavesIt() async throws { |
| 37 | let (model, server) = try await open() |
| 38 | await server.answer(403) |
| 39 | await revalidate(model) |
| 40 | XCTAssertEqual(model.documentAccess, .unavailable) |
| 41 | XCTAssertEqual(row(model), .unavailable) |
| 42 | XCTAssertTrue(model.inputPaused, "the editor becomes read-only") |
| 43 | XCTAssertFalse(model.enqueue(["op": .string("display_edit"), "at": .number(0), "length": .number(0), "text": .string("x")])) |
| 44 | |
| 45 | await server.answer(200, canWrite: true) |
| 46 | await revalidate(model) |
| 47 | XCTAssertEqual(model.documentAccess, .writable, "re-shared or restored: row 1 is left") |
| 48 | XCTAssertEqual(row(model), .saved) |
| 49 | XCTAssertFalse(model.inputPaused) |
| 50 | await model.stop() |
| 51 | } |
| 52 | |
| 53 | func testA404IsNoLongerAvailable() async throws { |
| 54 | let (model, server) = try await open() |
| 55 | await server.answer(404) |
| 56 | await revalidate(model) |
| 57 | XCTAssertEqual(model.documentAccess, .unavailable) |
| 58 | XCTAssertEqual(model.detail?.can_write, false) |
| 59 | await model.stop() |
| 60 | } |
| 61 | |
| 62 | func testA401PausesInputAndLeavesTheAccessAlone() async throws { |
| 63 | let (model, server) = try await open() |
| 64 | await server.answer(401) |
| 65 | await revalidate(model) |
| 66 | XCTAssertTrue(model.sessionPaused, "paused for the session's own reason") |
| 67 | XCTAssertFalse(model.enqueue(["op": .string("display_edit"), "at": .number(0), "length": .number(0), "text": .string("x")])) |
| 68 | XCTAssertEqual(model.detail?.can_write, true, "a 401 never touches can_write") |
| 69 | XCTAssertEqual(model.documentAccess, .writable, "and so no reader chrome") |
| 70 | XCTAssertTrue(model.inputBlocked, "input pauses while the session is re-minted") |
| 71 | |
| 72 | // The re-minted session's 200 resumes input, can_write unchanged. |
| 73 | await server.answer(200, canWrite: true) |
| 74 | await revalidate(model) |
| 75 | XCTAssertFalse(model.inputPaused) |
| 76 | XCTAssertEqual(model.documentAccess, .writable) |
| 77 | await model.stop() |
| 78 | } |
| 79 | |
| 80 | /// The re-minted session's 200 clears only the 401's own pause: input |
| 81 | /// paused for another reason (here a recovery) stays paused. |
| 82 | func testThe401ResumeLeavesAnotherPauseAlone() async throws { |
| 83 | let (model, server) = try await open() |
| 84 | model.inputPaused = true; model.errorKey = "native.recovered" |
| 85 | await server.answer(401) |
| 86 | await model.revalidateAccess() |
| 87 | XCTAssertTrue(model.sessionPaused) |
| 88 | await server.answer(200, canWrite: true) |
| 89 | await model.revalidateAccess() |
| 90 | XCTAssertFalse(model.sessionPaused, "the 401's pause is lifted") |
| 91 | XCTAssertTrue(model.inputPaused, "the recovery's pause is not the 401's to lift") |
| 92 | XCTAssertTrue(model.inputBlocked) |
| 93 | await model.stop() |
| 94 | } |
| 95 | |
| 96 | func testA200WithoutWriteIsViewOnlyInPlace() async throws { |
| 97 | let (model, server) = try await open() |
| 98 | model.selection = NSRange(location: 2, length: 1) |
| 99 | let identity = model.editorIdentity |
| 100 | await server.answer(200, canWrite: false) |
| 101 | await revalidate(model) |
| 102 | XCTAssertEqual(model.documentAccess, .readOnly) |
| 103 | XCTAssertEqual(row(model), .viewOnly) |
| 104 | XCTAssertTrue(model.inputPaused) |
| 105 | XCTAssertEqual(model.editorIdentity, identity, "applied in place: the same text view") |
| 106 | XCTAssertEqual(model.selection, NSRange(location: 2, length: 1), "the selection is kept") |
| 107 | await model.stop() |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /// LoopServer, with the document's detail answer under the test's control. |
| 112 | actor AccessServer: NativeTransport { |
| 113 | nonisolated let origin = URL(string: "https://docs.access.invalid")! |
| 114 | let inner = LoopServer() |
| 115 | private var status = 200 |
| 116 | private var canWrite = true |
| 117 | |
| 118 | func answer(_ status: Int, canWrite: Bool = true) { self.status = status; self.canWrite = canWrite } |
| 119 | |
| 120 | func responseDetails(_ path: String, method: String, body: Data?, contentType: String, maxBytes: Int) async throws -> NativeResponse { |
| 121 | if method == "GET", path == "/d/doc" { |
| 122 | guard status == 200 else { return NativeResponse(data: Data(), status: status, headers: [:]) } |
| 123 | let detail = DocumentDetail(sqid: "doc", title: "Access", guid: "render-loop", can_write: canWrite, can_share: canWrite) |
| 124 | return NativeResponse(data: try JSONEncoder().encode(detail), status: 200, headers: [:]) |
| 125 | } |
| 126 | return try await inner.responseDetails(path, method: method, body: body, contentType: contentType, maxBytes: maxBytes) |
| 127 | } |
| 128 | func stream(_ path: String, onOpen: @Sendable () async throws -> Void, onLine: @Sendable (String) async throws -> Void) async throws { |
| 129 | try await inner.stream(path, onOpen: onOpen, onLine: onLine) |
| 130 | } |
| 131 | func download(_ path: String, to destination: URL) async throws { throw NativeClientError.notFound } |
| 132 | } |
| 133 | |