skills/testing/test-contract/SKILL.md
Generate protocol/interface test suites that any implementation must pass. Define the contract once, test every implementation. Use when designing protocols or swapping implementations.
npx skillsauth add rshankras/claude-code-apple-skills test-contractInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Define behavioral contracts as test suites. Any class conforming to a protocol must pass the contract tests. Ensures consistent behavior across implementations — human-written, AI-generated, or mock.
Use this skill when the user:
Protocol: DataStore
├── SQLiteDataStore → must pass contract tests
├── InMemoryDataStore → must pass contract tests
├── MockDataStore → must pass contract tests
└── CloudDataStore → must pass contract tests
Write the contract once. Every implementation proves it works by passing the same tests.
Grep: "protocol.*\\{" to find protocols
Read: protocol definition
Understand:
For each protocol method, define required behaviors:
## DataStore Contract
### save(_ item:)
- Saving a new item increases count by 1
- Saving an existing item (same ID) updates it, count unchanged
- Saved item is retrievable by ID
- Throws on invalid item (empty title)
### fetch(id:)
- Returns item when it exists
- Returns nil when item doesn't exist
- Returns most recently saved version
### delete(id:)
- Deleting existing item decreases count by 1
- Deleting non-existent item does nothing (no throw)
- Deleted item is no longer fetchable
### fetchAll()
- Returns all saved items
- Returns empty array when store is empty
- Items are in consistent order (insertion or specified)
### Invariants
- count == fetchAll().count (always)
- save then fetch returns same item
- delete then fetch returns nil
import Testing
/// Contract tests for any DataStore implementation.
/// Subclass or call with your concrete implementation.
struct DataStoreContractTests<Store: DataStore> {
let makeStore: () -> Store
init(makeStore: @escaping () -> Store) {
self.makeStore = makeStore
}
// MARK: - save(_ item:)
@Test("save increases count")
func saveIncreasesCount() async throws {
let store = makeStore()
let item = Item(id: "1", title: "Test")
try await store.save(item)
#expect(store.count == 1)
}
@Test("save then fetch returns same item")
func saveAndFetch() async throws {
let store = makeStore()
let item = Item(id: "1", title: "Test")
try await store.save(item)
let fetched = try await store.fetch(id: "1")
#expect(fetched?.title == "Test")
}
@Test("save existing item updates it")
func saveUpdates() async throws {
let store = makeStore()
try await store.save(Item(id: "1", title: "Original"))
try await store.save(Item(id: "1", title: "Updated"))
let fetched = try await store.fetch(id: "1")
#expect(fetched?.title == "Updated")
#expect(store.count == 1)
}
@Test("save throws on invalid item")
func saveInvalidThrows() async {
let store = makeStore()
let invalid = Item(id: "1", title: "")
await #expect(throws: DataStoreError.invalidItem) {
try await store.save(invalid)
}
}
// MARK: - fetch(id:)
@Test("fetch returns nil for non-existent ID")
func fetchNonExistent() async throws {
let store = makeStore()
let result = try await store.fetch(id: "nonexistent")
#expect(result == nil)
}
// MARK: - delete(id:)
@Test("delete removes item")
func deleteRemoves() async throws {
let store = makeStore()
try await store.save(Item(id: "1", title: "Test"))
try await store.delete(id: "1")
#expect(store.count == 0)
let fetched = try await store.fetch(id: "1")
#expect(fetched == nil)
}
@Test("delete non-existent does nothing")
func deleteNonExistent() async throws {
let store = makeStore()
try await store.delete(id: "nonexistent")
#expect(store.count == 0)
}
// MARK: - fetchAll()
@Test("fetchAll returns all items")
func fetchAll() async throws {
let store = makeStore()
try await store.save(Item(id: "1", title: "A"))
try await store.save(Item(id: "2", title: "B"))
let all = try await store.fetchAll()
#expect(all.count == 2)
}
@Test("fetchAll returns empty when store is empty")
func fetchAllEmpty() async throws {
let store = makeStore()
let all = try await store.fetchAll()
#expect(all.isEmpty)
}
// MARK: - Invariants
@Test("count matches fetchAll count")
func countInvariant() async throws {
let store = makeStore()
try await store.save(Item(id: "1", title: "A"))
try await store.save(Item(id: "2", title: "B"))
try await store.delete(id: "1")
let all = try await store.fetchAll()
#expect(store.count == all.count)
}
}
@Suite("InMemoryDataStore Contract")
struct InMemoryDataStoreContractTests {
let contract = DataStoreContractTests { InMemoryDataStore() }
@Test("save increases count")
func saveIncreasesCount() async throws {
try await contract.saveIncreasesCount()
}
@Test("save then fetch returns same item")
func saveAndFetch() async throws {
try await contract.saveAndFetch()
}
// ... all contract tests
}
@Suite("SQLiteDataStore Contract")
struct SQLiteDataStoreContractTests {
let contract = DataStoreContractTests { SQLiteDataStore(path: ":memory:") }
@Test("save increases count")
func saveIncreasesCount() async throws {
try await contract.saveIncreasesCount()
}
// ... all contract tests
}
@Suite("DataStore Contract")
struct DataStoreContractSuite {
enum StoreType: String, CaseIterable {
case inMemory, sqlite, cloud
}
func makeStore(_ type: StoreType) -> any DataStore {
switch type {
case .inMemory: return InMemoryDataStore()
case .sqlite: return SQLiteDataStore(path: ":memory:")
case .cloud: return MockCloudDataStore()
}
}
@Test("save increases count", arguments: StoreType.allCases)
func saveIncreasesCount(type: StoreType) async throws {
let store = makeStore(type)
try await store.save(Item(id: "1", title: "Test"))
#expect(store.count == 1)
}
@Test("fetch non-existent returns nil", arguments: StoreType.allCases)
func fetchNonExistent(type: StoreType) async throws {
let store = makeStore(type)
let result = try await store.fetch(id: "nonexistent")
#expect(result == nil)
}
}
## Contract Tests: [Protocol Name]
### Protocol
```swift
protocol DataStore { ... }
| Behavior | Tests | Edge Cases | |----------|-------|------------| | save | 3 | empty title, duplicate ID | | fetch | 2 | non-existent ID | | delete | 2 | non-existent ID | | fetchAll | 2 | empty store | | invariants | 1 | count consistency |
Tests/Contracts/DataStoreContractTests.swiftTests/Contracts/InMemoryDataStoreContractTests.swift
## References
- `testing/tdd-feature/` — for TDD workflow using contract tests
- `generators/test-generator/` — for standard test generation
- Design by Contract (Bertrand Meyer) — theoretical foundation
development
US web checkout via the StoreKit External Purchase Link entitlement — currently 0% Apple commission (litigation ongoing), how to ship it safely, and how to architect for a commission flip so a future ruling is a config change, not a rewrite. Use when adding external purchase links, weighing web checkout vs IAP, or planning US-storefront pricing strategy.
tools
Revenue beyond the single-app price tag — own-app bundles, Family Sharing as a conversion lever, cross-developer bundles & suites, and institutional licensing via Group Purchases / Apple School & Business Manager. Use when a developer has multiple apps, a subscription worth sharing, complementary indie partners, or school/clinic/business buyers.
testing
Run a structured accessibility audit on an iOS/macOS app — automated XCUITest audits, Accessibility Inspector, manual VoiceOver/Dynamic Type passes, and App Store Accessibility Nutrition Label evaluation. Use before release, when preparing Nutrition Label declarations, or for EU Accessibility Act compliance.
tools
Stage-by-stage audit of an app's App Store growth machinery against a 54-item P0–P9 playbook — every item scored from an App Store Connect MCP call, a codebase check, or an explicit question to the user, then routed to the skill or command that fixes it. Read-only on App Store Connect. Use for a growth audit or scorecard, a pre-launch growth plan, a quarterly re-audit, or "which growth levers am I missing."