Cotton code
83 lines · 4.8 KB · SwiftRaw
#if os(iOS)import CottonFacesimport CottonKitimport CottonValuesimport XCTestimport SwiftUIimport UIKit@testable import CottonUI// Which faces a table offers, the one chosen, the one after it and the write// that wears it are the core's (`shell.opened`'s Face step, `shell.deck`'s// `hand_next`, `shell.input`'s `face` commit; their rules stand in// cotton-ffi). The shell draws the rows and commits what they say.@MainActor final class FaceChoiceTests: XCTestCase {    private let writes: JSONValue = .object(["kind": "face", "table": "person", "face": "person.passport"])    /// RULE: the hand paints, and says what the core says it wears next.    func testTheHandPaintsAndSaysTheCoresWords() throws {        let next = DeckAnswer.HandNext(face: "person.passport", label: "Face: File; double-tap for Passport", writes: writes)        let renderer = ImageRenderer(content: FaceChoiceControl(next: next).frame(width: 64, height: 64))        let image = try XCTUnwrap(renderer.uiImage?.cgImage)        var pixels = [UInt8](repeating: 0, count: 64 * 64 * 4)        let context = try XCTUnwrap(CGContext(data: &pixels, width: 64, height: 64, bitsPerComponent: 8,            bytesPerRow: 64 * 4, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue))        context.draw(image, in: CGRect(x: 0, y: 0, width: 64, height: 64))        XCTAssertGreaterThan(stride(from: 3, to: pixels.count, by: 4).filter { pixels[$0] != 0 }.count, 0)    }    /// RULE: a row's write goes to the core as the core said it, and a refusal comes back in its words.    func testARowCommitsWhatTheCoreSaid() async {        let core = InputCore { method, _ in            method == CottonMethod.shellInput ? .success(.object([:])) : .failure(CottonCallError(code: "missing", message: "Not here"))        }        let refused = await DeckEditor.commitRow(writes, core: core)        XCTAssertNil(refused)        XCTAssertEqual(core.asked.last?.request["writes"], writes)        XCTAssertEqual(core.asked.last?.request["op"]?.string, "commit")        let refusing = InputCore { _, _ in .success(.object(["refusal": "Not offered"])) }        let said = await DeckEditor.commitRow(writes, core: refusing)        XCTAssertEqual(said, "Not offered")    }    /// RULE: the hand and the Face tier both commit the core's row; no list of faces and no write of the choice stands in Swift.    func testNoFaceDecisionStandsInSwift() throws {        let fan = SourceSweep.withoutComments(try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/Deck/Shell/EditingFan.swift", ownedBy: "shell"))        let face = SourceSweep.withoutComments(try SourceSweep.mustRead("apps/cotton/Sources/CottonUI/Deck/FaceChoice.swift", ownedBy: "shell"))        XCTAssertTrue(fan.contains("case .act(.face):\n            editor.openFaces("))        XCTAssertTrue(face.contains("DeckEditor.commitRow(next.writes"))        for gone in ["store.create(", "offers", "FaceChoices", "openFaces"] {            XCTAssertFalse(face.contains(gone), "\(gone) left for the core")        }        XCTAssertFalse(fan.contains("updateGroup("), "a list's row commits through the core")    }    /// RULE: the Face act opens the tier on the core's rows; no rows, no tier.    func testTheFaceActOpensTheTierOnTheCoresRows() async throws {        let faces = ["person.rolodex", "person.file", "person.passport"]        let core = InputCore { method, request in            guard method == CottonMethod.shellOpened, request["step"]?.string == "face" else {                return .failure(CottonCallError(code: "missing", message: "Not here"))            }            XCTAssertEqual(request["face"]?.string, "person.rolodex")            let rows = faces.map { JSONValue.object(["value": .string($0), "words": .string("Face \($0)"), "chosen": .bool($0 == faces[0])]) }            return .success(.object(["step": .object(["kind": "face", "rows": .array(rows)])]))        }        let editor = DeckEditor.standing()        let target = DeckEditTarget(table: "person", record: "synthetic", date: nil)        editor.presentOpened(target, record: TableRecord(id: "synthetic", values: [:]), worn: "person.rolodex", core: core) {}        for _ in 0..<200 where editor.faces.isEmpty { await Task.yield() }        XCTAssertEqual(editor.faces.map(\.value), faces, "the faces are the core's step")        editor.openFaces(for: target)        XCTAssertEqual(editor.editing, .faces(target))        XCTAssertEqual(editor.navigation.open?.editorState, .faces(target))        editor.mark(target)        XCTAssertEqual(editor.navigation.open?.editorState, .acts(target), "the fan goes back to the card's acts")        let one = DeckEditor.standing()        one.openFaces(for: DeckEditTarget(table: "secret", record: "synthetic", date: nil))        XCTAssertNil(one.editing, "no face step is no choice")    }}#endif