Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Tests/SPGlassComponentsTests/GlassToastTests.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 Testing43@testable import SPGlassComponents4445/// A clock the tests drive by hand: every `sleep` suspends until the test resumes it.46private actor ManualSleeper {47 private var waiting: [CheckedContinuation<Void, any Error>] = []48 private(set) var requested: [Duration] = []4950 func sleep(_ duration: Duration) async throws {51 requested.append(duration)52 try await withCheckedThrowingContinuation { waiting.append($0) }53 }5455 /// Suspends until `count` sleeps are pending.56 func waitForSleepers(_ count: Int) async {57 while waiting.count < count { await Task.yield() }58 }5960 /// Lets the oldest pending sleep finish.61 func resumeFirst() {62 guard !waiting.isEmpty else { return }63 waiting.removeFirst().resume()64 }6566 /// Lets every pending sleep finish.67 func resumeAll() {68 let pending = waiting69 waiting.removeAll()70 for continuation in pending { continuation.resume() }71 }72}7374@MainActor75private final class AnnouncementLog {76 var messages: [String] = []77}7879@MainActor80@Suite("GlassToast")81struct GlassToastTests {82 private let sleeper = ManualSleeper()83 private let log = AnnouncementLog()8485 private func makeCenter(capacity: Int = 3) -> GlassToastCenter {86 let sleeper = sleeper, log = log87 return GlassToastCenter(capacity: capacity, sleep: { try await sleeper.sleep($0) }, announce: { log.messages.append($0) })88 }8990 @Test func toastsAreKeptInArrivalOrder() {91 let center = makeCenter()92 center.show(GlassToast("One", id: "1"))93 center.show(GlassToast("Two", id: "2"))94 center.show(GlassToast("Three", id: "3"))95 #expect(center.toasts.map(\.id) == ["1", "2", "3"])96 }9798 @Test func theOldestToastIsDroppedWhenTheCentreIsFull() {99 let center = makeCenter(capacity: 2)100 for index in 1...5 { center.show(GlassToast("Message \(index)", id: "\(index)")) }101 #expect(center.toasts.map(\.id) == ["4", "5"])102 }103104 @Test func capacityIsAtLeastOne() {105 let center = makeCenter(capacity: 0)106 center.show(GlassToast("A", id: "a"))107 center.show(GlassToast("B", id: "b"))108 #expect(center.capacity == 1)109 #expect(center.toasts.map(\.id) == ["b"])110 }111112 @Test func aToastIsDismissedWhenItsDurationElapses() async {113 let center = makeCenter()114 center.show(GlassToast("Saved", id: "saved", duration: .seconds(5)))115 await sleeper.waitForSleepers(1)116 #expect(center.toasts.count == 1)117 #expect(await sleeper.requested == [.seconds(5)])118 await sleeper.resumeAll()119 await center.waitForTimers()120 #expect(center.toasts.isEmpty)121 }122123 @Test func eachToastHasItsOwnTimer() async {124 let center = makeCenter()125 center.show(GlassToast("First", id: "1"))126 await sleeper.waitForSleepers(1)127 center.show(GlassToast("Second", id: "2"))128 await sleeper.waitForSleepers(2)129 await sleeper.resumeFirst()130 for _ in 0..<200 where center.toasts.count == 2 { await Task.yield() }131 #expect(center.toasts.map(\.id) == ["2"])132 await sleeper.resumeAll()133 await center.waitForTimers()134 #expect(center.toasts.isEmpty)135 }136137 @Test func manualDismissalCancelsTheTimer() async {138 let center = makeCenter()139 center.show(GlassToast("Old", id: "x"))140 await sleeper.waitForSleepers(1)141 center.dismiss("x")142 #expect(center.toasts.isEmpty)143 // A new toast reuses the identifier; the cancelled timer of the old one must not remove it.144 center.show(GlassToast.persistent("New", id: "x"))145 await sleeper.resumeAll()146 for _ in 0..<200 { await Task.yield() }147 #expect(center.toasts.map(\.message) == ["New"])148 }149150 @Test func aDuplicateIdentifierReplacesTheToastInPlaceAndRestartsItsTimer() async {151 let center = makeCenter()152 center.show(GlassToast("Uploading 1 of 3", id: "upload"))153 center.show(GlassToast("Other", id: "other"))154 await sleeper.waitForSleepers(2)155 center.show(GlassToast("Uploading 2 of 3", id: "upload"))156 await sleeper.waitForSleepers(3)157 #expect(center.toasts.map(\.id) == ["upload", "other"])158 #expect(center.toasts.first?.message == "Uploading 2 of 3")159 // The first sleeper belongs to the replaced toast: letting it finish must not dismiss the new one.160 await sleeper.resumeFirst()161 for _ in 0..<200 { await Task.yield() }162 #expect(center.toasts.map(\.id) == ["upload", "other"])163 await sleeper.resumeAll()164 await center.waitForTimers()165 #expect(center.toasts.isEmpty)166 }167168 @Test func persistentToastsHaveNoTimer() async {169 let center = makeCenter()170 center.show(.persistent("Offline", id: "offline"))171 for _ in 0..<50 { await Task.yield() }172 #expect(await sleeper.requested.isEmpty)173 #expect(center.toasts.count == 1)174 center.dismissAll()175 #expect(center.toasts.isEmpty)176 }177178 @Test func dismissAllCancelsEveryTimer() async {179 let center = makeCenter()180 center.show(GlassToast("A", id: "a"))181 center.show(GlassToast("B", id: "b"))182 await sleeper.waitForSleepers(2)183 center.dismissAll()184 center.show(.persistent("Kept", id: "a"))185 await sleeper.resumeAll()186 for _ in 0..<200 { await Task.yield() }187 #expect(center.toasts.map(\.message) == ["Kept"])188 }189190 @Test func theActionRunsOnceAndDismissesTheToast() {191 let center = makeCenter()192 let counter = AnnouncementLog()193 center.show(GlassToast("Message deleted", id: "undo", action: GlassToastAction("Undo") { counter.messages.append("undo") }))194 center.performAction(of: "undo")195 center.performAction(of: "undo")196 #expect(counter.messages == ["undo"])197 #expect(center.toasts.isEmpty)198 }199200 @Test func everyToastIsAnnounced() {201 let center = makeCenter()202 center.show(GlassToast("Draft saved", title: "Mail", id: "1"))203 center.show("Copied")204 #expect(log.messages == ["Mail, Draft saved", "Copied"])205 }206207 @Test func durationsAreLongEnoughToRead() {208 #expect(GlassToast("Quick", duration: .milliseconds(100)).duration == GlassToast.minimumDuration)209 #expect(GlassToast("Plain").duration == .seconds(4))210 #expect(GlassToast("With action", action: GlassToastAction("Undo") {}).duration == .seconds(6))211 #expect(GlassToast("Long", duration: .seconds(30)).duration == .seconds(30))212 #expect(GlassToast.persistent("Stays").duration == nil)213 }214215 @Test func placementsMapToEdges() {216 #expect(GlassToastPlacement.top.alignment == .top)217 #expect(GlassToastPlacement.bottom.alignment == .bottom)218 #expect(GlassToastPlacement.top.edge == .top)219 #expect(GlassToastPlacement.bottom.edge == .bottom)220 }221}222