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/StyleCoordinatorTests.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 ORIXMapRenderer91import ORIXMapStyle92import Testing93@testable import ORIXMaps9495struct StyleCoordinatorTests {96 private func localities(_ url: URL) throws -> [(name: String, coordinate: Coordinate, inRegion: Bool)] {97 let root = try #require(try JSONSerialization.jsonObject(with: Data(contentsOf: url)) as? [String: Any])98 let features = try #require(root["features"] as? [[String: Any]])99 return features.compactMap { f in100 guard let p = f["properties"] as? [String: Any], p["kind"] as? String == "locality",101 let c = (f["geometry"] as? [String: Any])?["coordinates"] as? [Double], c.count == 2 else { return nil }102 return ((p["name"] as? String) ?? "", Coordinate(latitude: c[1], longitude: c[0]), p["inRegion"] as? Bool == true)103 }104 }105106 @Test func bundledWorldDataIsPresent() {107 #expect(FileManager.default.fileExists(atPath: StyleCoordinator.baseMapURL.path))108 #expect(FileManager.default.fileExists(atPath: StyleCoordinator.worldLabelsURL.path))109 }110111 @Test func withoutRegionsTheBundledLabelsAreUsedAsTheyAre() {112 let cache = ScratchDirectory()113 let coordinator = StyleCoordinator(controller: MapController(), cacheDirectory: cache.url)114 #expect(coordinator.worldLabels(excluding: []) == StyleCoordinator.worldLabelsURL)115 #expect((try? FileManager.default.contentsOfDirectory(atPath: cache.url.path))?.isEmpty == true)116 }117118 @Test func citiesInsideADrawnRegionAreFlaggedAndOnlyThose() throws {119 let cache = ScratchDirectory()120 let coordinator = StyleCoordinator(controller: MapController(), cacheDirectory: cache.url)121 let url = try #require(coordinator.worldLabels(excluding: [montrealBox]))122 #expect(url != StyleCoordinator.worldLabelsURL)123 #expect(url.path.hasPrefix(cache.url.path))124 let before = try localities(StyleCoordinator.worldLabelsURL), after = try localities(url)125 #expect(before.count == after.count && !before.isEmpty)126 #expect(before.allSatisfy { !$0.inRegion })127 let flagged = after.filter(\.inRegion)128 #expect(!flagged.isEmpty, "the bundled labels include at least one city inside Greater Montréal")129 #expect(flagged.allSatisfy { montrealBox.contains($0.coordinate) })130 #expect(after.filter { !$0.inRegion }.allSatisfy { !montrealBox.contains($0.coordinate) })131 }132133 @Test func oneFileIsKeptPerRegionSet() throws {134 let cache = ScratchDirectory()135 let coordinator = StyleCoordinator(controller: MapController(), cacheDirectory: cache.url)136 let first = try #require(coordinator.worldLabels(excluding: [montrealBox]))137 #expect(coordinator.worldLabels(excluding: [montrealBox]) == first)138 let second = try #require(coordinator.worldLabels(excluding: [quebecCityBox, montrealBox]))139 #expect(second != first)140 // The order of the regions does not matter, and the file of the previous set was removed.141 #expect(coordinator.worldLabels(excluding: [montrealBox, quebecCityBox]) == second)142 let files = try FileManager.default.contentsOfDirectory(atPath: cache.url.path).filter { $0.hasPrefix("world-labels-") }143 #expect(files == [second.lastPathComponent])144 }145146 @Test func aRegionInsideAnotherIsNotDrawnTwice() {147 let province = BoundingBox(minLat: 44.9, minLon: -79.8, maxLat: 62.6, maxLon: -57.1)148 let both = [region("ca-qc-montreal", montrealBox), region("ca-qc", province)]149 #expect(StyleCoordinator.drawnRegions(both).map(\.id) == ["ca-qc"])150 // Kept when it brings newer data or deeper zooms; the installed order is preserved.151 #expect(StyleCoordinator.drawnRegions([region("ca-qc-montreal", montrealBox, version: "2026.12.01"), region("ca-qc", province)]).map(\.id) == ["ca-qc-montreal", "ca-qc"])152 #expect(StyleCoordinator.drawnRegions([region("ca-qc-montreal", montrealBox, maxZoom: 16), region("ca-qc", province)]).map(\.id) == ["ca-qc-montreal", "ca-qc"])153 // Disjoint regions are all drawn.154 #expect(StyleCoordinator.drawnRegions([region("a", montrealBox), region("b", quebecCityBox)]).map(\.id) == ["a", "b"])155 #expect(StyleCoordinator.drawnRegions([]).isEmpty)156 }157158 @Test func theStyleCarriesTheBaseMapAndTheDrawnRegions() throws {159 let cache = ScratchDirectory()160 let controller = MapController()161 let spy = SpyBackend()162 controller.attach(spy)163 let coordinator = StyleCoordinator(controller: controller, cacheDirectory: cache.url)164 coordinator.apply(theme: BuiltInThemes.day, language: "fr", regions: [])165 #expect(spy.lastStyleSources["base"] != nil)166 #expect(spy.lastStyleSources.keys.filter { $0.hasPrefix("r-") }.isEmpty)167168 let province = BoundingBox(minLat: 44.9, minLon: -79.8, maxLat: 62.6, maxLon: -57.1)169 coordinator.apply(theme: BuiltInThemes.day, language: "fr", regions: [region("ca-qc-montreal", montrealBox), region("ca-qc", province), region("other", BoundingBox(minLat: 10, minLon: 10, maxLat: 11, maxLon: 11))])170 #expect(Set(spy.lastStyleSources.keys.filter { $0.hasPrefix("r-") }) == ["r-ca-qc", "r-other"])171 let labels = try #require(spy.lastStyleSources[OverlayID.worldLabels]?["data"] as? String)172 #expect(labels.contains("world-labels-"), "labels come from the flagged copy once a region is drawn")173174 // The same request again does not reload the style; a new theme does.175 let count = spy.styles.count176 coordinator.apply(theme: BuiltInThemes.day, language: "fr", regions: [region("ca-qc-montreal", montrealBox), region("ca-qc", province), region("other", BoundingBox(minLat: 10, minLon: 10, maxLat: 11, maxLon: 11))])177 #expect(spy.styles.count == count)178 coordinator.apply(theme: BuiltInThemes.midnight, language: "fr", regions: [])179 #expect(spy.styles.count == count + 1)180 }181}182