mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-22 13:34:00 +00:00
与导入分离的「切换到 2.0」确认流程(spec #21 D12): - Domain/LegacyHandoff:硬编码白名单三个 Legacy label(.local/.http/.kcptun), bootout 按 service target(覆盖 plist 缺失错位形态),print 退出码验证 (0/113),`launchctl kill SIGTERM` 有文档后备,绝不泛匹配、不 pkill; 残留 plist(含 KeepAlive 形态)读内容判活跃性后 disable,阻止下次登录复活; 系统代理按特征值判定归属(Legacy 自身 off 模式的判定镜像:本地 PAC URL http://localhost:<port>/proxy.pac、SOCKS/HTTP 地址端口),仅清理归属为 Legacy 的服务,未知所有者点名保留;端口释放门禁用无复用选项的 bind 探测 (Legacy ss-local 带 --reuse-port),未释放不启动 2.0;Legacy 数据零删除。 - App 层:SystemConfigurationLegacyProxyCleaner(授权在最前、失败零影响、 事务回滚);WorkspaceLegacyAppController(共享 bundle id 需排除自身进程); LegacyHandoffSheet 确认/进度/报告呈现,含旧登录项人工指引与残留提示; 主窗口工具栏入口 + 导入完成后「切换到 2.0」入口;交接成功经正常启停入口 启动 2.0(健康门禁走 #29 路径)。 - bootout 实弹验证(Further Notes #5)闭环:docs/research/ legacy-handoff-bootout-live-fire.md——bootout 未加载 label 退出码为 3 (非 print 的 113),KeepAlive 残留 kill 后复活、bootout 后不再加载, 结论已回写产品代码与测试。
143 lines
4.5 KiB
Swift
143 lines
4.5 KiB
Swift
import Foundation
|
|
import XCTest
|
|
|
|
@testable import ShadowsocksX_NG2
|
|
|
|
/// 交接视图模型(issue #37):观察编排次序——确认门禁、执行前停 2.0、成功后
|
|
/// 启动 2.0、端口门禁失败不启动;确认按钮只对确有可停用内容且旧版 app 已退
|
|
/// 出的识别结果开放。
|
|
@MainActor
|
|
final class LegacyHandoffViewModelTests: XCTestCase {
|
|
func testHappyPathStopsProxyHandsOffThenStartsProxy() async {
|
|
let harness = makeHarness()
|
|
|
|
await harness.model.refresh()
|
|
XCTAssertTrue(harness.model.canConfirm)
|
|
|
|
await harness.model.performHandoff()
|
|
|
|
XCTAssertEqual(harness.model.phase, .completed)
|
|
XCTAssertEqual(harness.events, [.stop, .start])
|
|
XCTAssertTrue(harness.marker.completed)
|
|
XCTAssertEqual(harness.model.report?.bootedOutLabels, ["com.qiuyuzhou.shadowsocksX-NG.local"])
|
|
XCTAssertEqual(harness.model.handoffCompleted, true)
|
|
}
|
|
|
|
func testPortsNotReleasedFailsWithoutStartingProxy() async {
|
|
let occupancy = FakeOccupancyProbe()
|
|
occupancy.occupiers = [1086: "ss-local"]
|
|
let harness = makeHarness(occupancy: occupancy)
|
|
|
|
await harness.model.refresh()
|
|
await harness.model.performHandoff()
|
|
|
|
guard case .failed(let reason) = harness.model.phase else {
|
|
return XCTFail("端口未释放应显式失败:\(harness.model.phase)")
|
|
}
|
|
XCTAssertTrue(reason.contains("端口未释放"))
|
|
XCTAssertEqual(harness.events, [.stop])
|
|
XCTAssertFalse(harness.marker.completed)
|
|
}
|
|
|
|
func testLegacyAppRunningBlocksConfirmation() async {
|
|
let app = FakeLegacyAppController()
|
|
app.running = true
|
|
let harness = makeHarness(appController: app)
|
|
|
|
await harness.model.refresh()
|
|
|
|
XCTAssertFalse(harness.model.canConfirm)
|
|
XCTAssertEqual(harness.events, [])
|
|
await harness.model.performHandoff()
|
|
XCTAssertEqual(harness.model.phase, .ready)
|
|
XCTAssertEqual(harness.events, [])
|
|
}
|
|
|
|
func testConfirmationHiddenWhenNothingToStop() async {
|
|
let launchctl = FakeLaunchctl()
|
|
let harness = makeHarness(launchctl: launchctl)
|
|
launchctl.loadedLabels = []
|
|
|
|
await harness.model.refresh()
|
|
|
|
XCTAssertFalse(harness.model.canConfirm)
|
|
XCTAssertFalse(harness.model.hasUnknownState)
|
|
}
|
|
|
|
func testUnknownLaunchctlStateBlocksConfirmation() async {
|
|
let launchctl = FakeLaunchctl()
|
|
launchctl.printFailureDetail[LegacyLaunchAgentLabel.local.rawValue] = "launchctl: adhoc"
|
|
let harness = makeHarness(launchctl: launchctl)
|
|
|
|
await harness.model.refresh()
|
|
|
|
XCTAssertFalse(harness.model.canConfirm)
|
|
XCTAssertTrue(harness.model.hasUnknownState)
|
|
}
|
|
|
|
func testQuitLegacyAppRequestsQuitAndRedetects() async {
|
|
let app = FakeLegacyAppController()
|
|
app.running = true
|
|
let harness = makeHarness(appController: app)
|
|
|
|
await harness.model.refresh()
|
|
XCTAssertFalse(harness.model.canConfirm)
|
|
|
|
harness.model.quitLegacyApp()
|
|
await Task.yield()
|
|
|
|
XCTAssertTrue(app.quitRequested)
|
|
XCTAssertFalse(app.running)
|
|
}
|
|
|
|
// MARK: - 夹具
|
|
|
|
private func makeHarness(
|
|
launchctl: FakeLaunchctl = FakeLaunchctl(),
|
|
occupancy: FakeOccupancyProbe = FakeOccupancyProbe(),
|
|
appController: FakeLegacyAppController = FakeLegacyAppController()
|
|
) -> Harness {
|
|
let marker = FakeHandoffMarker()
|
|
let service = LegacyHandoffService(
|
|
launchctl: launchctl,
|
|
plistInspector: FakePlistInspector(residues: []),
|
|
appController: appController,
|
|
portsProvider: FixedPortsProvider(),
|
|
occupancyProbe: occupancy,
|
|
proxyCleaner: FakeProxyCleaner(
|
|
outcome: LegacyProxyCleanOutcome(cleanedServiceIDs: [], unknownOwnerServiceIDs: [])),
|
|
marker: marker,
|
|
portPollAttempts: 2,
|
|
portPollIntervalNanoseconds: 1,
|
|
removalVerifyAttempts: 2,
|
|
removalVerifyIntervalNanoseconds: 1)
|
|
launchctl.loadedLabels = [LegacyLaunchAgentLabel.local.rawValue]
|
|
let model = LegacyHandoffViewModel(service: service, appController: appController)
|
|
let harness = Harness(model: model, marker: marker, appController: appController)
|
|
model.startProxy = { harness.events.append(.start) }
|
|
model.stopProxy = { harness.events.append(.stop) }
|
|
return harness
|
|
}
|
|
|
|
private final class Harness {
|
|
let model: LegacyHandoffViewModel
|
|
let marker: FakeHandoffMarker
|
|
let appController: FakeLegacyAppController
|
|
var events: [ProxyEvent] = []
|
|
|
|
init(
|
|
model: LegacyHandoffViewModel, marker: FakeHandoffMarker,
|
|
appController: FakeLegacyAppController
|
|
) {
|
|
self.model = model
|
|
self.marker = marker
|
|
self.appController = appController
|
|
}
|
|
}
|
|
|
|
private enum ProxyEvent {
|
|
case start
|
|
case stop
|
|
}
|
|
}
|