Files
ShadowsocksX-NG/ShadowsocksX-NG2/Tests/ProxyRuntimeControllerTests+ResyncAndSettings.swift
T
Qiu Yuzhou effae048b8 feat(settings): remove holistic preferences reset
Not every preference warrants a reset; future resets are per
configuration item, added only where a field truly needs one (tracked
in #69). The whole chain goes: the 恢复默认 button and its confirmation
alert in SettingsView, the SettingsWorkflow reset/confirm/cancel
commands and the confirmation machinery they existed for (the
SettingsConfirmation enum had a single case), the
SettingsCommitting.resetPreferences seam entry, the runtime
resetPreferences() and ProxySettingsFileStore.reset() with its
file-rollback helpers and the rollbackFailed store error.

CONTEXT.md drops the three stale reset phrases and now records the
domain decision: no restore-all-factory-defaults action for
preferences; a reset, where it exists, is scoped to one configuration
item. ADR-0004/0005/0011 carry amendment notes since their reset
sentences describe a removed capability (0004's also predate the
PAC removal).
2026-09-27 13:29:32 +08:00

97 lines
3.9 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import Combine
import XCTest
@testable import ShadowsocksX_NG2
extension ProxyRuntimeControllerTests {
// MARK: GUI 重启重同步(D5)
func testResyncWithRegisteredAgentAndMatchingContractSkipsRewrite() async throws {
let seeded = try makeSeededCatalog()
let settingsStore = InMemoryProxySettingsStore()
let enabledSettings = ProxySettings(listen: ActivationFixture.listen, agentEnabled: true)
let controller = makeController(
probe: ProxyRuntimeFixture.FakeProbe.reachable(), agentStatus: .notRegistered,
settingsStore: settingsStore, settings: enabledSettings)
try await controller.activate(seeded.server)
await controller.setAgentEnabled(true)
XCTAssertTrue(agent.registerCount >= 1)
let contractMtime =
try FileManager.default.attributesOfItem(
atPath: runtime.contract.path
)[.modificationDate] as? Date
// 模拟 GUI 重启:全新控制器,agent 已注册、wrapper pid 存活(本测试进程)。
try Data("\(ProcessInfo.processInfo.processIdentifier)".utf8).write(to: runtime.pidFile)
let restarted = makeController(
probe: ProxyRuntimeFixture.FakeProbe.reachable(), agentStatus: .registered,
settingsStore: settingsStore, settings: enabledSettings)
await restarted.resyncOnLaunch()
XCTAssertEqual(restarted.state, .running)
XCTAssertEqual(agent.registerCount, 1, "已注册不再重复注册")
let afterResyncMtime =
try FileManager.default.attributesOfItem(
atPath: runtime.contract.path
)[.modificationDate] as? Date
XCTAssertEqual(contractMtime, afterResyncMtime, "相同契约内容跳过写入(幂等)")
XCTAssertTrue(
signals.signalsSent.filter { $0.signal != 0 }.isEmpty,
"内容一致不需要 reload 信号")
}
func testLegacyImportBoundaryStopsRuntimeWithoutRestoringSystemProxy() async throws {
let seeded = try makeSeededCatalog()
let settingsStore = InMemoryProxySettingsStore()
var importedSettings = ProxySettings()
importedSettings.preferredMode = .global
settingsStore.saved = importedSettings
let controller = makeController(
probe: ProxyRuntimeFixture.FakeProbe.reachable(),
settingsStore: settingsStore,
systemProxy: systemProxy)
try await controller.activate(seeded.server)
await controller.setAgentEnabled(true)
XCTAssertEqual(controller.state, .running)
await controller.legacyImportDidCommit()
XCTAssertEqual(controller.state, .off)
XCTAssertEqual(controller.settings, importedSettings)
XCTAssertEqual(controller.activeTargetID, seeded.server)
XCTAssertEqual(systemProxy.restoreCount, 0, "Legacy 导入不能写入或恢复系统代理")
XCTAssertTrue(systemProxy.applied.isEmpty, "导入边界不应重新应用系统代理")
}
func testUpdatingSettingsPersistsAndReactivatesTheRuntimeContract() async throws {
let seeded = try makeSeededCatalog()
let settingsStore = InMemoryProxySettingsStore()
let controller = makeController(
probe: ProxyRuntimeFixture.FakeProbe.reachable(),
settingsStore: settingsStore,
settings: ProxySettings(
listen: ActivationFixture.listen, agentEnabled: true, systemProxyEnabled: true))
try await controller.activate(seeded.server)
await controller.setAgentEnabled(true)
var next = controller.settings
next.timeoutSeconds = 120
next.verboseLogging = true
next.proxyExceptions = "localhost, 127.0.0.1"
try await controller.updateSettings(next)
let document = try XCTUnwrap(RuntimeFileStore(fileURL: runtime.contract).loadDocument())
XCTAssertEqual(controller.settings, next)
XCTAssertEqual(settingsStore.saved, next)
XCTAssertEqual(document.timeout, 120)
XCTAssertTrue(document.listen.verbose)
XCTAssertEqual(
systemProxy.applied.last?.exceptions,
FixedLocalProxyRanges.systemProxyExceptions(including: ["localhost", "127.0.0.1"]),
"系统代理意图开启时随设置更新重新应用")
}
}