Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Tests/SPGlassComponentsTests/GlassAlertTests.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@Suite("GlassAlert")46struct GlassAlertTests {47 private func validated(_ buttons: [GlassAlertButton], style: GlassAlertStyle = .alert) -> [GlassAlertButton] {48 GlassAlertValidator.validated(buttons, style: style, okTitle: "OK", cancelTitle: "Cancel")49 }5051 private func summary(_ buttons: [GlassAlertButton]) -> [String] {52 buttons.map { "\($0.title):\($0.role.rawValue)" }53 }5455 @Test func anAlertWithoutButtonsGetsOK() {56 #expect(summary(validated([])) == ["OK:default"])57 }5859 @Test func aConfirmationWithoutButtonsGetsCancel() {60 #expect(summary(validated([], style: .confirmation)) == ["Cancel:cancel"])61 }6263 @Test func onlyTheFirstCancelButtonKeepsItsRole() {64 let result = validated([.cancel("Not now"), GlassAlertButton("Save"), .cancel("Later")])65 #expect(summary(result) == ["Save:default", "Later:default", "Not now:cancel"])66 #expect(result.filter { $0.role == .cancel }.count == 1)67 }6869 @Test func aDestructiveActionAlwaysComesWithCancel() {70 let result = validated([.destructive("Delete") {}], style: .confirmation)71 #expect(summary(result) == ["Delete:destructive", "Cancel:cancel"])72 }7374 @Test func buttonsAreOrderedRegularThenDestructiveThenCancel() {75 let result = validated([.cancel(), .destructive("Delete") {}, GlassAlertButton("Archive"), .destructive("Erase all") {}, GlassAlertButton("Move")])76 #expect(result.map(\.role) == [.default, .default, .destructive, .destructive, .cancel])77 #expect(result.map(\.title).prefix(4) == ["Archive", "Move", "Delete", "Erase all"])78 }7980 @Test func theDefaultActionIsNeverDestructive() {81 let onlyDestructive = validated([.destructive("Delete") {}])82 #expect(GlassAlertValidator.defaultButtonIndex(in: onlyDestructive).map { onlyDestructive[$0].role } == .cancel)8384 let mixed = validated([.destructive("Delete") {}, GlassAlertButton("Keep"), .cancel()])85 #expect(GlassAlertValidator.defaultButtonIndex(in: mixed).map { mixed[$0].title } == "Keep")8687 #expect(GlassAlertValidator.defaultButtonIndex(in: []) == nil)88 }8990 @Test func validationKeepsActions() async {91 let flag = Flag()92 let result = validated([.cancel("First") {}, .cancel("Second") { flag.value = true }])93 let demoted = result.first { $0.title == "Second" }94 await demoted?.action()95 #expect(demoted?.role == .default)96 #expect(await flag.value)97 }9899 @Test func rolesMapToSystemRoles() {100 #expect(GlassAlertButtonRole.default.buttonRole == nil)101 #expect(GlassAlertButtonRole.cancel.buttonRole == .cancel)102 #expect(GlassAlertButtonRole.destructive.buttonRole == .destructive)103 }104105 @Test func anAlertValidatesItsOwnButtons() {106 let alert = GlassAlert(title: "Delete?", style: .confirmation, buttons: [.destructive("Delete") {}])107 #expect(alert.validatedButtons.map(\.role) == [.destructive, .cancel])108 #expect(alert.buttons.count == 1)109 }110}111112@MainActor113private final class Flag {114 var value = false115}116