103 lines · 4.0 KB · SwiftRaw
// `/settings/join-request/[id]` — the screen a `device_join_requested` push// opens, and the four states it can be in.//// Every fixture is synthetic.import CottonKitimport XCTest@testable import CottonUI@MainActorfinal class JoinRequestTests: XCTestCase { private static let waiting = """ {"pending":[ {"pairingId":"p-1","name":"Asha's iPhone","platform":"ios","appVersion":"1.2.3", "createdAt":"2026-09-06T09:58:00.000Z","expiresAt":"2026-09-06T10:08:00.000Z", "digits":"482913"}, {"pairingId":"p-2","name":"Someone else","platform":"mac","appVersion":null, "createdAt":"2026-09-06T09:00:00.000Z","expiresAt":"2026-09-06T09:10:00.000Z", "digits":"111111"} ]} """ private func subject( pending: String? = nil, pairingId: String = "p-1", extra: [String: ScriptedCore.Reply] = [:] ) -> (JoinRequestModel, ScriptedCore, ToastCenter) { var replies: [String: ScriptedCore.Reply] = [ CottonMethod.devicesPending: .ok(pending ?? JoinRequestTests.waiting) ] for (method, reply) in extra { replies[method] = reply } let core = ScriptedCore(replies) let toasts = ToastCenter(scheduler: ManualToastScheduler()) return ( JoinRequestModel(core: core, toasts: toasts, pairingId: pairingId), core, toasts ) } /// The `args` a call carried, read back off the wire. private func sent(_ json: String?) -> [String: Any]? { guard let data = json?.data(using: .utf8) else { return nil } return try? JSONSerialization.jsonObject(with: data) as? [String: Any] } /// **Yes echoes the ROW's digits**, which are the six the person compared. func testApprovingEchoesTheRowsOwnNumber() async throws { let (model, core, toasts) = subject( extra: [CottonMethod.devicesApprovePending: .ok("{\"approved\":true}")] ) await model.load() await model.approve() let calls = await core.calls let approve = try XCTUnwrap( calls.first { $0.method == CottonMethod.devicesApprovePending } ) let body = try XCTUnwrap(sent(approve.requestJSON)) XCTAssertEqual(body["pairingId"] as? String, "p-1") XCTAssertEqual(body["digits"] as? String, "482913") XCTAssertEqual(Set(body.keys), ["pairingId", "digits"]) XCTAssertTrue(model.answered, "and the host pops") XCTAssertEqual(toasts.current?.tone, .success) } /// A refused approve is one sentence and the screen STAYS: the request is /// still waiting and the person may try again. func testANumberThatDidNotMatchLeavesTheRequestWhereItWas() async { let (model, _, toasts) = subject( extra: [ CottonMethod.devicesApprovePending: .garbage( """ {"ok":false,"error":{"code":"pairing_refused","message":"no", "details":{"status":403}}} """ ) ] ) await model.load() await model.approve() XCTAssertEqual(toasts.current?.tone, .error) XCTAssertFalse(model.answered) XCTAssertEqual(model.digits, "482913") } /// A server too old to answer the digits puts no question, so there is /// nothing to approve: the second factor IS the comparison. func testARequestWithNoNumberOffersNothingToApprove() async { let (model, core, _) = subject( pending: """ {"pending":[{"pairingId":"p-1","name":"A phone","platform":"ios", "appVersion":null,"createdAt":"2026-09-06T09:58:00.000Z", "expiresAt":"2026-09-06T10:08:00.000Z","digits":null}]} """ ) await model.load() XCTAssertNil(model.digits) await model.approve() let calls = await core.methodsCalled() XCTAssertFalse(calls.contains(CottonMethod.devicesApprovePending)) }}