engsys 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +202 -0
  3. package/core/agents/aaron.md +152 -0
  4. package/core/agents/bert.md +115 -0
  5. package/core/agents/isabelle.md +136 -0
  6. package/core/agents/jody.md +150 -0
  7. package/core/agents/leith.md +111 -0
  8. package/core/agents/marcelo.md +282 -0
  9. package/core/agents/melvin.md +101 -0
  10. package/core/agents/nyx.md +152 -0
  11. package/core/agents/otto.md +168 -0
  12. package/core/agents/patricia.md +283 -0
  13. package/core/commands/design-audit-local.md +155 -0
  14. package/core/commands/design-audit.md +235 -0
  15. package/core/commands/design-critique.md +96 -0
  16. package/core/commands/file-issue.md +22 -0
  17. package/core/commands/generate-project.md +45 -0
  18. package/core/commands/implement-issue.md +37 -0
  19. package/core/commands/implement-project.md +40 -0
  20. package/core/commands/naturalize.md +61 -0
  21. package/core/commands/pre-push.md +29 -0
  22. package/core/commands/prep-review-collect.md +130 -0
  23. package/core/commands/prep-review-finalize.md +121 -0
  24. package/core/commands/prep-review-publish.md +113 -0
  25. package/core/commands/prep-review.md +65 -0
  26. package/core/commands/project-closeout.md +25 -0
  27. package/core/skills/agentic-eval/SKILL.md +195 -0
  28. package/core/skills/chrome-devtools/SKILL.md +97 -0
  29. package/core/skills/code-review/SKILL.md +26 -0
  30. package/core/skills/gh-cli/SKILL.md +2202 -0
  31. package/core/skills/git-commit/SKILL.md +124 -0
  32. package/core/skills/git-workflow-agents/SKILL.md +462 -0
  33. package/core/skills/git-workflow-agents/reference.md +220 -0
  34. package/core/skills/github-actions/SKILL.md +190 -0
  35. package/core/skills/github-issues/SKILL.md +154 -0
  36. package/core/skills/llm-structured-outputs/SKILL.md +323 -0
  37. package/core/skills/llm-structured-outputs/references/provider-details.md +392 -0
  38. package/core/skills/pre-push/SKILL.md +115 -0
  39. package/core/skills/refactor/SKILL.md +645 -0
  40. package/core/skills/web-design-reviewer/SKILL.md +371 -0
  41. package/core/skills/webapp-testing/SKILL.md +127 -0
  42. package/core/skills/webapp-testing/test-helper.js +56 -0
  43. package/core/templates/CLAUDE.md.tmpl +98 -0
  44. package/core/templates/adr-template.md +67 -0
  45. package/core/templates/gh-issue-templates/bug.md +39 -0
  46. package/core/templates/gh-issue-templates/content.md +42 -0
  47. package/core/templates/gh-issue-templates/enhancement.md +36 -0
  48. package/core/templates/gh-issue-templates/feature.md +39 -0
  49. package/core/templates/gh-issue-templates/infrastructure.md +41 -0
  50. package/core/templates/post-edit-reminders.sh.tmpl +19 -0
  51. package/core/templates/settings.json.tmpl +90 -0
  52. package/core/templates/settings.local.json.tmpl +3 -0
  53. package/core/workflows/agent-implementation-workflow.md +346 -0
  54. package/core/workflows/generate-project.md +258 -0
  55. package/core/workflows/implement-project-workflow.md +190 -0
  56. package/core/workflows/issue-tracking.md +89 -0
  57. package/core/workflows/project-closeout-ceremony.md +77 -0
  58. package/core/workflows/review-workflow.md +266 -0
  59. package/engsys.config.example.yaml +46 -0
  60. package/install +202 -0
  61. package/lessons-library/README.md +80 -0
  62. package/lessons-library/async-callbacks-verify-liveness.md +15 -0
  63. package/lessons-library/change-isnt-done-until-every-surface-updated.md +15 -0
  64. package/lessons-library/claim-then-act-for-irreversible-ops.md +16 -0
  65. package/lessons-library/co-commit-entangled-work.md +15 -0
  66. package/lessons-library/dependabot-triage-playbook.md +17 -0
  67. package/lessons-library/deploy-by-digest-and-verify-the-running-revision.md +15 -0
  68. package/lessons-library/enforce-your-guarantee-at-your-boundary.md +16 -0
  69. package/lessons-library/gate-changes-on-measurement-not-vibes.md +15 -0
  70. package/lessons-library/iac-first-no-console-changes.md +15 -0
  71. package/lessons-library/independent-objective-review-gate.md +15 -0
  72. package/lessons-library/keep-an-immutable-source-of-truth.md +15 -0
  73. package/lessons-library/long-agent-runs-checkpoint-not-poll.md +15 -0
  74. package/lessons-library/model-identity-with-stable-ids-and-provenance.md +15 -0
  75. package/lessons-library/operator-choices-are-first-class.md +15 -0
  76. package/lessons-library/prefer-tool-enforced-structured-output.md +15 -0
  77. package/lessons-library/prove-causation-before-acting.md +15 -0
  78. package/lessons-library/re-read-state-before-acting.md +14 -0
  79. package/lessons-library/read-layer-tolerates-unbackfilled-rows.md +15 -0
  80. package/lessons-library/shell-safety-pipefail-and-validate-before-teardown.md +14 -0
  81. package/lessons-library/shift-correctness-left-and-distrust-false-greens.md +15 -0
  82. package/lessons-library/stray-control-bytes-hide-changes.md +14 -0
  83. package/lessons-library/tests-can-assert-the-bug.md +15 -0
  84. package/lessons-library/verify-ground-truth-not-reports.md +15 -0
  85. package/lessons-library/worktrees-need-bootstrap-from-origin-main.md +15 -0
  86. package/lib/commands.js +356 -0
  87. package/lib/generate-team-avatars.mjs +251 -0
  88. package/lib/manifest.js +155 -0
  89. package/lib/render.js +135 -0
  90. package/lib/selftest.js +90 -0
  91. package/lib/util.js +89 -0
  92. package/lib/yaml.js +156 -0
  93. package/optional-agents/gary.md +86 -0
  94. package/optional-agents/jos.md +136 -0
  95. package/optional-agents/sandy.md +101 -0
  96. package/optional-agents/steve.md +161 -0
  97. package/package.json +43 -0
  98. package/stacks/cloud/aws/claude.fragment.md +17 -0
  99. package/stacks/cloud/aws/settings.fragment.json +39 -0
  100. package/stacks/cloud/aws/skills/aws-deployment-preflight/SKILL.md +165 -0
  101. package/stacks/cloud/aws/skills/cloud-architecture-aws/SKILL.md +265 -0
  102. package/stacks/cloud/azure/claude.fragment.md +17 -0
  103. package/stacks/cloud/azure/settings.fragment.json +45 -0
  104. package/stacks/cloud/azure/skills/azure-deployment-preflight/SKILL.md +175 -0
  105. package/stacks/cloud/azure/skills/cloud-architecture-azure/SKILL.md +211 -0
  106. package/stacks/cloud/cloudflare/claude.fragment.md +21 -0
  107. package/stacks/cloud/cloudflare/settings.fragment.json +31 -0
  108. package/stacks/cloud/cloudflare/skills/cloud-architecture-cloudflare/SKILL.md +294 -0
  109. package/stacks/cloud/cloudflare/skills/cloudflare-deployment-preflight/SKILL.md +175 -0
  110. package/stacks/cloud/gcp/claude.fragment.md +17 -0
  111. package/stacks/cloud/gcp/settings.fragment.json +40 -0
  112. package/stacks/cloud/gcp/skills/cloud-architecture-gcp/SKILL.md +208 -0
  113. package/stacks/cloud/gcp/skills/gcp-deployment-preflight/SKILL.md +137 -0
  114. package/stacks/db/mongo/skills/mongo-conventions/SKILL.md +96 -0
  115. package/stacks/db/prisma/claude.fragment.md +49 -0
  116. package/stacks/db/prisma/skills/docker-database-package-copy/SKILL.md +44 -0
  117. package/stacks/db/prisma/skills/prisma-conventions/SKILL.md +37 -0
  118. package/stacks/domain/mobile-growth/skills/apple-ads/SKILL.md +184 -0
  119. package/stacks/domain/mobile-growth/skills/apple-ads/references/benchmark-notes.md +47 -0
  120. package/stacks/domain/mobile-growth/skills/apple-ads/references/official-links.md +53 -0
  121. package/stacks/domain/mobile-growth/skills/google-play-growth/SKILL.md +197 -0
  122. package/stacks/domain/mobile-growth/skills/google-play-growth/references/benchmark-notes.md +47 -0
  123. package/stacks/domain/mobile-growth/skills/google-play-growth/references/official-links.md +45 -0
  124. package/stacks/iac/bicep/claude.fragment.md +14 -0
  125. package/stacks/iac/bicep/settings.fragment.json +20 -0
  126. package/stacks/iac/bicep/skills/iac-bicep/SKILL.md +113 -0
  127. package/stacks/iac/cdk/claude.fragment.md +14 -0
  128. package/stacks/iac/cdk/settings.fragment.json +23 -0
  129. package/stacks/iac/cdk/skills/iac-cdk/SKILL.md +104 -0
  130. package/stacks/iac/terraform/claude.fragment.md +13 -0
  131. package/stacks/iac/terraform/settings.fragment.json +25 -0
  132. package/stacks/iac/terraform/skills/iac-terraform/SKILL.md +93 -0
  133. package/stacks/iac/terraform/skills/terraform-conventions/SKILL.md +87 -0
  134. package/stacks/lang/kotlin/skills/android-testing/SKILL.md +263 -0
  135. package/stacks/lang/kotlin/skills/jetpack-compose/SKILL.md +264 -0
  136. package/stacks/lang/kotlin/skills/kotlin-coroutines/SKILL.md +329 -0
  137. package/stacks/lang/python/skills/python-conventions/SKILL.md +61 -0
  138. package/stacks/lang/shell/skills/shell-scripting/SKILL.md +110 -0
  139. package/stacks/lang/swift/skills/swift-concurrency/SKILL.md +423 -0
  140. package/stacks/lang/swift/skills/swift-concurrency/references/approachable-concurrency.md +80 -0
  141. package/stacks/lang/swift/skills/swift-concurrency/references/concurrency-patterns.md +233 -0
  142. package/stacks/lang/swift/skills/swift-concurrency/references/swiftui-concurrency.md +187 -0
  143. package/stacks/lang/swift/skills/swift-concurrency/references/synchronization-primitives.md +341 -0
  144. package/stacks/lang/swift/skills/swift-testing/SKILL.md +497 -0
  145. package/stacks/lang/swift/skills/swift-testing/references/testing-advanced.md +106 -0
  146. package/stacks/lang/swift/skills/swift-testing/references/testing-patterns.md +504 -0
  147. package/stacks/lang/swift/skills/swiftdata/SKILL.md +334 -0
  148. package/stacks/lang/swift/skills/swiftdata/references/core-data-coexistence.md +504 -0
  149. package/stacks/lang/swift/skills/swiftdata/references/swiftdata-advanced.md +975 -0
  150. package/stacks/lang/swift/skills/swiftdata/references/swiftdata-queries.md +675 -0
  151. package/stacks/lang/swift/skills/swiftui-patterns/SKILL.md +371 -0
  152. package/stacks/lang/swift/skills/swiftui-patterns/references/architecture-patterns.md +486 -0
  153. package/stacks/lang/swift/skills/swiftui-patterns/references/deprecated-migration.md +1097 -0
  154. package/stacks/lang/swift/skills/swiftui-patterns/references/design-polish.md +780 -0
  155. package/stacks/lang/swift/skills/swiftui-patterns/references/platform-and-sharing.md +696 -0
  156. package/stacks/lang/typescript/skills/typescript-conventions/SKILL.md +91 -0
  157. package/stacks/platform/android/claude.fragment.md +40 -0
  158. package/stacks/platform/android/hooks/pre-push-gradle.sh +70 -0
  159. package/stacks/platform/android/settings.fragment.json +13 -0
  160. package/stacks/platform/android/skills/android-build-conventions/SKILL.md +247 -0
  161. package/stacks/platform/ios/claude.fragment.md +24 -0
  162. package/stacks/platform/ios/hooks/pre-push-xcodebuild.sh +82 -0
  163. package/stacks/platform/ios/settings.fragment.json +21 -0
  164. package/stacks/platform/ios/skills/xcodebuildmcp-simulator-logs/SKILL.md +76 -0
  165. package/stacks/platform/web/skills/frontend-testing/SKILL.md +246 -0
  166. package/stacks/platform/web/skills/react-conventions/SKILL.md +261 -0
  167. package/stacks/platform/web/skills/web-platform-conventions/SKILL.md +55 -0
  168. package/stacks/tooling/issue-tracker-github/claude.fragment.md +10 -0
  169. package/stacks/tooling/issue-tracker-github/settings.fragment.json +24 -0
  170. package/stacks/tooling/issue-tracker-github/skills/issue-tracker-github/SKILL.md +278 -0
  171. package/stacks/tooling/issue-tracker-linear/claude.fragment.md +17 -0
  172. package/stacks/tooling/issue-tracker-linear/settings.fragment.json +9 -0
  173. package/stacks/tooling/issue-tracker-linear/skills/issue-tracker-linear/SKILL.md +183 -0
@@ -0,0 +1,497 @@
1
+ ---
2
+ name: swift-testing
3
+ description: "Write and migrate tests using the Swift Testing framework with @Test, @Suite, #expect, #require, confirmation, parameterized tests, test tags, traits, withKnownIssue, XCTest UI testing, XCUITest, test plan, mocking, test doubles, testable architecture, snapshot testing, async test patterns, test organization, and test-driven development in Swift. Use when writing or migrating tests with Swift Testing framework, implementing parameterized tests, working with test traits, converting XCTest to Swift Testing, or setting up test organization and mocking patterns."
4
+ ---
5
+
6
+ # Swift Testing
7
+
8
+ Swift Testing is the modern testing framework for Swift (Xcode 16+, Swift 6+). Prefer it over XCTest for all new unit tests. Use XCTest only for UI tests, performance benchmarks, and snapshot tests.
9
+
10
+ ## Contents
11
+
12
+ - [Basic Tests](#basic-tests)
13
+ - [@Test Traits](#test-traits)
14
+ - [#expect and #require](#expect-and-require)
15
+ - [@Suite and Test Organization](#suite-and-test-organization)
16
+ - [Known Issues](#known-issues)
17
+ - [Additional Patterns](#additional-patterns)
18
+ - [Parameterized Tests In Depth](#parameterized-tests-in-depth)
19
+ - [Tags and Suites In Depth](#tags-and-suites-in-depth)
20
+ - [Async Testing Patterns](#async-testing-patterns)
21
+ - [Traits In Depth](#traits-in-depth)
22
+ - [Common Mistakes](#common-mistakes)
23
+ - [Test Attachments](#test-attachments)
24
+ - [Exit Testing](#exit-testing)
25
+ - [Review Checklist](#review-checklist)
26
+ - [References](#references)
27
+
28
+ ---
29
+
30
+ ## Basic Tests
31
+
32
+ ```swift
33
+ import Testing
34
+
35
+ @Test("User can update their display name")
36
+ func updateDisplayName() {
37
+ var user = User(name: "Alice")
38
+ user.name = "Bob"
39
+ #expect(user.name == "Bob")
40
+ }
41
+ ```
42
+
43
+ ## @Test Traits
44
+
45
+ ```swift
46
+ @Test("Validates email format") // display name
47
+ @Test(.tags(.validation, .email)) // tags
48
+ @Test(.disabled("Server migration in progress")) // disabled
49
+ @Test(.enabled(if: ProcessInfo.processInfo.environment["CI"] != nil)) // conditional
50
+ @Test(.bug("https://github.com/org/repo/issues/42")) // bug reference
51
+ @Test(.timeLimit(.minutes(1))) // time limit
52
+ @Test("Timeout handling", .tags(.networking), .timeLimit(.seconds(30))) // combined
53
+ ```
54
+
55
+ ## #expect and #require
56
+
57
+ ```swift
58
+ // #expect records failure but continues execution
59
+ #expect(result == 42)
60
+ #expect(name.isEmpty == false)
61
+ #expect(items.count > 0, "Items should not be empty")
62
+
63
+ // #expect with error type checking
64
+ #expect(throws: ValidationError.self) {
65
+ try validate(email: "not-an-email")
66
+ }
67
+
68
+ // #expect with specific error value
69
+ #expect {
70
+ try validate(email: "")
71
+ } throws: { error in
72
+ guard let err = error as? ValidationError else { return false }
73
+ return err == .empty
74
+ }
75
+
76
+ // #require records failure AND stops test (like XCTUnwrap)
77
+ let user = try #require(await fetchUser(id: 1))
78
+ #expect(user.name == "Alice")
79
+
80
+ // #require for optionals -- unwraps or fails
81
+ let first = try #require(items.first)
82
+ #expect(first.isValid)
83
+ ```
84
+
85
+ **Rule: Use `#require` when subsequent assertions depend on the value. Use `#expect` for independent checks.**
86
+
87
+ ## @Suite and Test Organization
88
+
89
+ See [references/testing-patterns.md](references/testing-patterns.md) for suite organization, confirmation patterns, and known-issue handling.
90
+
91
+ ## Known Issues
92
+
93
+ Mark expected failures so they do not cause test failure:
94
+
95
+ ```swift
96
+ withKnownIssue("Propane tank is empty") {
97
+ #expect(truck.grill.isHeating)
98
+ }
99
+
100
+ // Intermittent / flaky failures
101
+ withKnownIssue(isIntermittent: true) {
102
+ #expect(service.isReachable)
103
+ }
104
+
105
+ // Conditional known issue
106
+ withKnownIssue {
107
+ #expect(foodTruck.grill.isHeating)
108
+ } when: {
109
+ !hasPropane
110
+ }
111
+ ```
112
+
113
+ If no known issues are recorded, Swift Testing records a distinct issue notifying you the problem may be resolved.
114
+
115
+ ## Additional Patterns
116
+
117
+ See [references/testing-patterns.md](references/testing-patterns.md) for complete examples of:
118
+
119
+ - **TestScoping** -- custom test lifecycle with setup/teardown consolidation
120
+ - **Mocking and Test Doubles** -- protocol-based doubles and testable architecture
121
+ - **View Model Testing** -- environment injection and dependency isolation
122
+ - **Async Patterns** -- clock injection and error path testing
123
+ - **XCUITest** -- page objects, performance testing, snapshot testing, and test file organization
124
+
125
+ ## Parameterized Tests In Depth
126
+
127
+ ### @Test with Arguments
128
+
129
+ Pass any `Sendable` & `Collection` to `arguments:`. Each element runs as an independent test case.
130
+
131
+ ```swift
132
+ // Enum-based: runs one case per enum value
133
+ enum Environment: String, CaseIterable, Sendable {
134
+ case development, staging, production
135
+ }
136
+
137
+ @Test("Base URL is valid for all environments", arguments: Environment.allCases)
138
+ func baseURLIsValid(env: Environment) throws {
139
+ let url = try #require(URL(string: Config.baseURL(for: env)))
140
+ #expect(url.scheme == "https")
141
+ }
142
+ ```
143
+
144
+ ### Ranges as Arguments
145
+
146
+ ```swift
147
+ @Test("Fibonacci is positive for small inputs", arguments: 1...20)
148
+ func fibonacciPositive(n: Int) {
149
+ #expect(fibonacci(n) > 0)
150
+ }
151
+ ```
152
+
153
+ ### Multiple Parameter Sources
154
+
155
+ Two argument collections produce a **cartesian product** (every combination):
156
+
157
+ ```swift
158
+ @Test(arguments: ["light", "dark"], ["iPhone", "iPad"])
159
+ func snapshotTest(colorScheme: String, device: String) {
160
+ // Runs 4 combinations: light+iPhone, light+iPad, dark+iPhone, dark+iPad
161
+ let config = SnapshotConfig(colorScheme: colorScheme, device: device)
162
+ #expect(config.isValid)
163
+ }
164
+ ```
165
+
166
+ Use `zip` for **1:1 pairing** (avoids cartesian product):
167
+
168
+ ```swift
169
+ @Test(arguments: zip(
170
+ [200, 201, 204],
171
+ ["OK", "Created", "No Content"]
172
+ ))
173
+ func httpStatusDescription(code: Int, expected: String) {
174
+ #expect(HTTPStatus(code).description == expected)
175
+ }
176
+ ```
177
+
178
+ ### Custom Argument Generators
179
+
180
+ Create a `CustomTestArgumentProviding` conformance or use computed static properties:
181
+
182
+ ```swift
183
+ struct APIEndpoint: Sendable {
184
+ let path: String
185
+ let expectedStatus: Int
186
+
187
+ static let testCases: [APIEndpoint] = [
188
+ .init(path: "/users", expectedStatus: 200),
189
+ .init(path: "/missing", expectedStatus: 404),
190
+ ]
191
+ }
192
+
193
+ @Test("API returns expected status", arguments: APIEndpoint.testCases)
194
+ func apiStatus(endpoint: APIEndpoint) async throws {
195
+ let response = try await client.get(endpoint.path)
196
+ #expect(response.statusCode == endpoint.expectedStatus)
197
+ }
198
+ ```
199
+
200
+ ## Tags and Suites In Depth
201
+
202
+ ### Custom Tag Definitions
203
+
204
+ Declare tags as static members on `Tag`:
205
+
206
+ ```swift
207
+ extension Tag {
208
+ @Tag static var networking: Self
209
+ @Tag static var database: Self
210
+ @Tag static var slow: Self
211
+ @Tag static var critical: Self
212
+ @Tag static var smoke: Self
213
+ }
214
+ ```
215
+
216
+ ### Filtering Tests by Tag
217
+
218
+ Run tagged tests from Xcode Test Plans or the command line. Tag-based filtering
219
+ syntax varies by toolchain — verify the exact flags for your Swift version:
220
+
221
+ ```bash
222
+ # Filter by tag (tooling-specific — verify syntax for your Swift version)
223
+ swift test --filter tag:networking
224
+ ```
225
+
226
+ In Xcode, configure Test Plans to include/exclude tags for different CI configurations (smoke tests vs full suite).
227
+
228
+ ### @Suite for Grouping
229
+
230
+ ```swift
231
+ @Suite("Shopping Cart Operations")
232
+ struct ShoppingCartTests {
233
+ let cart: ShoppingCart
234
+
235
+ // init acts as setUp -- runs before each test in the suite
236
+ init() {
237
+ cart = ShoppingCart()
238
+ cart.add(Product(name: "Widget", price: 9.99))
239
+ }
240
+
241
+ @Test func itemCount() {
242
+ #expect(cart.items.count == 1)
243
+ }
244
+
245
+ @Test func totalPrice() {
246
+ #expect(cart.total == 9.99)
247
+ }
248
+ }
249
+ ```
250
+
251
+ ### Suite-Level Setup and Teardown
252
+
253
+ Use `init` for setup and `deinit` for teardown. For async cleanup, use a `TestScoping` trait:
254
+
255
+ ```swift
256
+ @Suite(.tags(.database))
257
+ struct DatabaseTests {
258
+ let db: TestDatabase
259
+
260
+ init() async throws {
261
+ db = try await TestDatabase.createTemporary()
262
+ }
263
+
264
+ // deinit works for synchronous cleanup (struct suites only use init)
265
+ // For async teardown, use TestScoping trait instead
266
+
267
+ @Test func insertRecord() async throws {
268
+ try await db.insert(Record(id: 1, name: "Test"))
269
+ let count = try await db.count()
270
+ #expect(count == 1)
271
+ }
272
+ }
273
+ ```
274
+
275
+ ### Warning-Severity Issues
276
+
277
+ Record issues that surface in test output but don't fail the test. Use for performance regressions, deprecated paths, or non-critical checks you want to track.
278
+
279
+ ```swift
280
+ Issue.record(
281
+ "Processing took \(elapsed)s — exceeds 2s target",
282
+ severity: .warning
283
+ )
284
+ ```
285
+
286
+ ## Async Testing Patterns
287
+
288
+ ### Testing Async Functions
289
+
290
+ `@Test` functions can be `async` and `throws` directly:
291
+
292
+ ```swift
293
+ @Test func fetchUserProfile() async throws {
294
+ let service = UserService(client: MockHTTPClient())
295
+ let user = try await service.fetchProfile(id: 42)
296
+ #expect(user.name == "Alice")
297
+ }
298
+ ```
299
+
300
+ ### Testing Actor-Isolated Code
301
+
302
+ Access actor-isolated state with `await`:
303
+
304
+ ```swift
305
+ actor Counter {
306
+ private(set) var value = 0
307
+ func increment() { value += 1 }
308
+ }
309
+
310
+ @Test func counterIncrements() async {
311
+ let counter = Counter()
312
+ await counter.increment()
313
+ await counter.increment()
314
+ let value = await counter.value
315
+ #expect(value == 2)
316
+ }
317
+ ```
318
+
319
+ ### Timeout for Hanging Tests
320
+
321
+ Use `.timeLimit` to prevent tests from hanging indefinitely:
322
+
323
+ ```swift
324
+ @Test(.timeLimit(.seconds(5)))
325
+ func networkCallCompletes() async throws {
326
+ let result = try await api.fetchData()
327
+ #expect(result.isEmpty == false)
328
+ }
329
+ ```
330
+
331
+ If the test exceeds the time limit, it fails immediately with a clear timeout diagnostic.
332
+
333
+ ### Confirmation (Replacing XCTestExpectation)
334
+
335
+ `confirmation` replaces `XCTestExpectation` / `fulfill()` / `waitForExpectations`. It verifies that an event occurs the expected number of times:
336
+
337
+ ```swift
338
+ @Test func notificationPosted() async throws {
339
+ // Expects the closure to call confirm() exactly once
340
+ try await confirmation("UserDidLogin posted") { confirm in
341
+ let center = NotificationCenter.default
342
+ let observer = center.addObserver(
343
+ forName: .userDidLogin, object: nil, queue: .main
344
+ ) { _ in
345
+ confirm()
346
+ }
347
+ await loginService.login(user: "test", password: "pass")
348
+ center.removeObserver(observer)
349
+ }
350
+ }
351
+
352
+ // Expect multiple confirmations
353
+ @Test func batchProcessing() async throws {
354
+ try await confirmation("Items processed", expectedCount: 3) { confirm in
355
+ processor.onItemComplete = { _ in confirm() }
356
+ await processor.process(items: [a, b, c])
357
+ }
358
+ }
359
+ ```
360
+
361
+ ### Programmatic Cancellation
362
+
363
+ Stop a test without marking it passed or failed. The test is recorded as "cancelled" — distinct from failure or a known issue.
364
+
365
+ ```swift
366
+ @Test func requiresNetwork() throws {
367
+ guard NetworkMonitor.shared.isConnected else {
368
+ try Test.cancel("No network — skipping integration test")
369
+ }
370
+ // ... test continues if connected
371
+ }
372
+ ```
373
+
374
+ ## Traits In Depth
375
+
376
+ ### Conditional Traits
377
+
378
+ ```swift
379
+ // Enable only on CI
380
+ @Test(.enabled(if: ProcessInfo.processInfo.environment["CI"] != nil))
381
+ func integrationTest() async throws { ... }
382
+
383
+ // Disable with a reason
384
+ @Test(.disabled("Blocked by #123 -- server migration"))
385
+ func brokenEndpoint() async throws { ... }
386
+
387
+ // Bug reference -- links test to an issue tracker
388
+ @Test(.bug("https://github.com/org/repo/issues/42", "Intermittent timeout"))
389
+ func flakyNetworkTest() async throws { ... }
390
+ ```
391
+
392
+ ### Time Limits
393
+
394
+ ```swift
395
+ @Test(.timeLimit(.minutes(2)))
396
+ func longRunningImport() async throws {
397
+ try await importer.importLargeDataset()
398
+ }
399
+
400
+ // Apply time limit to entire suite
401
+ @Suite(.timeLimit(.seconds(30)))
402
+ struct FastTests {
403
+ @Test func quick1() { ... }
404
+ @Test func quick2() { ... }
405
+ }
406
+ ```
407
+
408
+ ### Custom Trait Definitions
409
+
410
+ Create reusable traits for common test configurations:
411
+
412
+ ```swift
413
+ struct DatabaseTrait: TestTrait, SuiteTrait, TestScoping {
414
+ func provideScope(
415
+ for test: Test,
416
+ testCase: Test.Case?,
417
+ performing function: @Sendable () async throws -> Void
418
+ ) async throws {
419
+ let db = try await TestDatabase.setUp()
420
+ defer { Task { await db.tearDown() } }
421
+ try await function()
422
+ }
423
+ }
424
+
425
+ extension Trait where Self == DatabaseTrait {
426
+ static var database: Self { .init() }
427
+ }
428
+
429
+ // Usage: any test with .database trait gets a fresh database
430
+ @Test(.database)
431
+ func insertUser() async throws { ... }
432
+ ```
433
+
434
+ ## Test Attachments
435
+
436
+ Attach diagnostic data to test results for debugging failures. See [references/testing-patterns.md](references/testing-patterns.md) for full examples.
437
+
438
+ ```swift
439
+ @Test func generateReport() async throws {
440
+ let report = try generateReport()
441
+ Attachment(report.data, named: "report.json").record()
442
+ #expect(report.isValid)
443
+ }
444
+ ```
445
+
446
+ Image attachments are available via cross-import overlays — import both `Testing` and a UI framework:
447
+
448
+ ```swift
449
+ import Testing
450
+ import UIKit
451
+
452
+ @Test func renderedChart() async throws {
453
+ let image = renderer.image { ctx in chartView.drawHierarchy(in: bounds, afterScreenUpdates: true) }
454
+ Attachment(image, named: "chart.png").record()
455
+ }
456
+ ```
457
+
458
+ ## Exit Testing
459
+
460
+ Test code that calls `exit()`, `fatalError()`, or `preconditionFailure()`. See [references/testing-patterns.md](references/testing-patterns.md) for details.
461
+
462
+ ```swift
463
+ @Test func invalidInputCausesExit() async {
464
+ await #expect(processExitsWith: .failure) {
465
+ processInvalidInput() // calls fatalError()
466
+ }
467
+ }
468
+ ```
469
+
470
+ ## Common Mistakes
471
+
472
+ 1. **Testing implementation, not behavior.** Test what the code does, not how.
473
+ 2. **No error path tests.** If a function can throw, test the throw path.
474
+ 3. **Flaky async tests.** Use `confirmation` with expected counts, not `sleep` calls.
475
+ 4. **Shared mutable state between tests.** Each test sets up its own state via `init()` in `@Suite`.
476
+ 5. **Missing accessibility identifiers in UI tests.** XCUITest queries rely on them.
477
+ 6. **Using `sleep` in tests.** Use `confirmation`, clock injection, or `withKnownIssue`.
478
+ 7. **Not testing cancellation.** If code supports `Task` cancellation, verify it cancels cleanly.
479
+ 8. **Mixing XCTest and Swift Testing in one file.** Keep them in separate files.
480
+ 9. **Non-Sendable test helpers shared across tests.** Ensure test helper types are Sendable when shared across concurrent test cases. Annotate MainActor-dependent test code with `@MainActor`.
481
+
482
+ ## Review Checklist
483
+
484
+ - [ ] All new tests use Swift Testing (`@Test`, `#expect`), not XCTest assertions
485
+ - [ ] Test names describe behavior (`fetchUserReturnsNilOnNetworkError` not `testFetchUser`)
486
+ - [ ] Error paths have dedicated tests
487
+ - [ ] Async tests use `confirmation()`, not `Task.sleep`
488
+ - [ ] Parameterized tests used for repetitive variations
489
+ - [ ] Tags applied for filtering (`.critical`, `.slow`)
490
+ - [ ] Mocks conform to protocols, not subclass concrete types
491
+ - [ ] No shared mutable state between tests
492
+ - [ ] Cancellation tested for cancellable async operations
493
+
494
+ ## References
495
+
496
+ - Testing patterns: [references/testing-patterns.md](references/testing-patterns.md)
497
+ - Advanced testing (warnings, cancellation, image attachments): [references/testing-advanced.md](references/testing-advanced.md)
@@ -0,0 +1,106 @@
1
+ # Advanced Testing Patterns
2
+
3
+ Warning-severity issues, programmatic test cancellation, and image attachments for Swift Testing.
4
+
5
+ ## Contents
6
+
7
+ - [Warning-Severity Issues](#warning-severity-issues)
8
+ - [Programmatic Test Cancellation](#programmatic-test-cancellation)
9
+ - [Exit Test Value Capturing](#exit-test-value-capturing)
10
+ - [Image Attachments](#image-attachments)
11
+ - [Proposal Reference](#proposal-reference)
12
+
13
+ ## Warning-Severity Issues
14
+
15
+ ST-0013 adds a `severity` parameter to `Issue.record()`. Warnings are surfaced in test output but do not cause the test to fail.
16
+
17
+ ```swift
18
+ @Test func dataIntegrity() {
19
+ let result = loadData()
20
+
21
+ // Hard failure — test fails
22
+ #expect(result.isValid)
23
+
24
+ // Warning — logged but test still passes
25
+ if result.processingTime > 2.0 {
26
+ Issue.record(
27
+ "Processing took \(result.processingTime)s — exceeds 2s target",
28
+ severity: .warning
29
+ )
30
+ }
31
+ }
32
+ ```
33
+
34
+ Use warnings for:
35
+ - Performance regressions that aren't blocking
36
+ - Deprecated code paths that still work
37
+ - Non-critical data quality checks
38
+ - Flaky conditions you want to track without failing CI
39
+
40
+ ## Programmatic Test Cancellation
41
+
42
+ ST-0016 adds `try Test.cancel()` to stop a test from within without marking it as passed or failed. The test is recorded as "cancelled."
43
+
44
+ ```swift
45
+ @Test func requiresNetwork() throws {
46
+ guard NetworkMonitor.shared.isConnected else {
47
+ try Test.cancel("No network — skipping integration test")
48
+ }
49
+
50
+ let response = try await APIClient.shared.healthCheck()
51
+ #expect(response.status == .ok)
52
+ }
53
+ ```
54
+
55
+ Key differences from other mechanisms:
56
+ - Throwing an error — marks the test as failed
57
+ - `withKnownIssue` — marks a failure as expected (test still runs)
58
+ - `try Test.cancel()` — marks the test as cancelled (neutral outcome, test stops)
59
+
60
+ ## Exit Test Value Capturing
61
+
62
+ ST-0012 allows exit tests to capture values from the enclosing scope. Previously, `#expect(exitsWith:)` closures could not reference local variables.
63
+
64
+ ```swift
65
+ @Test func exitCodeValidation() async {
66
+ let expectedCode: Int32 = 42
67
+ await #expect(exitsWith: .failure) {
68
+ exit(expectedCode) // Can now capture `expectedCode`
69
+ }
70
+ }
71
+ ```
72
+
73
+ ## Image Attachments
74
+
75
+ ST-0014 adds cross-import overlays for attaching images to test results. When you import both `Testing` and a UI framework, attachment initializers for platform image types become available.
76
+
77
+ ```swift
78
+ import Testing
79
+ import UIKit
80
+
81
+ @Test func renderedOutput() async throws {
82
+ let view = ChartView(data: sampleData)
83
+ let renderer = UIGraphicsImageRenderer(size: CGSize(width: 400, height: 300))
84
+ let image = renderer.image { ctx in
85
+ view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
86
+ }
87
+
88
+ // Attach UIImage directly — available via cross-import overlay
89
+ Attachment(image, named: "chart-output").record()
90
+ }
91
+ ```
92
+
93
+ Supported types via overlays:
94
+ - `UIImage` (when importing UIKit)
95
+ - `CGImage` (when importing CoreGraphics)
96
+ - `NSImage` (when importing AppKit — macOS)
97
+
98
+ ## Proposal Reference
99
+
100
+ | Proposal | Feature |
101
+ |---|---|
102
+ | ST-0009 | Attachments API (`Attachment` type, `.record()`) |
103
+ | ST-0012 | Exit test value capturing |
104
+ | ST-0013 | Warning-severity issues (`Issue.record` with `severity:`) |
105
+ | ST-0014 | Image attachments on Apple platforms (cross-import overlays) |
106
+ | ST-0016 | Programmatic test cancellation (`try Test.cancel()`) |