Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Tests/SPGlassMediaTests/GlassGalleryTests.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 Testing43import SPGlassCore44@testable import SPGlassMedia4546private actor CallCounter {47 private(set) var count = 048 func increment() { count += 1 }49}5051private struct LoadFailure: Error {}5253@Suite("Gallery")54struct GlassGalleryTests {55 @Test func theCacheEvictsTheLeastRecentlyUsedEntry() async {56 let cache = GlassBoundedCache<String, Int>(capacity: 2)57 await cache.insert(1, for: "a")58 await cache.insert(2, for: "b")59 #expect(await cache.value(for: "a") == 1) // "a" becomes the most recent60 await cache.insert(3, for: "c") // evicts "b"61 #expect(await cache.count == 2)62 #expect(await cache.value(for: "b") == nil)63 #expect(await cache.value(for: "a") == 1)64 #expect(await cache.value(for: "c") == 3)65 #expect(await cache.keysByRecency == ["a", "c"])66 }6768 @Test func theCacheNeverExceedsItsCapacity() async {69 let cache = GlassBoundedCache<Int, Int>(capacity: 8)70 for key in 0..<1_000 { await cache.insert(key, for: key) }71 #expect(await cache.count == 8)72 #expect(await cache.keysByRecency == Array(992..<1_000))73 await cache.insert(-1, for: 999)74 #expect(await cache.count == 8)75 #expect(await cache.value(for: 999) == -1)76 await cache.removeValue(for: 999)77 #expect(await cache.count == 7)78 await cache.removeAll()79 #expect(await cache.count == 0)80 }8182 @Test func aCapacityBelowOneIsRaisedToOne() async {83 let cache = GlassBoundedCache<String, Int>(capacity: 0)84 #expect(cache.capacity == 1)85 await cache.insert(1, for: "a")86 await cache.insert(2, for: "b")87 #expect(await cache.keysByRecency == ["b"])88 }8990 @Test func loadedImagesAreCachedAndTheLoaderRunsOnce() async {91 let cache = GlassGalleryCache(capacity: 4)92 let counter = CallCounter()93 let item = GlassGalleryItem(id: "one", description: "Sample") {94 await counter.increment()95 return Image(systemName: "photo")96 }97 #expect(await GlassGalleryLoading.load(item, cache: cache).value != nil)98 #expect(await GlassGalleryLoading.load(item, cache: cache).value != nil)99 #expect(await counter.count == 1)100 #expect(await cache.count == 1)101 }102103 @Test func localImagesBypassTheCache() async {104 let cache = GlassGalleryCache(capacity: 4)105 let item = GlassGalleryItem(id: "local", description: "Sample", image: Image(systemName: "photo"))106 #expect(await GlassGalleryLoading.load(item, cache: cache).value != nil)107 #expect(await cache.count == 0)108 }109110 @Test func aFailingLoaderGivesTheFailedStateAndCachesNothing() async {111 let cache = GlassGalleryCache(capacity: 4)112 let item = GlassGalleryItem(id: "broken", description: "Sample", load: { throw LoadFailure() })113 let state = await GlassGalleryLoading.load(item, cache: cache)114 guard case .failed(let message) = state else {115 Issue.record("Expected a failure")116 return117 }118 #expect(!message.isEmpty)119 #expect(await cache.count == 0)120 }121122 @Test func aCancelledLoadCachesNothing() async {123 let cache = GlassGalleryCache(capacity: 4)124 let item = GlassGalleryItem(id: "slow", description: "Sample") {125 try await Task.sleep(for: .seconds(30))126 return Image(systemName: "photo")127 }128 let task = Task { await GlassGalleryLoading.load(item, cache: cache) }129 task.cancel()130 let state = await task.value131 guard case .idle = state else {132 Issue.record("Expected the idle state after cancellation")133 return134 }135 #expect(await cache.count == 0)136 }137138 @Test func accessibilityLabelsGiveThePositionThenTheDescription() {139 let label = GlassGalleryLoading.accessibilityLabel(index: 1, count: 12, description: " Harbour at dusk ")140 #expect(label.contains("2"))141 #expect(label.contains("12"))142 #expect(label.hasSuffix("Harbour at dusk"))143 #expect(!GlassGalleryLoading.accessibilityLabel(index: 0, count: 1, description: " ").contains(","))144 }145146 @Test func zoomIsClamped() {147 #expect(GlassGalleryZoom.clampedScale(0.2) == 1)148 #expect(GlassGalleryZoom.clampedScale(2) == 2)149 #expect(GlassGalleryZoom.clampedScale(40) == 4)150 #expect(GlassGalleryZoom.clampedScale(.nan) == 1)151 #expect(GlassGalleryZoom.toggledScale(1) == GlassGalleryZoom.doubleTapScale)152 #expect(GlassGalleryZoom.toggledScale(3) == 1)153 }154155 @Test func panningStaysInsideTheZoomedImage() {156 let container = CGSize(width: 300, height: 200)157 #expect(GlassGalleryZoom.clampedOffset(CGSize(width: 80, height: 80), scale: 1, container: container) == .zero)158 #expect(GlassGalleryZoom.clampedOffset(CGSize(width: 500, height: -500), scale: 2, container: container) == CGSize(width: 150, height: -100))159 #expect(GlassGalleryZoom.clampedOffset(CGSize(width: 20, height: 10), scale: 2, container: container) == CGSize(width: 20, height: 10))160 #expect(GlassGalleryZoom.clampedOffset(CGSize(width: CGFloat.nan, height: 0), scale: 2, container: container) == .zero)161 }162163 @Test func navigationStopsAtBothEnds() {164 #expect(GlassGalleryZoom.index(0, movedBy: -1, count: 5) == 0)165 #expect(GlassGalleryZoom.index(0, movedBy: 1, count: 5) == 1)166 #expect(GlassGalleryZoom.index(4, movedBy: 1, count: 5) == 4)167 #expect(GlassGalleryZoom.index(9, movedBy: 0, count: 5) == 4)168 #expect(GlassGalleryZoom.index(2, movedBy: 1, count: 0) == 0)169 }170}171