按账号与相册隔离相机下载目录,并同步更新传输管道与测试。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@ -8,36 +8,73 @@ import XCTest
|
||||
|
||||
/// CameraDownloadStorage 相对路径持久化测试。
|
||||
final class CameraDownloadStorageTests: XCTestCase {
|
||||
/// 测试相对路径前缀。
|
||||
func testRelativePathUsesCameraDownloadsPrefix() {
|
||||
let fileURL = CameraDownloadStorage.downloadsDirectory.appendingPathComponent("1730_test.JPG")
|
||||
XCTAssertEqual(CameraDownloadStorage.relativePath(for: fileURL), "CameraDownloads/1730_test.JPG")
|
||||
/// 测试 scoped 原图相对路径前缀。
|
||||
func testRelativePathUsesScopedOriginalsPrefix() {
|
||||
let scope = CameraDownloadStorage.Scope(accountKey: "user_101", albumID: 6603)
|
||||
defer { CameraDownloadStorage.removeScopeDirectory(for: scope) }
|
||||
|
||||
let fileURL = CameraDownloadStorage.originalsDirectory(for: scope).appendingPathComponent("1730_test.JPG")
|
||||
XCTAssertEqual(
|
||||
CameraDownloadStorage.relativePath(for: fileURL),
|
||||
"CameraDownloads/user_101/6603/originals/1730_test.JPG"
|
||||
)
|
||||
}
|
||||
|
||||
/// 测试相对路径解析。
|
||||
func testResolveLocalURLFromRelativePath() {
|
||||
let stored = "CameraDownloads/1730_test.JPG"
|
||||
let stored = "CameraDownloads/user_101/6603/originals/1730_test.JPG"
|
||||
let resolved = CameraDownloadStorage.resolveLocalURL(from: stored)
|
||||
let expected = CameraDownloadStorage.documentsDirectory.appendingPathComponent(stored)
|
||||
XCTAssertEqual(resolved, expected)
|
||||
}
|
||||
|
||||
/// 测试旧版绝对路径迁移。
|
||||
func testMigrateStoredPathFromLegacyAbsolutePath() throws {
|
||||
let fileURL = CameraDownloadStorage.uniqueLocalURL(for: "legacy.jpg")
|
||||
try Data("jpeg".utf8).write(to: fileURL)
|
||||
defer { try? FileManager.default.removeItem(at: fileURL) }
|
||||
/// 测试账号或相册不同不会生成相同目录。
|
||||
func testScopedDirectoriesDoNotCollideAcrossAccountOrAlbum() {
|
||||
let accountScope = CameraDownloadStorage.Scope(accountKey: "user_101", albumID: 6603)
|
||||
let otherAccountScope = CameraDownloadStorage.Scope(accountKey: "user_102", albumID: 6603)
|
||||
let otherAlbumScope = CameraDownloadStorage.Scope(accountKey: "user_101", albumID: 6604)
|
||||
defer {
|
||||
CameraDownloadStorage.removeScopeDirectory(for: accountScope)
|
||||
CameraDownloadStorage.removeScopeDirectory(for: otherAccountScope)
|
||||
CameraDownloadStorage.removeScopeDirectory(for: otherAlbumScope)
|
||||
}
|
||||
|
||||
let migrated = CameraDownloadStorage.migrateStoredPath(fileURL.path)
|
||||
XCTAssertEqual(migrated, "CameraDownloads/\(fileURL.lastPathComponent)")
|
||||
XCTAssertEqual(CameraDownloadStorage.resolveLocalURL(from: migrated ?? "")?.path, fileURL.path)
|
||||
XCTAssertNotEqual(
|
||||
CameraDownloadStorage.originalsDirectory(for: accountScope).path,
|
||||
CameraDownloadStorage.originalsDirectory(for: otherAccountScope).path
|
||||
)
|
||||
XCTAssertNotEqual(
|
||||
CameraDownloadStorage.originalsDirectory(for: accountScope).path,
|
||||
CameraDownloadStorage.originalsDirectory(for: otherAlbumScope).path
|
||||
)
|
||||
}
|
||||
|
||||
/// 测试 scoped 目录清理只影响目标相册。
|
||||
func testRemoveScopeDirectoryOnlyDeletesTargetScope() throws {
|
||||
let scope = CameraDownloadStorage.Scope(accountKey: "user_101", albumID: 6603)
|
||||
let otherScope = CameraDownloadStorage.Scope(accountKey: "user_101", albumID: 6604)
|
||||
defer {
|
||||
CameraDownloadStorage.removeScopeDirectory(for: scope)
|
||||
CameraDownloadStorage.removeScopeDirectory(for: otherScope)
|
||||
}
|
||||
|
||||
let fileURL = CameraDownloadStorage.uniqueLocalURL(for: "target.jpg", scope: scope)
|
||||
let otherFileURL = CameraDownloadStorage.uniqueLocalURL(for: "other.jpg", scope: otherScope)
|
||||
try Data("target".utf8).write(to: fileURL)
|
||||
try Data("other".utf8).write(to: otherFileURL)
|
||||
|
||||
CameraDownloadStorage.removeScopeDirectory(for: scope)
|
||||
|
||||
XCTAssertFalse(FileManager.default.fileExists(atPath: fileURL.path))
|
||||
XCTAssertTrue(FileManager.default.fileExists(atPath: otherFileURL.path))
|
||||
}
|
||||
|
||||
/// 测试预览 URL 字符串生成。
|
||||
func testPreviewURLStringFromRelativePath() throws {
|
||||
let fileURL = CameraDownloadStorage.uniqueLocalURL(for: "preview.jpg")
|
||||
let scope = CameraDownloadStorage.Scope(accountKey: "user_101", albumID: 6603)
|
||||
let fileURL = CameraDownloadStorage.uniqueLocalURL(for: "preview.jpg", scope: scope)
|
||||
try Data("jpeg".utf8).write(to: fileURL)
|
||||
defer { try? FileManager.default.removeItem(at: fileURL) }
|
||||
defer { CameraDownloadStorage.removeScopeDirectory(for: scope) }
|
||||
|
||||
let stored = CameraDownloadStorage.relativePath(for: fileURL)
|
||||
let preview = CameraDownloadStorage.previewURLString(from: stored)
|
||||
|
||||
Reference in New Issue
Block a user