78 lines · 3.5 KB · SwiftRaw
// A core that answers the shell's input calls from a script, and keeps what// it was asked: the reader's tests hold what the shell SENDS and how it DRAWS// what the core answers. What a write writes is the core's, tested in Rust// (`crates/cotton-ffi/src/methods/shell_input_tests.rs`).import CottonKitimport Foundation@testable import CottonUIfinal class InputCore: CottonCalling, @unchecked Sendable { /// One call as it was made: the method and its request. struct Asked: Equatable { let method: String let request: JSONValue } private let lock = NSLock() private var kept: [Asked] = [] /// The answer to a call: its data, or a refusal `(code, message)`. private let answer: @Sendable (String, JSONValue) -> Result<JSONValue, CottonCallError> init(_ answer: @escaping @Sendable (String, JSONValue) -> Result<JSONValue, CottonCallError>) { self.answer = answer } /// Every call made so far, in order. var asked: [Asked] { lock.withLock { kept } } /// The calls of `method`, and of `op` where it names one. func asked(_ method: String, op: String? = nil) -> [JSONValue] { asked.filter { $0.method == method && (op == nil || $0.request["op"]?.string == op) }.map(\.request) } func callRaw(_ method: String, requestJSON: String) async throws -> String { let request = (try? JSONDecoder().decode(JSONValue.self, from: Data(requestJSON.utf8))) ?? .null lock.withLock { kept.append(Asked(method: method, request: request)) } let envelope: JSONValue = switch answer(method, request) { case .success(let data): .object(["ok": .bool(true), "data": data]) case .failure(let error): .object(["ok": .bool(false), "error": .object(["code": .string(error.code), "message": .string(error.message)])]) } return String(decoding: try JSONEncoder().encode(envelope), as: UTF8.self) } func subscribe(_ handler: @escaping @Sendable (CottonEvent) -> Void) async {} func close() async {} /// A core whose every `shell.input` ask answers the `editor` (`line` or /// `page`) holding `words`; any other call is refused. static func asking(_ editor: String, words: String = "", title: String? = nil) -> InputCore { InputCore { method, request in guard method == "shell.input", request["op"]?.string == "ask" else { return .failure(CottonCallError(code: "missing", message: "Not here")) } var ask: [String: JSONValue] = ["editor": .string(editor), "words": .string(words), "keeps_words": .bool(editor == "line"), "writes": .object(["kind": "column", "table": request["table"] ?? .null, "id": request["id"] ?? .null, "column": request["column"] ?? .null])] if let title { ask["title"] = .string(title) } return .success(.object(ask)) } } /// A JSON literal as the core would answer it. static func json(_ text: String) -> JSONValue { (try? JSONDecoder().decode(JSONValue.self, from: Data(text.utf8))) ?? .null }}/// Waits on the main actor until `holds` is true, or fails after a second.@MainActorfunc eventually(_ holds: @MainActor () -> Bool, file: StaticString = #filePath, line: UInt = #line) async throws { for _ in 0..<200 where !holds() { try await Task.sleep(for: .milliseconds(5)) } if !holds() { throw ScriptedTimeout(file: "\(file)", line: line) }}struct ScriptedTimeout: Error { let file: String let line: UInt}