Skip to content

Commit 9a0a733

Browse files
committed
Add test for paged read results handling
Signed-off-by: Claudio Cambra <developer@claudiocambra.com>
1 parent bfcf896 commit 9a0a733

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

Tests/NextcloudFileProviderKitTests/EnumeratorTests.swift

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,101 @@ final class EnumeratorTests: XCTestCase {
518518
)
519519
}
520520

521+
func testHandlePagedReadResults() throws {
522+
// 1. Arrange
523+
let dbManager = Self.dbManager
524+
let db = dbManager.ncDatabase()
525+
debugPrint(db)
526+
527+
let parentNKFile = remoteFolder.toNKFile()
528+
let childrenNKFiles = (0..<5).map { i in
529+
MockRemoteItem(
530+
identifier: "pagedChild\(i)",
531+
name: "pagedChild\(i).txt",
532+
remotePath: Self.account.davFilesUrl + "/folder/pagedChild\(i).txt",
533+
account: Self.account.ncKitAccount,
534+
username: Self.account.username,
535+
userId: Self.account.id,
536+
serverUrl: Self.account.serverUrl
537+
).toNKFile()
538+
}
539+
let followUpChildrenNKFiles = (5..<10).map { i in
540+
MockRemoteItem(
541+
identifier: "pagedChild\(i)",
542+
name: "pagedChild\(i).txt",
543+
remotePath: Self.account.davFilesUrl + "/folder/pagedChild\(i).txt",
544+
account: Self.account.ncKitAccount,
545+
username: Self.account.username,
546+
userId: Self.account.id,
547+
serverUrl: Self.account.serverUrl
548+
).toNKFile()
549+
}
550+
551+
// --- Scenario A: First Page (pageIndex == 0) ---
552+
// 2. Act
553+
let firstPageFiles = [parentNKFile] + childrenNKFiles
554+
let (firstPageResult, firstPageError) = Enumerator.handlePagedReadResults(
555+
files: firstPageFiles, pageIndex: 0, dbManager: dbManager
556+
)
557+
558+
// 3. Assert
559+
XCTAssertNil(firstPageError)
560+
// Result should only contain the children, not the parent.
561+
XCTAssertEqual(
562+
firstPageResult?.count, 5, "First page result should only contain children."
563+
)
564+
// The parent's metadata should be processed and saved to the DB.
565+
let parentMetadataFromDB = dbManager.itemMetadata(ocId: parentNKFile.ocId)
566+
XCTAssertNotNil(
567+
parentMetadataFromDB, "Parent folder metadata should be saved to DB from first page."
568+
)
569+
XCTAssertEqual(parentMetadataFromDB?.etag, parentNKFile.etag, "Parent etag should match.")
570+
// The children's metadata should also be saved.
571+
let childMetadataFromDB = dbManager.itemMetadata(ocId: "pagedChild0")
572+
XCTAssertNotNil(
573+
childMetadataFromDB, "Child metadata should be saved to DB from first page."
574+
)
575+
576+
// --- Scenario B: Follow-up Page (pageIndex > 0) ---
577+
// 4. Act
578+
let (followUpPageResult, followUpPageError) = Enumerator.handlePagedReadResults(
579+
files: followUpChildrenNKFiles, pageIndex: 1, dbManager: dbManager
580+
)
581+
582+
// 5. Assert
583+
XCTAssertNil(followUpPageError)
584+
// Result should contain all items passed in, as the parent is not included.
585+
XCTAssertEqual(
586+
followUpPageResult?.count, 5, "Follow-up page result should contain all its items."
587+
)
588+
let followUpChildMetadata = dbManager.itemMetadata(ocId: "pagedChild5")
589+
XCTAssertNotNil(
590+
followUpChildMetadata, "Child metadata should be saved to DB from follow-up page."
591+
)
592+
593+
// --- Scenario C: Root Folder (should not be ingested) ---
594+
// 6. Act
595+
var rootNKFile = MockRemoteItem.rootItem(account: Self.account).toNKFile()
596+
// The check is based on URL string matching.
597+
rootNKFile.path = Self.account.davFilesUrl
598+
599+
let (rootResult, rootError) = Enumerator.handlePagedReadResults(
600+
files: [rootNKFile], pageIndex: 0, dbManager: dbManager
601+
)
602+
603+
// 7. Assert
604+
XCTAssertNil(rootError)
605+
// The result should be empty as there are no children.
606+
XCTAssertEqual(
607+
rootResult?.count, 0, "Root folder enumeration should yield no children."
608+
)
609+
// No metadata should be saved for the root folder ID itself.
610+
XCTAssertNil(
611+
dbManager.itemMetadata(ocId: rootNKFile.ocId),
612+
"Metadata for the root folder itself should not be saved."
613+
)
614+
}
615+
521616
func testWorkingSetChangeEnumeration() async throws {
522617
let db = Self.dbManager.ncDatabase() // Strong ref for in memory test db
523618
debugPrint(db)

0 commit comments

Comments
 (0)