-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: calculate supported protocols - WPB-15297 (#2384)
- Loading branch information
Showing
9 changed files
with
328 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
WireDomain/Sources/WireDomain/UseCases/PullSelfUserClients.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// | ||
// Wire | ||
// Copyright (C) 2025 Wire Swiss GmbH | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see http://www.gnu.org/licenses/. | ||
// | ||
|
||
import CoreData | ||
import WireAPI | ||
|
||
/// Pull self clients from backend and update local state | ||
public struct PullSelfUserClients: PullSelfUserClientsProtocol { | ||
private let userClientsAPI: any UserClientsAPI | ||
private let userClientsLocalStore: any UserClientsLocalStoreProtocol | ||
|
||
init(userClientsAPI: any UserClientsAPI, userClientsLocalStore: any UserClientsLocalStoreProtocol) { | ||
self.userClientsAPI = userClientsAPI | ||
self.userClientsLocalStore = userClientsLocalStore | ||
} | ||
|
||
public func pullSelfClients() async throws { | ||
let remoteSelfClients = try await userClientsAPI.getSelfClients() | ||
|
||
for remoteSelfClient in remoteSelfClients { | ||
let localUserClient = await userClientsLocalStore.fetchOrCreateClient( | ||
id: remoteSelfClient.id | ||
) | ||
|
||
try await updateClient( | ||
id: remoteSelfClient.id, | ||
from: remoteSelfClient, | ||
isNewClient: localUserClient.isNew | ||
) | ||
} | ||
|
||
let deletedSelfClientsIDs = await userClientsLocalStore.deletedSelfClients( | ||
newClients: remoteSelfClients.map(\.id) | ||
) | ||
|
||
for deletedSelfClientID in deletedSelfClientsIDs { | ||
await userClientsLocalStore.deleteClient(id: deletedSelfClientID) | ||
} | ||
} | ||
|
||
func updateClient( | ||
id: String, | ||
from remoteClient: WireAPI.SelfUserClient, | ||
isNewClient: Bool | ||
) async throws { | ||
await userClientsLocalStore.updateClient( | ||
id: id, | ||
isNewClient: isNewClient, | ||
userClientInfo: remoteClient.toDomainModel() | ||
) | ||
} | ||
|
||
} | ||
|
||
public extension PullSelfUserClients { | ||
|
||
static func make( | ||
apiService: any APIServiceProtocol, | ||
apiVersion: WireAPI.APIVersion, | ||
context: NSManagedObjectContext | ||
) -> PullSelfUserClientsProtocol { | ||
let userClientsAPI = UserClientsAPIBuilder(apiService: apiService).makeAPI(for: apiVersion) | ||
|
||
let userLocalStore = UserLocalStore(context: context) | ||
let userClientsLocalStore = UserClientsLocalStore(context: context, userLocalStore: userLocalStore) | ||
|
||
return PullSelfUserClients(userClientsAPI: userClientsAPI, userClientsLocalStore: userClientsLocalStore) | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
WireDomain/Sources/WireDomain/UseCases/PullSelfUserClientsProtocol.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// Wire | ||
// Copyright (C) 2025 Wire Swiss GmbH | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see http://www.gnu.org/licenses/. | ||
// | ||
|
||
// sourcery: AutoMockable | ||
public protocol PullSelfUserClientsProtocol { | ||
|
||
func pullSelfClients() async throws | ||
} |
31 changes: 30 additions & 1 deletion
31
WireDomain/Sources/WireDomainSupport/Sourcery/generated/AutoMockable.generated.swift
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
111 changes: 111 additions & 0 deletions
111
WireDomain/Tests/WireDomainTests/UseCases/PullSelfUserClientsTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// | ||
// Wire | ||
// Copyright (C) 2025 Wire Swiss GmbH | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see http://www.gnu.org/licenses/. | ||
// | ||
|
||
import WireAPISupport | ||
import WireDataModel | ||
import WireDataModelSupport | ||
import WireDomainSupport | ||
import WireTestingPackage | ||
import XCTest | ||
@testable import WireAPI | ||
@testable import WireDomain | ||
|
||
final class PullSelfUserClientsTests: XCTestCase { | ||
|
||
private var sut: PullSelfUserClients! | ||
private var userClientsAPI: MockUserClientsAPI! | ||
private var userClientsLocalStore: MockUserClientsLocalStoreProtocol! | ||
private var stack: CoreDataStack! | ||
private var coreDataStackHelper: CoreDataStackHelper! | ||
private var modelHelper: ModelHelper! | ||
|
||
private var context: NSManagedObjectContext { | ||
stack.syncContext | ||
} | ||
|
||
override func setUp() async throws { | ||
coreDataStackHelper = CoreDataStackHelper() | ||
modelHelper = ModelHelper() | ||
stack = try await coreDataStackHelper.createStack() | ||
userClientsAPI = MockUserClientsAPI() | ||
userClientsLocalStore = MockUserClientsLocalStoreProtocol() | ||
|
||
sut = PullSelfUserClients( | ||
userClientsAPI: userClientsAPI, | ||
userClientsLocalStore: userClientsLocalStore | ||
) | ||
} | ||
|
||
override func tearDown() async throws { | ||
stack = nil | ||
userClientsAPI = nil | ||
sut = nil | ||
try coreDataStackHelper.cleanupDirectory() | ||
coreDataStackHelper = nil | ||
modelHelper = nil | ||
} | ||
|
||
// MARK: - Tests | ||
|
||
func testPullSelfClients_It_Invokes_Local_Store_And_User_Repo_Methods() async throws { | ||
// Mock | ||
|
||
let selfUserClient = await context.perform { [self] in | ||
return modelHelper.createSelfClient( | ||
id: Scaffolding.otherUserClientID, | ||
in: context | ||
) | ||
} | ||
|
||
userClientsAPI.getSelfClients_MockValue = [ | ||
Scaffolding.selfUserClient | ||
] | ||
|
||
userClientsLocalStore.fetchOrCreateClientId_MockValue = (selfUserClient, false) | ||
userClientsLocalStore.updateClientIdIsNewClientUserClientInfo_MockMethod = { _, _, _ in } | ||
userClientsLocalStore.deletedSelfClientsNewClients_MockValue = [Scaffolding.userClientID] | ||
userClientsLocalStore.deleteClientId_MockMethod = { _ in } | ||
|
||
// When | ||
|
||
try await sut.pullSelfClients() | ||
|
||
// Then | ||
|
||
XCTAssertEqual(userClientsLocalStore.fetchOrCreateClientId_Invocations.count, 1) | ||
XCTAssertEqual(userClientsLocalStore.updateClientIdIsNewClientUserClientInfo_Invocations.count, 1) | ||
XCTAssertEqual(userClientsLocalStore.deletedSelfClientsNewClients_Invocations.count, 1) | ||
XCTAssertEqual(userClientsLocalStore.deleteClientId_Invocations.count, 1) | ||
} | ||
|
||
private enum Scaffolding { | ||
static let userClientID = UUID.mockID1.uuidString | ||
static let otherUserClientID = UUID.mockID2.uuidString | ||
|
||
static let selfUserClient = WireAPI.SelfUserClient( | ||
id: userClientID, | ||
type: .permanent, | ||
activationDate: .now, | ||
label: "test", | ||
model: "test", | ||
deviceClass: .phone, | ||
capabilities: [] | ||
) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.