Files
ShadowsocksX-NG/ShadowsocksX-NG2/Tests/SettingsWorkflowTests.swift
T
Qiu Yuzhou 7d0ebc79b4 refactor(tests): drop TEST_HOST by linking tests against app-source static lib
单测不再以 app 可执行为宿主:新增 ShadowsocksX-NG2Core 静态库 target 同源
编译 App/(除 @main)与 Domain/,模块名沿用 ShadowsocksX_NG2 使全部测试的
@testable import 零改动;app target 模块改名 ShadowsocksX_NG2App 避免共享
Products 目录 swiftmodule 撞名。两个测试 target 改链 Core 后 xcodegen 不再
生成 TEST_HOST,xcodebuild test 只构建不启动 app——与安装版运行实例零接触
(pgrep 全程验证无 app 进程)。同源双编译沿用 Agent/Helper 共享 Domain 文件
的既定模式,Core 只被测试 target 依赖,发布链不编译它。

随之删除 MainApp.swift 的整套测试宿主脚手架(isUnitTesting/makeTesting/
Noop 服务/Ephemeral 存储,-142 行),app 在测试下不再启动,守卫失去存在
理由。宿主移除暴露三处 Bundle.main 依赖,改为显式缝:控制器新增 appBundle
注入(内置规则快照 BuiltinRuleCatalog 与 LaunchDaemon 清单指纹
launchDaemonPlistData(in:) 均按 bundle 读取,生产默认 .main 不变);XPC
listener delegate 期望客户端身份改为可注入(测试按本进程 SecCode 自查)。
测试经 AppArtifact.bundle(沿测试包落盘位置向上查找 app 产物,兼容
TEST_HOST 内嵌与平级两种布局)统一取产物。

665 单测 + 11 冒烟全绿,task build 生产构建不受影响。
2026-09-30 20:44:13 +08:00

89 lines
3.5 KiB
Swift

import XCTest
@testable import ShadowsocksX_NG2
/// SettingsWorkflow 与真实 ProxyRuntimeController 的监听设置提交集成。
@MainActor
final class SettingsWorkflowTests: XCTestCase {
private var runtime: ProxyRuntimeFixture.TemporaryRuntime!
private var catalogFileURL: URL!
private var activationFileURL: URL!
private var credentials: InMemoryCredentialStore!
private var agent: ProxyRuntimeFixture.FakeLaunchAgent!
private var systemProxy: ProxyRuntimeFixture.FakeSystemProxy!
private var settingsStore: InMemorySettingsStore!
override func setUp() async throws {
try await super.setUp()
runtime = ProxyRuntimeFixture.makeTemporaryRuntime()
catalogFileURL = runtime.directory.appendingPathComponent("catalog.json")
activationFileURL = runtime.directory.appendingPathComponent("activation.json")
credentials = InMemoryCredentialStore()
agent = ProxyRuntimeFixture.FakeLaunchAgent()
systemProxy = ProxyRuntimeFixture.FakeSystemProxy()
settingsStore = InMemorySettingsStore()
}
override func tearDown() async throws {
try? FileManager.default.removeItem(at: runtime.directory)
try await super.tearDown()
}
func testListenerModeSavePersistsWithoutStartingAnOffAgent() async throws {
let pair = makePair()
let outcome = await pair.workflow.saveListenerMode(.allIPv4AndIPv6Interfaces)
XCTAssertEqual(outcome, .saved(unknownOccupancy: []))
XCTAssertEqual(pair.controller.settings.listen.listenerMode, .allIPv4AndIPv6Interfaces)
XCTAssertEqual(settingsStore.saved?.listen.listenerMode, .allIPv4AndIPv6Interfaces)
XCTAssertEqual(pair.controller.state, .off)
XCTAssertEqual(agent.registerCount, 0)
}
private func makePair() -> (controller: ProxyRuntimeController, workflow: SettingsWorkflow) {
let catalogSnapshotReader = CatalogCommitCoordinator.bootstrap(
fileStore: CatalogFileStore(fileURL: catalogFileURL)
).catalogSnapshotReader
let runtimeFileStore = RuntimeFileStore(fileURL: runtime.contract)
agent.onRegister = { [runtimeFileStore] in
guard let document = runtimeFileStore.loadDocument() else { return }
try? runtimeFileStore.writeRuntimeReceipt(for: document, processID: 42)
}
let controller = ProxyRuntimeController(
catalogSnapshotReader: catalogSnapshotReader,
activationFileStore: ActivationStateFileStore(fileURL: activationFileURL),
runtimeFileStore: runtimeFileStore,
credentials: credentials,
plugins: ActivationFixture.plugins,
listenRestore: RestoredListenSettings(
settings: ActivationFixture.listen, unreadableError: nil),
settingsStore: settingsStore,
appBundle: AppArtifact.bundle,
agent: agent,
probe: ProxyRuntimeFixture.FakeProbe.reachable(),
systemProxy: systemProxy,
systemProxyHelper: ProxyRuntimeFixture.FakeSystemProxyHelperService(),
firewallExecutableURLs: [URL(fileURLWithPath: "/bundle/Helpers/sslocal")],
firewallPollIntervalNanoseconds: 1_000_000,
launchHealthTimeoutSeconds: 0.05,
sendSignal: { _, _ in 0 },
processIsAlive: { $0 == 42 })
let workflow = SettingsWorkflow(
committing: controller, occupancyProbe: FakeOccupancyProbe())
return (controller, workflow)
}
final class InMemorySettingsStore: ProxySettingsStoring {
var saved: ProxySettings?
func load() throws -> ProxySettings {
saved ?? ProxySettings()
}
func save(_ settings: ProxySettings) throws {
saved = settings
}
}
}