// One place a synthetic row is spelled — the merge every per-file builder was // re-inventing. // // Every screen on the day surface reads its rows as JSON, so every test of one // needs a row: a bag of keys with sane defaults and a couple of them changed // per case. Each file grew its own `private func goal(…)` / `place(…)` / // `stubNote(…)` doing the identical three steps — start from a base, overlay // what the case names, wrap it in `.object` — and the third step is where the // copies drifted. // // So the MERGE is here, once (`object`), and the per-kind bases beside it. The // bases are the keys the CONSUMER actually reads, and every builder's doc // comment names the file that reads them, because that is the file a key here // has to keep agreeing with: // // person → Screens/Day/People/PeopleImportSheet.swift (`read.peopleImport`) // alarm → Alarms/Alarm.swift, `Alarm.init(_ json:)` // push → Notifications/NotificationRoutes.swift // // A base carries the keys the read ALWAYS writes and nothing else. A key the // read only sometimes sends is the caller's to add, because "absent" is a fact // several of these screens turn on and a base that volunteered a `null` would // quietly delete the case. // // EVERY VALUE IS SYNTHETIC and obviously so. Nothing real ever goes in this // tree, so a fixture name is a placeholder a reader cannot mistake for a // person. import CottonKit import Foundation enum Fixture { // ── The merge ─────────────────────────────────────────────────────────── /// `base` overlaid with `overrides` — the merge every private per-file /// builder re-invented. /// /// An override WINS, including one whose value is `.null`: a test that says /// a column is null is saying something the base cannot say for it. static func object( _ base: [String: JSONValue], _ overrides: [String: JSONValue] = [:] ) -> JSONValue { .object(base.merging(overrides) { _, override in override }) } // ── The rows ──────────────────────────────────────────────────────────── /// One row of `read.people` (`cotton-ffi/src/phone/reads/person.rs`): /// `id` and `name` are always there — `name`, not `fullName` — `phones` and /// `emails` carry a `null` branch, and `org`, `roleTitle` and `city` are /// null when empty. /// /// `timezone`, `avatarKey` and `avatarUrl` are deliberately NOT in the /// base: a row that omits them is a row the screen says nothing about, and /// that absence is its own case. static func person(_ o: [String: JSONValue] = [:]) -> JSONValue { object( [ "id": .string("p1"), "name": .string("A Synthetic Person"), "phones": .array([]), "emails": .array([]), "org": .null, "roleTitle": .null, "city": .null, ], o) } /// A push's data object — the bag `NotificationRoutes` reads, with `type` /// on it. NOT nested under `body`: the delegate's own envelope is the /// caller's to wrap, because only one of the two tables sees it. static func push(_ type: String, _ fields: [String: JSONValue] = [:]) -> JSONValue { object(["type": .string(type)], fields) } // ── On the wire ───────────────────────────────────────────────────────── /// A `JSONValue` rendered as the string `ScriptedCore.Reply.ok` takes. /// /// Keys are SORTED, so the same fixture renders the same bytes on every /// run — a dictionary's own order is not one, and a test that compared two /// renderings would otherwise fail on a Tuesday. static func json(_ value: JSONValue) -> String { let encoder = JSONEncoder() encoder.outputFormatting = [.sortedKeys, .withoutEscapingSlashes] // The encode cannot fail: a `JSONValue` is by construction a JSON tree, // and there is no NaN branch in it to refuse. A `try!` here would be a // crash in a test bundle over a case that does not exist, so the // fallback is an empty object — loud in an assertion, silent nowhere. guard let data = try? encoder.encode(value), let text = String(data: data, encoding: .utf8) else { return "{}" } return text } }