// Keys › Devices' Approve on a locked vault: the core refuses vault_locked, // the shell reads this device's own vault key from the Keychain (on a phone // the read is the presence prompt) and runs the command once more with the // key lent for that call; a cancelled prompt runs nothing; a refusal after // that is the core's, in its words. @testable import CottonKit @testable import CottonUI import CottonValues import XCTest /// A core that refuses a command as vault_locked until it is handed a key. private actor LockedVaultCore: CottonCalling { private(set) var requests: [String] = [] let afterKey: String init(afterKey: String = #"{"ok":true,"data":{"command":"approve","kind":"update","receipt":"Approved Invented Mac"}}"#) { self.afterKey = afterKey } func callRaw(_ method: String, requestJSON: String) throws -> String { requests.append(requestJSON) guard requestJSON.contains("privateKey") else { return #"{"ok":false,"error":{"code":"vault_locked","message":"The vault is locked."}}"# } return afterKey } func subscribe(_ handler: @escaping @Sendable (CottonEvent) -> Void) {} func close() {} } final class VaultKeyCommandTests: XCTestCase { private let approve = RecordCommand(id: "approve", label: "Approve", method: "vault.approveDevice", kind: .update) /// A synthetic X25519 scalar, never anyone's key. private let synthetic = String(repeating: "A", count: 43) private func keyring(_ store: InMemoryVaultKeyStore) -> VaultKeyring { VaultKeyring(keys: store, cache: InMemorySecretStore()) } // RULE: A command the core refuses as vault_locked runs once more with this device's own vault key, read from the keyring and sent as the request's privateKey; the first request carried none. func testALockedVaultLendsTheKeychainKeyForTheOneCall() async throws { let core = LockedVaultCore() let store = InMemoryVaultKeyStore(seeded: synthetic) let change = try await core.recordCommand(table: "vault_key", id: "vk-invented", command: approve, pickedValue: .null, keyring: keyring(store)) let requests = await core.requests XCTAssertEqual(requests.count, 2) XCTAssertFalse(requests[0].contains("privateKey")) XCTAssertTrue(requests[1].contains(synthetic)) XCTAssertEqual(store.readCount, 1, "one presence prompt") XCTAssertEqual(change.said?.said, "Approved Invented Mac") } // RULE: A cancelled presence prompt sends nothing more to the core. func testACancelledPromptRunsNothing() async { let core = LockedVaultCore() let store = InMemoryVaultKeyStore(seeded: synthetic, failing: .cancelled) do { _ = try await core.recordCommand(table: "vault_key", id: "vk-invented", command: approve, pickedValue: .null, keyring: keyring(store)) XCTFail("a cancelled prompt approves nothing") } catch {} let requests = await core.requests XCTAssertEqual(requests.count, 1) } // RULE: A refusal after the key was lent is the core's own sentence. func testARefusalAfterTheKeyIsTheCoresWords() async { let core = LockedVaultCore(afterKey: #"{"ok":false,"error":{"code":"already_approved","message":"That device already opens your vault."}}"#) do { _ = try await core.recordCommand(table: "vault_key", id: "vk-invented", command: approve, pickedValue: .null, keyring: keyring(InMemoryVaultKeyStore(seeded: synthetic))) XCTFail("refused") } catch { XCTAssertEqual(ActionRefused.words(for: error), "That device already opens your vault.") } } }