Cotton code
80 lines · 3.1 KB · SwiftRaw
import XCTest@testable import CottonUIimport CottonKit/// Putting a secret on a machine, and taking a password manager in — the pure/// halves of both (`Sources/Vault/VaultMachines.swift`), which is all/// of them a test can reach. Moved with them out of the Passwords screens.////// **The path rules are NOT here.** They are Rust's/// (`cotton_client::vault::placements::resolve`) and are exhaustively tested/// there; restating them in Swift would be a second opinion about what a legal/// path is, and the two would eventually disagree. What Swift decides is when/// **Save** is allowed, which is not the same question.////// **Every fixture is invented** — machines nobody owns, a path nobody has/// ([nothing-real-ever-goes-in-cotton-s-public-tree]).final class VaultMachinesTests: XCTestCase {    // ── the machine list ───────────────────────────────────────────────────    private func json(_ text: String) -> JSONValue {        (try? JSONDecoder().decode(JSONValue.self, from: Data(text.utf8))) ?? .null    }    private func devices(_ rows: [String]) -> JSONValue {        json("""            { "thisDevice": "device-here", "devices": [\(rows.joined(separator: ","))] }            """)    }    private func mac(_ id: String, revoked: Bool = false) -> String {        """        { "deviceId": "\(id)", "name": "The \(id) Mac", "platform": "mac"          \(revoked ? ", \"revokedAt\": \"2026-09-01T00:00:00.000Z\"" : "") }        """    }    func testARevokedMachineIsNotOfferedBecauseNothingWouldEverApplyIt() {        let machines = PlacementMachine.all(in: devices([mac("gone", revoked: true)]))        XCTAssertEqual(machines, [])    }    // ── what Save is allowed to send ───────────────────────────────────────    private func asked(        deviceId: String? = "device-here",        path: String = "~/.config/example/token",        isEnv: Bool = false,        envName: String = "",        mode: Int = 0o600    ) -> PlacementRequest? {        PlacementRequest.from(            secretId: "secret-1",            deviceId: deviceId,            path: path,            isEnv: isEnv,            envName: envName,            mode: mode        )    }    // ── the three states a placement row draws ─────────────────────────────    // ── what an import says ────────────────────────────────────────────────    private var importAnswer: JSONValue {        json("""            {              "created": { "login": 2, "note": 1, "card": 0 },              "updated": { "login": 3 },              "createdTotal": 3,              "updatedTotal": 3,              "skipped": [                { "name": "personalinfo.csv", "reason": "this is people, not a secret" }              ]            }            """)    }}