Repository Public BSD-3-Clause
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Maps9// File : Apps/ORIXMaps/Tests/ORIXMapsTests/Support.swift10// Module : ORIXMapsTests11// Repository : https://git.spboucher.ai/orix-maps12// Created : 2026-11-2513// Last Modified : 2026-11-2514//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author : Simon-Pierre Boucher20// Position : Professor of Finance21// Institution : Université du Québec en Outaouais (UQO)22// Department : Département des sciences administratives23// Organization : SP Boucher24//25// Website : https://spboucher.ai26// Git : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General : info@spboucher.ai33// Professional : contact@spboucher.ai34// Direct : simon@spboucher.ai35// Research : research@spboucher.ai36// Development : dev@spboucher.ai37// Support : support@spboucher.ai38// Security/Admin : admin@spboucher.ai39// Notifications : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification : Public66// Distribution : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License : Proprietary, all rights reserved (see license file)80// License File : LICENSE81// Maintainer : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import Foundation89import ORIXGeoCore90import ORIXLocation91import ORIXMapRenderer92import Testing93@testable import ORIXMaps9495/// A temporary directory removed when the value goes away.96final class ScratchDirectory {97 let url: URL98 init() {99 url = FileManager.default.temporaryDirectory.appending(path: "orix-maps-tests-\(UUID().uuidString)", directoryHint: .isDirectory)100 try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)101 }102 deinit { try? FileManager.default.removeItem(at: url) }103104 func file(_ name: String, _ content: String) -> URL { file(name, Data(content.utf8)) }105 func file(_ name: String, _ data: Data) -> URL {106 let out = url.appending(path: name)107 try? data.write(to: out)108 return out109 }110}111112/// Records what the application asks the map engine to draw.113final class SpyBackend: MapBackend {114 var styles: [String] = []115 var overlays: [String: Data] = [:]116 var fits: [BoundingBox] = []117 /// Camera moves asked of the engine, with their animation duration.118 var cameras: [(camera: MapCamera, duration: TimeInterval)] = []119 func apply(camera: MapCamera, duration: TimeInterval) { cameras.append((camera, duration)) }120 func fit(bounds: BoundingBox, insets: MapInsets, maxZoom: Double, duration: TimeInterval) { fits.append(bounds) }121 func apply(styleJSON: String) { styles.append(styleJSON) }122 func apply(overlay id: String, geoJSON: Data) { overlays[id] = geoJSON }123 func apply(contentInsets: MapInsets, animated: Bool) {}124 func apply(frameRate: MapController.FrameRate) {}125 func coordinate(at point: CGPoint) -> Coordinate? { nil }126 func point(for coordinate: Coordinate) -> CGPoint? { nil }127 var visibleBounds: BoundingBox? { nil }128129 /// Number of features currently in an overlay source.130 func featureCount(_ id: String) -> Int {131 guard let data = overlays[id], let root = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else { return 0 }132 return (root["features"] as? [Any])?.count ?? 0133 }134135 /// The sources of the last style, by identifier.136 var lastStyleSources: [String: [String: Any]] {137 guard let text = styles.last, let root = try? JSONSerialization.jsonObject(with: Data(text.utf8)) as? [String: Any] else { return [:] }138 return (root["sources"] as? [String: [String: Any]]) ?? [:]139 }140}141142/// Waits until a condition on main-actor state holds, polling. Returns false on timeout.143func eventually(timeout: Duration = .seconds(10), _ condition: () -> Bool) async -> Bool {144 let deadline = ContinuousClock.now + timeout145 while ContinuousClock.now < deadline {146 if condition() { return true }147 try? await Task.sleep(for: .milliseconds(20))148 }149 return condition()150}151152/// Root of the repository, from this file's location (the simulator sees the host's files).153nonisolated let repositoryRoot = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()154 .deletingLastPathComponent().deletingLastPathComponent()155156func region(_ id: String, _ box: BoundingBox, version: String = "2026.11.24", maxZoom: Int = 15, routing: URL? = nil) -> MapRegion {157 MapRegion(id: id, nameEN: id, nameFR: id, bounds: box, dataVersion: version, dataDate: nil, minZoom: 0, maxZoom: maxZoom,158 mapURL: URL(fileURLWithPath: "/nonexistent/\(id).pmtiles"), searchURL: nil, routingURL: routing, installedBytes: 0, attributions: [])159}160161let montrealBox = BoundingBox(minLat: 45.15, minLon: -74.35, maxLat: 45.95, maxLon: -73.15)162let quebecCityBox = BoundingBox(minLat: 46.60, minLon: -71.75, maxLat: 47.10, maxLon: -70.85)163let downtownMontreal = Coordinate(latitude: 45.5017, longitude: -73.5673)164let olympicStadium = Coordinate(latitude: 45.5579, longitude: -73.5515)165let quebecCity = Coordinate(latitude: 46.8139, longitude: -71.2080)166167/// A stand-in for the device's position source: a fixed permission state, counting what is asked of it.168final class FakeDeviceProvider: LocationProvider {169 var authorization: LocationAuthorization170 var onAuthorization: ((LocationAuthorization) -> Void)?171 var onFix: ((LocationFix) -> Void)?172 var onHeading: ((HeadingFix) -> Void)?173 var onError: ((LocationError) -> Void)?174 private(set) var authorizationRequests = 0175 private(set) var started: [LocationUsage] = []176 init(authorization: LocationAuthorization) { self.authorization = authorization }177 func requestAuthorization() { authorizationRequests += 1 }178 func start(_ usage: LocationUsage) { started.append(usage) }179 func stop() {}180 func startHeading() {}181 func stopHeading() {}182}183