Cotton code
108 lines · 6.3 KB · SwiftRaw
// What Notes reads from the core's answers (CONTRACT.md §1, §2, §3, §4, §9,// §10, §11), decoded as the core writes them: the side page with its places,// tag tree and lists; a row's pin, checklist and thumbnail; the note a link// opens or the offer to make it; an export's file; the ask before deleting for// good; the chosen look with hide marks. Every note is invented.import XCTest@testable import CottonUI// The Mac stub build (swift-guards) has no iOS-only types this reads.#if os(iOS)@MainActorfinal class NotesCallsTests: XCTestCase {    private func decode<T: Decodable>(_ type: T.Type, _ json: String) throws -> T {        try JSONDecoder().decode(type, from: Data(json.utf8))    }    func testTheSidePage() throws {        let side = try decode(NotesSide.self, """        { "current": "all",          "places": [ { "id": "all", "view": "all", "label": "Notes", "icon": "notes", "count": 41, "list": { "view": "all" } },                      { "id": "todo", "view": "todo", "label": "Todo", "icon": "todo", "count": 5, "list": { "view": "todo" } } ],          "tags": { "label": "Tags", "tree": [            { "tag": "work", "name": "work", "count": 12, "depth": 0, "parent": null, "icon": "tag_work", "icon_chosen": false, "pinned": true,              "list": { "view": "tag", "tag": "work" },              "children": [ { "tag": "work/plans", "name": "plans", "count": 4, "depth": 1, "parent": "work", "icon": "hash",                              "icon_chosen": true, "pinned": false, "list": { "view": "tag", "tag": "work/plans" }, "children": [] } ] } ] },          "lists": { "label": "Lists", "items": [ { "id": "g1", "name": "Reading", "icon": "lists", "count": 7, "list": { "view": "list", "list": "g1" } } ] },          "foot": [ { "id": "bin", "view": "bin", "label": "Bin", "icon": "bin", "count": 1, "list": { "view": "bin" } } ],          "more": [ { "id": "settings", "label": "Settings", "icon": "settings" }, { "id": "account", "label": "Account", "icon": "account" } ],          "words": { "pin_tag": "Pin to the top", "unpin_tag": "Unpin", "choose_icon": "Choose an icon" } }        """)        XCTAssertEqual(side.places.map(\.id), ["all", "todo"])        XCTAssertEqual(side.places[1].list, NotesListAsk(view: "todo"))        XCTAssertEqual(side.tags.label, "Tags")        XCTAssertEqual(side.tags.items.first?.pinned, true)        XCTAssertEqual(side.tags.items.first?.children.first?.iconChosen, true)        XCTAssertEqual(side.lists.items.first?.label, "Reading", "a hand-made list says its name")        XCTAssertEqual(side.lists.items.first?.list, NotesListAsk(view: "list", list: "g1"))        XCTAssertEqual(side.more.map(\.id), ["settings", "account"])        XCTAssertEqual(side.words["choose_icon"], "Choose an icon")        XCTAssertEqual(side.icon(of: "Work"), "tag_work")        XCTAssertEqual(side.icon(of: "work/plans"), "hash")        XCTAssertEqual(side.icon(of: "nowhere"), "hash")    }    func testARowCarriesItsPinsChecklistAndThumbnail() throws {        let row = try decode(NotesRow.self, """        { "id": "n1", "title": "Garden plans", "preview": "The tomatoes", "preview_lines": 2, "empty": false, "date": "14:05",          "edited_at": "2026-11-20T10:00:00Z", "tags": ["home/garden"], "picture": "photo:abc", "archived": false, "binned": false,          "pinned": true, "pinned_in_notes": false, "checklist": { "done": 2, "total": 5 },          "thumbnail": { "photo": "photo:abc", "key": "media-1", "width": 1200, "height": 800 }, "by_assistant": true }        """)        XCTAssertTrue(row.pinned)        XCTAssertFalse(row.pinnedInNotes)        XCTAssertEqual(row.checklist, NotesRow.Checklist(done: 2, total: 5))        XCTAssertEqual(row.thumbnail, "media-1")        XCTAssertTrue(row.byAssistant)        let plain = try decode(NotesRow.self, #"{ "id": "n2", "checklist": null, "thumbnail": null }"#)        XCTAssertNil(plain.checklist)        XCTAssertNil(plain.thumbnail)    }    func testALinkOpensItsNoteOrOffersToMakeIt() throws {        let found = try decode(NotesLinked.self, #"{ "id": "n2", "heading_at": 40 }"#)        XCTAssertEqual(found.id, "n2")        XCTAssertEqual(found.headingAt, 40)        let missing = try decode(NotesLinked.self, #"{ "missing": true, "offer": "Make a note called Seeds", "title": "Seeds" }"#)        XCTAssertNil(missing.id)        XCTAssertEqual(missing.offer, "Make a note called Seeds")        XCTAssertEqual(missing.title, "Seeds")    }    func testDeletingForGoodAsksInTheCoresWordsFirst() throws {        let asked = try decode(NotesRemoved.self, """        { "ask": { "title": "Delete for good?", "message": "“Garden plans” will be gone from every device. This can’t be undone.",                   "act": "Delete", "cancel": "Cancel", "destructive": true } }        """)        XCTAssertEqual(asked.ask?.act, "Delete")        XCTAssertNil(asked.said)        let done = try decode(NotesRemoved.self, #"{ "id": "n1", "said": "Deleted for good" }"#)        XCTAssertNil(done.ask)        XCTAssertEqual(done.said, "Deleted for good")        let shown = NotesAsked(ask: try XCTUnwrap(asked.ask)) {}        XCTAssertEqual(shown.title, "Delete for good?")        XCTAssertEqual(shown.yes, "Delete")    }    func testAnExportIsAFileForTheShareSheet() throws {        let made = try decode(NotesExported.self, #"{ "path": "/tmp/x/Garden plans.md", "file_name": "Garden plans.md", "mime": "text/markdown", "count": 1 }"#)        XCTAssertEqual(made.fileName, "Garden plans.md")        XCTAssertEqual(NotesBook.exportFormats, ["markdown", "textbundle", "html", "pdf"])    }    func testTheChosenLookCarriesHideMarks() throws {        let colours = ##"{ "ground": "#F8F6F1", "text": "#34312C", "heading": "#1E1C19", "secondary": "#8F8A80", "accent": "#CF4A3C", "rule": "#E7E3DA", "highlight": "#F7E4A6", "code_ground": "#F0EDE6", "selection": "#F4D2CB", "link": "#4F6E96" }"##        let chosen = try decode(NotesSettingsChosen.self, """        { "light_theme": "linen", "dark_theme": "slate", "light": \(colours), "dark": \(colours), "font": "sans",          "font_size": 17, "line_height": 1.5, "line_width": 680, "paragraph_spacing": 0.4, "hide_marks": false }        """)        XCTAssertFalse(chosen.hideMarks)        XCTAssertFalse(chosen.look(dark: false).hideMarks)        XCTAssertEqual(chosen.look(dark: true).theme.colours.link, "#4F6E96")    }}#endif