Cotton code
115 lines · 7.3 KB · SwiftRaw
// Send's Add a person (14 Nov 2026): the Send tier draws the core's `add`// row last, a person who has not added this person back as the core words// them (a tap says so, nothing goes), the number typed in the short-text// input on a number's keyboard, and the tier read again once it is// committed. What the add does is the core's (`cotton_send.rs`) and the// Worker's (`POST /v1/chat/accept`).// The fan is drawn on the phone only (`EditingFan.swift` is `#if os(iOS)`).#if os(iOS)import CottonValuesimport XCTest@testable import CottonUI@MainActorfinal class SendAddPersonTests: XCTestCase {    private let target = DeckEditTarget(table: "book", record: "b1", date: nil)    nonisolated private static let step = #"""    {"step":{"kind":"send","says":"Send Emma, 1 MB","title":"Emma","bytes":1000000,     "rows":[{"value":"+15550100001","words":"Asha","number":"+15550100001","trusted":false,"initials":"A",              "detail":"1 MB","sends":{"to":"+15550100001"},"holds":[{"id":"trust","words":"Always keep from Asha"}]},             {"value":"+15550100002","words":"+15550100002","number":"+15550100002","initials":"+",              "waiting":true,"detail":"They have not added you yet"}],     "add":{"words":"Add a person","hint":"Their phone number","ask":{"kind":"people_add"}}}}    """#    /// RULE: a waiting person (no `sends`, no `holds`) decodes, says the    /// core's words at a tap and sends nothing; Add a person is drawn last.    func testTheTierDrawsWaitingPeopleAndAddLast() throws {        let step = try JSONDecoder().decode(SendStepAnswer.self, from: Data(Self.step.utf8)).step        let items = EditingFan.items(sending: step)        XCTAssertEqual(items.map(\.word), ["Asha", "+15550100002", "Add a person"])        XCTAssertEqual(items[0].does, .send(step.rows[0]))        XCTAssertEqual(items[1].does, .sendWaiting("They have not added you yet"))        XCTAssertEqual(items[1].detail, "They have not added you yet")        XCTAssertEqual(items[2].does, .sendAdd(try XCTUnwrap(step.add)))        XCTAssertTrue(step.rows[1].holds.isEmpty)    }    /// RULE: with nobody yet, the tier still offers Add a person, after the    /// door to Cotton chat where the app keeps chat.    func testNobodyYetStillOffersAdd() throws {        let json = #"{"step":{"kind":"send","rows":[],"empty":{"words":"Nobody to send to yet"},"add":{"words":"Add a person"}}}"#        let step = try JSONDecoder().decode(SendStepAnswer.self, from: Data(json.utf8)).step        XCTAssertEqual(EditingFan.items(sending: step).map(\.does), [.sendAdd(try XCTUnwrap(step.add))])        let door = #"{"step":{"kind":"send","rows":[],"empty":{"words":"Nobody","door":{"words":"Ask in Messages","path":"/thread/new"}},"add":{"words":"Add a person"}}}"#        let doored = try JSONDecoder().decode(SendStepAnswer.self, from: Data(door.utf8)).step        XCTAssertEqual(EditingFan.items(sending: doored).map(\.does), [.sendDoor("/thread/new"), .sendAdd(try XCTUnwrap(doored.add))])    }    /// RULE: Add a person asks its number of the core (`people_add`) on a    /// number's keyboard; committed, the core's words are said and the tier    /// is read again.    func testAddAsksTheNumberAndReadsTheTierAgain() async throws {        let core = InputCore { method, request in            switch (method, request["op"]?.string) {            case ("shell.opened", _):                return .success(InputCore.json(Self.step))            case ("shell.input", "ask"):                return .success(InputCore.json(#"{"editor":"line","words":"","hint":"Their phone number","keyboard":"phone","keeps_words":false,"writes":{"kind":"people_add"}}"#))            case ("shell.input", "commit"):                return .success(InputCore.json(#"{"said":"Added +15550100002"}"#))            default:                return .failure(CottonCallError(code: "missing", message: "Not here"))            }        }        let editor = DeckEditor.standing()        editor.open(.sends(target), core: core)        try await eventually { editor.sendStep != nil }        editor.addPerson(core: core)        try await eventually { editor.writing != nil }        XCTAssertEqual(editor.writing?.keyboard, "phone")        XCTAssertEqual(editor.writing?.hint, "Their phone number")        XCTAssertEqual(core.asked("shell.input", op: "ask").first?["kind"], "people_add")        editor.commit("+1 555 010 0002")        try await eventually { core.asked("shell.opened").count == 2 }        let sent = try XCTUnwrap(core.asked("shell.input", op: "commit").first)        XCTAssertEqual(sent["words"], "+1 555 010 0002")        XCTAssertEqual(sent["writes"], InputCore.json(#"{"kind":"people_add"}"#))    }    /// RULE: Account's Your people decodes as the core answers it (asking,    /// people, waiting; each act naming its call), and its cards say each    /// section's names and each person's words, detail and number.    func testAccountsPeopleAreTheCoresCards() throws {        let json = #"""        {"line":"Synced","stand":"ok","card":{"title":"Account","subtitle":"+15550100121","facts":[]},"actions":[],         "people":{"title":"Your people","add":{"words":"Add a person","hint":"Their phone number"},          "sections":[{"id":"asking","title":"Asking","rows":[{"number":"+15550100003","words":"Ravi wants to connect","detail":"+15550100003",                        "acts":[{"id":"accept","words":"Accept","primary":true,"call":{"method":"people.accept","request":{"number":"+15550100003"}}},                                {"id":"block","words":"Block","red":true,"call":{"method":"people.block","request":{"number":"+15550100003"}}}]}]},                      {"id":"waiting","title":"Waiting","rows":[{"number":"+15550100002","words":"+15550100002","detail":"They have not added you yet",                        "acts":[{"id":"remove","words":"Remove","red":true,"call":{"method":"people.remove","request":{"number":"+15550100002"}}}]}]}]}}        """#        let answer = try JSONDecoder().decode(AccountAnswer.self, from: Data(json.utf8))        let people = try XCTUnwrap(answer.people)        XCTAssertEqual(people.rows.map(\.row.number), ["+15550100003", "+15550100002"])        XCTAssertEqual(people.rows[0].row.acts?.first?.call.method, "people.accept")        XCTAssertEqual(people.rows[0].row.acts?.first?.call.request, InputCore.json(#"{"number":"+15550100003"}"#))        let card = AccountAnswer.drawn(people)        XCTAssertEqual(card.fields["title"], .text("Your people"))        XCTAssertEqual(card.fields["facts"], .list([            .object(["label": .text("Asking"), "said": .text("Ravi wants to connect")]),            .object(["label": .text("Waiting"), "said": .text("+15550100002")]),        ]))        let waiting = AccountAnswer.drawn(people.rows[1].row, in: people.rows[1].section)        XCTAssertEqual(waiting.fields["subtitle"], .text("They have not added you yet"))        let out = #"{"line":"","stand":"","card":{"title":"Account","subtitle":"","facts":[]},"actions":[],"people":{"title":"Your people","unreachable":"Your people are out of reach"}}"#        let unreached = try XCTUnwrap(try JSONDecoder().decode(AccountAnswer.self, from: Data(out.utf8)).people)        XCTAssertTrue(unreached.rows.isEmpty)        XCTAssertEqual(AccountAnswer.drawn(unreached).fields["subtitle"], .text("Your people are out of reach"))    }}#endif