Files
suixinkan_ios_new/suixinkanTests/CameraOTG/OTGPhotoStorageTests.swift
汉秋 be9c844bd3 将 otg_swift OTG 引擎迁入 Core/CameraOTG,并接入旅拍有线传图与历史导入。
恢复 USB 相机连接、边拍边传、三品牌驱动与 SwiftUI 历史导入页,通过适配层对接现有本地上传与 OSS 管道。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-06 11:22:07 +08:00

83 lines
3.2 KiB
Swift

//
// 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))
}
}