Files
Kymo/kymoTests/BackgroundTrendChecks.swift
T

63 lines
3.6 KiB
Swift

import Foundation
@main struct BackgroundTrendChecks {
@MainActor static func main() async throws {
let suite = "kymo-background-checks-" + UUID().uuidString
let defaults = UserDefaults(suiteName: suite)!
defer { defaults.removePersistentDomain(forName: suite) }
let preferences = SamplingPreferences(defaults: defaults)
preferences.panelMilliseconds = 2_000
preferences.backgroundMilliseconds = 30_000
preferences.backgroundEnabled = true
let store = MetricsStore(sampling: preferences)
store.setPanelVisible(true)
try await until { store.history.samples.count >= 2 }
store.setPanelVisible(false)
let hiddenCount = store.history.samples.count
try await Task.sleep(for: .seconds(6))
precondition(store.history.samples.count == hiddenCount, "The 30-second background deadline has not arrived")
precondition(store.processSnapshot == nil && store.energy == nil)
store.setPanelVisible(true)
try await until { store.history.samples.count > hiddenCount }
let samples = store.history.samples
let runs = PanelCurveGeometry.runs(dates: samples.map(\.date), values: samples.map(\.memory),
boundaryDates: Set(samples.filter(\.breakBefore).map(\.date)), intervals: samples.map(\.expectedInterval))
precondition(runs.count == 1, "Short hides with background recording enabled must keep a continuous curve")
print("PASS: six-second hide before the first 30-second background sample remains continuous")
// A full background interval must still collect real resource samples without process scans.
preferences.backgroundMilliseconds = 1_000
store.setPanelVisible(false)
let backgroundCount = store.history.samples.count
try await until { store.history.samples.count >= backgroundCount + 2 }
precondition(store.processSnapshot == nil && store.energy == nil)
store.setPanelVisible(true)
let lastBackgroundDate = store.snapshot.date
try await until { store.snapshot.date > lastBackgroundDate }
let recorded = store.history.samples
precondition(PanelCurveGeometry.runs(dates: recorded.map(\.date), values: recorded.map(\.memory),
boundaryDates: Set(recorded.filter(\.breakBefore).map(\.date)), intervals: recorded.map(\.expectedInterval)).count == 1)
print("PASS: hidden resource samples are recorded and reconnect on reopen")
preferences.backgroundEnabled = false
store.setPanelVisible(false)
let stoppedCount = store.history.samples.count
try await Task.sleep(for: .milliseconds(650))
precondition(store.history.samples.count == stoppedCount)
store.setPanelVisible(true)
try await until { store.history.samples.count > stoppedCount }
precondition(store.history.samples.last?.breakBefore == true)
let stopped = store.history.samples
precondition(PanelCurveGeometry.runs(dates: stopped.map(\.date), values: stopped.map(\.memory),
boundaryDates: Set(stopped.filter(\.breakBefore).map(\.date)), intervals: stopped.map(\.expectedInterval)).count == 2)
store.togglePause()
print("PASS: disabling background recording still stops sampling and preserves the real gap")
}
@MainActor private static func until(_ predicate: () -> Bool) async throws {
let deadline = ContinuousClock.now.advanced(by: .seconds(4))
while !predicate() && ContinuousClock.now < deadline { try await Task.sleep(for: .milliseconds(20)) }
precondition(predicate(), "Timed out waiting for a resource sample")
}
}