Cotton code
102 lines · 4.0 KB · SwiftRaw
// A core that answers from a script — the double every model test drives.//// It ships in `CottonKit` rather than in a test target on purpose, and the// precedent is the Rust half's own `transport/scripted.rs`: BOTH test bundles// need it (`CottonKitTests` for the boot gate, `CottonUITests` for the screens),// a test target cannot import another test target, and a third library existing// only to hold it would be a build-graph dependency for four targets.//// It is also what makes design P17 true rather than aspirational: a screen test// drives the model, and the model reaches the core through `CottonCalling`. So// every test in this app runs with no XCFramework, no app-group container, no// device key and no network — which is why they run on a checkout that has// never built the archive.import Foundation/// A scripted `CottonCalling`.////// Answers come from a table keyed by method name; anything not in the table/// answers `unknown_method`, which is what the real core does on a phone by/// construction. Every call is recorded in order, so a test asserts what was/// ASKED as well as what was drawn.public actor ScriptedCore: CottonCalling {    /// What one method answers.    public enum Reply: Sendable {        /// A successful answer, as the `data` half of the envelope.        case ok(String)        /// A refusal, as the `error` half.        case refuse(code: String, message: String)        /// The boundary answered something that is not an envelope.        case garbage(String)    }    public struct Recorded: Equatable, Sendable {        public let method: String        public let requestJSON: String    }    private var replies: [String: Reply]    private var handler: (@Sendable (CottonEvent) -> Void)?    private var pending: [CottonEvent] = []    public private(set) var calls: [Recorded] = []    public private(set) var closedCount = 0    /// Methods answered but left out of `calls`: a question every run asks,    /// which a test of what comes after it does not count.    private let quiet: Set<String>    public init(_ replies: [String: Reply] = [:], quiet: Set<String> = []) {        self.replies = replies        self.quiet = quiet    }    /// Set (or replace) one method's answer.    public func answer(_ method: String, _ reply: Reply) {        replies[method] = reply    }    /// Push an event, as the core would. Buffered and replayed if nobody has    /// subscribed yet — the real stream's own rule.    public func emit(kind: String, payload: JSONValue) {        let event = CottonEvent(kind: kind, payload: payload)        guard let handler else {            pending.append(event)            return        }        handler(event)    }    /// Every method asked, in order.    public func methodsCalled() -> [String] { calls.map(\.method) }    public func callRaw(_ method: String, requestJSON: String) throws -> String {        if !quiet.contains(method) { calls.append(Recorded(method: method, requestJSON: requestJSON)) }        switch replies[method] {        case .ok(let json):            return #"{"ok":true,"data":\#(json)}"#        case .refuse(let code, let message):            return #"{"ok":false,"error":{"code":"\#(code)","message":"\#(message)"}}"#        case .garbage(let text):            return text        case nil:            // As the real core says it once `for_person` has read it: a            // sentence, the method kept in the details for the log — never a            // read key a page would draw (R4, 18 Oct 2026: the preview's            // Members page said "read.members" as its note and its refusal).            return #"{"ok":false,"error":{"code":"unknown_method","message":"\#(CottonCallError.notOnThisDevice)","details":{"method":"\#(method)"}}}"#        }    }    public func subscribe(_ handler: @escaping @Sendable (CottonEvent) -> Void) {        self.handler = handler        let replay = pending        pending = []        for event in replay { handler(event) }    }    public func close() {        closedCount += 1        handler = nil    }}