Cotton code
123 lines · 7.2 KB · SwiftRaw
// What the Todos conversion restored from the old Todos board and todo page,// each through a key drawn once for any table (`Definitions/UpcomingKeys.swift`// and the list's own rules): the days soonest first with a day gone under// Today, the foot that says how late or when a snooze ends, Done and Dropped// after the open work, a ticked slip that stays where it stands, a new todo// due today, the repeat mark, "high priority", the repeat words and the// project's own Todos. Every record is invented.import CottonKitimport CottonValuesimport Foundationimport XCTest@testable import CottonUI@MainActorfinal class UpcomingKeysTests: XCTestCase {    private static var resources: Bundle? {        Bundle(url: SourceSweep.iosSources.appendingPathComponent("CottonUI/Resources"))    }    private func todos() throws -> TableDefinition {        let loaded = FixtureDefinitions.load(from: try XCTUnwrap(Self.resources))        XCTAssertEqual(loaded.failures, [])        return try XCTUnwrap(loaded.definitions.first { $0.id == "todo" })    }    private var ist: Calendar {        var out = Calendar(identifier: .gregorian)        out.timeZone = TimeZone(identifier: "Asia/Kolkata")!        out.locale = Locale(identifier: "en_US_POSIX")        return out    }    /// The headings the definition words (late, later, undated) and its    /// group's (done, dropped) — data, read from the definition, never spelled here.    private func headings(_ todos: TableDefinition) throws -> (late: String, later: String, undated: String, done: String, dropped: String) {        let upcoming = try XCTUnwrap(todos.upcoming)        let groups = try XCTUnwrap(todos.group?.sections)        func group(_ value: String) throws -> String {            try XCTUnwrap(groups.first { $0.values.contains(value) }?.heading)        }        return (try XCTUnwrap(upcoming.late), try XCTUnwrap(upcoming.later), try XCTUnwrap(upcoming.undated),                try group("done"), try group("dropped"))    }    /// Thursday 1 October 2026, ten in the morning in Kolkata.    private var now: Date { CottonDate.parse("2026-10-01T10:00:00+05:30", calendar: ist)! }    private func todo(        _ id: String, due: String? = nil, snoozed: String? = nil, status: String = "open", extra: [String: RecordValue] = [:]    ) -> TableRecord {        var values: [String: RecordValue] = ["title": .text("Example \(id)"), "status": .text(status)]        if let due { values["due_at"] = .text(due) }        if let snoozed { values["snoozed_until"] = .text(snoozed) }        return TableRecord(id: "example-\(id)", values: values.merging(extra) { _, new in new })    }    // ── The definition ──────────────────────────────────────────────────    // ── The days ────────────────────────────────────────────────────────    // ── The new todo ────────────────────────────────────────────────────    /// What the todo definition's own columns default to — an open todo of no    /// priority — which every blank page starts with (`createPreset`).    private let todoDefaults: [String: RecordValue] = ["status": .text("open"), "priority": .number(0)]    /// RULE: A todo made from the list's "+" is due today — the old sheet's "It lands on today" — over its columns' own defaults, and `/todos?new=1` opens that blank page as the list arrives.    func testANewTodoIsDueTodayAndTheLinkOpensIt() throws {        let todos = try todos()        XCTAssertEqual(todos.createPreset(now: now, calendar: ist), todoDefaults.merging(["due_at": .text("2026-10-01")]) { _, new in new })        var plain = todos        plain.create = ListCreate(label: "New todo")        XCTAssertEqual(plain.createPreset(now: now, calendar: ist), todoDefaults, "without a dated column the blank page takes the defaults alone")    }    /// RULE: A new todo made under a project (its own Todos, or the switcher) is filed under it — the old board's "+" filed it there — through the same narrowing the chips' records take, and the list's blank page is preset by that rule.    func testANewTodoUnderAProjectIsFiledUnderIt() throws {        let todos = try todos()        let preset = RecordListModel.narrowed(todos.createPreset(now: now, calendar: ist), filter: [:], project: "example-project-garden", in: todos)        XCTAssertEqual(preset, todoDefaults.merging(["due_at": .text("2026-10-01"), "project": .text("example-project-garden")]) { _, new in new })    }    // ── The marks ───────────────────────────────────────────────────────    /// RULE: A repeating todo's Repeats words open its repeat rule (the `todo_recurrence` record its `recurrence_id` names), and a todo that does not repeat opens nothing from them — the rule and its History are reached from the todo's page, never only through search.    func testTheRepeatWordsOpenTheRule() throws {        let todos = try todos()        let repeating = todo("r", extra: ["repeats": .text("Weekly"), "recurrence_id": .text("example-rule")])        let target = try XCTUnwrap(todos.opening(repeating, column: "repeats"))        XCTAssertEqual(target.table, "todo_recurrence")        XCTAssertEqual(target.id, "example-rule")        XCTAssertNil(todos.opening(todo("once"), column: "repeats"))    }    // ── The project's own Todos ─────────────────────────────────────────    /// RULE: A project's own Todos is the todo list under that project, named by its id or its slug (the id first), and a project nobody holds narrows to nothing — never to every todo.    func testAProjectsTodosResolvesItsIdOrSlugAndNothingElse() async throws {        var project = TableDefinition(            id: "project", name: "Project", plural: "Projects",            columns: [ColumnDefinition(key: "name", type: .text), ColumnDefinition(key: "slug", type: .text)],            title: "name", slip: SlipLayout(title: "name"), tile: TileLayout(face: .capGrid), page: [])        project.role = .project        project.scopeBy = "slug"        let store = FixtureRecords(definitions: [project], records: [            "project": [TableRecord(id: "example-project-garden", values: ["name": .text("Example Garden"), "slug": .text("example-garden")])],        ])        let byID = await ProjectListRoute.resolve("example-project-garden", in: store)        let bySlug = await ProjectListRoute.resolve("example-garden", in: store)        let missing = await ProjectListRoute.resolve("example-nowhere", in: store)        let noStore = await ProjectListRoute.resolve("example-garden", in: nil)        XCTAssertEqual(byID, .project("example-project-garden"))        XCTAssertEqual(bySlug, .project("example-project-garden"))        XCTAssertEqual(missing, ProjectListRoute.Found.none)        XCTAssertEqual(noStore, .failed)    }}