mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-16 05:04:41 +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。
360 lines
16 KiB
Swift
360 lines
16 KiB
Swift
import XCTest
|
|
|
|
@testable import ShadowsocksX_NG2
|
|
|
|
@MainActor
|
|
final class CustomRuleEditingWorkflowTests: XCTestCase {
|
|
func testIPPreviewNormalizesWithoutSavingAndSavePublishesNewRow() async throws {
|
|
let workflow = RulesWorkflow(
|
|
loadDocument: { CustomRuleDocument(rules: []) },
|
|
commitDocument: { RuleDocumentCommit(outcome: .saved, document: $0) },
|
|
builtinSnapshots: BuiltinRuleSnapshots(loader: { rulesFixture($0) }))
|
|
await workflow.refresh()
|
|
let version = workflow.snapshot.version
|
|
var draft = try XCTUnwrap(workflow.makeCustomRuleDraft())
|
|
draft.kind = .ipAddress
|
|
draft.content = " 2001:DB8::1 "
|
|
draft.action = .direct
|
|
let preview = await workflow.previewCustomRule(draft)
|
|
XCTAssertNil(preview.failure)
|
|
XCTAssertEqual(preview.rule?.match, .ipv6CIDR("2001:db8::1/128"))
|
|
XCTAssertEqual(preview.displayContent, "2001:db8::1")
|
|
XCTAssertEqual(workflow.snapshot.version, version)
|
|
XCTAssertFalse(workflow.snapshot.rows.contains { $0.customIDs.contains(draft.id) })
|
|
let outcome = await workflow.saveCustomRule(draft)
|
|
XCTAssertEqual(outcome, .committed(.saved))
|
|
let row = try XCTUnwrap(workflow.snapshot.rows.first { $0.customIDs.contains(draft.id) })
|
|
XCTAssertEqual(row.identity?.match, .ipv6CIDR("2001:db8::1/128"))
|
|
XCTAssertEqual(row.sources, [.custom])
|
|
XCTAssertNotEqual(workflow.snapshot.version, version)
|
|
}
|
|
func testEditingDisabledMergedRuleMovesOnlyCustomMembershipAndKeepsUUID() async throws {
|
|
let original = CustomRule(
|
|
action: .proxy, match: .domainSuffix("old.example"),
|
|
source: RuleSourceIdentity(kind: .custom, upstreamVersion: "imported", label: "User import"))
|
|
let builtin = ProxyRule(action: .proxy, match: original.match)
|
|
var saved = CustomRuleDocument(rules: [original], disabledIdentities: [original.identity])
|
|
let workflow = RulesWorkflow(
|
|
loadDocument: { saved },
|
|
commitDocument: {
|
|
saved = $0
|
|
return RuleDocumentCommit(outcome: .saved, document: saved)
|
|
},
|
|
builtinSnapshots: BuiltinRuleSnapshots(loader: {
|
|
rulesFixture($0, rules: $0 == .gfwlist ? [builtin] : [])
|
|
}))
|
|
await workflow.refresh()
|
|
var draft = try XCTUnwrap(workflow.makeCustomRuleDraft(editing: original.id))
|
|
let unchanged = await workflow.previewCustomRule(draft)
|
|
XCTAssertNil(unchanged.failure, "Editing excludes its own UUID")
|
|
draft.content = "new.example"
|
|
draft.action = .direct
|
|
let preview = await workflow.previewCustomRule(draft)
|
|
XCTAssertTrue(preview.inheritsDisablement)
|
|
XCTAssertEqual(preview.row?.isEnabled, false)
|
|
let result = await workflow.saveCustomRule(draft)
|
|
XCTAssertEqual(result, .committed(.saved))
|
|
XCTAssertEqual(saved.rules.count, 1)
|
|
XCTAssertEqual(saved.rules.first?.id, original.id)
|
|
XCTAssertEqual(saved.rules.first?.source, original.source)
|
|
XCTAssertEqual(
|
|
saved.disabledIdentities,
|
|
[original.identity, RuleIdentity(action: .direct, match: .domainSuffix("new.example"))])
|
|
let oldRow = try XCTUnwrap(workflow.snapshot.rows.first { $0.identity == original.identity })
|
|
XCTAssertEqual(oldRow.sources, [.gfwlist])
|
|
XCTAssertTrue(oldRow.customIDs.isEmpty)
|
|
XCTAssertFalse(oldRow.isEnabled)
|
|
let newRow = try XCTUnwrap(workflow.snapshot.rows.first { $0.customIDs.contains(original.id) })
|
|
XCTAssertEqual(newRow.sources, [.custom])
|
|
XCTAssertFalse(newRow.isEnabled)
|
|
}
|
|
|
|
func testCIDRRequiresAddressAndPrefixAndShowsMaskedNetwork() async throws {
|
|
let workflow = RulesWorkflow(
|
|
loadDocument: { CustomRuleDocument(rules: []) },
|
|
commitDocument: { RuleDocumentCommit(outcome: .saved, document: $0) },
|
|
builtinSnapshots: BuiltinRuleSnapshots(loader: { rulesFixture($0) }))
|
|
await workflow.refresh()
|
|
var draft = try XCTUnwrap(workflow.makeCustomRuleDraft())
|
|
draft.kind = .cidr
|
|
draft.action = .direct
|
|
for invalid in ["/8.8.8.8", "8.8.8.8/", "8.8.8.8", "8.8.8.8/33", "2001:db8::1/129"] {
|
|
draft.content = invalid
|
|
let preview = await workflow.previewCustomRule(draft)
|
|
XCTAssertEqual(preview.failure, .invalidInput(.cidr), invalid)
|
|
}
|
|
for (input, expected) in [
|
|
("8.8.8.9/24", "8.8.8.0/24"), ("2001:db8:1::123/48", "2001:db8:1::/48"),
|
|
] {
|
|
draft.content = input
|
|
let preview = await workflow.previewCustomRule(draft)
|
|
XCTAssertNil(preview.failure)
|
|
XCTAssertEqual(preview.displayContent, expected)
|
|
}
|
|
}
|
|
|
|
func testDuplicateAndFixedLocalConflictAreRejectedWithoutCommit() async throws {
|
|
let existing = CustomRule(action: .direct, match: .ipv4CIDR("8.8.8.8/32"))
|
|
var commits = 0
|
|
let workflow = RulesWorkflow(
|
|
loadDocument: { CustomRuleDocument(rules: [existing]) },
|
|
commitDocument: {
|
|
commits += 1
|
|
return RuleDocumentCommit(outcome: .saved, document: $0)
|
|
},
|
|
builtinSnapshots: BuiltinRuleSnapshots(loader: { rulesFixture($0) }))
|
|
await workflow.refresh()
|
|
var draft = try XCTUnwrap(workflow.makeCustomRuleDraft())
|
|
draft.kind = .ipAddress
|
|
draft.content = "8.8.8.8"
|
|
draft.action = .direct
|
|
let duplicate = await workflow.previewCustomRule(draft)
|
|
XCTAssertEqual(duplicate.failure, .duplicate)
|
|
let duplicateSave = await workflow.saveCustomRule(draft)
|
|
XCTAssertEqual(duplicateSave, .unavailable(.duplicate))
|
|
draft.kind = .cidr
|
|
draft.content = "8.8.8.8/32"
|
|
let cidr = await workflow.previewCustomRule(draft)
|
|
XCTAssertEqual(cidr.failure, .duplicate, "IP and full CIDR have one identity")
|
|
draft.action = .proxy
|
|
draft.kind = .ipAddress
|
|
for input in ["192.168.1.1", "::ffff:192.168.1.1", "::1"] {
|
|
draft.content = input
|
|
let conflict = await workflow.previewCustomRule(draft)
|
|
XCTAssertEqual(conflict.failure, .fixedLocalConflict, input)
|
|
let save = await workflow.saveCustomRule(draft)
|
|
XCTAssertEqual(save, .unavailable(.fixedLocalConflict))
|
|
}
|
|
XCTAssertEqual(commits, 0)
|
|
}
|
|
|
|
func testEquivalentBuiltinAndDisabledOrphanCanBeAddedWithoutEnabling() async throws {
|
|
let builtin = ProxyRule(action: .proxy, match: .domainSuffix("merge.example"))
|
|
let orphan = RuleIdentity(action: .proxy, match: .domainExact("orphan.example"))
|
|
let workflow = RulesWorkflow(
|
|
loadDocument: {
|
|
CustomRuleDocument(rules: [], disabledIdentities: [builtin.identity, orphan])
|
|
},
|
|
commitDocument: { RuleDocumentCommit(outcome: .saved, document: $0) },
|
|
builtinSnapshots: BuiltinRuleSnapshots(loader: {
|
|
rulesFixture($0, rules: $0 == .gfwlist ? [builtin] : [])
|
|
}))
|
|
await workflow.refresh()
|
|
var draft = try XCTUnwrap(workflow.makeCustomRuleDraft())
|
|
draft.content = ".MERGE.Example"
|
|
let preview = await workflow.previewCustomRule(draft)
|
|
XCTAssertNil(preview.failure)
|
|
XCTAssertEqual(preview.displayContent, "merge.example")
|
|
XCTAssertEqual(preview.row?.sources, [.custom, .gfwlist])
|
|
XCTAssertEqual(preview.row?.isEnabled, false)
|
|
let result = await workflow.saveCustomRule(draft)
|
|
XCTAssertEqual(result, .committed(.saved))
|
|
let row = try XCTUnwrap(workflow.snapshot.rows.first { $0.customIDs.contains(draft.id) })
|
|
XCTAssertEqual(row.sources, [.custom, .gfwlist])
|
|
XCTAssertFalse(row.isEnabled)
|
|
var next = try XCTUnwrap(workflow.makeCustomRuleDraft())
|
|
next.kind = .domainExact
|
|
next.content = "orphan.example"
|
|
let orphanPreview = await workflow.previewCustomRule(next)
|
|
XCTAssertEqual(orphanPreview.row?.isEnabled, false)
|
|
let orphanSave = await workflow.saveCustomRule(next)
|
|
XCTAssertEqual(orphanSave, .committed(.saved))
|
|
XCTAssertFalse(
|
|
try XCTUnwrap(workflow.snapshot.rows.first { $0.customIDs.contains(next.id) }).isEnabled)
|
|
}
|
|
|
|
func testDomainBoundaryAndNationalSuffixUseExistingNormalization() async throws {
|
|
let workflow = RulesWorkflow(
|
|
loadDocument: { CustomRuleDocument(rules: []) },
|
|
commitDocument: { RuleDocumentCommit(outcome: .saved, document: $0) },
|
|
builtinSnapshots: BuiltinRuleSnapshots(loader: { rulesFixture($0) }))
|
|
await workflow.refresh()
|
|
var draft = try XCTUnwrap(workflow.makeCustomRuleDraft())
|
|
draft.action = .direct
|
|
for kind in [CustomRuleDraft.Kind.domainExact, .domainSuffix] {
|
|
draft.kind = kind
|
|
for input in [
|
|
"", "例子.cn", "*.example.com", "https://example.com", "example.com/path", "^example$",
|
|
] {
|
|
draft.content = input
|
|
let preview = await workflow.previewCustomRule(draft)
|
|
XCTAssertEqual(preview.failure, .invalidInput(kind), input)
|
|
}
|
|
draft.content = " XN--FIQS8S.Example "
|
|
let valid = await workflow.previewCustomRule(draft)
|
|
XCTAssertNil(valid.failure)
|
|
XCTAssertEqual(valid.displayContent, "xn--fiqs8s.example")
|
|
}
|
|
draft.kind = .domainSuffix
|
|
draft.content = "CN"
|
|
let national = await workflow.previewCustomRule(draft)
|
|
XCTAssertNil(national.failure)
|
|
XCTAssertEqual(national.rule?.match, .domainSuffix("cn"))
|
|
draft.kind = .ipAddress
|
|
for input in ["8.8.8.8/32", "[2001:db8::1]", "8.8.8.8:80", "example.com"] {
|
|
draft.content = input
|
|
let invalid = await workflow.previewCustomRule(draft)
|
|
XCTAssertEqual(invalid.failure, .invalidInput(.ipAddress))
|
|
}
|
|
}
|
|
|
|
func testPreviewExplainsAbsorptionShadowingAndPartialOverlapButAllowsSave() async throws {
|
|
let broad = CustomRule(action: .direct, match: .domainSuffix("coverage.example"))
|
|
let proxy = CustomRule(action: .proxy, match: .domainExact("a.coverage.example"))
|
|
let workflow = RulesWorkflow(
|
|
loadDocument: { CustomRuleDocument(rules: [broad, proxy]) },
|
|
commitDocument: { RuleDocumentCommit(outcome: .saved, document: $0) },
|
|
builtinSnapshots: BuiltinRuleSnapshots(loader: { rulesFixture($0) }))
|
|
await workflow.refresh()
|
|
var draft = try XCTUnwrap(workflow.makeCustomRuleDraft())
|
|
draft.kind = .domainExact
|
|
draft.content = "a.coverage.example"
|
|
draft.action = .direct
|
|
let preview = await workflow.previewCustomRule(draft)
|
|
XCTAssertNil(preview.failure)
|
|
XCTAssertTrue(
|
|
preview.row?.relationships.contains {
|
|
$0.kind == .absorption && $0.extent == .full && $0.covering == [broad.identity]
|
|
} == true)
|
|
XCTAssertTrue(
|
|
preview.row?.relationships.contains {
|
|
$0.kind == .shadowing && $0.extent == .full && $0.covering == [proxy.identity]
|
|
} == true)
|
|
let saved = await workflow.saveCustomRule(draft)
|
|
XCTAssertEqual(saved, .committed(.saved))
|
|
await workflow.analysisTask?.value
|
|
XCTAssertEqual(
|
|
workflow.snapshot.rows.first { $0.customIDs.contains(draft.id) }?.relationships,
|
|
preview.row?.relationships)
|
|
var edited = try XCTUnwrap(workflow.makeCustomRuleDraft(editing: broad.id))
|
|
edited.content = "coverage.example"
|
|
let partial = await workflow.previewCustomRule(edited)
|
|
XCTAssertTrue(
|
|
partial.row?.relationships.contains { $0.kind == .shadowing && $0.extent == .partial } == true
|
|
)
|
|
}
|
|
|
|
func testStaleDraftCannotOverwriteAnotherSavedChangeAndDiscardingDraftDoesNothing() async throws {
|
|
let rule = CustomRule(action: .direct, match: .domainExact("kept.example"))
|
|
var commits = 0
|
|
let workflow = RulesWorkflow(
|
|
loadDocument: { CustomRuleDocument(rules: [rule]) },
|
|
commitDocument: {
|
|
commits += 1
|
|
return RuleDocumentCommit(outcome: .saved, document: $0)
|
|
},
|
|
builtinSnapshots: BuiltinRuleSnapshots(loader: { rulesFixture($0) }))
|
|
await workflow.refresh()
|
|
var discarded = try XCTUnwrap(workflow.makeCustomRuleDraft(editing: rule.id))
|
|
discarded.content = "discarded.example"
|
|
_ = await workflow.previewCustomRule(discarded)
|
|
XCTAssertEqual(commits, 0)
|
|
XCTAssertEqual(
|
|
workflow.snapshot.rows.first { $0.customIDs.contains(rule.id) }?.identity, rule.identity)
|
|
var stale = try XCTUnwrap(workflow.makeCustomRuleDraft())
|
|
stale.content = "new.example"
|
|
await workflow.setEnabled(false, identities: [rule.identity])
|
|
let result = await workflow.saveCustomRule(stale)
|
|
XCTAssertEqual(result, .unavailable(.staleDraft))
|
|
XCTAssertEqual(commits, 1)
|
|
XCTAssertFalse(workflow.snapshot.rows.contains { $0.customIDs.contains(stale.id) })
|
|
}
|
|
|
|
}
|
|
|
|
extension CustomRuleEditingWorkflowTests {
|
|
func testPendingSaveBlocksRepeatsAndFailureKeepsSnapshotAvailableForRetry() async throws {
|
|
let original = CustomRuleDocument(rules: [])
|
|
let started = expectation(description: "save awaiting adapter")
|
|
var continuation: CheckedContinuation<Void, Never>?
|
|
var commits = 0
|
|
let workflow = RulesWorkflow(
|
|
loadDocument: { original },
|
|
commitDocument: { _ in
|
|
commits += 1
|
|
await withCheckedContinuation {
|
|
continuation = $0
|
|
started.fulfill()
|
|
}
|
|
return RuleDocumentCommit(outcome: .persistenceFailed, document: original)
|
|
},
|
|
builtinSnapshots: BuiltinRuleSnapshots(loader: { rulesFixture($0) }))
|
|
await workflow.refresh()
|
|
let version = workflow.snapshot.version
|
|
var draft = try XCTUnwrap(workflow.makeCustomRuleDraft())
|
|
draft.content = "pending.example"
|
|
let pending = Task { await workflow.saveCustomRule(draft) }
|
|
await fulfillment(of: [started], timeout: 3)
|
|
XCTAssertTrue(workflow.snapshot.isCommitting)
|
|
XCTAssertEqual(workflow.snapshot.operationStatus, .updating)
|
|
let repeated = await workflow.saveCustomRule(draft)
|
|
XCTAssertEqual(repeated, .unavailable(.busy))
|
|
XCTAssertNil(workflow.makeCustomRuleDraft())
|
|
continuation?.resume()
|
|
let result = await pending.value
|
|
XCTAssertEqual(result, .committed(.persistenceFailed))
|
|
XCTAssertEqual(commits, 1)
|
|
XCTAssertEqual(workflow.snapshot.version, version)
|
|
XCTAssertEqual(workflow.snapshot.commitOutcome, .persistenceFailed)
|
|
XCTAssertFalse(workflow.snapshot.rows.contains { $0.customIDs.contains(draft.id) })
|
|
let retry = await workflow.previewCustomRule(draft)
|
|
XCTAssertNil(retry.failure)
|
|
}
|
|
|
|
func testSavedEditRejectsStaleDraftAndIncompleteCollection() async throws {
|
|
let rule = CustomRule(action: .direct, match: .domainSuffix("old.example"))
|
|
let original = CustomRuleDocument(rules: [rule])
|
|
let outcome = CustomRuleUpdateOutcome.saved
|
|
let workflow = RulesWorkflow(
|
|
loadDocument: { original },
|
|
commitDocument: { RuleDocumentCommit(outcome: outcome, document: $0) },
|
|
builtinSnapshots: BuiltinRuleSnapshots(loader: { rulesFixture($0) }))
|
|
XCTAssertNil(workflow.makeCustomRuleDraft())
|
|
await workflow.refresh()
|
|
var draft = try XCTUnwrap(workflow.makeCustomRuleDraft(editing: rule.id))
|
|
draft.content = "new.example"
|
|
let result = await workflow.saveCustomRule(draft)
|
|
XCTAssertEqual(result, .committed(outcome))
|
|
XCTAssertEqual(workflow.snapshot.commitOutcome, outcome)
|
|
XCTAssertEqual(
|
|
workflow.snapshot.rows.first { $0.customIDs.contains(rule.id) }?.content, "new.example")
|
|
let stale = await workflow.previewCustomRule(draft)
|
|
XCTAssertEqual(stale.failure, .staleDraft)
|
|
let incomplete = RulesWorkflow(
|
|
loadDocument: { throw CustomRuleStoreError.corrupt(detail: "fixture error") },
|
|
commitDocument: { RuleDocumentCommit(outcome: .saved, document: $0) },
|
|
builtinSnapshots: BuiltinRuleSnapshots(loader: { rulesFixture($0) }))
|
|
await incomplete.refresh()
|
|
XCTAssertNil(incomplete.makeCustomRuleDraft())
|
|
let refused = await incomplete.saveCustomRule(draft)
|
|
XCTAssertEqual(refused, .unavailable(.incompleteCollection))
|
|
}
|
|
}
|
|
|
|
extension CustomRuleEditingWorkflowTests {
|
|
func testDisabledEditCannotInheritDisablementOntoImmutableFixedIdentity() async throws {
|
|
let original = CustomRule(action: .direct, match: .domainExact("disabled.example"))
|
|
var commits = 0
|
|
let workflow = RulesWorkflow(
|
|
loadDocument: {
|
|
CustomRuleDocument(rules: [original], disabledIdentities: [original.identity])
|
|
},
|
|
commitDocument: {
|
|
commits += 1
|
|
return RuleDocumentCommit(outcome: .saved, document: $0)
|
|
},
|
|
builtinSnapshots: BuiltinRuleSnapshots(loader: { rulesFixture($0) }))
|
|
await workflow.refresh()
|
|
var draft = try XCTUnwrap(workflow.makeCustomRuleDraft(editing: original.id))
|
|
draft.kind = .cidr
|
|
draft.content = "127.0.0.0/8"
|
|
let preview = await workflow.previewCustomRule(draft)
|
|
XCTAssertEqual(preview.failure, .fixedPolicyDisablement)
|
|
XCTAssertNil(preview.document)
|
|
let result = await workflow.saveCustomRule(draft)
|
|
guard case .unavailable = result else { return XCTFail("Must reject fixed-policy disablement") }
|
|
XCTAssertEqual(commits, 0)
|
|
XCTAssertFalse(workflow.snapshot.rows.filter(\.isFixed).contains { !$0.isEnabled })
|
|
}
|
|
}
|