// // DataAccessSyncTests.swift // PadelClubDataTests // // Created by Laurent Morvillier on 02/05/2025. // import Testing @testable import PadelClubData @testable import LeStorage struct SyncDataAccessTests { let username1: String = "UserDataTests" let password1: String = "MyPass1234--" let username2: String = "seconduser" let password2: String = "MyPass1234--" var storeCenterA: StoreCenter var storeCenterB: StoreCenter init() async throws { let conf = Config.server let dirA = "storageA" let dirB = "storageB" FileManager.default.deleteDirectoryInDocuments(directoryName: dirA) FileManager.default.deleteDirectoryInDocuments(directoryName: dirB) self.storeCenterA = StoreCenter(directoryName: dirA) self.storeCenterB = StoreCenter(directoryName: dirB) // StoreCenter A self.storeCenterA.configureURLs(secureScheme: conf.secure, domain: conf.domain, webSockets: false, useSynchronization: true) self.storeCenterA.tokenKeychain = MockKeychainStore(fileName: "\(dirA)/token.json") self.storeCenterA.deviceKeychain = MockKeychainStore(fileName: "\(dirA)/device.json") try self.storeCenterA.deviceKeychain.add(value: UUID().uuidString) self.storeCenterA.classProject = "PadelClubData" let token = try? self.storeCenterA.rawTokenShouldNotBeUsed() if token == nil { try await self.login(storeCenter: self.storeCenterA, username: self.username1, password: self.password1) } // StoreCenter B self.storeCenterB.configureURLs(secureScheme: conf.secure, domain: conf.domain, webSockets: false, useSynchronization: true) self.storeCenterB.tokenKeychain = MockKeychainStore(fileName: "\(dirB)/token.json") self.storeCenterB.deviceKeychain = MockKeychainStore(fileName: "\(dirB)/device.json") try self.storeCenterB.deviceKeychain.add(value: UUID().uuidString) self.storeCenterB.classProject = "PadelClubData" let token2 = try? self.storeCenterB.rawTokenShouldNotBeUsed() if token2 == nil { try await self.login(storeCenter: self.storeCenterB, username: self.username2, password: self.password2) } } mutating func login(storeCenter: StoreCenter, username: String, password: String) async throws { let _: CustomUser = try await storeCenter.service().login(username: username, password: password) } @Test func testSetup() async throws { #expect(self.storeCenterA.isAuthenticated) #expect(self.storeCenterB.isAuthenticated) guard let userId1 = self.storeCenterA.userId else { throw TestError.notAuthenticated } guard let userId2 = self.storeCenterB.userId else { throw TestError.notAuthenticated } #expect(userId1 != userId2) } /// In this test, the first user: /// - creates an event and a tournament /// - shares the tournament with a second user /// - remove the sharing with the second user /// We test that the data is properly received and removed upon the sharing actions @Test func testTournamentSharing() async throws { guard let userId1 = self.storeCenterA.userId else { throw TestError.notAuthenticated } guard let userId2 = self.storeCenterB.userId else { throw TestError.notAuthenticated } // Setup let eventColA: SyncedCollection = await self.storeCenterA.mainStore.asyncLoadingSynchronizedCollection() let tournamentColA: SyncedCollection = await self.storeCenterA.mainStore.asyncLoadingSynchronizedCollection() let eventColB: SyncedCollection = await self.storeCenterB.mainStore.asyncLoadingSynchronizedCollection() let tournamentColB: SyncedCollection = await self.storeCenterB.mainStore.asyncLoadingSynchronizedCollection() var dataAccesses: [DataAccess] = try await self.storeCenterA.service().get() if let dataAccessCollection = self.storeCenterA.dataAccessCollection { try await dataAccessCollection.deleteAsync(contentOfs: dataAccesses) } else { Issue.record("dataAccessCollection should not be nil") } try await eventColA.deleteAsync(contentOfs: Array(eventColA)) try await tournamentColA.deleteAsync(contentOfs: Array(tournamentColA)) let _ = try await self.storeCenterB.testSynchronizeOnceAsync() #expect(eventColB.count == 0) #expect(tournamentColB.count == 0) // Create let eventA = Event(creator: userId1) try await eventColA.addOrUpdateAsync(instance: eventA) let tournamentA = Tournament(event: eventA.id, name: "P100") try await tournamentColA.addOrUpdateAsync(instance: tournamentA) // Share with user2 try await self.storeCenterA.setAuthorizedUsersAsync(for: tournamentA, users: [userId2]) var dataB = try await self.storeCenterB.testSynchronizeOnceAsync() var syncDataB = try SyncData(data: dataB, storeCenter: self.storeCenterB) #expect(syncDataB.shared.count == 1) // the tournament #expect(syncDataB.grants.count == 1) // the granted event #expect(eventColB.count == 1) #expect(eventColB.first?.sharing == .granted) #expect(tournamentColB.count == 1) #expect(tournamentColB.first?.sharing == .shared) // Remove sharing from user2 try await self.storeCenterA.setAuthorizedUsersAsync(for: tournamentA, users: []) dataB = try await self.storeCenterB.testSynchronizeOnceAsync() syncDataB = try SyncData(data: dataB, storeCenter: self.storeCenterB) #expect(syncDataB.revocations.count == 1) #expect(syncDataB.revocationParents.count == 1) #expect(eventColB.count == 0) #expect(tournamentColB.count == 0) dataAccesses = try await self.storeCenterA.service().get() #expect(dataAccesses.count == 0) } /// In this test, the first user: /// - creates a club and 2 events in that club /// - shares both events with a second user /// - removes the sharing of 1 event /// Here we want to test that the Club instance remains even if one event is removed from the second user @Test func testSharedRelationship() async throws { guard let userId1 = self.storeCenterA.userId else { throw TestError.notAuthenticated } guard let userId2 = self.storeCenterB.userId else { throw TestError.notAuthenticated } // Setup let eventColA: SyncedCollection = await self.storeCenterA.mainStore.asyncLoadingSynchronizedCollection() let clubColA: SyncedCollection = await self.storeCenterA.mainStore.asyncLoadingSynchronizedCollection() let eventColB: SyncedCollection = await self.storeCenterB.mainStore.asyncLoadingSynchronizedCollection() let clubColB: SyncedCollection = await self.storeCenterB.mainStore.asyncLoadingSynchronizedCollection() if let dataAccessCollection = self.storeCenterA.dataAccessCollection { try await dataAccessCollection.deleteAsync(contentOfs: Array(dataAccessCollection)) } try await eventColA.deleteAsync(contentOfs: Array(eventColA)) try await clubColA.deleteAsync(contentOfs: Array(clubColA)) let _ = try await self.storeCenterB.testSynchronizeOnceAsync() #expect(eventColB.count == 0) #expect(clubColB.count == 0) // Create let clubA = Club(creator: userId1, name: "Club A", acronym: "CA") try await clubColA.addOrUpdateAsync(instance: clubA) let event1A = Event(creator: userId1, club: clubA.id, name: "event 1") let event2A = Event(creator: userId1, club: clubA.id, name: "event 2") try await eventColA.addOrUpdateAsync(contentOfs: [event1A, event2A]) // Share with user2 try await self.storeCenterA.setAuthorizedUsersAsync(for: event1A, users: [userId2]) try await self.storeCenterA.setAuthorizedUsersAsync(for: event2A, users: [userId2]) var dataB = try await self.storeCenterB.testSynchronizeOnceAsync() var syncDataB = try SyncData(data: dataB, storeCenter: self.storeCenterB) #expect(syncDataB.shared.count == 1) #expect(syncDataB.grants.count == 1) let clubGrants = syncDataB.grants.first { $0.type == Club.self } let eventGrants = syncDataB.shared.first { $0.type == Event.self } #expect(clubGrants?.items.count == 1) #expect(eventGrants?.items.count == 2) #expect(eventColB.count == 2) #expect(clubColB.count == 1) // Remove sharing from user2 try await self.storeCenterA.setAuthorizedUsersAsync(for: event1A, users: []) dataB = try await self.storeCenterB.testSynchronizeOnceAsync() syncDataB = try SyncData(data: dataB, storeCenter: self.storeCenterB) #expect(syncDataB.revocations.count == 1) #expect(syncDataB.revocationParents.count == 1) #expect(eventColB.count == 1) #expect(clubColB.count == 1) // club remains because used in event2A } /// In this test, the first user: /// - creates one event and 2 clubs /// - shares the event with a second user /// - changes the club on the event /// Here we want to test that the first Club is removed and the second one is received @Test func testRelationshipChange() async throws { guard let userId1 = self.storeCenterA.userId else { throw TestError.notAuthenticated } guard let userId2 = self.storeCenterB.userId else { throw TestError.notAuthenticated } // Setup let eventColA: SyncedCollection = await self.storeCenterA.mainStore.asyncLoadingSynchronizedCollection() let clubColA: SyncedCollection = await self.storeCenterA.mainStore.asyncLoadingSynchronizedCollection() let eventColB: SyncedCollection = await self.storeCenterB.mainStore.asyncLoadingSynchronizedCollection() let clubColB: SyncedCollection = await self.storeCenterB.mainStore.asyncLoadingSynchronizedCollection() if let dataAccessCollection = self.storeCenterA.dataAccessCollection { try await dataAccessCollection.deleteAsync(contentOfs: Array(dataAccessCollection)) } try await eventColA.deleteAsync(contentOfs: Array(eventColA)) try await clubColA.deleteAsync(contentOfs: Array(clubColA)) let _ = try await self.storeCenterB.testSynchronizeOnceAsync() #expect(eventColB.count == 0) #expect(clubColB.count == 0) // Create let club1A = Club(creator: userId1, name: "Club 1", acronym: "C1") try await clubColA.addOrUpdateAsync(instance: club1A) let club2A = Club(creator: userId1, name: "Club 2", acronym: "C2") try await clubColA.addOrUpdateAsync(instance: club2A) let eventA = Event(creator: userId1, club: club1A.id, name: "event 1") try await eventColA.addOrUpdateAsync(instance: eventA) // Share with user2 try await self.storeCenterA.setAuthorizedUsersAsync(for: eventA, users: [userId2]) let _ = try await self.storeCenterB.testSynchronizeOnceAsync() #expect(eventColB.count == 1) #expect(clubColB.count == 1) // Change the club eventA.club = club2A.id try await eventColA.addOrUpdateAsync(instance: eventA) let dataB = try await self.storeCenterB.testSynchronizeOnceAsync() let syncDataB = try SyncData(data: dataB, storeCenter: self.storeCenterB) #expect(syncDataB.sharedRelationshipSets.count == 1) #expect(syncDataB.sharedRelationshipRemovals.count == 1) #expect(eventColB.first?.club == club2A.id) } /// In this test, the first user: /// - creates one event /// - shares the event with a second user /// The second user: /// - changes the club /// Here we want to test that the first Club is removed and the second one is received @Test func testRelationshipChangesByAgent() async throws { guard let userId1 = self.storeCenterA.userId else { throw TestError.notAuthenticated } guard let userId2 = self.storeCenterB.userId else { throw TestError.notAuthenticated } // Setup let eventColA: SyncedCollection = await self.storeCenterA.mainStore.asyncLoadingSynchronizedCollection() let clubColA: SyncedCollection = await self.storeCenterA.mainStore.asyncLoadingSynchronizedCollection() let courtsColA: SyncedCollection = await self.storeCenterA.mainStore.asyncLoadingSynchronizedCollection() let eventColB: SyncedCollection = await self.storeCenterB.mainStore.asyncLoadingSynchronizedCollection() let clubColB: SyncedCollection = await self.storeCenterB.mainStore.asyncLoadingSynchronizedCollection() let courtsColB: SyncedCollection = await self.storeCenterB.mainStore.asyncLoadingSynchronizedCollection() if let dataAccessCollection = self.storeCenterA.dataAccessCollection { try await dataAccessCollection.deleteAsync(contentOfs: Array(dataAccessCollection)) } try await eventColA.deleteAsync(contentOfs: Array(eventColA)) try await clubColA.deleteAsync(contentOfs: Array(clubColA)) let _ = try await self.storeCenterB.testSynchronizeOnceAsync() #expect(eventColB.count == 0) #expect(clubColB.count == 0) // Create let clubA = Club(creator: userId1, name: "Club 1", acronym: "C1") try await clubColA.addOrUpdateAsync(instance: clubA) let eventA = Event(creator: userId1, club: clubA.id, name: "event 1") try await eventColA.addOrUpdateAsync(instance: eventA) let court1A = Court(index: 0, club: clubA.id) let court2A = Court(index: 1, club: clubA.id) try await courtsColA.addOrUpdateAsync(contentOfs: [court1A, court2A]) // Share with user2 try await self.storeCenterA.setAuthorizedUsersAsync(for: eventA, users: [userId2]) let _ = try await self.storeCenterB.testSynchronizeOnceAsync() guard let eventB = eventColB.first else { throw TestError.missingEvent } #expect(eventA.id == eventB.id) #expect(clubColB.count == 1) // Second user changes the club let club2B = Club(creator: userId2, name: "Club 2", acronym: "C2") try await clubColB.addOrUpdateAsync(instance: club2B) eventB.club = club2B.id try await eventColB.addOrUpdateAsync(instance: eventB) let _ = try await self.storeCenterA.testSynchronizeOnceAsync() #expect(eventA.club == club2B.id) #expect(clubColB.count == 1) #expect(courtsColB.count == 0) } }