import Foundation import RastrilloApple import RastrilloNative import RastrilloTestSupport import XCTest @testable import DocsKit /// Spec ยง6 and plan S2a step 4: what `refreshAccess` does with each answer. /// 401 pauses input and leaves the access alone; 403 and 404 are "No longer /// available", left again on a later 200; a 200 without can_write is View /// only, in place. @MainActor final class DocsAccessTests: XCTestCase { private func open() async throws -> (DocsModel, AccessServer) { let server = AccessServer() try await server.inner.seed([.object(["type": .string("paragraph"), "children": .array([ .object(["type": .string("text"), "runs": .array([.object(["insert": .string("Hello"), "marks": .object([:])])])])])])]) let connectivity = NativeConnectivity(source: ScriptedPathSource(.satisfied)) let account = DocsOfflineAccount(store: EphemeralDocsStore(), accountID: UUID().uuidString, connectivity: connectivity, transport: { server }) let model = DocsModel(transport: { server }, connectivity: connectivity, offlineAccount: account) await model.open("doc") XCTAssertNotNil(model.projection, "the document opened") XCTAssertEqual(model.documentAccess, .writable) return (model, server) } /// Asks again, as the window does after a refused write or stream. private func revalidate(_ model: DocsModel) async { model.inputPaused = true await model.retry() } private func row(_ model: DocsModel) -> DocsSubtitleRow { DocsSubtitleRow(access: model.documentAccess, status: .saved, savingShown: false, loading: model.loading) } func testA403IsNoLongerAvailableAndALater200LeavesIt() async throws { let (model, server) = try await open() await server.answer(403) await revalidate(model) XCTAssertEqual(model.documentAccess, .unavailable) XCTAssertEqual(row(model), .unavailable) XCTAssertTrue(model.inputPaused, "the editor becomes read-only") XCTAssertFalse(model.enqueue(["op": .string("display_edit"), "at": .number(0), "length": .number(0), "text": .string("x")])) await server.answer(200, canWrite: true) await revalidate(model) XCTAssertEqual(model.documentAccess, .writable, "re-shared or restored: row 1 is left") XCTAssertEqual(row(model), .saved) XCTAssertFalse(model.inputPaused) await model.stop() } func testA404IsNoLongerAvailable() async throws { let (model, server) = try await open() await server.answer(404) await revalidate(model) XCTAssertEqual(model.documentAccess, .unavailable) XCTAssertEqual(model.detail?.can_write, false) await model.stop() } func testA401PausesInputAndLeavesTheAccessAlone() async throws { let (model, server) = try await open() await server.answer(401) await revalidate(model) XCTAssertTrue(model.sessionPaused, "paused for the session's own reason") XCTAssertFalse(model.enqueue(["op": .string("display_edit"), "at": .number(0), "length": .number(0), "text": .string("x")])) XCTAssertEqual(model.detail?.can_write, true, "a 401 never touches can_write") XCTAssertEqual(model.documentAccess, .writable, "and so no reader chrome") XCTAssertTrue(model.inputBlocked, "input pauses while the session is re-minted") // The re-minted session's 200 resumes input, can_write unchanged. await server.answer(200, canWrite: true) await revalidate(model) XCTAssertFalse(model.inputPaused) XCTAssertEqual(model.documentAccess, .writable) await model.stop() } /// The re-minted session's 200 clears only the 401's own pause: input /// paused for another reason (here a recovery) stays paused. func testThe401ResumeLeavesAnotherPauseAlone() async throws { let (model, server) = try await open() model.inputPaused = true; model.errorKey = "native.recovered" await server.answer(401) await model.revalidateAccess() XCTAssertTrue(model.sessionPaused) await server.answer(200, canWrite: true) await model.revalidateAccess() XCTAssertFalse(model.sessionPaused, "the 401's pause is lifted") XCTAssertTrue(model.inputPaused, "the recovery's pause is not the 401's to lift") XCTAssertTrue(model.inputBlocked) await model.stop() } func testA200WithoutWriteIsViewOnlyInPlace() async throws { let (model, server) = try await open() model.selection = NSRange(location: 2, length: 1) let identity = model.editorIdentity await server.answer(200, canWrite: false) await revalidate(model) XCTAssertEqual(model.documentAccess, .readOnly) XCTAssertEqual(row(model), .viewOnly) XCTAssertTrue(model.inputPaused) XCTAssertEqual(model.editorIdentity, identity, "applied in place: the same text view") XCTAssertEqual(model.selection, NSRange(location: 2, length: 1), "the selection is kept") await model.stop() } } /// LoopServer, with the document's detail answer under the test's control. actor AccessServer: NativeTransport { nonisolated let origin = URL(string: "https://docs.access.invalid")! let inner = LoopServer() private var status = 200 private var canWrite = true func answer(_ status: Int, canWrite: Bool = true) { self.status = status; self.canWrite = canWrite } func responseDetails(_ path: String, method: String, body: Data?, contentType: String, maxBytes: Int) async throws -> NativeResponse { if method == "GET", path == "/d/doc" { guard status == 200 else { return NativeResponse(data: Data(), status: status, headers: [:]) } let detail = DocumentDetail(sqid: "doc", title: "Access", guid: "render-loop", can_write: canWrite, can_share: canWrite) return NativeResponse(data: try JSONEncoder().encode(detail), status: 200, headers: [:]) } return try await inner.responseDetails(path, method: method, body: body, contentType: contentType, maxBytes: maxBytes) } func stream(_ path: String, onOpen: @Sendable () async throws -> Void, onLine: @Sendable (String) async throws -> Void) async throws { try await inner.stream(path, onOpen: onOpen, onLine: onLine) } func download(_ path: String, to destination: URL) async throws { throw NativeClientError.notFound } }