Files
ShadowsocksX-NG/ShadowsocksX-NG2/Tests/Servers/ServerNameWorkflowTests.swift
T
Qiu Yuzhou 20a62117f2 refactor(tests): 按功能领域重组 Tests/ 目录
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。
2026-10-05 20:37:35 +08:00

305 lines
14 KiB
Swift

import Foundation
import Testing
@testable import ShadowsocksX_NG2
private struct ServerNameImportCase: Sendable {
let host: String
let port: Int
let supplied: String?
let expected: String
static let all: [Self] = [
.init(host: "203.0.113.7", port: 8388, supplied: nil, expected: "203.0.113.7:8388"),
.init(host: "203.0.113.7", port: 8389, supplied: "", expected: "203.0.113.7:8389"),
.init(host: "server.example", port: 443, supplied: "", expected: "server.example:443"),
.init(host: "2001:db8::7", port: 8388, supplied: " \t\n ", expected: "[2001:db8::7]:8388"),
.init(host: "server.example", port: 8388, supplied: " 香港 01 ", expected: " 香港 01 "),
]
}
/// 名称行为通过目录命令与导入 interface 观察,覆盖持久化和无副作用拒绝。
@MainActor
struct ServerNameWorkflowTests {
@Test(arguments: ["", " \t\n ", "\u{3000}"])
func manualCreateAndEditRejectBlankNamesWithoutWrites(_ name: String) async throws {
try await withWorkflow { workflow, store, credentials in
let id = try await workflow.createServer(Self.draft(name: "原名"), into: nil)
let before = try store.load()
let secretsBefore = credentials.storageSnapshot
let treeBefore = workflow.tree
do {
_ = try await workflow.createServer(Self.draft(name: name), into: nil)
Issue.record("空白名称应拒绝新建")
} catch {
#expect(error as? ServerFormError == .emptyName)
}
do {
try await workflow.updateServer(id, draft: Self.draft(name: name, password: "changed"))
Issue.record("空白名称应拒绝编辑")
} catch {
#expect(error as? ServerFormError == .emptyName)
}
#expect(try store.load() == before)
#expect(credentials.storageSnapshot == secretsBefore)
#expect(workflow.tree == treeBefore)
}
}
@Test
func manualNamesAreTrimmedAndGeneratedNamesSurviveEndpointEdits() async throws {
try await withWorkflow { workflow, store, _ in
let created = try await workflow.createServer(Self.draft(name: " \t新名称\n "), into: nil)
#expect(try workflow.serverEditForm(for: created)?.remark == "新名称")
try await workflow.updateServer(created, draft: Self.draft(name: "\n编辑名称\t"))
#expect(try workflow.serverEditForm(for: created)?.remark == "编辑名称")
_ = await workflow.importServers(
from: [
.clipboardText(
SsUri(method: "aes-256-gcm", password: "p", host: "203.0.113.7", port: 8388)
.encode())
], into: nil)
let imported = try #require(workflow.tree.roots.last)
var edited = Self.draft(name: imported.name)
edited.address = "198.51.100.9"
edited.port = 9999
try await workflow.updateServer(imported.id, draft: edited)
#expect(try workflow.serverEditForm(for: imported.id)?.remark == "203.0.113.7:8388")
#expect(try store.load().catalog.entry(for: imported.id)?.displayName == "203.0.113.7:8388")
}
}
@Test(arguments: ServerNameImportCase.all)
fileprivate func uriImportsSaveNames(_ sample: ServerNameImportCase) async throws {
try await withWorkflow { workflow, store, _ in
let uri = SsUri(
method: "aes-256-gcm", password: "p", host: sample.host, port: sample.port,
remark: sample.supplied)
let result = await workflow.importServers(from: [.clipboardText(uri.encode())], into: nil)
#expect(result.sources.first?.result.importedCount == 1)
let node = try #require(workflow.tree.roots.first)
#expect(node.name == sample.expected)
#expect(try workflow.serverEditForm(for: node.id)?.remark == sample.expected)
#expect(try store.load().catalog.entry(for: node.id)?.displayName == sample.expected)
}
}
@Test(arguments: ServerNameImportCase.all)
fileprivate func legacyImportsSaveNames(_ sample: ServerNameImportCase) async throws {
try await withWorkflow { _, store, credentials in
var profile: [String: Any] = [
"ServerHost": sample.host, "ServerPort": sample.port,
"Method": "aes-256-gcm", "Password": "p",
]
if let supplied = sample.supplied { profile["Remark"] = supplied }
let snapshot = try LegacySnapshot(propertyList: ["ServerProfiles": [profile]])
let importer = LegacyImportService(
source: FixedLegacySnapshotProvider(snapshot: snapshot), catalogStore: store,
credentials: credentials, marker: InMemoryLegacyImportMarker())
let result = try importer.importCurrentSnapshot()
#expect(result.report.importedServerCount == 1)
let catalog = try store.load().catalog
let group = try #require(catalog.entry(for: result.groupID))
guard case .group(let fields) = group.kind else {
Issue.record("预期导入分组")
return
}
let id = try #require(fields.children.first)
let entry = try #require(catalog.entry(for: id))
#expect(entry.displayName == sample.expected)
guard case .server(let server) = entry.kind else {
Issue.record("预期服务器")
return
}
#expect(server.remark == sample.expected)
}
}
@Test(arguments: ServerNameImportCase.all)
fileprivate func subscriptionImportsSaveNames(_ sample: ServerNameImportCase) async throws {
let fetcher = FakeSubscriptionFetcher(behavior: .success(try Self.document(sample)))
try await withWorkflow(fetcher: fetcher) { workflow, store, _ in
let subscription = try await workflow.createSubscription(
urlString: "https://provider.example/s")
let group = try #require(workflow.tree.node(withID: subscription.groupID))
let node = try #require(group.childNodes.first)
#expect(node.name == sample.expected)
#expect(try workflow.serverEditForm(for: node.id)?.remark == sample.expected)
#expect(try store.load().catalog.entry(for: node.id)?.displayName == sample.expected)
}
}
@Test
func subscriptionRenamesKeepIdentityAndRefreshesRegenerateMissingNames() async throws {
let initial = ServerNameImportCase.all[0]
let fetcher = FakeSubscriptionFetcher(behavior: .success(try Self.document(initial)))
try await withWorkflow(fetcher: fetcher) { workflow, _, _ in
let subscription = try await workflow.createSubscription(
urlString: "https://provider.example/s")
let id = try #require(workflow.tree.node(withID: subscription.groupID)?.childNodes.first?.id)
fetcher.setBehavior(
.success(
try Self.document(
.init(host: initial.host, port: initial.port, supplied: "远端新名", expected: "远端新名"))))
await workflow.refreshSubscription(subscription.id)
#expect(workflow.tree.node(withID: id)?.name == "远端新名")
fetcher.setBehavior(.success(try Self.document(initial, password: "updated-password")))
await workflow.refreshSubscription(subscription.id)
#expect(try workflow.serverEditForm(for: id)?.password == "updated-password")
fetcher.setBehavior(.success(try Self.document(initial)))
await workflow.refreshSubscription(subscription.id)
#expect(workflow.tree.node(withID: id)?.name == "203.0.113.7:8388")
fetcher.setBehavior(.success(try Self.document(ServerNameImportCase.all[1])))
await workflow.refreshSubscription(subscription.id)
let replacement = try #require(
workflow.tree.node(withID: subscription.groupID)?.childNodes.first)
#expect(replacement.name == "203.0.113.7:8389")
#expect(replacement.id != id)
}
}
@Test
func subscriptionRefreshKeepsReusedSecretsAndRemovesUnusedPluginOptions() async throws {
let sample = ServerNameImportCase.all[0]
let fetcher = FakeSubscriptionFetcher(
behavior: .success(
try Self.document(
sample, plugin: "v2ray-plugin", pluginOptions: "mode=websocket")))
try await withWorkflow(fetcher: fetcher) { workflow, _, credentials in
let subscription = try await workflow.createSubscription(
urlString: "https://provider.example/s")
let id = try #require(workflow.tree.node(withID: subscription.groupID)?.childNodes.first?.id)
#expect(credentials.storageCount == 3)
fetcher.setBehavior(
.success(
try Self.document(
sample, password: "new-password", plugin: "v2ray-plugin", pluginOptions: "mode=quic")))
await workflow.refreshSubscription(subscription.id)
let updated = try #require(try workflow.serverEditForm(for: id))
#expect(updated.password == "new-password")
#expect(updated.plugin.options == "mode=quic")
#expect(credentials.storageCount == 3)
fetcher.setBehavior(.success(try Self.document(sample, password: "new-password")))
await workflow.refreshSubscription(subscription.id)
let cleared = try #require(try workflow.serverEditForm(for: id))
#expect(cleared.password == "new-password")
#expect(cleared.plugin.selection == .none)
#expect(credentials.storageCount == 2)
#expect(!credentials.storageSnapshot.values.contains("mode=quic"))
}
}
@Test
func duplicateEndpointsRejectRefreshAndPreserveSavedSnapshot() async throws {
let sample = ServerNameImportCase.all[0]
let fetcher = FakeSubscriptionFetcher(behavior: .success(try Self.document(sample)))
try await withWorkflow(fetcher: fetcher) { workflow, store, credentials in
let subscription = try await workflow.createSubscription(
urlString: "https://provider.example/s")
let treeBefore = workflow.tree
let catalogBefore = try store.load().catalog
let secretsBefore = credentials.storageSnapshot
let duplicate = Data(
"""
{"version":1,"servers":[
{"server":"203.0.113.7","server_port":8388,"method":"aes-256-gcm","password":"p","remarks":"甲"},
{"server":"203.0.113.7","server_port":8388,"method":"aes-256-gcm","password":"other","remarks":"乙"}
]}
""".utf8)
fetcher.setBehavior(.success(duplicate))
await workflow.refreshSubscription(subscription.id)
#expect(workflow.tree == treeBefore)
#expect(try store.load().catalog == catalogBefore)
#expect(credentials.storageSnapshot == secretsBefore)
guard case .failed(_, let failure) = workflow.subscriptions.first?.status else {
Issue.record("重复端点应拒绝整个刷新")
return
}
#expect(failure == .duplicateIdentity)
}
}
@Test(arguments: ["server", "server_port", "method", "password", "plugin", "plugin_opts"])
func idLessIdentityDependsOnlyOnEndpoint(_ changedField: String) throws {
let base: [String: Any] = [
"server": "203.0.113.7", "server_port": 8388, "method": "aes-256-gcm",
"password": "p", "plugin": "v2ray-plugin", "plugin_opts": "mode=websocket",
]
let replacements: [String: Any] = [
"server": "203.0.113.8", "server_port": 8389, "method": "aes-128-gcm",
"password": "p2", "plugin": "other-plugin", "plugin_opts": "mode=quic",
]
func identity(_ server: [String: Any]) throws -> NodeID {
let data = try JSONSerialization.data(withJSONObject: ["version": 1, "servers": [server]])
let snapshot = try SubscriptionDocumentParser.parse(
data, subscriptionID: NodeID(rawValue: "sub"))
guard case .server(let leaf) = snapshot.root.children.first else {
throw NSError(domain: "ServerNameTests", code: 1)
}
return leaf.id
}
var changed = base
changed[changedField] = replacements[changedField]
if changedField == "server" || changedField == "server_port" {
#expect(try identity(base) != identity(changed))
} else {
#expect(try identity(base) == identity(changed))
}
}
@Test
func providerIDsAllowDistinctServersAtTheSameEndpoint() throws {
let data = Data(
"""
{"version":1,"servers":[
{"id":"aaaaaaaa-0000-4000-8000-00000000000a","server":"203.0.113.7","server_port":8388,
"method":"aes-256-gcm","password":"p","remarks":"甲"},
{"id":"bbbbbbbb-0000-4000-8000-00000000000b","server":"203.0.113.7","server_port":8388,
"method":"aes-256-gcm","password":"other","remarks":"乙"}
]}
""".utf8)
let snapshot = try SubscriptionDocumentParser.parse(
data, subscriptionID: NodeID(rawValue: "sub"))
#expect(snapshot.root.children.count == 2)
}
}
extension ServerNameWorkflowTests {
private static func draft(name: String, password: String = "p") -> ServerEditDraft {
ServerEditDraft(
address: "203.0.113.7", port: 8388, encryptionMethod: "aes-256-gcm", password: password,
remark: name, plugin: .none, pluginOptions: nil)
}
private static func document(
_ sample: ServerNameImportCase, password: String = "p", plugin: String? = nil,
pluginOptions: String? = nil
) throws -> Data {
var server: [String: Any] = [
"server": sample.host, "server_port": sample.port, "method": "aes-256-gcm",
"password": password,
]
if let supplied = sample.supplied { server["remarks"] = supplied }
if let plugin { server["plugin"] = plugin }
if let pluginOptions { server["plugin_opts"] = pluginOptions }
return try JSONSerialization.data(withJSONObject: ["version": 1, "servers": [server]])
}
private func withWorkflow(
fetcher: SubscriptionFetching = FakeSubscriptionFetcher(behavior: .success(Data())),
_ body:
@MainActor (CatalogWorkflow, CatalogFileStore, InMemoryCredentialStore) async throws -> Void
) async throws {
let directory = FileManager.default.temporaryDirectory
.appendingPathComponent("server-name-tests-\(UUID().uuidString)", isDirectory: true)
defer { try? FileManager.default.removeItem(at: directory) }
let store = CatalogFileStore(fileURL: directory.appendingPathComponent("catalog.json"))
let credentials = InMemoryCredentialStore()
let coordinator = CatalogCommitCoordinator(fileStore: store, runtime: FakeCatalogRuntime())
let workflow = makeCatalogWorkflow(
coordinator: coordinator, credentials: credentials, subscriptionFetcher: fetcher)
try await body(workflow, store, credentials)
}
}