Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for MaterialisedEnumerationObserver #34

Merged
merged 5 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ import OSLog
public class MaterialisedEnumerationObserver: NSObject, NSFileProviderEnumerationObserver {
static let logger = Logger(subsystem: Logger.subsystem, category: "materialisedobservation")
public let ncKitAccount: String
let dbManager: FilesDatabaseManager
private let completionHandler: (_ deletedOcIds: Set<String>) -> Void
private var allEnumeratedItemIds: Set<String> = .init()

public required init(
ncKitAccount: String, completionHandler: @escaping (_ deletedOcIds: Set<String>) -> Void
ncKitAccount: String,
dbManager: FilesDatabaseManager = FilesDatabaseManager.shared,
completionHandler: @escaping (_ deletedOcIds: Set<String>) -> Void
) {
self.ncKitAccount = ncKitAccount
self.dbManager = dbManager
self.completionHandler = completionHandler
super.init()
}
Expand All @@ -43,6 +47,7 @@ public class MaterialisedEnumerationObserver: NSObject, NSFileProviderEnumeratio
Self.handleEnumeratedItems(
allEnumeratedItemIds,
account: ncKitAccount,
dbManager: dbManager,
completionHandler: completionHandler
)
}
Expand All @@ -54,15 +59,17 @@ public class MaterialisedEnumerationObserver: NSObject, NSFileProviderEnumeratio
Self.handleEnumeratedItems(
allEnumeratedItemIds,
account: ncKitAccount,
dbManager: dbManager,
completionHandler: completionHandler
)
}

static func handleEnumeratedItems(
_ itemIds: Set<String>, account: String,
_ itemIds: Set<String>,
account: String,
dbManager: FilesDatabaseManager,
completionHandler: @escaping (_ deletedOcIds: Set<String>) -> Void
) {
let dbManager = FilesDatabaseManager.shared
let databaseLocalFileMetadatas = dbManager.itemMetadatas(account: account)
var noLongerMaterialisedIds = Set<String>()

Expand Down
50 changes: 50 additions & 0 deletions Tests/Interface/MockEnumerator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// MockEnumerator.swift
// NextcloudFileProviderKit
//
// Created by Claudio Cambra on 2024-12-20.
//

import Foundation
import FileProvider
import NextcloudFileProviderKit

public class MockEnumerator: NSObject, NSFileProviderEnumerator {
let account: Account
let dbManager: FilesDatabaseManager
let remoteInterface: any RemoteInterface
public var enumeratorItems: [ItemMetadata] = []

public init(
account: Account, dbManager: FilesDatabaseManager, remoteInterface: any RemoteInterface
) {
self.account = account
self.dbManager = dbManager
self.remoteInterface = remoteInterface
}

public func enumerateItems(
for observer: any NSFileProviderEnumerationObserver, startingAt page: NSFileProviderPage
) {
var items: [Item] = []
for item in enumeratorItems {
guard let parentItemIdentifier = dbManager.parentItemIdentifierFromMetadata(item) else {
print("Could not get parent item identifier for \(item)")
continue
}
let item = Item(
metadata: item,
parentItemIdentifier: parentItemIdentifier,
account: account,
remoteInterface: remoteInterface
)
items.append(item)
}
observer.didEnumerate(items)
observer.finishEnumerating(upTo: nil)
}

public func invalidate() {
print("MockEnumerator invalidated")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// MaterialisedEnumerationObserverTests.swift
// NextcloudFileProviderKit
//
// Created by Claudio Cambra on 2024-12-20.
//

import FileProvider
import Foundation
import NextcloudKit
import NextcloudFileProviderKit
import RealmSwift
import TestInterface
import XCTest

final class MaterialisedEnumerationObserverTests: XCTestCase {
static let account = Account(
user: "testUser", id: "testUserId", serverUrl: "https://mock.nc.com", password: "abcd"
)

override func setUp() {
super.setUp()
Realm.Configuration.defaultConfiguration.inMemoryIdentifier = name
}

func testMaterialisedObserver() async {
let dbManager = FilesDatabaseManager(realmConfig: .defaultConfiguration)
let remoteInterface = MockRemoteInterface()
let expect = XCTestExpectation(description: "Enumerator")
let observer = MaterialisedEnumerationObserver(
ncKitAccount: Self.account.ncKitAccount, dbManager: dbManager
) { deletedOcIds in
XCTAssertTrue(deletedOcIds.isEmpty)
expect.fulfill()
}
let enumerator = MockEnumerator(
account: Self.account, dbManager: dbManager, remoteInterface: remoteInterface
)
enumerator.enumerateItems(for: observer, startingAt: NSFileProviderPage(Data(count: 1)))
await fulfillment(of: [expect], timeout: 1)
}

func testMaterialisedFiles() async {
let itemA = ItemMetadata()
itemA.apply(account: Self.account)
itemA.apply(fileName: "itemA")
itemA.ocId = "itemA"
itemA.serverUrl = Self.account.davFilesUrl

let itemB = ItemMetadata()
itemB.apply(account: Self.account)
itemB.apply(fileName: "itemB")
itemB.ocId = "itemB"
itemB.serverUrl = Self.account.davFilesUrl

let dbManager = FilesDatabaseManager(realmConfig: .defaultConfiguration)
dbManager.addItemMetadata(itemA)
dbManager.addItemMetadata(itemB)

let remoteInterface = MockRemoteInterface()
let expect = XCTestExpectation(description: "Enumerator")
let observer = MaterialisedEnumerationObserver(
ncKitAccount: Self.account.ncKitAccount, dbManager: dbManager
) { deletedOcIds in
XCTAssertEqual(deletedOcIds.count, 1) // Item B deleted
XCTAssertEqual(deletedOcIds.first, itemB.ocId)
expect.fulfill()
}
let enumerator = MockEnumerator(
account: Self.account, dbManager: dbManager, remoteInterface: remoteInterface
)
enumerator.enumeratorItems = [itemA] // No item B
enumerator.enumerateItems(for: observer, startingAt: NSFileProviderPage(Data(count: 1)))
await fulfillment(of: [expect], timeout: 1)
}
}