Cotton code
98 lines · 4.5 KB · SwiftRaw
// Notes as its table's list and page, with nothing the old Notes screens and// note editor showed lost (rebuild item 4, the no-loss audit's fifteen): the// Markdown page with its pictures where the words put them, the strip, the// pager and its pins, the conversation, the byline and the managed note kept// read-only, the edited date, History, a project's journal page, months on a// long list, the two-up grid, the borrowed title, New note and Archive. Each// is a generic definition key or surface drawn once, never Notes' own Swift.// Every record is invented.import CottonKitimport CottonValuesimport Foundationimport XCTest@testable import CottonUI@MainActorfinal class NotesNoLossTests: XCTestCase {    private static var resources: Bundle? {        Bundle(url: SourceSweep.iosSources.appendingPathComponent("CottonUI/Resources"))    }    private static var calendar: Calendar {        var out = Calendar(identifier: .gregorian)        out.timeZone = TimeZone(identifier: "Asia/Kolkata") ?? .gmt        out.locale = Locale(identifier: "en_US_POSIX")        return out    }    private func note() throws -> TableDefinition {        let loaded = FixtureDefinitions.load(from: try XCTUnwrap(Self.resources))        return try XCTUnwrap(loaded.definitions.first { $0.id == "note" })    }    private func notes() throws -> [TableRecord] {        try FixtureDefinitions.fixtureRecords(for: "note", bundle: try XCTUnwrap(Self.resources))    }    private func fixture(_ id: String) throws -> TableRecord {        try XCTUnwrap(try notes().first { $0.id == id })    }    private func at(_ iso: String) throws -> Date {        try XCTUnwrap(CottonDate.parse(iso, calendar: Self.calendar))    }    /// `month`: the core's month for the stamp in the phone's zone and its    /// heading (`@day_parts`), as a read carries them; none when it will not read.    private func dated(_ id: String, _ iso: String, month: (key: String, title: String)? = nil) -> TableRecord {        let record = TableRecord(id: id, values: ["title": .text(id), "updated_at": .text(iso)])        guard let month else { return record }        return CoreSaid.read(record, days: ["updated_at": CoreSaid.day(month.key + "-01", monthTitle: month.title)])    }    /// RULE: The keys this conversion adds (top-level by_month and create, grid default) decode, survive a round trip, and read as before when absent — a list unsplit by month, a grid that opens as a list, no "+".    func testTheNewKeysDecodeAndTheirAbsenceReadsAsBefore() throws {        let definition = try note()        XCTAssertEqual(try JSONDecoder().decode(TableDefinition.self, from: JSONEncoder().encode(definition)), definition)        let bare = try TableDefinition.decode(Data("""            {"id":"example","name":"Example","plural":"Examples","builtin":false,             "columns":[{"key":"title","type":"text"}],"title":"title","slip":{"title":"title"},             "tile":{"face":"slip-list"},"page":[{"heading":null,"columns":["title"]}],"grid":{"columns":3}}            """.utf8))        XCTAssertNil(bare.byMonth)        XCTAssertNil(bare.create)    }    // ── (7) Whose words these are, and the managed note ──    // ── (8, 9) The edited date, and History ──    // ── (10) A project's journal page, opened by its id ──    /// RULE: `/notes/note/[id]` opens a project's journal page in the journal table — headed by its day — and any other id as a note, drawn from the one read that decided it (a second read before the first frame drew notes late, threadload 16 Oct 2026); a failed read is the note's page, which says so; `new` is a blank note page.    func testANoteRouteOpensAJournalPageAsTheJournals() async throws {        let written = TableRecord(id: "n1", values: ["title": .text("A note")])        let store = FixtureRecords(definitions: [try note()], records: ["note": [written]])        let found = await NoteRoute.read("n1", in: store)        let journal = await NoteRoute.read("j1", in: store)        let unread = await NoteRoute.read("n1", in: nil)        XCTAssertEqual(found, .note(written))        XCTAssertEqual(journal, .notANote)        XCTAssertEqual(unread, .unread)        XCTAssertEqual(NoteRoute.table(of: .notANote), "journal")        XCTAssertEqual(NoteRoute.table(of: .unread), "note")    }    // ── (11) Months on a long list ──    // ── (13) The untitled note's borrowed title ──    // ── (14) New note ──    // ── (15) Archive ──}