//
//  CameraDownloadStorageTests.swift
//  suixinkanTests
//

import XCTest
@testable import suixinkan

/// CameraDownloadStorage 相对路径持久化测试。
final class CameraDownloadStorageTests: XCTestCase {
    /// 测试 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/user_101/6603/originals/1730_test.JPG"
        let resolved = CameraDownloadStorage.resolveLocalURL(from: stored)
        let expected = CameraDownloadStorage.documentsDirectory.appendingPathComponent(stored)
        XCTAssertEqual(resolved, expected)
    }

    /// 测试账号或相册不同不会生成相同目录。
    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)
        }

        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 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 { CameraDownloadStorage.removeScopeDirectory(for: scope) }

        let stored = CameraDownloadStorage.relativePath(for: fileURL)
        let preview = CameraDownloadStorage.previewURLString(from: stored)
        XCTAssertFalse(preview.isEmpty)
        XCTAssertTrue(preview.hasPrefix("file://"))
    }
}
