SPB Git

Repository Public MIT

GlassMapsTests.swift 14.0 KB 307 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Tests/SPGlassMapsTests/GlassMapsTests.swift8// Module           : Tests9// Repository       : https://git.spboucher.ai/sp-glasskit10// Project Website  : https://glass.spboucher.ai11// Created          : 2026-12-0212// Last Modified    : 2026-12-0213//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import SwiftUI42import Observation43import Testing44import SPGlassCore45@testable import SPGlassMaps46#if canImport(MapKit)47import MapKit48#endif49#if canImport(AppKit)50import AppKit51#endif5253/// A map engine without any map framework, as a proprietary renderer would provide.54@MainActor55@Observable56final class FakeMapEngine: GlassMapEngine {57    var heading: Double? = 9058    var zoomLevel = 359    var centeredOnUser = false60    var canZoomIn: Bool { zoomLevel < 4 }61    var canZoomOut: Bool { zoomLevel > 1 }62    func zoomIn() { if canZoomIn { zoomLevel += 1 } }63    func zoomOut() { if canZoomOut { zoomLevel -= 1 } }64    func resetNorth() { heading = 0 }65    func centerOnUser() { centeredOnUser = true }66}6768@Suite("Map controls and engine")69struct GlassMapControlsTests {70    @MainActor71    @Test func anEngineWithoutMapKitDrivesTheControls() {72        let engine = FakeMapEngine()73        engine.zoomIn()74        #expect(!engine.canZoomIn)75        engine.zoomIn()76        #expect(engine.zoomLevel == 4)77        engine.zoomOut(); engine.zoomOut(); engine.zoomOut(); engine.zoomOut()78        #expect(engine.zoomLevel == 1)79        #expect(!engine.canZoomOut)80        engine.resetNorth()81        #expect(engine.heading == 0)82        _ = GlassMapControls(engine: engine)83        _ = GlassMapControls(onZoomIn: {}, onZoomOut: {})84    }8586    @Test func zoomStepsStayInsideTheRange() {87        let range = 100.0...10_000.088        #expect(GlassMapZoom.distance(after: .in, from: 1_000, in: range) == 500)89        #expect(GlassMapZoom.distance(after: .out, from: 1_000, in: range) == 2_000)90        #expect(GlassMapZoom.distance(after: .in, from: 150, in: range) == 100)91        #expect(GlassMapZoom.distance(after: .out, from: 8_000, in: range) == 10_000)92        #expect(GlassMapZoom.distance(after: .in, from: 1_000, factor: 4, in: range) == 250)93        #expect(GlassMapZoom.distance(after: .in, from: 1_000, factor: 0.5, in: range) == 500)94        #expect(GlassMapZoom.distance(after: .in, from: .nan, in: range) == 5_000)95        #expect(!GlassMapZoom.canZoomIn(distance: 100, in: range))96        #expect(GlassMapZoom.canZoomIn(distance: 101, in: range))97        #expect(!GlassMapZoom.canZoomOut(distance: 10_000, in: range))98        #expect(GlassMapZoom.canZoomOut(distance: 500, in: range))99        #expect(!GlassMapZoom.canZoomOut(distance: .nan, in: range))100    }101102    #if canImport(MapKit)103    @MainActor104    @Test func theMapKitAdapterZoomsAndResetsThroughTheCameraPosition() {105        let start = MapCamera(centerCoordinate: CLLocationCoordinate2D(latitude: 45.5, longitude: -73.6), distance: 4_000, heading: 135, pitch: 0)106        let engine = GlassMapKitEngine(position: .camera(start), distanceRange: 1_000...8_000)107        #expect(engine.heading == 135)108        #expect(engine.canZoomIn && engine.canZoomOut)109        engine.zoomIn()110        #expect(engine.camera?.distance == 2_000)111        engine.zoomIn()112        #expect(engine.camera?.distance == 1_000)113        #expect(!engine.canZoomIn)114        engine.zoomOut(); engine.zoomOut(); engine.zoomOut(); engine.zoomOut()115        #expect(engine.camera?.distance == 8_000)116        #expect(!engine.canZoomOut)117        engine.resetNorth()118        #expect(engine.heading == 0)119        #expect(engine.position.camera?.heading == 0)120        #expect(engine.position.camera?.centerCoordinate.latitude == 45.5)121    }122123    @MainActor124    @Test func theMapKitAdapterIsInertUntilTheMapReportsACamera() {125        let engine = GlassMapKitEngine()126        #expect(engine.heading == nil)127        #expect(!engine.canZoomIn && !engine.canZoomOut)128        engine.zoomIn()129        engine.resetNorth()130        #expect(engine.camera == nil)131        engine.cameraChanged(MapCamera(centerCoordinate: CLLocationCoordinate2D(latitude: 0, longitude: 0), distance: 5_000, heading: -90))132        #expect(engine.heading == 270)133        #expect(engine.canZoomIn)134    }135    #endif136}137138@Suite("Compass")139struct GlassCompassTests {140    @Test func headingsAreNormalisedToOneTurn() {141        #expect(GlassHeading.normalized(0) == 0)142        #expect(GlassHeading.normalized(360) == 0)143        #expect(GlassHeading.normalized(725) == 5)144        #expect(GlassHeading.normalized(-90) == 270)145        #expect(GlassHeading.normalized(-720) == 0)146        #expect(GlassHeading.normalized(.nan) == 0)147        #expect(GlassHeading.normalized(.infinity) == 0)148        #expect(GlassHeading.normalized(-0.0000000000000001) < 360)149    }150151    @Test func rotationTakesTheShortestArc() {152        #expect(GlassHeading.shortestDelta(from: 350, to: 10) == 20)153        #expect(GlassHeading.shortestDelta(from: 10, to: 350) == -20)154        #expect(GlassHeading.shortestDelta(from: 0, to: 180) == -180)155        #expect(GlassHeading.shortestDelta(from: 90, to: 90) == 0)156        #expect(GlassHeading.shortestDelta(from: 0, to: 179) == 179)157        #expect(GlassHeading.shortestDelta(from: 720, to: -350) == 10)158    }159160    @Test func theNeedleNeverMakesAFullTurnAcrossNorth() {161        // Heading 350° means the needle sits at -350° ≡ 10°. Moving to heading 10° must travel 20°, not 340°.162        var rotation = GlassHeading.needleRotation(current: 0, heading: 350)163        #expect(rotation == 10)164        rotation = GlassHeading.needleRotation(current: rotation, heading: 10)165        #expect(rotation == -10)166        rotation = GlassHeading.needleRotation(current: rotation, heading: 0)167        #expect(rotation == 0)168        // Many small clockwise steps accumulate instead of wrapping.169        var accumulated = 0.0170        for heading in stride(from: 0.0, through: 720, by: 30) {171            accumulated = GlassHeading.needleRotation(current: accumulated, heading: heading)172        }173        #expect(accumulated == -720)174    }175176    @Test func theAccessibilityValueGivesDegreesOrUnavailable() {177        #expect(GlassHeading.displayDegrees(359.6) == 0)178        #expect(GlassHeading.displayDegrees(-90) == 270)179        #expect(GlassHeading.accessibilityValue(270).contains("270"))180        #expect(GlassHeading.accessibilityValue(nil) == GlassL10n.string(.compassUnavailable))181        #expect(GlassHeading.accessibilityValue(.nan) == GlassL10n.string(.compassUnavailable))182        #expect(GlassCompassSymbols.needle(isAvailable: true) != GlassCompassSymbols.needle(isAvailable: false))183    }184}185186@Suite("Location button")187struct GlassLocationButtonTests {188    @Test func everyStateHasASymbolAndALabel() {189        for state in GlassLocationState.allCases {190            let presentation = GlassLocationPresentation(state)191            #expect(!presentation.symbolName.isEmpty)192            #expect(!presentation.label.isEmpty)193        }194    }195196    @Test func statesMapToTheirPresentation() {197        #expect(GlassLocationPresentation(.idle).labelKey == .locate)198        #expect(GlassLocationPresentation(.locating).labelKey == .locating)199        #expect(GlassLocationPresentation(.locating).isBusy)200        #expect(GlassLocationPresentation(.following).labelKey == .locationFollowing)201        #expect(GlassLocationPresentation(.following).isEmphasized)202        #expect(GlassLocationPresentation(.following).symbolName == "location.fill")203        #expect(GlassLocationPresentation(.denied).labelKey == .locationDenied)204        #expect(GlassLocationPresentation(.denied).symbolName == "location.slash")205        #expect(GlassLocationPresentation(.unavailable).labelKey == .locationUnavailable)206    }207208    @Test func onlyUnavailableDisablesTheButton() {209        #expect(GlassLocationState.allCases.filter { !GlassLocationPresentation($0).isEnabled } == [.unavailable])210    }211212    @Test func deniedAndUnavailableDifferFromIdleBySymbolNotOnlyByColour() {213        let idle = GlassLocationPresentation(.idle)214        #expect(GlassLocationPresentation(.denied).symbolName != idle.symbolName)215        #expect(GlassLocationPresentation(.unavailable).symbolName != idle.symbolName)216        #expect(GlassLocationPresentation(.denied).label != GlassLocationPresentation(.unavailable).label)217    }218}219220@Suite("Map pin")221struct GlassMapPinTests {222    private let english = Locale(identifier: "en_US")223224    @Test func clusterCountsAreCapped() {225        #expect(GlassMapPinFormatting.clusterText(count: 7, locale: english) == "7")226        #expect(GlassMapPinFormatting.clusterText(count: 99, locale: english) == "99")227        #expect(GlassMapPinFormatting.clusterText(count: 100, locale: english) == "99+")228        #expect(GlassMapPinFormatting.clusterText(count: 5_000, cap: 999, locale: english) == "999+")229        #expect(GlassMapPinFormatting.clusterText(count: 1_500, cap: 9_999, locale: english) == "1,500")230        #expect(GlassMapPinFormatting.clusterText(count: -3, locale: english) == "0")231        #expect(GlassMapPinFormatting.clusterText(count: 5, cap: 0, locale: english) == "1+")232    }233234    @Test func markerTextIsShort() {235        #expect(GlassMapPinFormatting.markerText("  A ") == "A")236        #expect(GlassMapPinFormatting.markerText("1234567") == "1234")237        #expect(GlassMapPinFormatting.markerText("👩‍👩‍👧‍👦🇨🇦éa̐b") == "👩‍👩‍👧‍👦🇨🇦éa̐")238        #expect(GlassMapPinFormatting.markerText("") == "")239    }240241    @Test func accessibilityLabelsPreferTheTitle() {242        #expect(GlassMapPinFormatting.accessibilityLabel(content: .symbol("fork.knife"), title: "Chez Sample") == "Chez Sample")243        #expect(GlassMapPinFormatting.accessibilityLabel(content: .text("$$"), title: nil) == "$$")244        #expect(GlassMapPinFormatting.accessibilityLabel(content: .text("$$"), title: "Mid-range") == "Mid-range")245        let cluster = GlassMapPinFormatting.accessibilityLabel(content: .cluster(count: 132, cap: 99), title: nil)246        #expect(cluster.contains("132"))247        let named = GlassMapPinFormatting.accessibilityLabel(content: .cluster(count: 4, cap: 99), title: "Old Port")248        #expect(named.hasPrefix("Old Port"))249        #expect(named.contains("4"))250    }251}252253@Suite("Map panel")254struct GlassMapPanelTests {255    @Test func compactWidthsUseTheSheetAndEverythingElseTheSidePanel() {256        #expect(GlassMapPanelLayout.form(isCompactWidth: true, platform: .iOS) == .sheet)257        #expect(GlassMapPanelLayout.form(isCompactWidth: true, platform: .iPadOS) == .sheet)258        #expect(GlassMapPanelLayout.form(isCompactWidth: false, platform: .iOS) == .side)259        #expect(GlassMapPanelLayout.form(isCompactWidth: false, platform: .iPadOS) == .side)260        #expect(GlassMapPanelLayout.form(isCompactWidth: true, platform: .macOS) == .side)261        #expect(GlassMapPanelLayout.form(isCompactWidth: false, platform: .macOS) == .side)262    }263264    @Test func theMapStaysInteractiveBelowTheFullDetent() {265        #expect(GlassMapPanelLayout.isMapInteractive(at: .peek))266        #expect(GlassMapPanelLayout.isMapInteractive(at: .half))267        #expect(!GlassMapPanelLayout.isMapInteractive(at: .full))268        #expect(GlassMapPanelLayout.largestInteractiveDetent == .half)269    }270271    @Test func detentsRoundTripThroughTheSystemDetents() {272        for detent in GlassMapPanelDetent.allCases {273            #expect(GlassMapPanelLayout.detent(for: GlassMapPanelLayout.presentationDetent(detent)) == detent)274        }275        #expect(GlassMapPanelLayout.detent(for: .fraction(0.2)) == .half)276        #expect(Set(GlassMapPanelDetent.allCases.map(GlassMapPanelLayout.presentationDetent)).count == 3)277    }278279    @Test func theSidePanelWidthIsBounded() {280        #expect(GlassMapPanelLayout.sideWidth(container: 1_000) == 340)281        #expect(GlassMapPanelLayout.sideWidth(container: 700) == 280)282        #expect(GlassMapPanelLayout.sideWidth(container: 2_000) == 380)283        #expect(GlassMapPanelLayout.sideWidth(container: 250) == 218)284        #expect(GlassMapPanelLayout.sideWidth(container: 10) == 0)285    }286287    @Test func collapsingTogglesBetweenPeekAndHalf() {288        #expect(GlassMapPanelLayout.toggled(.peek) == .half)289        #expect(GlassMapPanelLayout.toggled(.half) == .peek)290        #expect(GlassMapPanelLayout.toggled(.full) == .peek)291    }292}293294#if canImport(AppKit)295@Suite("Map symbols")296struct GlassMapSymbolTests {297    @Test func everySymbolExistsInSFSymbols() {298        var names = GlassLocationState.allCases.map { GlassLocationPresentation($0).symbolName }299        names += [GlassCompassSymbols.needle(isAvailable: true), GlassCompassSymbols.needle(isAvailable: false)]300        names += ["plus", "minus", "chevron.up", "chevron.down", "xmark", "checkmark", "magnifyingglass", GlassMapResult(id: "x", title: "x").systemImage]301        for name in names {302            #expect(NSImage(systemSymbolName: name, accessibilityDescription: nil) != nil, "Unknown SF Symbol \(name)")303        }304    }305}306#endif307
Files main
Menu

Theme