Cotton code
161 lines · 6.5 KB · SwiftRaw
// What a note carries, and the four rules the screen stands on.//// The read shape is `get_record`'s: `attachments: [{kind, id, position,// caption, recordCaption, title, contentType|mime, key, missing, bytes}]`,// absent or empty until the tools lane answers one. Every fixture here is// invented.import CottonKitimport XCTest@testable import CottonUIprivate enum AttachmentFixtures {    static func row(        kind: String,        id: String,        position: Int,        caption: String? = nil,        recordCaption: String? = nil,        title: String? = nil,        key: String? = nil,        mime: String? = nil,        missing: Bool = false    ) -> JSONValue {        var out: [String: JSONValue] = [            "kind": .string(kind),            "id": .string(id),            "position": .number(Double(position)),            "missing": .bool(missing),        ]        if let caption { out["caption"] = .string(caption) }        if let recordCaption { out["recordCaption"] = .string(recordCaption) }        if let title { out["title"] = .string(title) }        if let key { out["key"] = .string(key) }        if let mime { out["contentType"] = .string(mime) }        return .object(out)    }    static func record(_ rows: [JSONValue]) -> JSONValue {        .object(["id": .string("note:1"), "body": .string(""), "attachments": .array(rows)])    }    static func attachment(        kind: String = "photo",        id: String,        position: Int = 0,        caption: String? = nil,        recordCaption: String? = nil,        title: String? = nil,        key: String? = nil,        contentType: String? = nil,        missing: Bool = false    ) -> RecordAttachment {        RecordAttachment(            kind: kind,            id: id,            position: position,            caption: caption,            recordCaption: recordCaption,            title: title,            key: key,            missing: missing,            contentType: contentType        )    }}// ── The read ────────────────────────────────────────────────────────────────final class RecordAttachmentsReadTests: XCTestCase {}// ── The missing rule ────────────────────────────────────────────────────────final class RecordAttachmentMissingTests: XCTestCase {    /// The tables the app ships, which say which attachments draw as prints.    static var shipped: TableDefinitions {        TableDefinitions(FixtureDefinitions.load(from: Bundle(url: SourceSweep.iosSources.appendingPathComponent("CottonUI/Resources"))).definitions)    }}// ── The list, added to and taken from ───────────────────────────────────────final class RecordAttachmentListTests: XCTestCase {    private let one = AttachmentFixtures.attachment(id: "photo:a", position: 0)    private let two = AttachmentFixtures.attachment(kind: "video", id: "video:b", position: 1)    func testTheWriteCarriesThePairAndNothingElse() {        let captioned = AttachmentFixtures.attachment(            id: "photo:a", caption: "From the read", key: "k", contentType: "image/jpeg")        XCTAssertEqual(            RecordAttachments.argument([captioned, two]),            .array([                .object(["kind": .string("photo"), "id": .string("photo:a")]),                .object(["kind": .string("video"), "id": .string("video:b")]),            ]),            "an entry with no `caption` key leaves the stored caption alone"        )    }}// ── Inline, or in the strip ─────────────────────────────────────────────────final class RecordAttachmentPartitionTests: XCTestCase {    private let picture = AttachmentFixtures.attachment(id: "photo:a", position: 0)    private let film = AttachmentFixtures.attachment(        kind: "video", id: "video:b", position: 1)    private let paper = AttachmentFixtures.attachment(        kind: "document", id: "6f1e", position: 2)}// ── Which caption a tile writes under itself ────────────────────────────────/// Two captions arrive on one row and they are not the same sentence: `caption`/// is what THIS note said about the picture, `recordCaption` what the record/// says about itself everywhere. The note's own words win; the record's stand/// when the note wrote none; the title carries a row from a server that answers/// neither, because both fields are optional on the wire.final class RecordAttachmentCaptionTests: XCTestCase {}// ── Moving one, and captioning one ──────────────────────────────────────────/// The two edit-mode verbs that rewrite the WHOLE list.////// `update_note`'s `attachments` replaces the set in the order it is given, so/// an order is only ever expressible whole — and an entry with NO `caption` key/// leaves the stored caption alone, which is the only reason a reorder can be/// sent without every caption on the note being rewritten by the phone.final class RecordAttachmentMoveTests: XCTestCase {    private let one = AttachmentFixtures.attachment(id: "photo:a", position: 0)    private let two = AttachmentFixtures.attachment(        kind: "video", id: "video:b", position: 1)    private let three = AttachmentFixtures.attachment(        kind: "document", id: "6f1e", position: 2)    private var list: [RecordAttachment] { [one, two, three] }    // ── A move made in the strip ──────────────────────────────────────────}/// A caption written onto ONE picture, with every other entry's left alone.final class RecordAttachmentCaptionWriteTests: XCTestCase {    private let one = AttachmentFixtures.attachment(        id: "photo:a", position: 0, caption: "The wall before",        recordCaption: "Kitchen, July")    private let two = AttachmentFixtures.attachment(        kind: "video", id: "video:b", position: 1, recordCaption: "The rehearsal")    private let three = AttachmentFixtures.attachment(        kind: "document", id: "6f1e", position: 2, title: "Lease")    private var list: [RecordAttachment] { [one, two, three] }}