[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M6G2PY8B2FWZTWPZ84H83Y7C":3,"skill-same:01M6G2PY8B2FWZTWPZ84H83Y7C":67},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":23,"publishedAt":38,"linkOnly":39,"markdownContent":40,"markdownBody":41,"frontMatter":42,"refName":44,"commitSha":45,"skillDirectory":46,"forksCount":47,"repositoryHtmlUrl":48,"repositoryHomepageUrl":6,"ownerHtmlUrl":49,"ownerBlogUrl":50,"ownerDescription":51,"updatedAt":52,"install":53,"resources":66},"01M6G2PY8B2FWZTWPZ84H83Y7C","swift-protocol-di-testing",null,"Protocol-based dependency injection for testable Swift code — mock file system, network, and external APIs using focused protocols and Swift Testing.","oabdelmaksoud\u002FAgention","oabdelmaksoud","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F50699149?v=4","01M6D5CVVN778ZCDP1WHS816SP","Omar Abdelmaksoud","01M6D8A4TE76V3DCSH3D94VG1V",2,"MIT","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Fswift-protocol-di-testing\u002FSKILL.md",[18],{"categoryNo":19,"categoryName":20,"taxonomyType":21,"taxonomyTypeDesc":22},"01JBSEEDCAT000000000000008","测试与质量","SCENARIO","使用场景",[24,25,26,27,28,29,30,31,32,33,34,35,36,37],"testing","swift","protocol","swift protocol di testing","code","protocols","file","network","injection","system","dependency","swift testing","dependency injection","mock","2026-11-10 10:12:30",false,"---\nname: swift-protocol-di-testing\ndescription: Protocol-based dependency injection for testable Swift code — mock file system, network, and external APIs using focused protocols and Swift Testing.\norigin: ECC\n---\n\n# Swift Protocol-Based Dependency Injection for Testing\n\nPatterns for making Swift code testable by abstracting external dependencies (file system, network, iCloud) behind small, focused protocols. Enables deterministic tests without I\u002FO.\n\n## When to Activate\n\n- Writing Swift code that accesses file system, network, or external APIs\n- Need to test error handling paths without triggering real failures\n- Building modules that work across environments (app, test, SwiftUI preview)\n- Designing testable architecture with Swift concurrency (actors, Sendable)\n\n## Core Pattern\n\n### 1. Define Small, Focused Protocols\n\nEach protocol handles exactly one external concern.\n\n```swift\n\u002F\u002F File system access\npublic protocol FileSystemProviding: Sendable {\n    func containerURL(for purpose: Purpose) -> URL?\n}\n\n\u002F\u002F File read\u002Fwrite operations\npublic protocol FileAccessorProviding: Sendable {\n    func read(from url: URL) throws -> Data\n    func write(_ data: Data, to url: URL) throws\n    func fileExists(at url: URL) -> Bool\n}\n\n\u002F\u002F Bookmark storage (e.g., for sandboxed apps)\npublic protocol BookmarkStorageProviding: Sendable {\n    func saveBookmark(_ data: Data, for key: String) throws\n    func loadBookmark(for key: String) throws -> Data?\n}\n```\n\n### 2. Create Default (Production) Implementations\n\n```swift\npublic struct DefaultFileSystemProvider: FileSystemProviding {\n    public init() {}\n\n    public func containerURL(for purpose: Purpose) -> URL? {\n        FileManager.default.url(forUbiquityContainerIdentifier: nil)\n    }\n}\n\npublic struct DefaultFileAccessor: FileAccessorProviding {\n    public init() {}\n\n    public func read(from url: URL) throws -> Data {\n        try Data(contentsOf: url)\n    }\n\n    public func write(_ data: Data, to url: URL) throws {\n        try data.write(to: url, options: .atomic)\n    }\n\n    public func fileExists(at url: URL) -> Bool {\n        FileManager.default.fileExists(atPath: url.path)\n    }\n}\n```\n\n### 3. Create Mock Implementations for Testing\n\n```swift\npublic final class MockFileAccessor: FileAccessorProviding, @unchecked Sendable {\n    public var files: [URL: Data] = [:]\n    public var readError: Error?\n    public var writeError: Error?\n\n    public init() {}\n\n    public func read(from url: URL) throws -> Data {\n        if let error = readError { throw error }\n        guard let data = files[url] else {\n            throw CocoaError(.fileReadNoSuchFile)\n        }\n        return data\n    }\n\n    public func write(_ data: Data, to url: URL) throws {\n        if let error = writeError { throw error }\n        files[url] = data\n    }\n\n    public func fileExists(at url: URL) -> Bool {\n        files[url] != nil\n    }\n}\n```\n\n### 4. Inject Dependencies with Default Parameters\n\nProduction code uses defaults; tests inject mocks.\n\n```swift\npublic actor SyncManager {\n    private let fileSystem: FileSystemProviding\n    private let fileAccessor: FileAccessorProviding\n\n    public init(\n        fileSystem: FileSystemProviding = DefaultFileSystemProvider(),\n        fileAccessor: FileAccessorProviding = DefaultFileAccessor()\n    ) {\n        self.fileSystem = fileSystem\n        self.fileAccessor = fileAccessor\n    }\n\n    public func sync() async throws {\n        guard let containerURL = fileSystem.containerURL(for: .sync) else {\n            throw SyncError.containerNotAvailable\n        }\n        let data = try fileAccessor.read(\n            from: containerURL.appendingPathComponent(\"data.json\")\n        )\n        \u002F\u002F Process data...\n    }\n}\n```\n\n### 5. Write Tests with Swift Testing\n\n```swift\nimport Testing\n\n@Test(\"Sync manager handles missing container\")\nfunc testMissingContainer() async {\n    let mockFileSystem = MockFileSystemProvider(containerURL: nil)\n    let manager = SyncManager(fileSystem: mockFileSystem)\n\n    await #expect(throws: SyncError.containerNotAvailable) {\n        try await manager.sync()\n    }\n}\n\n@Test(\"Sync manager reads data correctly\")\nfunc testReadData() async throws {\n    let mockFileAccessor = MockFileAccessor()\n    mockFileAccessor.files[testURL] = testData\n\n    let manager = SyncManager(fileAccessor: mockFileAccessor)\n    let result = try await manager.loadData()\n\n    #expect(result == expectedData)\n}\n\n@Test(\"Sync manager handles read errors gracefully\")\nfunc testReadError() async {\n    let mockFileAccessor = MockFileAccessor()\n    mockFileAccessor.readError = CocoaError(.fileReadCorruptFile)\n\n    let manager = SyncManager(fileAccessor: mockFileAccessor)\n\n    await #expect(throws: SyncError.self) {\n        try await manager.sync()\n    }\n}\n```\n\n## Best Practices\n\n- **Single Responsibility**: Each protocol should handle one concern — don't create \"god protocols\" with many methods\n- **Sendable conformance**: Required when protocols are used across actor boundaries\n- **Default parameters**: Let production code use real implementations by default; only tests need to specify mocks\n- **Error simulation**: Design mocks with configurable error properties for testing failure paths\n- **Only mock boundaries**: Mock external dependencies (file system, network, APIs), not internal types\n\n## Anti-Patterns to Avoid\n\n- Creating a single large protocol that covers all external access\n- Mocking internal types that have no external dependencies\n- Using `#if DEBUG` conditionals instead of proper dependency injection\n- Forgetting `Sendable` conformance when used with actors\n- Over-engineering: if a type has no external dependencies, it doesn't need a protocol\n\n## When to Use\n\n- Any Swift code that touches file system, network, or external APIs\n- Testing error handling paths that are hard to trigger in real environments\n- Building modules that need to work in app, test, and SwiftUI preview contexts\n- Apps using Swift concurrency (actors, structured concurrency) that need testable architecture\n","# Swift Protocol-Based Dependency Injection for Testing\n\nPatterns for making Swift code testable by abstracting external dependencies (file system, network, iCloud) behind small, focused protocols. Enables deterministic tests without I\u002FO.\n\n## When to Activate\n\n- Writing Swift code that accesses file system, network, or external APIs\n- Need to test error handling paths without triggering real failures\n- Building modules that work across environments (app, test, SwiftUI preview)\n- Designing testable architecture with Swift concurrency (actors, Sendable)\n\n## Core Pattern\n\n### 1. Define Small, Focused Protocols\n\nEach protocol handles exactly one external concern.\n\n```swift\n\u002F\u002F File system access\npublic protocol FileSystemProviding: Sendable {\n    func containerURL(for purpose: Purpose) -> URL?\n}\n\n\u002F\u002F File read\u002Fwrite operations\npublic protocol FileAccessorProviding: Sendable {\n    func read(from url: URL) throws -> Data\n    func write(_ data: Data, to url: URL) throws\n    func fileExists(at url: URL) -> Bool\n}\n\n\u002F\u002F Bookmark storage (e.g., for sandboxed apps)\npublic protocol BookmarkStorageProviding: Sendable {\n    func saveBookmark(_ data: Data, for key: String) throws\n    func loadBookmark(for key: String) throws -> Data?\n}\n```\n\n### 2. Create Default (Production) Implementations\n\n```swift\npublic struct DefaultFileSystemProvider: FileSystemProviding {\n    public init() {}\n\n    public func containerURL(for purpose: Purpose) -> URL? {\n        FileManager.default.url(forUbiquityContainerIdentifier: nil)\n    }\n}\n\npublic struct DefaultFileAccessor: FileAccessorProviding {\n    public init() {}\n\n    public func read(from url: URL) throws -> Data {\n        try Data(contentsOf: url)\n    }\n\n    public func write(_ data: Data, to url: URL) throws {\n        try data.write(to: url, options: .atomic)\n    }\n\n    public func fileExists(at url: URL) -> Bool {\n        FileManager.default.fileExists(atPath: url.path)\n    }\n}\n```\n\n### 3. Create Mock Implementations for Testing\n\n```swift\npublic final class MockFileAccessor: FileAccessorProviding, @unchecked Sendable {\n    public var files: [URL: Data] = [:]\n    public var readError: Error?\n    public var writeError: Error?\n\n    public init() {}\n\n    public func read(from url: URL) throws -> Data {\n        if let error = readError { throw error }\n        guard let data = files[url] else {\n            throw CocoaError(.fileReadNoSuchFile)\n        }\n        return data\n    }\n\n    public func write(_ data: Data, to url: URL) throws {\n        if let error = writeError { throw error }\n        files[url] = data\n    }\n\n    public func fileExists(at url: URL) -> Bool {\n        files[url] != nil\n    }\n}\n```\n\n### 4. Inject Dependencies with Default Parameters\n\nProduction code uses defaults; tests inject mocks.\n\n```swift\npublic actor SyncManager {\n    private let fileSystem: FileSystemProviding\n    private let fileAccessor: FileAccessorProviding\n\n    public init(\n        fileSystem: FileSystemProviding = DefaultFileSystemProvider(),\n        fileAccessor: FileAccessorProviding = DefaultFileAccessor()\n    ) {\n        self.fileSystem = fileSystem\n        self.fileAccessor = fileAccessor\n    }\n\n    public func sync() async throws {\n        guard let containerURL = fileSystem.containerURL(for: .sync) else {\n            throw SyncError.containerNotAvailable\n        }\n        let data = try fileAccessor.read(\n            from: containerURL.appendingPathComponent(\"data.json\")\n        )\n        \u002F\u002F Process data...\n    }\n}\n```\n\n### 5. Write Tests with Swift Testing\n\n```swift\nimport Testing\n\n@Test(\"Sync manager handles missing container\")\nfunc testMissingContainer() async {\n    let mockFileSystem = MockFileSystemProvider(containerURL: nil)\n    let manager = SyncManager(fileSystem: mockFileSystem)\n\n    await #expect(throws: SyncError.containerNotAvailable) {\n        try await manager.sync()\n    }\n}\n\n@Test(\"Sync manager reads data correctly\")\nfunc testReadData() async throws {\n    let mockFileAccessor = MockFileAccessor()\n    mockFileAccessor.files[testURL] = testData\n\n    let manager = SyncManager(fileAccessor: mockFileAccessor)\n    let result = try await manager.loadData()\n\n    #expect(result == expectedData)\n}\n\n@Test(\"Sync manager handles read errors gracefully\")\nfunc testReadError() async {\n    let mockFileAccessor = MockFileAccessor()\n    mockFileAccessor.readError = CocoaError(.fileReadCorruptFile)\n\n    let manager = SyncManager(fileAccessor: mockFileAccessor)\n\n    await #expect(throws: SyncError.self) {\n        try await manager.sync()\n    }\n}\n```\n\n## Best Practices\n\n- **Single Responsibility**: Each protocol should handle one concern — don't create \"god protocols\" with many methods\n- **Sendable conformance**: Required when protocols are used across actor boundaries\n- **Default parameters**: Let production code use real implementations by default; only tests need to specify mocks\n- **Error simulation**: Design mocks with configurable error properties for testing failure paths\n- **Only mock boundaries**: Mock external dependencies (file system, network, APIs), not internal types\n\n## Anti-Patterns to Avoid\n\n- Creating a single large protocol that covers all external access\n- Mocking internal types that have no external dependencies\n- Using `#if DEBUG` conditionals instead of proper dependency injection\n- Forgetting `Sendable` conformance when used with actors\n- Over-engineering: if a type has no external dependencies, it doesn't need a protocol\n\n## When to Use\n\n- Any Swift code that touches file system, network, or external APIs\n- Testing error handling paths that are hard to trigger in real environments\n- Building modules that need to work in app, test, and SwiftUI preview contexts\n- Apps using Swift concurrency (actors, structured concurrency) that need testable architecture\n",{"name":5,"origin":43,"description":7},"ECC","main","a318f48de3c5718e8ea2e9f6fae7790e8a759e85","upstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Fswift-protocol-di-testing",0,"https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud","https:\u002F\u002Fwww.linkedin.com\u002Fin\u002Fo-abdelmaksoud\u002F","Full-Stack Engineer | AI Systems Architect | Building autonomous multi-agent systems","2026-03-08 21:23:03",{"source":8,"skillName":5,"commands":54,"prompt":64,"zipAvailable":65},[55,58,61],{"tool":56,"command":57},"npx","npx skills add oabdelmaksoud\u002FAgention --skill swift-protocol-di-testing",{"tool":59,"command":60},"bunx","bunx skills add oabdelmaksoud\u002FAgention --skill swift-protocol-di-testing",{"tool":62,"command":63},"pnpm","pnpm dlx skills add oabdelmaksoud\u002FAgention --skill swift-protocol-di-testing","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Fswift-protocol-di-testing\u002FSKILL.md",true,[],[68,90,102,133,159,171],{"skillNo":69,"name":70,"nameZh":6,"description":71,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":72,"categories":73,"keywords":77,"publishedAt":38},"01M6G2QFCBNBYAJF2KTK85G9A6","visa-doc-translate","Translate visa application documents (images) to English and create a bilingual PDF with original and translation","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Fvisa-doc-translate\u002FSKILL.md",[74],{"categoryNo":75,"categoryName":76,"taxonomyType":21,"taxonomyTypeDesc":22},"01JBSEEDCAT000000000000002","文档处理",[78,79,80,81,82,83,84,85,86,87,88,89],"translate","doc","visa doc translate","application","english","bilingual","with","translation","images","PDF","documents","create",{"skillNo":91,"name":92,"nameZh":6,"description":93,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":94,"categories":95,"keywords":96,"publishedAt":38},"01M6G2QB09TSK6BY4ME5X20EVQ","verification-loop","A comprehensive verification system for Claude Code sessions.","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Fverification-loop\u002FSKILL.md",[],[97,98,99,100,28,101,33],"verification","loop","verification loop","sessions","claude code sessions",{"skillNo":103,"name":104,"nameZh":6,"description":105,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":106,"categories":107,"keywords":117,"publishedAt":38},"01M6G2Q6V2F7G03VAAF27FXPJR","tdd-workflow","Use this skill when writing new features, fixing bugs, or refactoring code. Enforces test-driven development with 80%+ coverage including unit, integration, and E2E tests.","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Ftdd-workflow\u002FSKILL.md",[108,109,112],{"categoryNo":19,"categoryName":20,"taxonomyType":21,"taxonomyTypeDesc":22},{"categoryNo":110,"categoryName":111,"taxonomyType":21,"taxonomyTypeDesc":22},"01JBSEEDCAT000000000000023","Skill与智能体开发",{"categoryNo":113,"categoryName":114,"taxonomyType":115,"taxonomyTypeDesc":116},"01JBSEEDCAT000000000000030","开发工程师","PROFESSION","适用职业",[118,119,120,28,121,122,123,124,125,126,127,128,84,129,130,131,132],"workflow","tdd","tdd workflow","tests","use","coverage","refactoring","e2e","integration","driven","unit","development","writing","test","e2e tests",{"skillNo":134,"name":135,"nameZh":6,"description":136,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":137,"categories":138,"keywords":139,"publishedAt":38},"01M6G2Q359YJRWAR2PFCEQAXF3","swiftui-patterns","SwiftUI architecture patterns, state management with @Observable, view composition, navigation, performance optimization, and modern iOS\u002FmacOS UI best practices.","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Fswiftui-patterns\u002FSKILL.md",[],[140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,84],"patterns","swiftui","swiftui patterns","state","macos","performance","architecture","state management","best practices","ui","Optimization","ios","architecture patterns","Composition","performance optimization","view","management","swiftui architecture","navigation",{"skillNo":160,"name":161,"nameZh":6,"description":162,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":163,"categories":164,"keywords":165,"publishedAt":38},"01M6G2PSCN2GXV9FFZDRRRWVB6","swift-concurrency-6-2","Swift 6.2 Approachable Concurrency — single-threaded by default, @concurrent for explicit background offloading, isolated conformances for main actor types.","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Fswift-concurrency-6-2\u002FSKILL.md",[],[166,167,168,25,169,44,170],"swift concurrency 6 2","concurrency","swift concurrency","default","types",{"skillNo":172,"name":173,"nameZh":6,"description":174,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":175,"categories":176,"keywords":180,"publishedAt":38},"01M6G2PMSS5S9ZW0YM7X2BAP8D","swift-actor-persistence","Thread-safe data persistence in Swift using actors — in-memory cache with file-backed storage, eliminating data races by design.","https:\u002F\u002Fgithub.com\u002Foabdelmaksoud\u002FAgention\u002Fblob\u002Fmain\u002Fupstream-sync\u002Fwork\u002Fecc\u002Fskills\u002Fswift-actor-persistence\u002FSKILL.md",[177],{"categoryNo":178,"categoryName":179,"taxonomyType":21,"taxonomyTypeDesc":22},"01JBSEEDCAT000000000000015","视觉设计",[25,181,182,183,184,185,186,30,187,188,84],"swift actor persistence","persistence","design","safe","memory","storage","cache","data"]