Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
}
}

/// Manages KeyPath application preferences and settings

Check warning on line 169 in Sources/KeyPathAppKit/Services/Configuration/PreferencesService.swift

View workflow job for this annotation

GitHub Actions / code-quality

A doc comment should be attached to a declaration (orphaned_doc_comment)
// SAFETY: @unchecked Sendable β€” all mutable state is backed by UserDefaults (thread-safe)
// and the shared instance is confined to @MainActor. @Observable macro prevents conforming
// to Sendable directly; @unchecked bridges the gap.
Expand All @@ -179,7 +179,13 @@
static var canonicalDefaults: UserDefaults {
TestEnvironment.isRunningTests
? .standard
: UserDefaults(suiteName: "com.keypath.KeyPath")!
: canonicalDefaults(using: UserDefaults(suiteName: "com.keypath.KeyPath"))
}

/// The suite can be unavailable during early app launch. Preferences must
/// still initialize so launchd does not restart the app in a crash loop.
static func canonicalDefaults(using suiteDefaults: UserDefaults?) -> UserDefaults {
suiteDefaults ?? .standard
}

static let leaderKeyPreferenceKey = "KeyPath.LeaderKey.Preference"
Expand Down
10 changes: 10 additions & 0 deletions Tests/KeyPathTests/PreferencesServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import XCTest

@MainActor
final class PreferencesServiceTests: XCTestCase {
func testCanonicalDefaultsFallsBackToStandardWhenSuiteIsUnavailable() {
let key = "PreferencesServiceTests.canonicalDefaultsFallback"
let defaults = PreferencesService.canonicalDefaults(using: nil)
defer { UserDefaults.standard.removeObject(forKey: key) }

defaults.set(true, forKey: key)

XCTAssertTrue(UserDefaults.standard.bool(forKey: key))
}

// MARK: - Port Validation

func testIsValidPort_AcceptsUserPorts() {
Expand Down
Loading