Cotton code
136 lines · 8.9 KB · SwiftRaw
import CottonKitimport XCTest@testable import CottonUI/// The database caps under a fan Home's tiles (fan2 part 6; fan3 part 1 made/// the row an arc), on a synthetic device holding two databases. Every name/// is invented.@MainActorfinal class DatabaseCapRowTests: XCTestCase {    private func held(_ json: String) async throws -> (DatabasesModel, () -> Void) {        let core = ScriptedCore([CottonMethod.readDatabases: .ok(json)])        let suite = "DatabaseCapRowTests-\(UUID().uuidString)"        let defaults = try XCTUnwrap(UserDefaults(suiteName: suite))        let model = DatabasesModel(source: CoreDatabases(core: core), defaults: defaults)        await model.refresh()        return (model, { defaults.removePersistentDomain(forName: suite) })    }    private static let two = #"{"databases":[{"databaseId":"P","name":"Mine","kind":"personal"},{"databaseId":"T","name":"Tin Factory","kind":"shared"}],"shown":[],"elsewhere":[]}"#    /// RULE: a thought said after a database's cap is chosen lands in THAT database — the cap moves the window to it, and the record button's box writes where the window shows — so a cap never shows one database while the thought goes to another.    func testAThoughtAfterAChoiceLandsInTheChosenDatabase() async throws {        let (model, done) = try await held(Self.two)        defer { done() }        let scene = DatabaseScene(shownId: "P")        XCTAssertEqual(Set(DatabaseCapRow.caps(model).compactMap(\.database?.databaseId)), Set(model.ordered.map(\.databaseId)))        XCTAssertNil(HomeInput.destination(scene.shownId, in: model).id, "the personal database is the box's own")        await DatabaseCapRow.choose("T", scene: scene, in: model)        XCTAssertEqual(scene.shownId, "T")        XCTAssertEqual(HomeInput.destination(scene.shownId, in: model).id, "T")        await DatabaseCapRow.choose("unheld", scene: scene, in: model)        XCTAssertEqual(scene.shownId, "T", "a database not held is never chosen")    }    /// RULE: The caps' arc centres on the personal database with the others either side by name, and its last cap opens the Databases screen (new or join) — the head draws no database symbol, so with one database held the arc still stands, its own cap and the last (defaults, night plan).    func testThePersonalDatabaseIsAtTheCentreAndTheLastCapOpensDatabases() async throws {        let (one, doneOne) = try await held(#"{"databases":[{"databaseId":"P","name":"Mine","kind":"personal"}],"shown":[],"elsewhere":[]}"#)        defer { doneOne() }        XCTAssertEqual(DatabaseCapRow.caps(one).map(\.key), ["P", "databases"])        let (model, done) = try await held(#"{"databases":[{"databaseId":"Z","name":"Zinnia Club","kind":"shared"},{"databaseId":"P","name":"Mine","kind":"personal"},{"databaseId":"A","name":"Allotment","kind":"shared"},{"databaseId":"T","name":"Tin Factory","kind":"shared"},{"databaseId":"B","name":"Book Group","kind":"shared"}],"shown":[],"elsewhere":[]}"#)        defer { done() }        let caps = DatabaseCapRow.caps(model)        XCTAssertEqual(caps.map(\.key), ["A", "B", "P", "T", "Z", "databases"])        XCTAssertEqual(caps.last, .databases)        for count in 1...6 {            let others = (0..<count).map { #"{"databaseId":"S\#($0)","name":"Shared \#($0)","kind":"shared"}"# }            let (many, doneMany) = try await held(#"{"databases":[{"databaseId":"P","name":"Mine","kind":"personal"},"# + others.joined(separator: ",") + #"],"shown":[],"elsewhere":[]}"#)            let caps = DatabaseCapRow.caps(many)            XCTAssertEqual(caps.firstIndex { $0.database?.isPersonal == true }, (caps.count - 1) / 2, "the personal database stands at the arc's centre")            doneMany()        }    }    /// RULE: "open <database>" chooses its cap and "open <tool> on <database>" chooses the cap and turns that database's fan to the tool; a database not held, or a name near two, chooses nothing.    func testSpokenDatabaseChoosesTheCap() async {        let fan: [(key: String, name: String)] = [("home", "Home"), ("notes", "Notes")]        let databases: [(id: String, name: String)] = [("P", "Mine"), ("T", "Tin Factory")]        func goes(_ words: String) async -> SpokenHome.Goes? {            await SpokenHome.goes(                words, definitions: TableDefinitions([]), fan: fan,                databases: databases, find: { _ in [] })        }        let factory = await goes("open Tin Factory")        let notes = await goes("Can you open notes on Tin Factory?")        let elsewhere = await goes("open the garden club")        XCTAssertEqual(factory, .database("T", tile: nil))        XCTAssertEqual(notes, .database("T", tile: "notes"))        XCTAssertNil(elsewhere)    }    #if os(iOS)    /// RULE: a launch draws Me and the Databases sheet opens only from a person — the caps' arc acts on a rest only when a finger or a tap on a cap brought it; the arc mounting, its caps arriving (a recount that lands it on the last cap), a turn the code asks for and the turn back are nobody's and open nothing (walker pass 4, 16 Oct 2026: every cold launch flashed the Databases sheet for 0.75 s).    func testOnlyAPersonsRestOpensDatabases() throws {        let geometry = FanGeometry(scale: Theme.paper.scale)        let motion = FanMotion(geometry: geometry, spring: FanSpring(scale: Theme.paper.scale))        motion.recount(4)        motion.turn(to: 3, animated: false)        XCTAssertEqual(motion.settled, 3)        XCTAssertFalse(motion.byPerson, "a turn the code asks for is nobody's")        motion.turn(to: 1, animated: false, byPerson: true)        XCTAssertTrue(motion.byPerson, "a tap on a cap is a person's")        motion.turn(to: 3, animated: false)        motion.recount(2)        XCTAssertEqual(motion.settled, 1)        XCTAssertFalse(motion.byPerson, "caps arriving or going land the arc for nobody")        motion.beginDrag()        motion.release(pointsPerSecond: 0, flings: false)        XCTAssertTrue(motion.byPerson, "a finger on the arc is a person's")    }    #endif    /// RULE: The caps turn on an arc concentric with the tiles' — the same pivot below the screen, a smaller radius, the tiles' geometry type and motion parameterised by size and step, never a copy (fan3 part 1).    func testTheCapsArcSharesTheTilesPivot() {        let scale = Theme.paper.scale        let fan = FanGeometry(scale: scale)        let above = fan.height        let caps = DatabaseCapRow.geometry(scale: scale, fan: fan, above: above)        let width = 393.0        // The tiles' pivot, in the caps' band (which stands `above` lower).        XCTAssertEqual(caps.pivot(width: width).x, fan.pivot(width: width).x, accuracy: 1e-9)        XCTAssertEqual(caps.pivot(width: width).y, fan.pivot(width: width).y - above, accuracy: 1e-9)        XCTAssertLessThan(caps.radius, fan.radius)        // A cap a step away stands a cap and a gap along the arc from the chosen one.        let along = caps.step * .pi / 180 * caps.radius        XCTAssertEqual(along, Double(scale[.fanCapSize] + scale[.fanCapsGap]), accuracy: 1e-9)    }    /// RULE: a cap is chosen by a person's settled choice — a tap's turn as the arc heads for the cap (292: no frame of the old database's cards under the new cap), a swipe only where the arc comes to rest — so scrolling the caps to a far cap switches through none of the databases on the way (walker294 row 18); the arc mounting, its caps arriving and a turn back choose nothing.    func testASwipeChoosesWhereItRestsAndATapAsItTurns() {        XCTAssertTrue(ArcChoice.chooses(.heading, byPerson: true, swiped: false), "a tap switches as the arc turns")        XCTAssertFalse(ArcChoice.chooses(.settled, byPerson: true, swiped: false), "and its rest switches nothing again")        XCTAssertFalse(ArcChoice.chooses(.heading, byPerson: true, swiped: true),                       "a swipe's heading is where a fling may land, not a choice")        XCTAssertTrue(ArcChoice.chooses(.settled, byPerson: true, swiped: true), "a swipe chooses where it rests")        for move in [ArcChoice.Move.heading, .settled] {            for swiped in [false, true] {                XCTAssertFalse(ArcChoice.chooses(move, byPerson: false, swiped: swiped), "nobody's motion chooses nothing")            }        }    }    #if os(iOS)    /// RULE: a finger on the arc marks the motion a swipe and a tap's turn clears it, so the caps tell a swipe's heading from a tap's (walker294 row 18).    func testAFingerIsASwipeAndATapsTurnIsNot() {        let motion = FanMotion(geometry: FanGeometry(scale: Theme.paper.scale), spring: FanSpring(scale: Theme.paper.scale))        motion.recount(5)        motion.beginDrag()        XCTAssertTrue(motion.swiped)        motion.release(pointsPerSecond: 0, flings: false)        motion.turn(to: 2, animated: false, byPerson: true)        XCTAssertFalse(motion.swiped, "the tap that follows the touch is a tap's turn")        XCTAssertTrue(motion.byPerson)    }    #endif}