last one
This commit is contained in:
@@ -571,3 +571,258 @@ struct DeltaSyncSavingsTests {
|
||||
#expect(status.deltaSyncSavingsPercent == 75.0)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - DeltaSyncResult Tests
|
||||
|
||||
struct DeltaSyncResultTests {
|
||||
@Test("DeltaSyncResult deltaSavingsRatio is 0 when no data")
|
||||
func zeroRatio() {
|
||||
let result = DeltaSyncResult(
|
||||
alertsChanged: false,
|
||||
exposuresChanged: false,
|
||||
watchlistChanged: false,
|
||||
bytesTransferred: 0,
|
||||
deltaSavings: 0,
|
||||
newAlerts: [],
|
||||
newExposures: []
|
||||
)
|
||||
#expect(result.deltaSavingsRatio == 0)
|
||||
}
|
||||
|
||||
@Test("DeltaSyncResult deltaSavingsRatio calculates correctly")
|
||||
func ratioCalculation() {
|
||||
let result = DeltaSyncResult(
|
||||
alertsChanged: true,
|
||||
exposuresChanged: false,
|
||||
watchlistChanged: false,
|
||||
bytesTransferred: 250,
|
||||
deltaSavings: 750,
|
||||
newAlerts: [],
|
||||
newExposures: []
|
||||
)
|
||||
// 750 / (250 + 750) = 0.75
|
||||
#expect(result.deltaSavingsRatio == 0.75)
|
||||
}
|
||||
|
||||
@Test("DeltaSyncResult deltaSavingsRatio is 1.0 when nothing transferred")
|
||||
func allSaved() {
|
||||
let result = DeltaSyncResult(
|
||||
alertsChanged: false,
|
||||
exposuresChanged: false,
|
||||
watchlistChanged: false,
|
||||
bytesTransferred: 0,
|
||||
deltaSavings: 1000,
|
||||
newAlerts: [],
|
||||
newExposures: []
|
||||
)
|
||||
#expect(result.deltaSavingsRatio == 1.0)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - BatteryUsageMetrics Tests
|
||||
|
||||
struct BatteryUsageMetricsTests {
|
||||
@Test("BatteryUsageMetrics defaults are correct")
|
||||
func defaults() {
|
||||
let metrics = BatteryUsageMetrics()
|
||||
#expect(metrics.totalSyncTime == 0)
|
||||
#expect(metrics.syncCount == 0)
|
||||
#expect(metrics.totalBytesTransferred == 0)
|
||||
#expect(metrics.estimatedBatteryDrain == 0)
|
||||
#expect(metrics.averageSyncDuration == 0)
|
||||
#expect(metrics.batteryDrainPerSync == 0)
|
||||
}
|
||||
|
||||
@Test("BatteryUsageMetrics records sync correctly")
|
||||
func recordSync() {
|
||||
var metrics = BatteryUsageMetrics()
|
||||
metrics.recordSync(duration: 2.5, bytesTransferred: 10240)
|
||||
#expect(metrics.syncCount == 1)
|
||||
#expect(metrics.totalSyncTime == 2.5)
|
||||
#expect(metrics.totalBytesTransferred == 10240)
|
||||
#expect(metrics.averageSyncDuration == 2.5)
|
||||
#expect(metrics.estimatedBatteryDrain > 0)
|
||||
}
|
||||
|
||||
@Test("BatteryUsageMetrics accumulates across multiple syncs")
|
||||
func accumulate() {
|
||||
var metrics = BatteryUsageMetrics()
|
||||
metrics.recordSync(duration: 2.0, bytesTransferred: 5000)
|
||||
metrics.recordSync(duration: 3.0, bytesTransferred: 10000)
|
||||
#expect(metrics.syncCount == 2)
|
||||
#expect(metrics.totalSyncTime == 5.0)
|
||||
#expect(metrics.totalBytesTransferred == 15000)
|
||||
#expect(metrics.averageSyncDuration == 2.5)
|
||||
}
|
||||
|
||||
@Test("BatteryUsageMetrics calculates drain per sync")
|
||||
func batteryDrainPerSync() {
|
||||
var metrics = BatteryUsageMetrics()
|
||||
metrics.recordSync(duration: 2.0, bytesTransferred: 10240)
|
||||
metrics.recordSync(duration: 3.0, bytesTransferred: 20480)
|
||||
#expect(metrics.batteryDrainPerSync > 0)
|
||||
#expect(metrics.batteryDrainPerSync == metrics.estimatedBatteryDrain / 2.0)
|
||||
}
|
||||
|
||||
@Test("BatteryUsageMetrics reset clears all values")
|
||||
func reset() {
|
||||
var metrics = BatteryUsageMetrics()
|
||||
metrics.recordSync(duration: 2.0, bytesTransferred: 10240)
|
||||
metrics.reset()
|
||||
#expect(metrics.syncCount == 0)
|
||||
#expect(metrics.totalSyncTime == 0)
|
||||
#expect(metrics.totalBytesTransferred == 0)
|
||||
#expect(metrics.estimatedBatteryDrain == 0)
|
||||
}
|
||||
|
||||
@Test("BatteryUsageMetrics is Codable")
|
||||
func codable() throws {
|
||||
var metrics = BatteryUsageMetrics()
|
||||
metrics.recordSync(duration: 2.5, bytesTransferred: 10240)
|
||||
let data = try JSONEncoder().encode(metrics)
|
||||
let decoded = try JSONDecoder().decode(BatteryUsageMetrics.self, from: data)
|
||||
#expect(decoded.syncCount == 1)
|
||||
#expect(decoded.totalSyncTime == 2.5)
|
||||
#expect(decoded.totalBytesTransferred == 10240)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SyncStatus Extended Tests
|
||||
|
||||
struct SyncStatusExtendedTests {
|
||||
@Test("SyncStatus totalSyncCount starts at 0")
|
||||
func defaultSyncCount() {
|
||||
let status = SyncStatus()
|
||||
#expect(status.totalSyncCount == 0)
|
||||
}
|
||||
|
||||
@Test("SyncStatus syncSummary shows never when no syncs")
|
||||
func syncSummaryNoSyncs() {
|
||||
let status = SyncStatus()
|
||||
#expect(status.syncSummary.contains("No syncs"))
|
||||
}
|
||||
|
||||
@Test("SyncStatus syncSummary shows count and stats")
|
||||
func syncSummaryWithStats() {
|
||||
var status = SyncStatus()
|
||||
status.totalSyncCount = 5
|
||||
status.totalBytesTransferred = 2048
|
||||
status.deltaSyncSavings = 1024
|
||||
let summary = status.syncSummary
|
||||
#expect(summary.contains("5 syncs"))
|
||||
#expect(summary.contains("33%"))
|
||||
}
|
||||
|
||||
@Test("SyncStatus equality includes totalSyncCount")
|
||||
func equalityIncludesSyncCount() {
|
||||
var status1 = SyncStatus()
|
||||
var status2 = SyncStatus()
|
||||
status1.totalSyncCount = 5
|
||||
status2.totalSyncCount = 10
|
||||
#expect(status1 != status2)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SyncStatusManager Extended Tests
|
||||
|
||||
@MainActor
|
||||
struct SyncStatusManagerExtendedTests {
|
||||
@Test("SyncStatusManager completeSync increments totalSyncCount")
|
||||
func incrementSyncCount() {
|
||||
let manager = SyncStatusManager(defaults: UserDefaults(suiteName: UUID().uuidString)!)
|
||||
#expect(manager.status.totalSyncCount == 0)
|
||||
manager.completeSync(bytesTransferred: 100, deltaSavings: 50)
|
||||
#expect(manager.status.totalSyncCount == 1)
|
||||
manager.completeSync(bytesTransferred: 200, deltaSavings: 100)
|
||||
#expect(manager.status.totalSyncCount == 2)
|
||||
}
|
||||
|
||||
@Test("SyncStatusManager persists totalSyncCount")
|
||||
func persistSyncCount() {
|
||||
let defaults = UserDefaults(suiteName: UUID().uuidString)!
|
||||
let manager1 = SyncStatusManager(defaults: defaults)
|
||||
manager1.completeSync(bytesTransferred: 100, deltaSavings: 50)
|
||||
manager1.completeSync(bytesTransferred: 200, deltaSavings: 100)
|
||||
|
||||
let manager2 = SyncStatusManager(defaults: defaults)
|
||||
#expect(manager2.status.totalSyncCount == 2)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - BackgroundTaskScheduler Extended Tests
|
||||
|
||||
struct BackgroundTaskSchedulerExtendedTests {
|
||||
@Test("BackgroundTaskScheduler rescheduleAllTasks does not crash")
|
||||
func rescheduleAllTasks() {
|
||||
let scheduler = BackgroundTaskScheduler()
|
||||
// Should not throw
|
||||
scheduler.rescheduleAllTasks()
|
||||
}
|
||||
|
||||
@Test("BackgroundTaskScheduler rescheduleAllTasks skips when task is running")
|
||||
func rescheduleSkipsWhenRunning() {
|
||||
let scheduler = BackgroundTaskScheduler()
|
||||
// Initially no task running, reschedule should work
|
||||
scheduler.rescheduleAllTasks()
|
||||
#expect(scheduler.currentlyHasRunningTask == false)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - BackgroundSyncService Extended Tests
|
||||
|
||||
struct BackgroundSyncServiceExtendedTests {
|
||||
@Test("BackgroundSyncService battery metrics start at zero")
|
||||
func batteryMetricsStartAtZero() {
|
||||
let metrics = BackgroundSyncService.shared.currentBatteryMetrics
|
||||
#expect(metrics.syncCount == 0)
|
||||
#expect(metrics.totalSyncTime == 0)
|
||||
#expect(metrics.estimatedBatteryDrain == 0)
|
||||
}
|
||||
|
||||
@Test("BackgroundSyncService resetBatteryMetrics clears values")
|
||||
func resetBatteryMetrics() {
|
||||
BackgroundSyncService.shared.resetBatteryMetrics()
|
||||
let metrics = BackgroundSyncService.shared.currentBatteryMetrics
|
||||
#expect(metrics.syncCount == 0)
|
||||
#expect(metrics.totalSyncTime == 0)
|
||||
#expect(metrics.estimatedBatteryDrain == 0)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - ConditionalResponseMetadata Tests
|
||||
|
||||
struct ConditionalResponseMetadataTests {
|
||||
@Test("ConditionalResponseMetadata defaults are correct")
|
||||
func defaults() {
|
||||
let metadata = ConditionalResponseMetadata(
|
||||
etag: nil,
|
||||
lastModified: nil,
|
||||
notModified: false
|
||||
)
|
||||
#expect(metadata.etag == nil)
|
||||
#expect(metadata.lastModified == nil)
|
||||
#expect(metadata.notModified == false)
|
||||
}
|
||||
|
||||
@Test("ConditionalResponseMetadata stores ETag and lastModified")
|
||||
func storesHeaders() {
|
||||
let metadata = ConditionalResponseMetadata(
|
||||
etag: "\"abc123\"",
|
||||
lastModified: "Wed, 21 Oct 2015 07:28:00 GMT",
|
||||
notModified: false
|
||||
)
|
||||
#expect(metadata.etag == "\"abc123\"")
|
||||
#expect(metadata.lastModified == "Wed, 21 Oct 2015 07:28:00 GMT")
|
||||
#expect(metadata.notModified == false)
|
||||
}
|
||||
|
||||
@Test("ConditionalResponseMetadata marks 304 response")
|
||||
func notModified() {
|
||||
let metadata = ConditionalResponseMetadata(
|
||||
etag: "\"abc123\"",
|
||||
lastModified: nil,
|
||||
notModified: true
|
||||
)
|
||||
#expect(metadata.notModified == true)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user