mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-23 22:36:55 +00:00
Tests/ 根层原有 138 个平铺测试文件,无法从目录读出测试覆盖的功能领域。 按功能领域重组为 17 个目录(Catalog/Workflow 与 Rules/Custom 两处嵌套,分别 对应 App/Application/CatalogWorkflow 与 Domain/Rules/Custom):Architecture / Support / Agent / Activation / Catalog / Subscription / Rules / ProxyRuntime / ProxyControl / Runtime / Settings / SystemProxy / Plugin / Diagnostics / Credentials / Workspace / Servers;Smoke/ 保持独立 target 不动。138 个移动中 132 个是 100% 相似度的纯重命名,其余 6 个在重命名之上带路径反推改写(见下)。 本次不是纯搬移:测试代码比生产代码多一层位置耦合,有 7 处用固定层数的 deletingLastPathComponent() 从 #filePath 反推仓库路径(都假定文件就在 Tests/ 根层)。这 7 处改走新增的 Tests/Support/TestSourceTree.swift——从调用点沿父 目录上溯找到含 project.yml 的目录,因此不再依赖父目录层数,以后移动测试文件 也不会静默指错。 配套改动: - 新增 Tests 布局守卫 TestsLayoutTests,断言 Tests/ 根层无平铺 .swift。比 Domain 那条弱一档——根层的 Info.plist(info.path 生成物)与 Fixtures/RuleSnapshots 下的 JSON(folder resource)都必须保留,不能用 「树下只有 .swift」覆盖。 - project.yml:Smoke target 按文件引用的 Tests/AppArtifact.swift 改指 Tests/Support/;两处注释里写死的 Tests/SkeletonTests.swift 随移动更新。 excludes 的裸名 "Smoke" / "Fixtures" 仍匹配 Tests/ 直属子目录,无需改动。 - README 的 Tests 条目展开为目录说明,并修掉 Domain 条目里写死的 Tests/SkeletonTests.swift。 - 顺带删去 DomainLayoutTests 文档注释里「整理前 62 个文件」这类易漂移的数字。 验证:xcodegen generate 后单测 target 为 139 个源文件(全部非 Smoke 文件,0 个 Smoke 文件混入)且仍以 Fixtures/RuleSnapshots 为资源,Smoke target 为 8 个 Smoke 文件加 AppArtifact;依赖路径反推的 6 个测试类全绿(路径指错即失败); task format 未改写任何被移动文件;task lint 与整理前同为 21 条内容型 warning; build 0 error;task test 1025 用例全通过(整理前 1024,+1 为新守卫); TestsLayoutTests 经变异验证(根层投放探针即失败);packaging-gate ALL CHECKS PASSED。
185 lines
7.7 KiB
Swift
185 lines
7.7 KiB
Swift
import XCTest
|
|
|
|
@testable import ShadowsocksX_NG2
|
|
|
|
/// 订阅快照的目录应用语义(issue #35):固定分组身份保留;远端删除不留
|
|
/// 墓碑;结构、字段与顺序远端权威,不存在本地启用覆盖层。
|
|
final class SubscriptionSnapshotApplicationTests: XCTestCase {
|
|
private let groupID = NodeID(rawValue: "sub1:fixed-group")
|
|
|
|
private func makeCatalog() throws -> ConfigurationCatalog {
|
|
var catalog = ConfigurationCatalog()
|
|
try catalog.addGroup("订阅分组", source: .subscription, id: groupID)
|
|
let nested = NodeID(rawValue: "sub1:g:jp")
|
|
try catalog.addGroup("日本", source: .subscription, id: nested, to: groupID)
|
|
let serverA = NodeID(rawValue: "sub1:id:a")
|
|
try catalog.addServer(
|
|
CatalogFixtures.serverFields(remark: "香港 01"), source: .subscription, id: serverA,
|
|
to: groupID)
|
|
let serverB = NodeID(rawValue: "sub1:id:b")
|
|
try catalog.addServer(
|
|
CatalogFixtures.serverFields(remark: "日本 02"), source: .subscription, id: serverB,
|
|
to: nested)
|
|
return catalog
|
|
}
|
|
|
|
private func leaf(_ id: String, fields: ServerFields = CatalogFixtures.serverFields(remark: ""))
|
|
-> CatalogSubscriptionSnapshot.Child
|
|
{
|
|
.server(CatalogSubscriptionSnapshot.ServerLeaf(id: NodeID(rawValue: id), fields: fields))
|
|
}
|
|
|
|
private func group(_ id: String, _ name: String, children: [CatalogSubscriptionSnapshot.Child])
|
|
-> CatalogSubscriptionSnapshot.Child
|
|
{
|
|
.group(
|
|
CatalogSubscriptionSnapshot.Group(id: NodeID(rawValue: id), name: name, children: children))
|
|
}
|
|
|
|
// MARK: 快照应用
|
|
|
|
func testSnapshotReplacesSubtreeWithoutLocalEnableOverlay() throws {
|
|
var catalog = try makeCatalog()
|
|
|
|
let document = CatalogSubscriptionSnapshot(
|
|
name: "远端新名",
|
|
root: .init(
|
|
id: groupID, name: "ignored-root",
|
|
children: [
|
|
leaf("sub1:id:b"),
|
|
group(
|
|
"sub1:g:jp", "JP", children: [leaf("sub1:id:a"), leaf("sub1:id:new")]),
|
|
]))
|
|
let removed = try catalog.applySubscriptionSnapshot(document, into: groupID)
|
|
|
|
XCTAssertEqual(removed.count, 2, "旧树两台服务器全部视为移除(b 从嵌套组移到根)")
|
|
// 固定分组身份保持不变,名称跟随远端;远端子序原样落目录。
|
|
let fixed = try XCTUnwrap(catalog.entry(for: groupID))
|
|
XCTAssertEqual(
|
|
try catalog.children(of: groupID),
|
|
[NodeID(rawValue: "sub1:id:b"), NodeID(rawValue: "sub1:g:jp")])
|
|
guard case .group(let fields) = fixed.kind else { return XCTFail("固定分组应存在") }
|
|
XCTAssertEqual(fields.name, "远端新名")
|
|
XCTAssertEqual(fixed.source, .subscription)
|
|
// 旧树已重建,无墓碑:固定分组 + 根序 b + 嵌套组 + a + 新节点。
|
|
XCTAssertEqual(catalog.entries.count, 5)
|
|
}
|
|
|
|
func testRemovedRemoteMembersLeaveNoTombstone() throws {
|
|
var catalog = try makeCatalog()
|
|
let removedTarget = NodeID(rawValue: "sub1:id:b")
|
|
|
|
let document = CatalogSubscriptionSnapshot(
|
|
name: "订阅分组", root: .init(id: groupID, name: "", children: [leaf("sub1:id:a")]))
|
|
_ = try catalog.applySubscriptionSnapshot(document, into: groupID)
|
|
|
|
XCTAssertNil(catalog.entry(for: removedTarget), "远端删除即移除")
|
|
XCTAssertNil(catalog.entry(for: NodeID(rawValue: "sub1:g:jp")), "空嵌套组一并移除")
|
|
XCTAssertEqual(catalog.entries.count, 2)
|
|
}
|
|
|
|
func testFixedGroupStateIsFullyRemoteOwnedOnRefresh() throws {
|
|
var catalog = try makeCatalog()
|
|
|
|
let document = CatalogSubscriptionSnapshot(
|
|
name: "新名", root: .init(id: groupID, name: "", children: [leaf("sub1:id:a")]))
|
|
_ = try catalog.applySubscriptionSnapshot(document, into: groupID)
|
|
|
|
XCTAssertEqual(try XCTUnwrap(catalog.entry(for: groupID)).displayName, "新名")
|
|
}
|
|
|
|
func testApplyToManualGroupRejected() throws {
|
|
var catalog = ConfigurationCatalog()
|
|
let manual = try catalog.addGroup("手动组")
|
|
let document = CatalogSubscriptionSnapshot(
|
|
name: "x", root: .init(id: groupID, name: "", children: []))
|
|
|
|
XCTAssertThrowsError(try catalog.applySubscriptionSnapshot(document, into: manual))
|
|
}
|
|
|
|
func testApplyToMissingGroupRejected() throws {
|
|
var catalog = ConfigurationCatalog()
|
|
let document = CatalogSubscriptionSnapshot(
|
|
name: "x", root: .init(id: groupID, name: "", children: []))
|
|
|
|
XCTAssertThrowsError(
|
|
try catalog.applySubscriptionSnapshot(document, into: NodeID(rawValue: "ghost")))
|
|
}
|
|
|
|
// MARK: 订阅子树删除
|
|
|
|
func testRemoveSubscriptionSubtreeRemovesEverythingAndReportsCredentials() throws {
|
|
var catalog = try makeCatalog()
|
|
let manual = try catalog.addGroup("手动组", id: NodeID(rawValue: "manual:g"))
|
|
_ = try catalog.addTestServer("手动服务器", id: NodeID(rawValue: "manual:s"), to: manual)
|
|
|
|
let removed = try catalog.removeSubscriptionSubtree(of: groupID)
|
|
|
|
XCTAssertEqual(removed.count, 4, "固定分组 + 嵌套组 + 两台服务器")
|
|
XCTAssertEqual(catalog.entries.count, 2, "手动子树不受影响")
|
|
XCTAssertFalse(catalog.rootChildren.contains(groupID))
|
|
XCTAssertTrue(removed.allSatisfy { $0.source == .subscription })
|
|
// 返回条目带凭据引用供调用方清理。
|
|
let credentialRefs = removed.compactMap { entry -> CredentialReference? in
|
|
if case .server(let fields) = entry.kind { return fields.passwordRef }
|
|
return nil
|
|
}
|
|
XCTAssertEqual(credentialRefs.count, 2)
|
|
}
|
|
|
|
func testRemoveMissingSubscriptionRejected() {
|
|
var catalog = ConfigurationCatalog()
|
|
XCTAssertThrowsError(
|
|
try catalog.removeSubscriptionSubtree(of: NodeID(rawValue: "ghost")))
|
|
}
|
|
|
|
// MARK: 订阅子树变更后重展开(接线语义:issue #35 验收 4)
|
|
|
|
func testActiveTargetInsideRefreshedSubtreeStillExpandsAfterSnapshot() throws {
|
|
var catalog = try makeCatalog()
|
|
var machine = ActivationStateMachine(activeTargetID: NodeID(rawValue: "sub1:id:a"))
|
|
let credentials = InMemoryCredentialStore()
|
|
|
|
// 快照后 a 仍在(身份不变)→ 目标有效,重展开产出部署效应。叶子无插件,
|
|
// 激活校验只依赖凭据解析;快照重建后按目录里的真实引用解析秘密。
|
|
let document = CatalogSubscriptionSnapshot(
|
|
name: "订阅分组",
|
|
root: .init(
|
|
id: groupID, name: "",
|
|
children: [
|
|
.server(
|
|
CatalogSubscriptionSnapshot.ServerLeaf(
|
|
id: NodeID(rawValue: "sub1:id:a"),
|
|
fields: ServerFields(
|
|
address: "203.0.113.1", port: 8388,
|
|
encryptionMethod: "aes-256-gcm", passwordRef: .fresh())))
|
|
]))
|
|
_ = try catalog.applySubscriptionSnapshot(document, into: groupID)
|
|
for entry in catalog.entries.values {
|
|
if case .server(let fields) = entry.kind {
|
|
try credentials.save("resolved", for: fields.passwordRef)
|
|
}
|
|
}
|
|
|
|
let effect = machine.catalogDidCommit(
|
|
catalog, credentials: credentials, plugins: NoManagedPluginProvider(),
|
|
options: RuntimeDocumentOptions(listen: SslocalListenSettings()))
|
|
guard case .deployed(let configuration) = try XCTUnwrap(effect) else {
|
|
return XCTFail("身份未变的活动目标应部署而非清除")
|
|
}
|
|
XCTAssertEqual(configuration.targetID, NodeID(rawValue: "sub1:id:a"))
|
|
|
|
// 快照清空该目标 → 清除并停止。
|
|
let empty = CatalogSubscriptionSnapshot(
|
|
name: "订阅分组", root: .init(id: groupID, name: "", children: []))
|
|
_ = try catalog.applySubscriptionSnapshot(empty, into: groupID)
|
|
let cleared = machine.catalogDidCommit(
|
|
catalog, credentials: credentials, plugins: NoManagedPluginProvider(),
|
|
options: RuntimeDocumentOptions(listen: SslocalListenSettings()))
|
|
guard case .clearedAndStopped = try XCTUnwrap(cleared) else {
|
|
return XCTFail("目标随远端删除消失后应清除并停止")
|
|
}
|
|
XCTAssertNil(machine.activeTargetID)
|
|
}
|
|
}
|