LocalData/Tests/LocalDataTests/FileStorageHelperTests.swift

56 lines
2.1 KiB
Swift

import Foundation
import Testing
@testable import LocalData
@Suite struct FileStorageHelperTests {
private let helper: FileStorageHelper
private let testBaseURL: URL
init() {
testBaseURL = FileManager.default.temporaryDirectory.appending(path: "LocalDataTests-\(UUID().uuidString)")
helper = FileStorageHelper(configuration: FileStorageConfiguration(baseURL: testBaseURL))
}
@Test func documentsDirectoryRoundTrip() async throws {
let fileName = "test_file_\(UUID().uuidString).data"
let data = Data("file-content".utf8)
try await helper.write(data, to: .documents, fileName: fileName)
let exists = await helper.exists(in: .documents, fileName: fileName)
#expect(exists == true)
let retrieved = try await helper.read(from: .documents, fileName: fileName)
#expect(retrieved == data)
let size = try await helper.size(of: .documents, fileName: fileName)
#expect(size == Int64(data.count))
let list = try await helper.list(in: .documents)
#expect(list.contains(fileName))
try await helper.delete(from: .documents, fileName: fileName)
let afterDelete = await helper.exists(in: .documents, fileName: fileName)
#expect(afterDelete == false)
}
@Test func customDirectoryCreation() async throws {
let tempDir = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString)
let fileName = "custom.txt"
let data = Data("custom".utf8)
try await helper.write(data, to: .custom(tempDir), fileName: fileName)
let retrieved = try await helper.read(from: .custom(tempDir), fileName: fileName)
#expect(retrieved == data)
// Cleanup
try? FileManager.default.removeItem(at: tempDir)
}
@Test func readNonExistentFileReturnsNil() async throws {
let result = try await helper.read(from: .caches, fileName: "nonexistent_\(UUID().uuidString)")
#expect(result == nil)
}
}