// // OTGPhotoStorageTests.swift // suixinkanTests // import XCTest @testable import suixinkan /// OTGPhotoFileWriter scoped 路径写入与去重测试。 final class OTGPhotoStorageTests: XCTestCase { private let accountKey = "test_account" private let albumID = 12345 override func tearDown() { let directory = OTGPhotoFileWriter.originalsDirectory(accountKey: accountKey, albumID: albumID) try? FileManager.default.removeItem(at: directory.deletingLastPathComponent()) super.tearDown() } /// 测试 scoped 原图目录按账号与相册隔离。 func testOriginalsDirectoryScopedByAccountAndAlbum() { let directory = OTGPhotoFileWriter.originalsDirectory(accountKey: accountKey, albumID: albumID) XCTAssertTrue(directory.path.contains("CameraDownloads")) XCTAssertTrue(directory.path.contains("\(albumID)")) XCTAssertTrue(directory.lastPathComponent == "originals") XCTAssertTrue(FileManager.default.fileExists(atPath: directory.path)) } /// 测试 writeImage 落盘并返回相对路径。 func testWriteImageReturnsRelativePath() throws { let data = Data(repeating: 0xAB, count: 64) let fileURL = try OTGPhotoFileWriter.writeImage( data, filename: "DSC_0001.JPG", accountKey: accountKey, albumID: albumID ) XCTAssertTrue(FileManager.default.fileExists(atPath: fileURL.path)) let relativePath = OTGPhotoFileWriter.relativePath(for: fileURL) XCTAssertFalse(relativePath.hasPrefix("/")) XCTAssertTrue(relativePath.contains("DSC_0001.JPG")) let resolved = CameraDownloadStorage.resolveLocalURL(from: relativePath) XCTAssertEqual(resolved?.standardizedFileURL, fileURL.standardizedFileURL) } /// 测试同名文件自动追加序号避免覆盖。 func testUniqueFileURLAppendsCounterForDuplicates() throws { let data = Data(repeating: 0xCD, count: 16) let firstURL = try OTGPhotoFileWriter.writeImage( data, filename: "duplicate.JPG", accountKey: accountKey, albumID: albumID ) let secondURL = try OTGPhotoFileWriter.uniqueFileURL( filename: "duplicate.JPG", accountKey: accountKey, albumID: albumID ) XCTAssertNotEqual(firstURL.lastPathComponent, secondURL.lastPathComponent) XCTAssertTrue(secondURL.lastPathComponent.contains("duplicate_1")) } /// 测试 moveDownloadedFile 将临时文件移入 scoped 目录。 func testMoveDownloadedFileIntoScopedDirectory() throws { let tempURL = FileManager.default.temporaryDirectory.appendingPathComponent("otg-move-test.JPG") try Data(repeating: 0xEF, count: 32).write(to: tempURL) defer { try? FileManager.default.removeItem(at: tempURL) } let destination = try OTGPhotoFileWriter.moveDownloadedFile( from: tempURL, filename: "imported.JPG", accountKey: accountKey, albumID: albumID ) XCTAssertTrue(FileManager.default.fileExists(atPath: destination.path)) XCTAssertFalse(FileManager.default.fileExists(atPath: tempURL.path)) } }