// // PadelClubDataTests.swift // PadelClubDataTests // // Created by Laurent Morvillier on 15/04/2025. // import Testing @testable import PadelClubData @testable import LeStorage enum TestError: Error { case notAuthenticated case sameDeviceId case missingEvent } struct PadelClubDataTests { let username: String = "UserDataTests" let password: String = "MyPass1234--" init() async throws { let conf = Config.server StoreCenter.main.configureURLs(secureScheme: conf.secure, domain: conf.domain) StoreCenter.main.tokenKeychain = MockKeychainStore(fileName: "token.json") try await self.login() } mutating func login() async throws { let _: CustomUser = try await StoreCenter.main.service().login(username: self.username, password: self.password) } @Test func testAuthentication() { #expect(StoreCenter.main.isAuthenticated) } @Test func createTournament() async throws { guard let userId = StoreCenter.main.userId else { throw TestError.notAuthenticated } // Cleanup let eventCol: SyncedCollection = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection() eventCol.reset() let events: [Event] = try await StoreCenter.main.service().get() try await eventCol.deleteAsync(contentOfs: Array(events)) #expect(eventCol.count == 0) let tournamentCol: SyncedCollection = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection() tournamentCol.reset() let tournaments: [Tournament] = try await StoreCenter.main.service().get() try await tournamentCol.deleteAsync(contentOfs: Array(tournaments)) #expect(tournamentCol.count == 0) // Create let event: Event = Event(creator: userId, club: nil, name: "test") try await eventCol.addOrUpdateAsync(instance: event) let tournament: Tournament = Tournament.fake() tournament.event = event.id try await tournamentCol.addOrUpdateAsync(instance: tournament) // Test server content try await eventCol.loadDataFromServerIfAllowed(clear: true) #expect(eventCol.count == 1) try await tournamentCol.loadDataFromServerIfAllowed(clear: true) #expect(tournamentCol.count == 1) } @Test func dualStoreCenter() async throws { let conf = Config.server let secondStoreServer = StoreCenter() secondStoreServer.configureURLs(secureScheme: conf.secure, domain: conf.domain) secondStoreServer.tokenKeychain = MockKeychainStore(fileName: "token.json") let _: CustomUser = try await secondStoreServer.service().login(username: self.username, password: self.password) #expect(StoreCenter.main.isAuthenticated) #expect(secondStoreServer.isAuthenticated) } @Test func testWebsocketSynchronization() async throws { let secondStoreServer = StoreCenter() secondStoreServer.configureURLs(secureScheme: false, domain: "127.0.0.1:8000") secondStoreServer.tokenKeychain = MockKeychainStore(fileName: "token.json") let events = DataStore.shared.events try await DataStore.shared.events.deleteAsync(contentOfs: Array(events)) } }