Files
ShadowsocksX-NG/ShadowsocksX-NG2/Tests/SilentLaunchControllerTests.swift
T
Qiu Yuzhou c50470dc89 refactor(settings): move silent-launch persistence to UserDefaults
Migrate SilentLaunchStore from its versioned JSON file to the app's
standard defaults domain (key `silentLaunch`), the platform's canonical
home for GUI-scalar preferences, so the GUI preference category stops
growing one file per item. Reads stay safe-side strict: a missing key
or a value not bridgeable to Bool loads as not silent (plain
bool(forKey:) would coerce "yes" to true). The file-form
persistence-failure naming path is dropped — UserDefaults.set reports
no failure — so SilentLaunchController keeps its persist-then-flip seam
while errorMessage and its SettingsView presentation go away.

The legacy file never shipped in any release; migrateLegacyFileIfPresent
runs in the production composition root only, preserves the value on
developer machines, and deletes the file (no-op when absent). The test
composition uses a dedicated suite cleared at assembly; unit tests use
per-test suites with removePersistentDomain teardown, never the
standard domain.

ADR-0017 carries a dated amendment (Status plus the affected
consequence line); the settings.json separation rationale stands
unchanged.
2026-09-29 16:06:52 +08:00

53 lines
1.6 KiB
Swift

import XCTest
@testable import ShadowsocksX_NG2
/// 静默启动控制器(ADR 0017):初始值来自持久化;翻转先落盘后翻内存。
/// UserDefaults 无失败信号,文件版的「失败保原值并点名」路径不再存在。
@MainActor
final class SilentLaunchControllerTests: XCTestCase {
private var suiteName: String!
private var defaults: UserDefaults!
private var store: SilentLaunchStore!
override func setUpWithError() throws {
try super.setUpWithError()
suiteName = "silent-launch-controller-tests-\(UUID().uuidString)"
defaults = UserDefaults(suiteName: suiteName)!
defaults.removePersistentDomain(forName: suiteName)
store = SilentLaunchStore(defaults: defaults)
}
override func tearDownWithError() throws {
defaults.removePersistentDomain(forName: suiteName)
try super.tearDownWithError()
}
func testInitialValueComesFromStore() {
XCTAssertFalse(
SilentLaunchController(store: store).isEnabled, "无持久化记录按出厂默认:不静默")
store.save(silentLaunchEnabled: true)
XCTAssertTrue(SilentLaunchController(store: store).isEnabled)
}
func testEnablePersistsBeforeFlippingPublishedState() {
let controller = SilentLaunchController(store: store)
controller.setEnabled(true)
XCTAssertTrue(controller.isEnabled)
XCTAssertTrue(store.loadSilentLaunchEnabled())
}
func testDisablePersists() {
store.save(silentLaunchEnabled: true)
let controller = SilentLaunchController(store: store)
controller.setEnabled(false)
XCTAssertFalse(controller.isEnabled)
XCTAssertFalse(store.loadSilentLaunchEnabled())
}
}