// The Passwords table as a whole: the vault's pure rules (`PasswordFacts`, // carried over from the old screens' suites), the `secret` definition and the // keys it brought to the shared format (`choice_names`, a list-valued slip // subtitle, `empty`), the privacy of its board tile, the capture bar a vault // table never draws, the one clipboard every copy goes through, and the old // Passwords screens being gone. Every value here is invented. import CottonKit import CottonValues import Foundation import XCTest @testable import CottonUI @MainActor final class PasswordsTableTests: XCTestCase { private func secret() throws -> TableDefinition { try XCTUnwrap(try PasswordsRecordsTests.definitions().first { $0.id == "secret" }) } // ── The vault's pure rules ───────────────────────────────────────────── /// RULE: Every kind of secret has a name and fields, exactly one primary field and it first, keyed by the payload's own keys. func testEveryKindHasANameAndFieldsWithOnePrimaryFirst() { for kind in SecretKind.allCases { XCTAssertFalse(PasswordFacts.label(kind).isEmpty, "\(kind) has no name") let fields = PasswordFacts.fields(kind) XCTAssertFalse(fields.isEmpty, "\(kind) has no fields") XCTAssertEqual(fields.filter(\.isPrimary).count, 1, "\(kind)") XCTAssertTrue(fields.first?.isPrimary == true, "\(kind)'s primary field is not first") } XCTAssertFalse(PasswordFacts.creatable.contains(.document), "a document's password is made from the document") // What the fields build decodes as the payload it claims to be. let built = PasswordFacts.payloadFields(.login, from: ["password": PasswordsRecordsTests.password]) var object: [String: JSONValue] = ["kind": .string("login")] for (key, value) in built { object[key] = .string(value) } let encoded = try? JSONEncoder().encode(JSONValue.object(object)) XCTAssertNotNil(encoded.flatMap { try? SecretPayload.decode($0) }) } /// RULE: An empty sealed field is left out of a payload rather than sent empty, and a value is never trimmed. func testAnEmptyFieldIsLeftOutAndNothingIsTrimmed() { let built = PasswordFacts.payloadFields(.login, from: ["password": "an-invented-pass", "notes": "", "totp": " ", "cvv": "1"]) XCTAssertEqual(built.keys.sorted(), ["password", "totp"], "a field the kind does not have, or an empty one, was sent") XCTAssertEqual(built["totp"], " ") } /// RULE: A hidden password is dots only — no character of it is drawn before Show; the last four after the dots suit a card number alone. func testAMaskIsDotsOnlyExceptACardsLastFour() { let dots = "\u{2022}\u{2022}\u{2022}\u{2022}\u{2022}\u{2022}\u{2022}\u{2022}" XCTAssertEqual(PasswordFacts.masked(kind: .login, hint: "ne42"), dots, "a password's last four were in the clear") XCTAssertEqual(PasswordFacts.masked(kind: .apiKey, hint: "9f3c"), dots) XCTAssertEqual(PasswordFacts.masked(kind: .note, hint: "ate."), dots) XCTAssertEqual(PasswordFacts.masked(kind: .identity, hint: "3456"), dots) XCTAssertEqual(PasswordFacts.masked(kind: .card, hint: "4417"), "\u{2022}\u{2022}\u{2022}\u{2022} 4417") XCTAssertEqual(PasswordFacts.masked(kind: .card, hint: nil), dots) XCTAssertEqual(PasswordFacts.masked(kind: .card, hint: " "), dots) } /// RULE: The vault's state is one line in priority order — the wait, then the old key, then nothing — in the crate's own device-neutral words. func testTheStatusLineSaysTheWaitThenTheOldKeyThenNothing() { XCTAssertEqual(PasswordFacts.statusLine(hasKey: true, wrapped: false, legacyCount: 9), PasswordFacts.waitingForTheKey) XCTAssertEqual(PasswordFacts.statusLine(hasKey: false, wrapped: false, legacyCount: 0), PasswordFacts.notGivenTheKey) let legacy = PasswordFacts.statusLine(hasKey: true, wrapped: true, legacyCount: 1) XCTAssertNotNil(legacy) XCTAssertNotEqual(legacy, PasswordFacts.waitingForTheKey) XCTAssertNotEqual(legacy, PasswordFacts.notGivenTheKey) XCTAssertNil(PasswordFacts.statusLine(hasKey: true, wrapped: true, legacyCount: 0), "a green tick nobody asked for is furniture") } /// RULE: Where Show and Copy cannot be offered, the line says why — the old key, then the wait — and an open vault or an empty secret says nothing. func testTheLockedNoteSaysWhyTheControlsAreAbsent() { XCTAssertEqual(PasswordFacts.lockedNote(isSet: true, isLegacy: false, hasKey: true, wrapped: false), PasswordFacts.waitingForTheKey) XCTAssertEqual(PasswordFacts.lockedNote(isSet: true, isLegacy: false, hasKey: false, wrapped: false), PasswordFacts.notGivenTheKey) XCTAssertEqual(PasswordFacts.lockedNote(isSet: true, isLegacy: true, hasKey: true, wrapped: true), PasswordFacts.sealedUnderTheOldKey) XCTAssertNil(PasswordFacts.lockedNote(isSet: true, isLegacy: false, hasKey: true, wrapped: true)) XCTAssertNil(PasswordFacts.lockedNote(isSet: false, isLegacy: false, hasKey: true, wrapped: false)) } // ── The definition ───────────────────────────────────────────────────── /// RULE: No vault table's board tile shows a record or a count — the Passwords tile is a picture only. func testAVaultTileShowsNoRecordAndNoCount() throws { for definition in try PasswordsRecordsTests.definitions() where definition.engine == "vault" { for size in TileSize.allCases { XCTAssertEqual(BoardTile.capacity(definition.tile.face, size, scale: Theme.paper.scale), 0, "\(definition.id) draws names on the board at \(size)") } } } /// RULE: No face of the secret table draws what the vault keeps: the store's sealed blocks (`sealed`, as the Worker sealed it, and `value_enc`, sealed on the device under the vault key as a passkey's private key is, 702f81ee36) and the hint (the primary field's last characters) are readonly columns the page, the slip, the card and the parts never name — only the copy act reads a sealed block, opened on the device — and no column is a plain secret: only the device's sealed block is answered masked. func testNoFaceDrawsASealedValue() throws { let secret = try secret() let card = secret.card?.fields.flatMap { [$0.column].compactMap { $0 } + $0.also } ?? [] let parts = (secret.parts?.head.facts ?? []) + (secret.parts?.cards ?? []).compactMap(\.column) + (secret.parts?.strip ?? []).compactMap(\.column) let drawn = Set(secret.page.flatMap(\.columns) + [secret.slip.title] + secret.slip.subtitles + [secret.slip.trailing].compactMap { $0 } + card + parts + secret.search) let kept = secret.columns.filter { column in ["payload", "sealed", "ciphertext", "nonce", "password", "value", "hint", "_enc"].contains { column.key.contains($0) } } // history_enc (0388854d0) seals a login's earlier passwords as // value_enc seals its secret: a second sealed block, never drawn. XCTAssertEqual(Set(kept.map(\.key)), ["sealed", "value_enc", "history_enc", "hint"], "the vault keeps its sealed blocks and hint, and nothing else of a value") for column in kept { XCTAssertTrue(column.readonly, "\(column.key) is Cotton's to set") XCTAssertFalse(drawn.contains(column.key), "\(column.key) is drawn on a face") } for column in secret.columns where !["value_enc", "history_enc"].contains(column.key) { XCTAssertFalse(column.isSecret, "a sealed value is the sealed block's, never a column") } } // ── The capture bar and the clipboard ────────────────────────────────── /// RULE: Every copy a list or a page makes goes through the Clipboard seam, and a sealed one carries its 120 seconds. func testEveryCopyGoesThroughTheClipboardSeam() throws { let board = RecordingClipboard() _ = RecordActionOutcomes.copy("https://example.invalid", clipboard: board) XCTAssertEqual(board.plain, ["https://example.invalid"]) XCTAssertEqual( RecordActionOutcomes.copy("an-invented-token", expiresAfter: SecretClipboard.seconds, clipboard: board), RecordActionOutcomes.copiedLine(forgets: true)) XCTAssertEqual(board.entries.map(\.seconds), [120]) let mac = RecordingClipboard(expires: false) XCTAssertEqual( RecordActionOutcomes.copy("an-invented-token", expiresAfter: SecretClipboard.seconds, clipboard: mac), RecordActionOutcomes.copiedLine(forgets: false)) // No surface writes to a pasteboard of its own. for (name, text) in try SourceSweep.swiftSources() where name.hasPrefix("CottonUI/Surfaces/") || name.hasPrefix("CottonUI/Sources/") { let code = SourceSweep.withoutComments(text) XCTAssertEqual(try SourceSweep.matches(#"UIPasteboard|NSPasteboard"#, in: code), [], "\(name) writes to the pasteboard itself") } } /// RULE: An act's typed value is handed on exactly as typed, and a field of spaces is nothing typed. func testAnActsTypedValueIsHandedOnAsTyped() { let act = RecordAction(id: "new-login", label: "New login", input: RecordActionInput(prompt: "The password", secure: true)) XCTAssertNil(act.entered) XCTAssertEqual(act.entering(" pass ").entered, " pass ") XCTAssertFalse(EnteredValue.isGiven(" \n")) XCTAssertTrue(EnteredValue.isGiven(" pass ")) XCTAssertFalse(RecordActionInput(prompt: "Body", secure: true, multiline: true).secure, "a secure field is one line") } // ── The old screens ──────────────────────────────────────────────────── /// The screens, models and drawings the Passwords list and page replaced. static let replaced = [ "PasswordsScreen", "PasswordsModel", "SecretScreen", "SecretModel", "SecretForm", "SecretFormModel", "NewSecretScreen", "SecretTarget", "PasswordTag", "PasswordTagCell", "KeyTagShape", "KeyTagPaper", "TagEyelet", "PlaceOnMachineSheet", "KindPicker", ] }