SPB Git

Repository Public BSD-3-Clause

ThemeStoreTests.swift 10.6 KB 240 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Maps9// File             : Apps/ORIXMaps/Tests/ORIXMapsTests/ThemeStoreTests.swift10// Module           : ORIXMapsTests11// Repository       : https://git.spboucher.ai/orix-maps12// Created          : 2026-11-2513// Last Modified    : 2026-11-2514//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : Proprietary, all rights reserved (see license file)80// License File     : LICENSE81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import Foundation89import ORIXMapStyle90import Testing91@testable import ORIXMaps9293struct ThemeStoreTests {94    @Test func startsWithTheBuiltInThemesOnly() {95        let dir = ScratchDirectory()96        let store = ThemeStore(directory: dir.url)97        #expect(store.customThemes.isEmpty)98        #expect(store.all.map(\.id) == BuiltInThemes.all.map(\.id))99        #expect(BuiltInThemes.all.count == 8)100        #expect(store.isBuiltIn("orix.day") && !store.isBuiltIn("user.x"))101        #expect(store.theme(id: "orix.midnight")?.isDark == true)102        #expect(store.theme(id: "nope") == nil)103    }104105    @Test func savingABuiltInCreatesAUserCopyAndLeavesTheOriginal() throws {106        let dir = ScratchDirectory()107        let store = ThemeStore(directory: dir.url)108        var edited = BuiltInThemes.day109        edited.roadWidthScale = 1.7110        let saved = try store.save(edited)111        #expect(saved.id.hasPrefix("user."))112        #expect(store.customThemes.map(\.id) == [saved.id])113        #expect(store.theme(id: "orix.day") == BuiltInThemes.day)114        #expect(store.theme(id: saved.id)?.roadWidthScale == 1.7)115    }116117    @Test func savingAUserThemeReplacesIt() throws {118        let dir = ScratchDirectory()119        let store = ThemeStore(directory: dir.url)120        var copy = try store.duplicate(BuiltInThemes.day, name: "Mine")121        copy.labelDensity = 0.1122        let again = try store.save(copy)123        #expect(again.id == copy.id)124        #expect(store.customThemes.count == 1)125        #expect(store.theme(id: copy.id)?.labelDensity == 0.1)126    }127128    @Test func savedValuesAreSanitized() throws {129        let dir = ScratchDirectory()130        let store = ThemeStore(directory: dir.url)131        var wild = BuiltInThemes.day132        wild.roadWidthScale = 40; wild.labelDensity = -3; wild.name = "   "133        let saved = try store.save(wild)134        #expect(saved.roadWidthScale == 2 && saved.labelDensity == 0 && saved.name == "Theme")135    }136137    @Test func duplicateGivesAnIndependentNamedCopy() throws {138        let dir = ScratchDirectory()139        let store = ThemeStore(directory: dir.url)140        let a = try store.duplicate(BuiltInThemes.midnight, name: "Night drive")141        let b = try store.duplicate(a, name: "Night drive 2")142        #expect(a.id != b.id && a.id.hasPrefix("user.") && b.id.hasPrefix("user."))143        #expect(a.name == "Night drive" && b.name == "Night drive 2")144        #expect(a.isDark && a.colors == BuiltInThemes.midnight.colors)145        #expect(store.customThemes.map(\.name) == ["Night drive", "Night drive 2"])146    }147148    @Test func deleteRemovesUserThemesAndNeverBuiltInOnes() throws {149        let dir = ScratchDirectory()150        let store = ThemeStore(directory: dir.url)151        let copy = try store.duplicate(BuiltInThemes.day, name: "Mine")152        store.delete(id: "orix.day")153        #expect(store.theme(id: "orix.day") != nil)154        #expect(store.all.count == BuiltInThemes.all.count + 1)155        store.delete(id: copy.id)156        #expect(store.customThemes.isEmpty)157        #expect((try? FileManager.default.contentsOfDirectory(atPath: dir.url.path))?.isEmpty == true)158    }159160    @Test func themesAreReadBackFromDisk() throws {161        let dir = ScratchDirectory()162        let copy = try ThemeStore(directory: dir.url).duplicate(BuiltInThemes.terrain, name: "Hills")163        let reopened = ThemeStore(directory: dir.url)164        #expect(reopened.customThemes == [copy])165    }166167    @Test func filesThatAreNotUserThemesAreIgnoredOnLoad() throws {168        let dir = ScratchDirectory()169        _ = dir.file("user.broken.orixtheme", "{ not a theme")170        _ = dir.file("orix.day.orixtheme", String(decoding: try ThemeDocument(theme: BuiltInThemes.midnight).exported(), as: UTF8.self))171        _ = dir.file("notes.txt", "hello")172        let store = ThemeStore(directory: dir.url)173        #expect(store.customThemes.isEmpty)174        #expect(store.theme(id: "orix.day") == BuiltInThemes.day)175    }176177    @Test func exportThenImportRoundTrips() throws {178        let dir = ScratchDirectory()179        let store = ThemeStore(directory: dir.url)180        var mine = try store.duplicate(BuiltInThemes.day, name: "Été / Summer: 2026")181        mine.showBuildings = false182        mine = try store.save(mine)183        let file = try store.exportFile(for: mine)184        #expect(file.pathExtension == "orixtheme")185        #expect(file.deletingPathExtension().lastPathComponent == "Été  Summer 2026")186        let imported = try store.importTheme(from: file)187        #expect(imported.id != mine.id && imported.id.hasPrefix("user."))188        #expect(imported.name == mine.name && imported.colors == mine.colors && !imported.showBuildings)189        // Importing the same file again never replaces a theme: a third one appears.190        _ = try store.importTheme(from: file)191        #expect(store.customThemes.count == 3)192    }193194    @Test func exportOfANamelessThemeStillHasAFileName() throws {195        let dir = ScratchDirectory()196        var t = BuiltInThemes.day197        t.name = "///"198        let file = try ThemeStore(directory: dir.url).exportFile(for: t)199        #expect(file.lastPathComponent == "theme.orixtheme")200    }201202    @Test func importRefusesWhatIsNotATheme() {203        let dir = ScratchDirectory()204        let files = ScratchDirectory()205        let store = ThemeStore(directory: dir.url)206        #expect(throws: ThemeDocument.ImportError.notAThemeFile) { try store.importTheme(from: files.file("a.orixtheme", "{\"hello\": 1}")) }207        #expect(throws: ThemeDocument.ImportError.notAThemeFile) { try store.importTheme(from: files.file("b.orixtheme", "\u{0}\u{1}garbage")) }208        #expect(throws: ThemeDocument.ImportError.notAThemeFile) { try store.importTheme(from: files.file("c.json", "")) }209        #expect(store.customThemes.isEmpty)210    }211212    @Test func importRefusesANewerFormatVersion() throws {213        let dir = ScratchDirectory()214        let files = ScratchDirectory()215        var object = try #require(try JSONSerialization.jsonObject(with: ThemeDocument(theme: BuiltInThemes.day).exported()) as? [String: Any])216        object["version"] = 99217        let file = files.file("future.orixtheme", try JSONSerialization.data(withJSONObject: object))218        #expect(throws: ThemeDocument.ImportError.unsupportedVersion(99)) { try ThemeStore(directory: dir.url).importTheme(from: file) }219    }220221    @Test func importRefusesAnOversizedFile() {222        let dir = ScratchDirectory()223        let files = ScratchDirectory()224        let big = files.file("big.orixtheme", Data(repeating: 0x20, count: ThemeDocument.maxSize + 1))225        #expect(throws: ThemeDocument.ImportError.tooLarge(ThemeDocument.maxSize + 1)) { try ThemeStore(directory: dir.url).importTheme(from: big) }226    }227228    @Test func importOfAMissingFileThrows() {229        let dir = ScratchDirectory()230        #expect(throws: (any Error).self) { try ThemeStore(directory: dir.url).importTheme(from: URL(fileURLWithPath: "/nonexistent/x.orixtheme")) }231    }232233    @Test func builtInDisplayNamesAreLocalized() {234        for theme in BuiltInThemes.all { #expect(!theme.displayName.isEmpty) }235        var custom = BuiltInThemes.day236        custom.id = "user.abc"; custom.name = "Mon thème"237        #expect(custom.displayName == "Mon thème")238    }239}240
Files main
Menu

Theme