Cotton code
61 lines · 3.4 KB · SwiftRaw
// Cotton Keys' Credential Exchange import over a scripted core: the export's// JSON goes to passkey.importCxf whole, its answer becomes the toast's counts// and skips, a locked vault (no person here) and an unreadable export come// back as refusals with nothing written, and the encoder writes binary as// base64url and times as whole UNIX seconds, as CXF 1.0 §2.1 says them.// Every id and key is invented.import Foundationimport XCTest@testable import CottonKitfinal class PasskeyImportTests: XCTestCase {    private let answer = #"""    {"imported":[{"id":"row-a","rp_id":"example.com","user_name":"person@example.com"},                 {"id":"row-b","rp_id":"login.example.org","user_name":"person"}],     "skipped":[{"title":"Example 2FA","reason":"a totp credential: Cotton Keys imports passkeys only"}],     "exporter":"Synthetic Exporter","unnamed":["item.lastModified"]}    """#    // RULE (keys-cxf-phone, 8 Nov 2026): the export's JSON reaches    // passkey.importCxf whole, and its answer says the sites, the skips with    // their titles and reasons, and the exporter.    func testAnExportReachesTheCoreAndItsAnswerIsRead() async throws {        let core = ScriptedCore([CottonMethod.passkeyImportCxf: .ok(answer)])        let json = #"{"version":{"major":1,"minor":0}}"#        let outcome = await PasskeyImport(core: core).run(json: json, presence: false)        XCTAssertEqual(outcome.sites, ["example.com", "login.example.org"])        XCTAssertEqual(outcome.skipped, [.init(title: "Example 2FA", reason: "a totp credential: Cotton Keys imports passkeys only")])        XCTAssertEqual(outcome.exporter, "Synthetic Exporter")        XCTAssertNil(outcome.refused)        let calls = await core.calls        XCTAssertEqual(calls.map(\.method), [CottonMethod.passkeyImportCxf])        XCTAssertTrue(calls[0].requestJSON.contains("major"), calls[0].requestJSON)        XCTAssertFalse(calls[0].requestJSON.contains("privateKey"), "no vault key lent unasked")    }    // RULE: a locked vault with nobody here, or an export that does not read,    // is a refusal in words and nothing imported.    func testALockedVaultOrABadExportIsARefusal() async {        let locked = ScriptedCore([CottonMethod.passkeyImportCxf: .refuse(code: "vault_locked", message: "The vault is locked.")])        let first = await PasskeyImport(core: locked).run(json: "{}", presence: false)        XCTAssertTrue(first.locked)        XCTAssertEqual(first.sites, [])        let bad = ScriptedCore([CottonMethod.passkeyImportCxf: .refuse(code: "bad_export", message: "this is not a Credential Exchange export: the export has no version")])        let second = await PasskeyImport(core: bad).run(json: "{}", presence: false)        XCTAssertFalse(second.locked)        XCTAssertEqual(second.refused, "this is not a Credential Exchange export: the export has no version")    }    // RULE: binary is base64url without padding and a time is whole UNIX    // seconds, the spellings the core's CXF reader reads.    func testTheEncoderWritesCXFSpellings() throws {        struct Probe: Encodable {            var id: Data            var at: Date        }        let probe = Probe(id: Data([0xfb, 0xff, 0xfe, 0x01]), at: Date(timeIntervalSince1970: 1_767_225_600.75))        let text = String(decoding: try PasskeyImport.encoder().encode(probe), as: UTF8.self)        XCTAssertEqual(text, #"{"at":1767225600,"id":"-__-AQ"}"#)    }}