import CottonValues import Foundation import CottonKit import XCTest @testable import CottonUI #if canImport(UIKit) import UIKit #elseif canImport(AppKit) import AppKit #endif @MainActor final class SecretRevealTests: XCTestCase { static let definition = TableDefinition( id: "code_credential", name: "Credential", plural: "Credentials", columns: [ ColumnDefinition(key: "name", type: .text), ColumnDefinition(key: "token", type: .text, secret: true, reveal: ColumnReveal(once: true, expires: 60)), ColumnDefinition(key: "reveal_count", type: .number), ColumnDefinition(key: "last_revealed", type: .date) ], title: "name", slip: SlipLayout(title: "name"), tile: TileLayout(face: .slipList), page: [PageSection(heading: nil, columns: ["name", "token"])] ) func testFailedMetadataWriteDoesNotCopy() async throws { let records = FixtureRecords(definitions: [Self.definition], records: [:]) let clipboard = RecordingClipboard() let value = RevealedValue(title: "Credential", value: "synthetic", note: "Save it", record: "missing", column: "token") do { _ = try await SecretCopy.copy(value, definition: Self.definition, store: records, clipboard: clipboard); XCTFail("missing row copied") } catch {} XCTAssertTrue(clipboard.entries.isEmpty) } func testRevealPayloadUsesTheColumnPolicyAndOrdinaryCopyStillWorks() async throws { let records = FixtureRecords(definitions: [Self.definition], records: ["code_credential": [TableRecord(id: "new", values: ["name": .text("Example")])]]) let clipboard = RecordingClipboard() let value = RevealedValue(title: "Credential", value: "synthetic", note: "Save it", record: "new", column: "token") let change = try await SecretCopy.copy(value, definition: Self.definition, store: records, clipboard: clipboard) XCTAssertEqual(change?.after?["reveal_count"], .number(1)) XCTAssertEqual(clipboard.entries.first?.value, "synthetic") XCTAssertEqual(clipboard.entries.first?.seconds, 60, "a column's policy may ask for less than the secret ceiling") let plain = RevealedValue(title: "Old key", value: "example", note: "Save it") _ = try await SecretCopy.copy(plain, definition: Self.definition, store: records, clipboard: clipboard) XCTAssertEqual(clipboard.entries.last?.seconds, 120, "all transient reveal cards expire at 120 s") } /// RULE: A vault copy expires from the clipboard at 120 s on every platform; a column's policy may only ask for less. Two minutes is the owner's number (14 Oct 2026): long enough to paste on another device, short enough that a password does not sit on the clipboard. func testAVaultCopyExpiresAtTwoMinutes() { XCTAssertEqual(SecretClipboard.seconds, 120) XCTAssertEqual(SecretClipboard.seconds(policy: nil), 120) XCTAssertEqual(SecretClipboard.seconds(policy: 86400), 120, "a policy never lengthens a secret's stay") XCTAssertEqual(SecretClipboard.seconds(policy: 15), 15) } /// RULE: Every secret Cotton copies is marked concealed (org.nspasteboard.ConcealedType), so no clip reader — Cotton's Clips or another app's — keeps it, and on iOS it never leaves the device. func testASecretCopyIsConcealed() { #if canImport(UIKit) let board = UIPasteboard.withUniqueName() defer { UIPasteboard.remove(withName: board.name) } XCTAssertTrue(SystemClipboard(pasteboard: board).put("synthetic", expiringAfter: 5)) XCTAssertTrue(board.contains(pasteboardTypes: [PasteboardMark.concealed])) #elseif canImport(AppKit) let board = NSPasteboard(name: NSPasteboard.Name("cotton-test-\(UUID())")) defer { board.releaseGlobally() } XCTAssertTrue(SystemClipboard(pasteboard: board).put("synthetic", expiringAfter: 5)) XCTAssertTrue(board.types?.contains(NSPasteboard.PasteboardType(PasteboardMark.concealed)) == true) XCTAssertEqual(board.string(forType: .string), "synthetic") #endif } /// RULE: iOS secret copies carry the requested expiration date; Mac expiry clears only the copy it owns, never a newer clipboard value. func testPlatformPasteboardExpiry() async throws { #if canImport(UIKit) let board = UIPasteboard.withUniqueName() defer { UIPasteboard.remove(withName: board.name) } let clipboard = SystemClipboard(pasteboard: board) XCTAssertTrue(clipboard.put("synthetic", expiringAfter: 0.1)) XCTAssertEqual(board.string, "synthetic") try await Task.sleep(for: .milliseconds(200)) XCTAssertNil(board.string) #elseif canImport(AppKit) let board = NSPasteboard(name: NSPasteboard.Name("cotton-test-\(UUID())")) defer { board.releaseGlobally() } let clipboard = SystemClipboard(pasteboard: board) XCTAssertTrue(clipboard.put("synthetic", expiringAfter: 0.05)) XCTAssertEqual(board.string(forType: .string), "synthetic") try await Task.sleep(for: .milliseconds(150)) XCTAssertNil(board.string(forType: .string)) _ = clipboard.put("old", expiringAfter: 0.05) board.clearContents(); board.setString("newer", forType: .string) try await Task.sleep(for: .milliseconds(150)) XCTAssertEqual(board.string(forType: .string), "newer") #endif } func testConcurrentCopiesReleaseOnlyOneValue() async throws { let records = FixtureRecords(definitions: [Self.definition], records: ["code_credential": [TableRecord(id: "new", values: [:])]]) let clipboard = RecordingClipboard() let value = RevealedValue(title: "Credential", value: "synthetic", note: "", record: "new", column: "token") async let one = try? SecretCopy.copy(value, definition: Self.definition, store: records, clipboard: clipboard) async let two = try? SecretCopy.copy(value, definition: Self.definition, store: records, clipboard: clipboard) _ = await (one, two) XCTAssertEqual(clipboard.entries.count, 1) } func testClipboardRefusalDoesNotPermitAnotherReveal() async throws { let records = FixtureRecords(definitions: [Self.definition], records: ["code_credential": [TableRecord(id: "new", values: [:])]]) let refusing = RecordingClipboard(expires: false) let value = RevealedValue(title: "Credential", value: "synthetic", note: "", record: "new", column: "token") do { _ = try await SecretCopy.copy(value, definition: Self.definition, store: records, clipboard: refusing); XCTFail("refusal lost") } catch {} let next = RecordingClipboard() do { _ = try await SecretCopy.copy(value, definition: Self.definition, store: records, clipboard: next); XCTFail("copied again") } catch {} XCTAssertTrue(next.entries.isEmpty) } }