Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Tests/SPGlassDataTests/GlassTimelineTests.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 Foundation42import Testing43@testable import SPGlassData4445@Suite("GlassTimeline")46struct GlassTimelineTests {47 static func calendar(_ zone: String) -> Calendar {48 var calendar = Calendar(identifier: .gregorian)49 calendar.timeZone = TimeZone(identifier: zone) ?? .gmt50 return calendar51 }5253 let toronto = GlassTimelineTests.calendar("America/Toronto")54 let utc = GlassTimelineTests.calendar("UTC")55 let tokyo = GlassTimelineTests.calendar("Asia/Tokyo")5657 func date(_ year: Int, _ month: Int, _ day: Int, _ hour: Int, _ minute: Int = 0, in calendar: Calendar) throws -> Date {58 try #require(calendar.date(from: DateComponents(year: year, month: month, day: day, hour: hour, minute: minute)))59 }6061 func event(_ id: String, _ date: Date) -> GlassTimelineEvent {62 GlassTimelineEvent(id: id, date: date, title: "Event \(id)")63 }6465 @Test func sortsAscendingAndDescending() throws {66 let events = [67 event("b", try date(2026, 5, 2, 9, in: utc)),68 event("c", try date(2026, 5, 3, 9, in: utc)),69 event("a", try date(2026, 5, 1, 9, in: utc)),70 ]71 #expect(GlassTimelineLogic.sorted(events, order: .ascending).map(\.id) == ["a", "b", "c"])72 #expect(GlassTimelineLogic.sorted(events, order: .descending).map(\.id) == ["c", "b", "a"])73 }7475 @Test("Events with the same date keep their original order in both directions")76 func sortIsStable() throws {77 let instant = try date(2026, 5, 1, 9, in: utc)78 let events = [event("x", instant), event("y", instant), event("z", instant)]79 #expect(GlassTimelineLogic.sorted(events, order: .ascending).map(\.id) == ["x", "y", "z"])80 #expect(GlassTimelineLogic.sorted(events, order: .descending).map(\.id) == ["x", "y", "z"])81 }8283 @Test func groupsByCalendarDay() throws {84 let events = [85 event("1", try date(2026, 5, 1, 8, in: toronto)),86 event("2", try date(2026, 5, 1, 22, in: toronto)),87 event("3", try date(2026, 5, 2, 7, in: toronto)),88 event("4", try date(2026, 5, 4, 12, in: toronto)),89 ]90 let groups = GlassTimelineLogic.groups(events, calendar: toronto)91 #expect(groups.map { $0.events.map(\.id) } == [["1", "2"], ["3"], ["4"]])92 #expect(groups.map(\.day) == [93 try date(2026, 5, 1, 0, in: toronto), try date(2026, 5, 2, 0, in: toronto), try date(2026, 5, 4, 0, in: toronto),94 ])95 let descending = GlassTimelineLogic.groups(events, order: .descending, calendar: toronto)96 #expect(descending.map { $0.events.map(\.id) } == [["4"], ["3"], ["2", "1"]])97 #expect(Set(groups.map(\.id)).count == groups.count)98 }99100 @Test func emptyInputHasNoGroup() {101 #expect(GlassTimelineLogic.groups([], calendar: utc).isEmpty)102 }103104 @Test("The same instants fall on different days in different time zones")105 func timeZonesDecideTheDay() throws {106 // 03:30 and 05:30 UTC on March 10: both March 10 in UTC and Tokyo, but March 9 (23:30) and March 10 (01:30) in Toronto.107 let events = [108 event("late", try date(2026, 3, 10, 3, 30, in: utc)),109 event("later", try date(2026, 3, 10, 5, 30, in: utc)),110 ]111 #expect(GlassTimelineLogic.groups(events, calendar: utc).count == 1)112 #expect(GlassTimelineLogic.groups(events, calendar: tokyo).count == 1)113 let local = GlassTimelineLogic.groups(events, calendar: toronto)114 #expect(local.count == 2)115 #expect(local.first?.day == (try date(2026, 3, 9, 0, in: toronto)))116 }117118 @Test("A 23-hour day (spring forward) is one group")119 func springForward() throws {120 // In Toronto, clocks jump from 02:00 to 03:00 on 2026-03-08.121 let events = [122 event("before", try date(2026, 3, 8, 0, 30, in: toronto)),123 event("after", try date(2026, 3, 8, 23, 30, in: toronto)),124 event("next", try date(2026, 3, 9, 0, 15, in: toronto)),125 ]126 #expect(events[1].date.timeIntervalSince(events[0].date) == 22 * 3600)127 let groups = GlassTimelineLogic.groups(events, calendar: toronto)128 #expect(groups.map { $0.events.map(\.id) } == [["before", "after"], ["next"]])129 }130131 @Test("A 25-hour day (fall back) is one group")132 func fallBack() throws {133 // In Toronto, clocks go back from 02:00 to 01:00 on 2026-11-01.134 let start = try date(2026, 11, 1, 0, 0, in: toronto)135 let events = [136 event("start", start),137 event("repeated-hour", start.addingTimeInterval(1.5 * 3600)),138 event("second-pass", start.addingTimeInterval(2.5 * 3600)),139 event("end", start.addingTimeInterval(24.5 * 3600)),140 event("tomorrow", start.addingTimeInterval(25.5 * 3600)),141 ]142 let groups = GlassTimelineLogic.groups(events, calendar: toronto)143 #expect(groups.map { $0.events.map(\.id) } == [["start", "repeated-hour", "second-pass", "end"], ["tomorrow"]])144 #expect(groups.last?.day == (try date(2026, 11, 2, 0, in: toronto)))145 }146147 @Test func dayTitlesAreLocalised() throws {148 let day = try date(2026, 3, 9, 0, in: toronto)149 #expect(GlassTimelineLogic.dayTitle(day, calendar: toronto, locale: Locale(identifier: "en_US")) == "Monday, March 9, 2026")150 let french = GlassTimelineLogic.dayTitle(day, calendar: toronto, locale: Locale(identifier: "fr_CA"))151 #expect(french.contains("lundi"))152 #expect(french.contains("mars"))153 }154155 @Test func dayTitlesUseTheCalendarTimeZone() throws {156 // Midnight in Toronto is already the afternoon of the same day in Tokyo, and the evening before in Los Angeles.157 let day = try date(2026, 3, 9, 0, in: toronto)158 let english = Locale(identifier: "en_US")159 #expect(GlassTimelineLogic.dayTitle(day, calendar: tokyo, locale: english) == "Monday, March 9, 2026")160 #expect(GlassTimelineLogic.dayTitle(day, calendar: GlassTimelineTests.calendar("America/Los_Angeles"), locale: english) == "Sunday, March 8, 2026")161 }162163 @Test func timesUseTheCalendarTimeZone() throws {164 let instant = try date(2026, 5, 1, 14, 5, in: utc)165 let french = Locale(identifier: "fr_FR")166 #expect(GlassTimelineLogic.timeText(instant, calendar: utc, locale: french) == "14:05")167 #expect(GlassTimelineLogic.timeText(instant, calendar: tokyo, locale: french) == "23:05")168 }169}170