Files
ShadowsocksX-NG/ShadowsocksX-NG2/Tests/StatusMenuModelTests.swift
T
Qiu Yuzhou 2fe401cd50 feat(ng2): add catalog workflow module as single UI-facing seam (#41)
建立目录工作流深 module(App/CatalogWorkflow/),收窄主窗口与配置 UI 对
配置目录、凭据存储、订阅快照、Legacy 导入与运行时收敛的访问:

- UI-facing interface:非敏感树/编辑面/订阅 projection、opaque NodeID 查询、
  typed command(ServerEditDraft 等)、typed error 与 structured outcome
  (BatchImportOutcome 逐行点名、RemovalOutcome selection invalidation);
  秘密值只在显式编辑/分享/订阅 URL 命令中出现。
- 服务器编辑:配置与凭据作为一个逻辑变更,CredentialWriteJournal 保证目录
  持久化失败时旧配置与旧凭据都保持有效(story 12/13,含测试)。
- 提交与收敛分离:目录提交成功与 runtimeSync 收敛阶段是独立观察面,运行时
  失败不回滚已提交目录(story 38/39,含测试)。
- 订阅:创建/刷新/换址/删除沿用原子快照、last-good 保留与凭据回滚;卡片
  projection 只含 host,结构化刷新状态由呈现层本地化。
- Legacy 导入端到端编排:不驱动运行时收敛、不自动重复、显式再导入创建独立
  分组;handoff 仍经组合根注入的独立边界。
- Deletion test:删除 CatalogViewModel/Types/+Subscriptions 旧 facade,全部
  视图与状态菜单迁移到新 seam;选择、pane、sheet、alert 状态收归 UI 持有
  (ErrorAlertPresenter)。导入失败行以行号点名,不再回显 ss:// 行内容。

测试经 module interface 观察(395 项全部通过):持久化失败凭据回滚、提交/
收敛阶段分离、收敛失败不回滚、导入不触发运行时等新增场景;既有 Domain、
协调器与运行时测试保持不变。

Out of scope(spec #41):设置编辑、ProxyRuntimeController、Legacy handoff、
新视觉设计、独立编译 target。
2026-09-22 11:39:07 +08:00

154 lines
6.0 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 XCTest
@testable import ShadowsocksX_NG2
/// 状态菜单八项白名单的纯呈现逻辑(spec #21 D11,issue #31):头部状态摘要
/// 映射、HTTP 导出行派生、「切换模式」循环、活动目标级联树快照。
final class StatusMenuModelTests: XCTestCase {
// MARK: - 头部状态摘要
func testSummaryRunningStateIsOnWithNoDetail() {
let summary = StatusMenuModel.summary(
state: .running, mode: .pac, targetPath: "分组A / 香港 01")
XCTAssertTrue(summary.isOn)
XCTAssertEqual(summary.status, "代理运行中")
XCTAssertNil(summary.detail)
}
func testSummaryOffStateIsOff() {
let summary = StatusMenuModel.summary(state: .off, mode: .global, targetPath: nil)
XCTAssertFalse(summary.isOn)
XCTAssertEqual(summary.status, "代理未运行")
XCTAssertNil(summary.detail)
}
func testSummaryFailureStatesCarryNamedDetail() {
// 启动/激活失败:代理未在运行,开关意图为「启动」。
for (state, expectedStatus) in [
(ProxyRuntimeController.ProxyState.launchFailed(detail: "HTTP 端点未就绪"), "启动失败"),
(.activationFailed(reason: "尚未激活任何服务器"), "无法启动"),
] {
let summary = StatusMenuModel.summary(state: state, mode: .manual, targetPath: nil)
XCTAssertFalse(summary.isOn)
XCTAssertEqual(summary.status, expectedStatus)
XCTAssertEqual(summary.detail, failureDetail(of: state))
}
// 系统代理失败:代理本体仍在运行,开关意图保持「停止」。
let summary = StatusMenuModel.summary(
state: .systemProxyFailed(detail: "系统代理写入失败"), mode: .manual, targetPath: nil)
XCTAssertTrue(summary.isOn)
XCTAssertEqual(summary.status, "系统代理未应用")
XCTAssertEqual(summary.detail, "系统代理写入失败")
}
private func failureDetail(
of state: ProxyRuntimeController.ProxyState
) -> String? {
switch state {
case .launchFailed(let detail), .activationFailed(let detail), .systemProxyFailed(let detail):
return detail
default:
return nil
}
}
func testSummaryCarriesModeLabelAndTargetPath() {
let summary = StatusMenuModel.summary(
state: .running, mode: .externalPAC(URL(string: "http://example.com/pac")!),
targetPath: "订阅分组 / 嵌套分组 / 日本 02")
XCTAssertEqual(summary.modeLabel, "外部 PAC")
XCTAssertEqual(summary.targetPath, "订阅分组 / 嵌套分组 / 日本 02")
}
// MARK: - 复制 HTTP 导出行
func testHTTPExportLineUsesLoopbackDefaults() {
XCTAssertEqual(
StatusMenuModel.httpExportLine(settings: SslocalListenSettings()),
"export http_proxy=http://127.0.0.1:1087;export https_proxy=http://127.0.0.1:1087;")
}
func testHTTPExportLineUsesAdvertisedAddressAndPortInHostScope() {
var settings = SslocalListenSettings()
settings.scope = .host(advertisedAddress: "192.168.1.10")
settings.httpPort = 8080
XCTAssertEqual(
StatusMenuModel.httpExportLine(settings: settings),
"export http_proxy=http://192.168.1.10:8080;export https_proxy=http://192.168.1.10:8080;")
}
func testHTTPExportLineUnavailableWhenHTTPInboundDisabled() {
var settings = SslocalListenSettings()
settings.httpProxyEnabled = false
XCTAssertNil(StatusMenuModel.httpExportLine(settings: settings))
}
// MARK: - 全局快捷键「切换模式」循环
func testNextModeCyclesBuiltinModes() {
XCTAssertEqual(StatusMenuModel.nextMode(after: .pac), .global)
XCTAssertEqual(StatusMenuModel.nextMode(after: .global), .manual)
XCTAssertEqual(StatusMenuModel.nextMode(after: .manual), .pac)
}
func testNextModeFromExternalPACReturnsToPAC() {
XCTAssertEqual(
StatusMenuModel.nextMode(after: .externalPAC(URL(string: "http://example.com/pac")!)),
.pac)
}
func testNextModeSkipsLegacyDisabledModes() {
let enabledModes: Set<ProxyModeKind> = [.pac, .manual]
XCTAssertEqual(
StatusMenuModel.nextMode(after: .pac, availableModes: enabledModes),
.manual)
XCTAssertEqual(
StatusMenuModel.nextMode(after: .manual, availableModes: enabledModes),
.pac)
}
// MARK: - 活动目标路径(目录树 projection 派生,issue #41)
/// 目录树 projection 构建夹具(同工作流 module 的推导路径)。
private func makeTree(from catalog: ConfigurationCatalog) -> CatalogTreeSnapshot {
CatalogTreeSnapshot.build(
from: catalog, credentials: InMemoryCredentialStore(),
plugins: NoManagedPluginProvider())
}
func testTargetPathResolvesThroughTreeProjection() throws {
let fixture = try CatalogFixtures.makeSubscriptionFixture()
let tree = makeTree(from: fixture.catalog)
XCTAssertEqual(tree.roots.count, 1)
XCTAssertEqual(
StatusMenuModel.targetPath(in: tree.roots, activeTargetID: fixture.serverIDs[0]),
"订阅分组 / 香港 01")
XCTAssertEqual(
StatusMenuModel.targetPath(in: tree.roots, activeTargetID: fixture.serverIDs[1]),
"订阅分组 / 嵌套分组 / 日本 02")
}
func testTargetPathMarksGroupTargetAndAddressFallback() throws {
var catalog = ConfigurationCatalog()
let serverID = NodeID(rawValue: "manual:server")
try catalog.addServer(CatalogFixtures.serverFields(remark: ""), id: serverID)
let groupID = NodeID(rawValue: "manual:group")
try catalog.addGroup("本地分组", id: groupID)
let tree = makeTree(from: catalog)
XCTAssertEqual(
StatusMenuModel.targetPath(in: tree.roots, activeTargetID: groupID), "本地分组")
XCTAssertEqual(
StatusMenuModel.targetPath(in: tree.roots, activeTargetID: serverID), "203.0.113.7")
}
func testTargetPathNilWhenAbsentOrMissing() throws {
let fixture = try CatalogFixtures.makeSubscriptionFixture()
let tree = makeTree(from: fixture.catalog)
XCTAssertNil(StatusMenuModel.targetPath(in: tree.roots, activeTargetID: nil))
XCTAssertNil(
StatusMenuModel.targetPath(in: tree.roots, activeTargetID: NodeID(rawValue: "gone")),
"目标已不在树中为 nil")
}
}