import CottonKit import Foundation import XCTest @testable import CottonUI #if os(iOS) /// Cotton Sites on iPhone reads the core's answers as `crates/cotton-ffi/src/phone/sites_app_tests.rs` shapes them /// (CONTRACT "The core calls each screen needs"). Every address is invented. @MainActor final class SitesAnswersTests: XCTestCase { private func decode(_ json: String, as: T.Type) throws -> T { try JSONDecoder().decode(T.self, from: Data(json.utf8)) } private static let row = """ {"id":"a1","letter":"W","label":"Work mail","site":"mail.example","line":"mail.example · Inbox (4)","open":"https://mail.example/inbox", "stay":true,"pinned":true,"hidden":false,"trackers":true,"sign_in_once":false,"store":"site-a1","store_uuid":"6F1C2E9A-0B4D-4C1E-9E2A-1A2B3C4D5E6F"} """ func testTheListReadsFirstRunAndHiddenBehindTheUnlock() throws { let home = try decode(""" {"title":"Sites","search":{"line":"Search, or type an address"},"add":"Add a site","you":"You and settings","rows":[\(Self.row)], "quick":{"label":"Quick visit","line":"Keeps nothing at all"}, "hidden":{"label":"Hidden, showing","line":"Face ID to show","line_android":"Unlock to show","locked":false,"hide":"Hide","rows":[]}} """, as: SitesHome.self) XCTAssertEqual(home.rows.first?.line, "mail.example · Inbox (4)") XCTAssertEqual(home.rows.first?.storeUUID, "6F1C2E9A-0B4D-4C1E-9E2A-1A2B3C4D5E6F") XCTAssertNil(home.first) XCTAssertEqual(home.hidden.hide, "Hide") let empty = try decode(""" {"title":"Sites","search":{"line":"Search, or type an address"},"add":"Add a site","you":"You","rows":[], "quick":{"label":"Quick visit","line":"Keeps nothing at all"},"hidden":{"label":"Hidden","line":"Face ID to show","locked":true}, "first":{"line":"Each site here keeps its own sign-in.","add":"Add your first site","quick":"Quick visit, keeps nothing"}} """, as: SitesHome.self) XCTAssertEqual(empty.first?.line, "Each site here keeps its own sign-in.") XCTAssertNil(empty.hidden.rows) } func testTheChooserKeepsTheWholeLinkForTheLoadAndShowsItMasked() throws { let choice = try decode(""" {"title":"Open a link from Post","url":"https://club.example/login?token=Zx81kQpLm2","shown":"https://club.example/login?token=Zx•••", "once":"This link signs you in. It loads only where you choose, and only once.", "at_site":{"label":"At club.example","rows":[\(Self.row)]},"others":{"label":"Another of your sites…","rows":[]}, "hidden":{"label":"Hidden","line":"Face ID to show","locked":true},"quick":{"label":"Quick visit","line":"Keeps nothing at all"}, "new_site":{"label":"A new site","line":"Starts empty, with its own sign-in"},"nothing_yet":"Nothing loads until you choose."} """, as: SitesChoice.self) XCTAssertEqual(choice.url, "https://club.example/login?token=Zx81kQpLm2") XCTAssertTrue(choice.shown.hasSuffix("Zx•••")) XCTAssertEqual(choice.atSite.rows.count, 1) XCTAssertTrue(choice.hidden.locked) } func testOpeningReadsTheQuickWordsTheArrivedLineAndTheSavedState() throws { let quick = try decode(""" {"store":"quick-9","url":"https://duckduckgo.com/?q=x","stay":false,"private":true,"title":"Quick visit", "quick":{"strip":"Search · nothing kept","name":"Search","name_line":"Gone when you leave","close":"Close"}} """, as: SitesOpened.self) XCTAssertEqual(quick.quick?.nameLine, "Gone when you leave") let site = try decode(""" {"store":"site-a1","store_uuid":"6F1C2E9A-0B4D-4C1E-9E2A-1A2B3C4D5E6F","url":"https://mail.example/m/1","stay":true,"private":false, "title":"Work mail","arrived":{"line":"From Post, opened in Work mail.","wrong":"Wrong place?"},"state":"c3RhdGU=","trackers":true} """, as: SitesOpened.self) XCTAssertEqual(site.arrived?.wrong, "Wrong place?") XCTAssertEqual(site.state.flatMap { Data(base64Encoded: $0) }, Data("state".utf8)) } func testSettingsReadOnOffAndChoicesUnderOneValueKey() throws { let you = try decode(""" {"title":"You","card":{"count":"2 sites"}, "rows":[{"id":"look","label":"Look"}, {"id":"engine","label":"Search with","value":"DuckDuckGo","choices":[{"id":"duckduckgo","label":"DuckDuckGo","chosen":true}, {"id":"google","label":"Google","chosen":false,"line":"Google, still in a place that keeps nothing"}]}, {"id":"trackers","label":"Trackers","value":true,"line":"Blocked"}, {"id":"hide_after","label":"Hidden sites","value":"now","choices":[{"id":"now","label":"Hide when I leave"},{"id":"minute","label":"Hide a minute after I leave"}]}], "shared":[{"id":"sync","label":"Sync","line":"Your list, never sign-ins"},{"id":"privacy","label":"Privacy","line":"Assistants never see Sites."}]} """, as: SitesSettings.self) XCTAssertEqual(you.rows[1].text, "DuckDuckGo") XCTAssertEqual(you.rows[1].choices?.last?.id, "google") XCTAssertEqual(you.rows[2].on, true) XCTAssertNil(you.rows[2].text) XCTAssertEqual(you.rows[3].text, "now") XCTAssertEqual(you.shared[1].line, "Assistants never see Sites.") } /// The Look preview's rows are Sites' own (a letter, a name, a second line) under the key Days uses for its times. func testTheLookPreviewReadsSitesRowsAndTheBar() throws { let preview = try decode(""" {"rows":[{"letter":"W","label":"Work mail","site":"mail.example","line":"mail.example · Inbox (4)","stay":true}], "bar":{"label":"Work mail","site":"mail.example","thread":true}} """, as: LookAnswer.Preview.self) XCTAssertNil(preview.rows) XCTAssertEqual(preview.sites?.first?.line, "mail.example · Inbox (4)") XCTAssertEqual(preview.bar?.thread, true) let days = try decode(""" {"day":"Tuesday","rows":[{"time":"9:00","title":"Standup"}]} """, as: LookAnswer.Preview.self) XCTAssertEqual(days.rows?.first?.title, "Standup") XCTAssertNil(days.sites) } func testTheIconsDrawFromThePagesPaths() { for icon in [SitesIcon.search, .sites, .reload, .copy, .lock, .face, .rest, .wifi, .gear] where !icon.path.isEmpty { XCTAssertFalse(SVGPath.parse(icon.path).isEmpty, "\(icon)") XCTAssertLessThanOrEqual(SVGPath.parse(icon.path).boundingRect.maxX, 22.5, "\(icon)") } } } #endif