// 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.importFoundation/// 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.publicenumReply: 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) }publicstructRecorded: Equatable, Sendable {publiclet method: Stringpubliclet requestJSON: String }privatevar replies: [String: Reply]privatevar handler: (@Sendable (CottonEvent) -> Void)?privatevar pending: [CottonEvent] = []publicprivate(set) var calls: [Recorded] = []publicprivate(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.privatelet quiet: Set<String>publicinit(_ replies: [String: Reply] = [:], quiet: Set<String> = []) {self.replies = repliesself.quiet = quiet }/// Set (or replace) one method's answer.publicfunc 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.publicfunc emit(kind: String, payload: JSONValue) {let event = CottonEvent(kind: kind, payload: payload)guardlet handler else { pending.append(event)return } handler(event) }/// Every method asked, in order.publicfunc methodsCalled() -> [String] { calls.map(\.method) }publicfunc 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 textcasenil:// 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)"}}}"# } }publicfunc subscribe(_ handler: @escaping @Sendable (CottonEvent) -> Void) {self.handler = handlerlet replay = pending pending = []for event in replay { handler(event) } }publicfunc close() { closedCount += 1 handler = nil }}