| 1 | @testable import BaseKit |
| 2 | import Foundation |
| 3 | import XCTest |
| 4 | |
| 5 | /// P12: where each of Home's apps opens. |
| 6 | final class OpenerTests: XCTestCase { |
| 7 | let docs = HomeData.App(id: "docs", kind: .oficina, name: "Docs", url: URL(string: "https://fennel.docs.oficina.works/"), bundleID: "works.oficina.docs") |
| 8 | let calls = HomeData.App(id: "calls", kind: .oficina, name: "Calls", url: URL(string: "https://fennel.calls.oficina.works/"), |
| 9 | bundleID: "works.oficina.calls", scheme: "oficina-calls") |
| 10 | let equipo = HomeData.App(id: "equipo", kind: .oficina, name: "Equipo", url: URL(string: "https://equipo.fennel.example/"), |
| 11 | bundleID: "works.oficina.equipo", scheme: "works.oficina.equipo") |
| 12 | let welcome = HomeData.App(id: "welcome", kind: .installed, name: "Welcome", url: URL(string: "https://welcome.fennel.example/"), tint: "amber") |
| 13 | let installedDocs = URL(fileURLWithPath: "/Applications/Oficina Docs.app") |
| 14 | |
| 15 | func testTheMacOpensAnInstalledOficinaAppWhenSettingsSaysSo() { |
| 16 | let installed: (String) -> URL? = { $0 == "works.oficina.docs" ? self.installedDocs : nil } |
| 17 | XCTAssertEqual(Opener.mac(docs, preference: .app, installed: installed), .application(installedDocs)) |
| 18 | XCTAssertEqual(Opener.mac(docs, preference: .browser, installed: installed), .url(docs.url!)) |
| 19 | XCTAssertEqual(Opener.mac(calls, preference: .app, installed: installed), .url(calls.url!), "not installed: the web") |
| 20 | XCTAssertEqual(Opener.mac(welcome, preference: .app, installed: { _ in self.installedDocs }), .url(welcome.url!), "Store apps open on the web") |
| 21 | } |
| 22 | |
| 23 | func testTheIPhoneOpensCallsAndEquipoByTheirSchemes() { |
| 24 | let all: (URL) -> Bool = { _ in true } |
| 25 | XCTAssertEqual(Opener.phone(calls, canOpen: all), .url(URL(string: "oficina-calls://")!)) |
| 26 | XCTAssertEqual(Opener.phone(equipo, canOpen: all), .url(URL(string: "works.oficina.equipo://")!)) |
| 27 | XCTAssertEqual(Opener.phone(calls, canOpen: { _ in false }), .url(calls.url!), "not installed: the web") |
| 28 | XCTAssertEqual(Opener.phone(docs, canOpen: all), .url(docs.url!), "no scheme yet: the web") |
| 29 | XCTAssertEqual(Opener.phone(welcome, canOpen: all), .url(welcome.url!)) |
| 30 | } |
| 31 | |
| 32 | func testInstalledNamesAndHomesGroups() throws { |
| 33 | let home = try JSONDecoder().decode(HomeData.self, from: Fixture.data("home.json")) |
| 34 | XCTAssertEqual(home.launchable.map(\.id), ["calendar", "calls", "welcome", "future"]) |
| 35 | XCTAssertEqual(home.own.map(\.id), ["store"]) |
| 36 | XCTAssertTrue(home.stepsOpen) |
| 37 | XCTAssertEqual(Opener.installedNames(home.apps, installed: { $0 == "works.oficina.calls" ? self.installedDocs : nil }), ["Calls"]) |
| 38 | } |
| 39 | |
| 40 | func testTileArtParses() { |
| 41 | let people = #"<svg viewBox="0 0 24 24"><circle cx="9" cy="9" r="3"/><circle cx="16.5" cy="10.5" r="2.5"/><path d="M3.5 19a5.5 5.5 0 0 1 11 0M14 19a4 4 0 0 1 7 0"/></svg>"# |
| 42 | let box = SVGGlyph(svg: people).path(in: CGRect(x: 0, y: 0, width: 24, height: 24)).boundingRect |
| 43 | XCTAssertEqual(box.minX, 3.5, accuracy: 0.1) |
| 44 | XCTAssertEqual(box.minY, 6, accuracy: 0.1) |
| 45 | XCTAssertEqual(box.maxX, 21, accuracy: 0.1) |
| 46 | XCTAssertTrue(SVGGlyph(svg: "").path(in: CGRect(x: 0, y: 0, width: 24, height: 24)).isEmpty) |
| 47 | } |
| 48 | |
| 49 | func testTheColophonShipsWithItsLicences() { |
| 50 | let entries = BaseColophon.sections.flatMap(\.entries) |
| 51 | XCTAssertEqual(Set(entries.map(\.id)), ["timeless-sans", "dm-sans", "keyboardshortcuts", "swift-cmark", "rastrillo-native", "sparkle"]) |
| 52 | for e in entries where e.id != "timeless-sans" { XCTAssertGreaterThan(e.licenceText.count, 200, e.id) } |
| 53 | // What each is and what Base uses it for come from the catalog. |
| 54 | for e in entries { |
| 55 | XCTAssertNotEqual(e.blurb, e.blurbKey, "\(e.blurbKey) is not in the catalog") |
| 56 | XCTAssertNotEqual(e.usage, e.usageKey, "\(e.usageKey) is not in the catalog") |
| 57 | } |
| 58 | XCTAssertEqual(entries.first { $0.id == "swift-cmark" }?.usage, "Formats the Assistant's replies.") |
| 59 | XCTAssertEqual(entries.first { $0.id == "dm-sans" }?.blurb, |
| 60 | "A sans serif typeface made by Colophon Foundry for DeepMind, designed to stay clear at small sizes.") |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /// The kit's `://open` (Task 1), as Base handles it. |
| 65 | @MainActor final class OpenURLTests: XCTestCase { |
| 66 | func testOpenBringsAskForwardAndAPageOpensOnTheWeb() { |
| 67 | var asked = 0 |
| 68 | var pages: [String] = [] |
| 69 | let handler = BaseOpenURL(defaults: UserDefaults(suiteName: "OpenURLTests.\(UUID())")!, showAsk: { asked += 1 }, openWeb: { pages.append($0) }) |
| 70 | handler.handle(URL(string: "works.oficina.base://open")!) |
| 71 | handler.handle(URL(string: "works.oficina.base://open?path=/store")!) |
| 72 | handler.handle(URL(string: "works.oficina.base://open?path=//evil.example/")!) |
| 73 | handler.handle(URL(string: "works.oficina.docs://open")!) |
| 74 | XCTAssertEqual(asked, 1) |
| 75 | XCTAssertEqual(pages, ["/store"]) |
| 76 | } |
| 77 | } |
| 78 | |