import Foundation @testable import suixinkan /// 定位测试 target 的附件资源,避免依赖开发机文件路径或真实网络。 enum TrainingDocumentTestFixtures { /// 返回已打包的本地文档,资源不会由预览器删除。 static func document(kind: TrainingDocumentKind = .txt, resource: String = "training-preview-sample") throws -> TrainingLocalDocument { let bundle = Bundle(for: TrainingDocumentBundleMarker.self) let file = bundle.url(forResource: resource, withExtension: kind.rawValue) ?? bundle.url(forResource: resource, withExtension: kind.rawValue, subdirectory: "Fixtures/TrainingDocuments") guard let file else { throw CocoaError(.fileNoSuchFile) } return TrainingLocalDocument(fileURL: file, kind: kind, name: "\(resource).\(kind.rawValue)", temporaryDirectory: nil) } } /// 给 Bundle 定位测试资源使用的普通 NSObject 类型。 private final class TrainingDocumentBundleMarker: NSObject {} /// 注入真实本地文件,可在下载入口挂起或失败,仍通过正式渲染器展示。 struct TrainingFixtureDocumentLoader: TrainingDocumentLoading { /// 可覆盖一次下载过程;默认依据授权附件类型读取测试文件。 var handler: (@Sendable (TrainingAttachmentInfo, TrainingAttachmentLink) async throws -> TrainingLocalDocument)? /// 创建无网络的文档加载器,允许测试控制请求的完成时机。 init(handler: (@Sendable (TrainingAttachmentInfo, TrainingAttachmentLink) async throws -> TrainingLocalDocument)? = nil) { self.handler = handler } /// 保留正式格式识别规则,只替换文件获取路径。 func load(attachment: TrainingAttachmentInfo, link: TrainingAttachmentLink) async throws -> TrainingLocalDocument { if let handler { return try await handler(attachment, link) } let kind = try TrainingDocumentKind.resolve(name: attachment.name, mimeType: attachment.mimeType, url: link.url) return try TrainingDocumentTestFixtures.document(kind: kind) } }