fix(file-provider): isolate shared capabilities request options

Fetch capabilities with default request options when sharing the request by account. Keep file-specific upload preconditions on the upload so they cannot affect other callers waiting for capabilities.

Assisted-by: Codex:GPT-6
Signed-off-by: Claudio Cambra <[email protected]>
This commit is contained in:
Claudio Cambra committed 2026-11-06 00:56:20 +08:00
1 parent 9e4eaae057
commit eaae00706c
4 files changed
+37 -6

No files matched your search

@@ -165,13 +165,14 @@ public protocol RemoteInterface: Sendable {
}
public extension RemoteInterface {
/// Cancelling a caller stops its wait; the request continues only while other callers still need it.
/// Callers share one capabilities request per account.
/// Cancelling a caller stops its wait. The request continues only while other callers still need it.
func currentCapabilities(
account: Account,
options: NKRequestOptions = .init()
account: Account
) async -> (account: String, capabilities: Capabilities?, data: Data?, error: NKError) {
await RetrievedCapabilitiesActor.shared.currentCapabilities(forAccount: account.ncKitAccount) { sharedTaskHandler in
await fetchCapabilities(account: account, options: options, taskHandler: sharedTaskHandler)
// Keep file-specific preconditions such as If-Match from affecting other callers.
await fetchCapabilities(account: account, options: .init(), taskHandler: sharedTaskHandler)
}
}
@@ -154,7 +154,7 @@ private func performUpload(
return chunkSize
}
let (_, capabilities, _, error) = await remoteInterface.currentCapabilities(account: account, options: options)
let (_, capabilities, _, error) = await remoteInterface.currentCapabilities(account: account)
guard error == .success,
let capabilities,
@@ -645,6 +645,8 @@ public class MockRemoteInterface: RemoteInterface, @unchecked Sendable {
/// Optional hook invoked after a delete path is captured and before the mock applies it.
public var deleteCallHandler: (() -> Void)?
public private(set) var lastCapabilitiesRequestOptions: NKRequestOptions?
/// Records the `If-Match` header the most recent upload call carried (nil if none).
/// Lets tests assert the optimistic-concurrency precondition was sent, and with
/// which etag. Captured before any injected `uploadError` short-circuit.
@@ -1480,9 +1482,10 @@ public class MockRemoteInterface: RemoteInterface, @unchecked Sendable {
public func fetchCapabilities(
account: Account,
options _: NKRequestOptions,
options: NKRequestOptions,
taskHandler: @Sendable @escaping (URLSessionTask) -> Void
) async -> (account: String, capabilities: Capabilities?, data: Data?, error: NKError) {
lastCapabilitiesRequestOptions = options
if let capabilitiesHandler {
await capabilitiesHandler(account, taskHandler)
}
@@ -772,6 +772,33 @@ final class UploadTests: NextcloudFileProviderKitTestCase {
)
}
func testSharedCapabilitiesLookupOmitsUploadPrecondition() async throws {
let root = MockRemoteItem.rootItem(account: Self.account)
let remoteInterface = MockRemoteInterface(account: Self.account, rootItem: root)
let fileURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
let contents = Data("Updated file".utf8)
try contents.write(to: fileURL)
defer { try? FileManager.default.removeItem(at: fileURL) }
let ifMatch = "\"base-etag\""
let result = await NextcloudFileProviderKit.upload(
fileLocatedAt: fileURL.path,
toRemotePath: Self.account.davFilesUrl + "/file.txt",
usingRemoteInterface: remoteInterface,
withAccount: Self.account,
forItemWithIdentifier: UUID().uuidString,
dbManager: Self.dbManager,
options: NKRequestOptions(customHeader: ["If-Match": ifMatch]),
log: FileProviderLogMock()
)
XCTAssertEqual(result.remoteError, .success)
let capabilityOptions = try XCTUnwrap(remoteInterface.lastCapabilitiesRequestOptions)
XCTAssertNil(capabilityOptions.customHeader?["If-Match"])
XCTAssertEqual(remoteInterface.lastUploadIfMatchHeader, ifMatch)
XCTAssertEqual(root.children.first?.data, contents)
}
func testUsingServerCapabilitiesChunkSize() async throws {
let capabilities = ##"""
{