117 lines
3.8 KiB
Swift
117 lines
3.8 KiB
Swift
import Foundation
|
|
import Testing
|
|
@testable import LocalData
|
|
|
|
@Suite struct SerializerTests {
|
|
|
|
@Test func jsonSerializerRoundTrip() throws {
|
|
let serializer: Serializer<String> = .json
|
|
let value = "test-string"
|
|
let data = try serializer.encode(value)
|
|
let decoded = try serializer.decode(data)
|
|
#expect(decoded == value)
|
|
}
|
|
|
|
@Test func plistSerializerRoundTrip() throws {
|
|
let serializer: Serializer<[String: Int]> = .plist
|
|
let value = ["key": 42]
|
|
let data = try serializer.encode(value)
|
|
let decoded = try serializer.decode(data)
|
|
#expect(decoded == value)
|
|
}
|
|
|
|
@Test func dataSerializerPassThrough() throws {
|
|
let serializer: Serializer<Data> = .data
|
|
let value = Data("raw-data".utf8)
|
|
let data = try serializer.encode(value)
|
|
#expect(data == value)
|
|
let decoded = try serializer.decode(data)
|
|
#expect(decoded == value)
|
|
}
|
|
|
|
@Test func customSerializer() throws {
|
|
let serializer = Serializer<Int>(
|
|
encode: { Data("\($0)".utf8) },
|
|
decode: {
|
|
guard let s = String(data: $0, encoding: .utf8), let i = Int(s) else {
|
|
throw StorageError.deserializationFailed
|
|
}
|
|
return i
|
|
},
|
|
name: "int-string"
|
|
)
|
|
|
|
let value = 12345
|
|
let data = try serializer.encode(value)
|
|
#expect(String(data: data, encoding: .utf8) == "12345")
|
|
let decoded = try serializer.decode(data)
|
|
#expect(decoded == value)
|
|
}
|
|
}
|
|
|
|
@Suite struct ConfigurationTests {
|
|
|
|
@Test func storageConfigurationDefaults() {
|
|
let config = StorageConfiguration.default
|
|
#expect(config.defaultKeychainService == nil)
|
|
#expect(config.defaultAppGroupIdentifier == nil)
|
|
}
|
|
|
|
@Test func encryptionConfigurationDefaults() {
|
|
let config = EncryptionConfiguration.default
|
|
#expect(config.masterKeyService == "LocalData")
|
|
#expect(config.masterKeyAccount == "MasterKey")
|
|
}
|
|
|
|
@Test func syncConfigurationDefaults() {
|
|
let config = SyncConfiguration.default
|
|
#expect(config.maxAutoSyncSize == 100_000)
|
|
}
|
|
}
|
|
|
|
@Suite struct EnumPropertyTests {
|
|
|
|
@Test func keychainAccessibilityProperties() {
|
|
for level in KeychainAccessibility.allCases {
|
|
// Verify cfString is accessible (critical for security framework)
|
|
let _ = level.cfString
|
|
#expect(!level.displayName.isEmpty)
|
|
}
|
|
}
|
|
|
|
@Test func keychainAccessControlProperties() {
|
|
for control in KeychainAccessControl.allCases {
|
|
#expect(!control.displayName.isEmpty)
|
|
}
|
|
}
|
|
|
|
@Test func platformAvailabilityExists() {
|
|
// Just verify all cases exist as defined
|
|
let cases: [PlatformAvailability] = [.all, .phoneOnly, .watchOnly, .phoneWithWatchSync]
|
|
#expect(cases.count == 4)
|
|
}
|
|
}
|
|
|
|
@Suite struct ErrorLogicTests {
|
|
|
|
@Test func storageErrorEquality() {
|
|
#expect(StorageError.notFound == .notFound)
|
|
#expect(StorageError.serializationFailed != .deserializationFailed)
|
|
#expect(StorageError.keychainError(1) == .keychainError(1))
|
|
#expect(StorageError.keychainError(1) != .keychainError(2))
|
|
#expect(StorageError.unregisteredKey("a") == .unregisteredKey("a"))
|
|
#expect(StorageError.unregisteredKey("a") != .unregisteredKey("b"))
|
|
}
|
|
}
|
|
|
|
@Suite struct DirectoryLogicTests {
|
|
|
|
@Test func fileDirectoryUrls() {
|
|
#expect(!FileDirectory.documents.url().path.isEmpty)
|
|
#expect(!FileDirectory.caches.url().path.isEmpty)
|
|
|
|
let customUrl = URL(fileURLWithPath: "/tmp/custom")
|
|
#expect(FileDirectory.custom(customUrl).url() == customUrl)
|
|
}
|
|
}
|