77 lines · 3.8 KB · SwiftRaw
// A core that answers `shell.list` the plainest way: the rows a store's own// source handed (`rows`), in their order, one unheaded section, paged by// cursor. What the list SHOWS is the core's to test (`shell_list_tests.rs`);// a Swift test of the reader — its asking cadence, its receipts, its seeds —// needs only an answer that pages the rows it handed.import CottonKitimport CottonValuesimport Foundation@testable import CottonUIfinal class ListCoreFake: CottonCalling, @unchecked Sendable { private let lock = NSLock() private var _asks: [[String: Any]] = [] /// How many `shell.list` asks it answered. var asks: Int { lock.withLock { _asks.count } } /// Each ask, as JSON. var asked: [[String: Any]] { lock.withLock { _asks } } /// What every row's slip title says, by its data's column. let titleColumn: String /// Answers with this refusal instead, while set. var refusing: String? init(titleColumn: String = "title") { self.titleColumn = titleColumn } func callRaw(_ method: String, requestJSON: String) async throws -> String { guard method == CottonMethod.shellList else { return #"{"ok":true,"data":{}}"# } let ask = (try? JSONSerialization.jsonObject(with: Data(requestJSON.utf8))) as? [String: Any] ?? [:] lock.withLock { _asks.append(ask) } if let refusing { return #"{"ok":false,"error":{"code":"unreadable","message":"\#(refusing)"}}"# } if let deleting = ask["deleting"] as? [String] { // Select's Delete over those picked: all taken, in plain words with their count. let n = deleting.count let answer: [String: Any] = [ "table": ask["table"] as? String ?? "", "deleting": [ "ids": deleting, "asks": true, "word": "Delete", "question": ["title": "Delete \(n)?", "message": "", "confirm": "Delete"], "done": "Deleted \(n)", "undone": "Put back \(n)", "outcome": "\(n) deleted", ], ] let data = try JSONSerialization.data(withJSONObject: ["ok": true, "data": answer]) return String(decoding: data, as: UTF8.self) } let rows = ask["rows"] as? [[String: Any]] ?? [] let cursor = ask["cursor"] as? Int ?? 0 let limit = max(1, ask["limit"] as? Int ?? 50) let page = Array(rows.dropFirst(cursor).prefix(limit)) let answered: [[String: Any]] = page.map { row in let id = row["id"] as? String ?? "" let data = row["data"] as? [String: Any] ?? [:] return [ "id": id, "dimmed": false, "deletes": true, "acts": [], "press": [], "tap": "open.page", "slip": ["title": data[titleColumn] as? String ?? "", "record": ["id": id, "data": data]], ] } let next: Any = cursor + limit < rows.count ? cursor + limit : NSNull() let answer: [String: Any] = [ "table": ask["table"] as? String ?? "", "title": "Invented list", "sections": answered.isEmpty ? [] : [["id": "all", "open": true, "folds": false, "rows": answered]], "acts": [:], "menu": [], "links": [], "groups": NSNull(), "offers_new_group": false, "index": NSNull(), "filter": NSNull(), "empty": rows.isEmpty ? ["title": "Nothing yet", "message": ""] : NSNull(), "clear": NSNull(), "select": ["deletes": true, "adds_to_group": false, "word": "Delete"], "reorders": false, "polls": NSNull(), "across": NSNull(), "recent": [], "notes": [], "count": rows.count, "next": next, ] let data = try JSONSerialization.data(withJSONObject: ["ok": true, "data": answer]) return String(decoding: data, as: UTF8.self) } func subscribe(_ handler: @escaping @Sendable (CottonEvent) -> Void) {} func close() {}}