@testable import BaseKit import Foundation import XCTest /// P12: where each of Home's apps opens. final class OpenerTests: XCTestCase { let docs = HomeData.App(id: "docs", kind: .oficina, name: "Docs", url: URL(string: "https://fennel.docs.oficina.works/"), bundleID: "works.oficina.docs") let calls = HomeData.App(id: "calls", kind: .oficina, name: "Calls", url: URL(string: "https://fennel.calls.oficina.works/"), bundleID: "works.oficina.calls", scheme: "oficina-calls") let equipo = HomeData.App(id: "equipo", kind: .oficina, name: "Equipo", url: URL(string: "https://equipo.fennel.example/"), bundleID: "works.oficina.equipo", scheme: "works.oficina.equipo") let welcome = HomeData.App(id: "welcome", kind: .installed, name: "Welcome", url: URL(string: "https://welcome.fennel.example/"), tint: "amber") let installedDocs = URL(fileURLWithPath: "/Applications/Oficina Docs.app") func testTheMacOpensAnInstalledOficinaAppWhenSettingsSaysSo() { let installed: (String) -> URL? = { $0 == "works.oficina.docs" ? self.installedDocs : nil } XCTAssertEqual(Opener.mac(docs, preference: .app, installed: installed), .application(installedDocs)) XCTAssertEqual(Opener.mac(docs, preference: .browser, installed: installed), .url(docs.url!)) XCTAssertEqual(Opener.mac(calls, preference: .app, installed: installed), .url(calls.url!), "not installed: the web") XCTAssertEqual(Opener.mac(welcome, preference: .app, installed: { _ in self.installedDocs }), .url(welcome.url!), "Store apps open on the web") } func testTheIPhoneOpensCallsAndEquipoByTheirSchemes() { let all: (URL) -> Bool = { _ in true } XCTAssertEqual(Opener.phone(calls, canOpen: all), .url(URL(string: "oficina-calls://")!)) XCTAssertEqual(Opener.phone(equipo, canOpen: all), .url(URL(string: "works.oficina.equipo://")!)) XCTAssertEqual(Opener.phone(calls, canOpen: { _ in false }), .url(calls.url!), "not installed: the web") XCTAssertEqual(Opener.phone(docs, canOpen: all), .url(docs.url!), "no scheme yet: the web") XCTAssertEqual(Opener.phone(welcome, canOpen: all), .url(welcome.url!)) } func testInstalledNamesAndHomesGroups() throws { let home = try JSONDecoder().decode(HomeData.self, from: Fixture.data("home.json")) XCTAssertEqual(home.launchable.map(\.id), ["calendar", "calls", "welcome", "future"]) XCTAssertEqual(home.own.map(\.id), ["store"]) XCTAssertTrue(home.stepsOpen) XCTAssertEqual(Opener.installedNames(home.apps, installed: { $0 == "works.oficina.calls" ? self.installedDocs : nil }), ["Calls"]) } func testTileArtParses() { let people = #""# let box = SVGGlyph(svg: people).path(in: CGRect(x: 0, y: 0, width: 24, height: 24)).boundingRect XCTAssertEqual(box.minX, 3.5, accuracy: 0.1) XCTAssertEqual(box.minY, 6, accuracy: 0.1) XCTAssertEqual(box.maxX, 21, accuracy: 0.1) XCTAssertTrue(SVGGlyph(svg: "").path(in: CGRect(x: 0, y: 0, width: 24, height: 24)).isEmpty) } func testTheColophonShipsWithItsLicences() { let entries = BaseColophon.sections.flatMap(\.entries) XCTAssertEqual(Set(entries.map(\.id)), ["timeless-sans", "dm-sans", "keyboardshortcuts", "swift-cmark", "rastrillo-native", "sparkle"]) for e in entries where e.id != "timeless-sans" { XCTAssertGreaterThan(e.licenceText.count, 200, e.id) } // What each is and what Base uses it for come from the catalog. for e in entries { XCTAssertNotEqual(e.blurb, e.blurbKey, "\(e.blurbKey) is not in the catalog") XCTAssertNotEqual(e.usage, e.usageKey, "\(e.usageKey) is not in the catalog") } XCTAssertEqual(entries.first { $0.id == "swift-cmark" }?.usage, "Formats the Assistant's replies.") XCTAssertEqual(entries.first { $0.id == "dm-sans" }?.blurb, "A sans serif typeface made by Colophon Foundry for DeepMind, designed to stay clear at small sizes.") } } /// The kit's `://open` (Task 1), as Base handles it. @MainActor final class OpenURLTests: XCTestCase { func testOpenBringsAskForwardAndAPageOpensOnTheWeb() { var asked = 0 var pages: [String] = [] let handler = BaseOpenURL(defaults: UserDefaults(suiteName: "OpenURLTests.\(UUID())")!, showAsk: { asked += 1 }, openWeb: { pages.append($0) }) handler.handle(URL(string: "works.oficina.base://open")!) handler.handle(URL(string: "works.oficina.base://open?path=/store")!) handler.handle(URL(string: "works.oficina.base://open?path=//evil.example/")!) handler.handle(URL(string: "works.oficina.docs://open")!) XCTAssertEqual(asked, 1) XCTAssertEqual(pages, ["/store"]) } }