import Foundation @main struct DetailDemandChecks { @MainActor static func main() async throws { let suite = "kymo-detail-checks-" + UUID().uuidString let defaults = UserDefaults(suiteName: suite)! defer { defaults.removePersistentDomain(forName: suite) } let preferences = SamplingPreferences(defaults: defaults) preferences.panelMilliseconds = 500 preferences.backgroundEnabled = false let probe = ProcessProbe() let store = MetricsStore(sampling: preferences, sampler: SystemSampler(processRead: { probe.read(&$0) })) store.setPanelVisible(true) try await until { store.history.samples.count >= 2 } precondition(probe.count == 0 && store.processSnapshot == nil && store.energy == nil, "Disabled process demand must not enumerate processes or publish process power") store.setDetailDemand(processes: true, slowMetrics: true) try await until { probe.count > 0 } // Withdraw demand while the synchronous OS read still holds the single slot. store.setDetailDemand(processes: false, slowMetrics: true) try await Task.sleep(for: .milliseconds(350)) precondition(store.processSnapshot == nil, "A completion from the withdrawn demand must not publish") let stopped = probe.count try await Task.sleep(for: .milliseconds(650)) precondition(probe.count == stopped && store.snapshot.cpu != nil) store.setDetailDemand(processes: true, slowMetrics: true) try await until { store.processSnapshot != nil } precondition(probe.peak == 1) precondition(store.history.samples.dropFirst().allSatisfy { !$0.breakBefore && $0.cpu != nil }, "Switching detail demand must not reset base CPU deltas or split history") store.setDetailDemand(processes: false, slowMetrics: true) precondition(store.processSnapshot == nil && store.energy == nil) store.togglePause() let pausedCount = store.history.samples.count store.setDetailDemand(processes: true, slowMetrics: true) try await Task.sleep(for: .milliseconds(650)) precondition(store.history.samples.count == pausedCount && store.processSnapshot == nil) store.togglePause() try await until { store.processSnapshot != nil } store.setPanelVisible(false) precondition(store.processSnapshot == nil && store.energy == nil) store.togglePause() print("PASS: disabled demand performs no process reads, unified demand supports both details, withdrawal rejects stale completion, single in-flight read, continuous base history and pause/hidden precedence") } @MainActor private static func until(_ condition: () -> Bool) async throws { let deadline = ContinuousClock.now.advanced(by: .seconds(4)) while !condition() && ContinuousClock.now < deadline { try await Task.sleep(for: .milliseconds(10)) } precondition(condition(), "Timed out waiting for detail demand") } } private final class ProcessProbe: @unchecked Sendable { private let lock = NSLock() private var reads = 0 private var active = 0 private var maximum = 0 var count: Int { lock.lock(); defer { lock.unlock() }; return reads } var peak: Int { lock.lock(); defer { lock.unlock() }; return maximum } func read(_ monitor: inout ProcessMonitor) -> ProcessSnapshot { lock.lock(); reads += 1; active += 1; maximum = max(maximum, active); lock.unlock() defer { lock.lock(); active -= 1; lock.unlock() } Thread.sleep(forTimeInterval: 0.25) return monitor.sample() } }