ndomo 0.1.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 (247) hide show
  1. package/.bun-version +1 -0
  2. package/.dockerignore +79 -0
  3. package/.editorconfig +18 -0
  4. package/.env.example +19 -0
  5. package/.github/CODEOWNERS +8 -0
  6. package/.github/ISSUE_TEMPLATE/bug_report.yml +62 -0
  7. package/.github/ISSUE_TEMPLATE/config.yml +2 -0
  8. package/.github/ISSUE_TEMPLATE/feature_request.yml +34 -0
  9. package/.github/dependabot.yml +36 -0
  10. package/.github/pull_request_template.md +24 -0
  11. package/.github/release.yml +30 -0
  12. package/.github/workflows/gitleaks.yml +28 -0
  13. package/.github/workflows/release-please.yml +27 -0
  14. package/.github/workflows/smoke.yml +29 -0
  15. package/.husky/commit-msg +1 -0
  16. package/CHANGELOG.md +114 -0
  17. package/Dockerfile +32 -0
  18. package/README.es.md +174 -0
  19. package/README.md +187 -0
  20. package/agents/chronicler.md +98 -0
  21. package/agents/ci-smith.md +136 -0
  22. package/agents/craftsman.md +341 -0
  23. package/agents/deploy-smith.md +138 -0
  24. package/agents/foreman.md +377 -0
  25. package/agents/go-smith.md +164 -0
  26. package/agents/guild.md +188 -0
  27. package/agents/inspector.md +83 -0
  28. package/agents/js-smith.md +127 -0
  29. package/agents/ops-scout.md +173 -0
  30. package/agents/painter.md +200 -0
  31. package/agents/python-smith.md +120 -0
  32. package/agents/ranger.md +307 -0
  33. package/agents/release-smith.md +165 -0
  34. package/agents/rust-smith.md +159 -0
  35. package/agents/sage.md +178 -0
  36. package/agents/scout.md +144 -0
  37. package/agents/scribe.md +156 -0
  38. package/agents/smith.md +201 -0
  39. package/agents/vue-smith.md +155 -0
  40. package/agents/warden.md +216 -0
  41. package/agents/zig-smith.md +156 -0
  42. package/bin/ndomo-analyses.ts +4 -0
  43. package/bin/ndomo-status.ts +4 -0
  44. package/biome.json +57 -0
  45. package/bun.lock +514 -0
  46. package/commitlint.config.js +3 -0
  47. package/config/ndomo.config.json +258 -0
  48. package/config/ndomo.schema.json +166 -0
  49. package/docs/agents.md +375 -0
  50. package/docs/bugs/plan-create-orphan-fk.md +131 -0
  51. package/docs/bugs/task_create_batch-order-index-collision.md +158 -0
  52. package/docs/configuration.md +276 -0
  53. package/docs/database.md +364 -0
  54. package/docs/features/feature-flexible-builder-v1.md +724 -0
  55. package/docs/features/feature-flexible-builder-v2.md +882 -0
  56. package/docs/features/feature-flexible-builder.md +974 -0
  57. package/docs/http-server.md +244 -0
  58. package/docs/installation.md +259 -0
  59. package/docs/integrations.md +129 -0
  60. package/docs/operations/anti-pattern-sub-agent-verify-2026-06-21.md +32 -0
  61. package/docs/operations/audit-v1.md +417 -0
  62. package/docs/operations/audit-v2.md +197 -0
  63. package/docs/operations/audit-v3.md +306 -0
  64. package/docs/operations/db-optimize-foundations.md +123 -0
  65. package/docs/operations/verify-gate-architecture.md +82 -0
  66. package/docs/workflows.md +448 -0
  67. package/opencode.json +5 -0
  68. package/package.json +65 -0
  69. package/release-please-config.json +11 -0
  70. package/scripts/dev-bust-cache.sh +164 -0
  71. package/scripts/install.sh +688 -0
  72. package/scripts/smoke-e2e.ts +704 -0
  73. package/scripts/smoke-hot.ts +417 -0
  74. package/scripts/smoke-http.sh +228 -0
  75. package/scripts/smoke-v4.ts +256 -0
  76. package/scripts/smoke-v5.ts +397 -0
  77. package/scripts/smoke.sh +9 -0
  78. package/scripts/uninstall.sh +224 -0
  79. package/skills/api-security-best-practices/SKILL.md +915 -0
  80. package/skills/bash-scripting/SKILL.md +201 -0
  81. package/skills/bun/SKILL.md +313 -0
  82. package/skills/cavecrew/SKILL.md +82 -0
  83. package/skills/caveman/SKILL.md +74 -0
  84. package/skills/caveman-review/README.md +33 -0
  85. package/skills/caveman-review/SKILL.md +55 -0
  86. package/skills/find-skills/SKILL.md +142 -0
  87. package/skills/frontend-design/LICENSE.txt +177 -0
  88. package/skills/frontend-design/SKILL.md +55 -0
  89. package/skills/golang-patterns/SKILL.md +674 -0
  90. package/skills/golang-security/SKILL.md +185 -0
  91. package/skills/golang-security/evals/evals.json +595 -0
  92. package/skills/golang-security/references/architecture.md +268 -0
  93. package/skills/golang-security/references/checklist.md +80 -0
  94. package/skills/golang-security/references/cookies.md +200 -0
  95. package/skills/golang-security/references/cryptography.md +424 -0
  96. package/skills/golang-security/references/filesystem.md +285 -0
  97. package/skills/golang-security/references/injection.md +315 -0
  98. package/skills/golang-security/references/logging.md +163 -0
  99. package/skills/golang-security/references/memory-safety.md +241 -0
  100. package/skills/golang-security/references/network.md +253 -0
  101. package/skills/golang-security/references/secrets.md +189 -0
  102. package/skills/golang-security/references/third-party.md +159 -0
  103. package/skills/golang-security/references/threat-modeling.md +189 -0
  104. package/skills/golang-testing/SKILL.md +720 -0
  105. package/skills/grill-me/SKILL.md +7 -0
  106. package/skills/javascript-testing-patterns/SKILL.md +537 -0
  107. package/skills/javascript-testing-patterns/references/advanced-testing-patterns.md +513 -0
  108. package/skills/modern-javascript-patterns/SKILL.md +43 -0
  109. package/skills/modern-javascript-patterns/references/advanced-patterns.md +487 -0
  110. package/skills/modern-javascript-patterns/references/details.md +457 -0
  111. package/skills/python-anti-patterns/SKILL.md +349 -0
  112. package/skills/python-design-patterns/SKILL.md +85 -0
  113. package/skills/python-design-patterns/references/details.md +353 -0
  114. package/skills/python-error-handling/SKILL.md +193 -0
  115. package/skills/python-error-handling/references/details.md +171 -0
  116. package/skills/python-testing-patterns/SKILL.md +278 -0
  117. package/skills/python-testing-patterns/references/advanced-patterns.md +411 -0
  118. package/skills/python-testing-patterns/references/details.md +349 -0
  119. package/skills/rust-patterns/SKILL.md +500 -0
  120. package/skills/rust-testing/SKILL.md +501 -0
  121. package/skills/security-review/SKILL.md +504 -0
  122. package/skills/security-review/cloud-infrastructure-security.md +361 -0
  123. package/skills/vue-best-practices/SKILL.md +154 -0
  124. package/skills/vue-best-practices/references/animation-class-based-technique.md +254 -0
  125. package/skills/vue-best-practices/references/animation-state-driven-technique.md +291 -0
  126. package/skills/vue-best-practices/references/component-async.md +97 -0
  127. package/skills/vue-best-practices/references/component-data-flow.md +307 -0
  128. package/skills/vue-best-practices/references/component-fallthrough-attrs.md +174 -0
  129. package/skills/vue-best-practices/references/component-keep-alive.md +137 -0
  130. package/skills/vue-best-practices/references/component-slots.md +216 -0
  131. package/skills/vue-best-practices/references/component-suspense.md +228 -0
  132. package/skills/vue-best-practices/references/component-teleport.md +108 -0
  133. package/skills/vue-best-practices/references/component-transition-group.md +128 -0
  134. package/skills/vue-best-practices/references/component-transition.md +125 -0
  135. package/skills/vue-best-practices/references/composables.md +290 -0
  136. package/skills/vue-best-practices/references/directives.md +162 -0
  137. package/skills/vue-best-practices/references/perf-avoid-component-abstraction-in-lists.md +159 -0
  138. package/skills/vue-best-practices/references/perf-v-once-v-memo-directives.md +182 -0
  139. package/skills/vue-best-practices/references/perf-virtualize-large-lists.md +187 -0
  140. package/skills/vue-best-practices/references/plugins.md +166 -0
  141. package/skills/vue-best-practices/references/reactivity.md +344 -0
  142. package/skills/vue-best-practices/references/render-functions.md +201 -0
  143. package/skills/vue-best-practices/references/sfc.md +310 -0
  144. package/skills/vue-best-practices/references/state-management.md +135 -0
  145. package/skills/vue-best-practices/references/updated-hook-performance.md +187 -0
  146. package/skills/vue-pinia-best-practices/SKILL.md +21 -0
  147. package/skills/vue-pinia-best-practices/reference/pinia-no-active-pinia-error.md +248 -0
  148. package/skills/vue-pinia-best-practices/reference/pinia-setup-store-return-all-state.md +227 -0
  149. package/skills/vue-pinia-best-practices/reference/pinia-store-destructuring-breaks-reactivity.md +193 -0
  150. package/skills/vue-pinia-best-practices/reference/state-url-for-ephemeral-filters.md +238 -0
  151. package/skills/vue-pinia-best-practices/reference/state-use-pinia-for-large-apps.md +262 -0
  152. package/skills/vue-pinia-best-practices/reference/store-method-binding-parentheses.md +191 -0
  153. package/skills/zig-0.16/SKILL.md +840 -0
  154. package/skills/zig-0.16/scripts/check-zig-version.sh +21 -0
  155. package/src/cli/analyses.ts +280 -0
  156. package/src/cli/index.ts +108 -0
  157. package/src/cli/serve.ts +192 -0
  158. package/src/cli/smoke.ts +131 -0
  159. package/src/cli/status.test.ts +204 -0
  160. package/src/cli/status.ts +263 -0
  161. package/src/cli/vacuum.test.ts +82 -0
  162. package/src/cli/vacuum.ts +96 -0
  163. package/src/config/schema.test.ts +88 -0
  164. package/src/config/schema.ts +64 -0
  165. package/src/db/analyses-migration.test.ts +210 -0
  166. package/src/db/analyses.test.ts +466 -0
  167. package/src/db/analyses.ts +375 -0
  168. package/src/db/auto-checkpoint.ts +131 -0
  169. package/src/db/client.test.ts +129 -0
  170. package/src/db/client.ts +55 -0
  171. package/src/db/fts-escape.ts +20 -0
  172. package/src/db/incidents.test.ts +201 -0
  173. package/src/db/incidents.ts +93 -0
  174. package/src/db/index.ts +86 -0
  175. package/src/db/migrations-v13.test.ts +141 -0
  176. package/src/db/migrations-v8.test.ts +301 -0
  177. package/src/db/migrations.ts +147 -0
  178. package/src/db/plan-archive.test.ts +180 -0
  179. package/src/db/plan-archive.ts +274 -0
  180. package/src/db/plan-create.test.ts +276 -0
  181. package/src/db/plan-create.ts +78 -0
  182. package/src/db/plan-files.test.ts +289 -0
  183. package/src/db/plan-update-status.ts +287 -0
  184. package/src/db/plans.test.ts +490 -0
  185. package/src/db/plans.ts +534 -0
  186. package/src/db/resolve-project-dir.test.ts +143 -0
  187. package/src/db/resolve-project-dir.ts +75 -0
  188. package/src/db/rollbacks.test.ts +150 -0
  189. package/src/db/rollbacks.ts +67 -0
  190. package/src/db/schema.ts +907 -0
  191. package/src/db/sessions.test.ts +80 -0
  192. package/src/db/sessions.ts +135 -0
  193. package/src/db/shutdown.test.ts +147 -0
  194. package/src/db/shutdown.ts +45 -0
  195. package/src/db/tasks.test.ts +921 -0
  196. package/src/db/tasks.ts +747 -0
  197. package/src/db/types.ts +619 -0
  198. package/src/http/__tests__/auth.test.ts +196 -0
  199. package/src/http/__tests__/routes.test.ts +465 -0
  200. package/src/http/__tests__/sse.test.ts +317 -0
  201. package/src/http/auth.ts +72 -0
  202. package/src/http/middleware/cors.ts +53 -0
  203. package/src/http/middleware/security-headers.ts +21 -0
  204. package/src/http/routes/events.ts +112 -0
  205. package/src/http/routes/health.ts +51 -0
  206. package/src/http/routes/plans.ts +66 -0
  207. package/src/http/routes/sessions.ts +50 -0
  208. package/src/http/routes/tasks.ts +60 -0
  209. package/src/http/server.ts +95 -0
  210. package/src/http/sse.ts +116 -0
  211. package/src/index.ts +37 -0
  212. package/src/lib.ts +65 -0
  213. package/src/mem/scoped.ts +65 -0
  214. package/src/orchestrator/background.test.ts +268 -0
  215. package/src/orchestrator/background.ts +293 -0
  216. package/src/orchestrator/memory-hook.ts +182 -0
  217. package/src/orchestrator/reconciler.ts +123 -0
  218. package/src/orchestrator/scheduler.test.ts +300 -0
  219. package/src/orchestrator/scheduler.ts +243 -0
  220. package/src/plugin.test.ts +2574 -0
  221. package/src/plugin.ts +1690 -0
  222. package/src/sdk/client.ts +66 -0
  223. package/src/worktrees/manager.ts +236 -0
  224. package/src/worktrees/state.ts +87 -0
  225. package/tests/integration/ranger-flow.test.ts +257 -0
  226. package/tools/analysis_archive.ts +28 -0
  227. package/tools/analysis_create.ts +55 -0
  228. package/tools/analysis_get.ts +33 -0
  229. package/tools/analysis_link_plan.ts +44 -0
  230. package/tools/analysis_list.ts +48 -0
  231. package/tools/analysis_search.ts +36 -0
  232. package/tools/analysis_update.ts +44 -0
  233. package/tools/plan_approve.ts +31 -0
  234. package/tools/plan_create.ts +58 -0
  235. package/tools/plan_get.ts +40 -0
  236. package/tools/plan_list.ts +37 -0
  237. package/tools/plan_search.ts +34 -0
  238. package/tools/plan_update_status.ts +71 -0
  239. package/tools/session_checkpoint.ts +31 -0
  240. package/tools/session_end.ts +26 -0
  241. package/tools/session_start.ts +43 -0
  242. package/tools/task_create_batch.ts +70 -0
  243. package/tools/task_list.ts +35 -0
  244. package/tools/task_next_for_agent.ts +30 -0
  245. package/tools/task_search.ts +34 -0
  246. package/tools/task_update_status.ts +37 -0
  247. package/tsconfig.json +31 -0
@@ -0,0 +1,501 @@
1
+ ---
2
+ name: rust-testing
3
+ description: Rust testing patterns including unit tests, integration tests, async testing, property-based testing, mocking, and coverage. Follows TDD methodology.
4
+ metadata:
5
+ origin: ECC
6
+ ---
7
+
8
+ # Rust Testing Patterns
9
+
10
+ Comprehensive Rust testing patterns for writing reliable, maintainable tests following TDD methodology.
11
+
12
+ ## When to Use
13
+
14
+ - Writing new Rust functions, methods, or traits
15
+ - Adding test coverage to existing code
16
+ - Creating benchmarks for performance-critical code
17
+ - Implementing property-based tests for input validation
18
+ - Following TDD workflow in Rust projects
19
+
20
+ ## How It Works
21
+
22
+ 1. **Identify target code** — Find the function, trait, or module to test
23
+ 2. **Write a test** — Use `#[test]` in a `#[cfg(test)]` module, rstest for parameterized tests, or proptest for property-based tests
24
+ 3. **Mock dependencies** — Use mockall to isolate the unit under test
25
+ 4. **Run tests (RED)** — Verify the test fails with the expected error
26
+ 5. **Implement (GREEN)** — Write minimal code to pass
27
+ 6. **Refactor** — Improve while keeping tests green
28
+ 7. **Check coverage** — Use cargo-llvm-cov, target 80%+
29
+
30
+ ## TDD Workflow for Rust
31
+
32
+ ### The RED-GREEN-REFACTOR Cycle
33
+
34
+ ```
35
+ RED → Write a failing test first
36
+ GREEN → Write minimal code to pass the test
37
+ REFACTOR → Improve code while keeping tests green
38
+ REPEAT → Continue with next requirement
39
+ ```
40
+
41
+ ### Step-by-Step TDD in Rust
42
+
43
+ ```rust
44
+ // RED: Write test first, use todo!() as placeholder
45
+ pub fn add(a: i32, b: i32) -> i32 { todo!() }
46
+
47
+ #[cfg(test)]
48
+ mod tests {
49
+ use super::*;
50
+ #[test]
51
+ fn test_add() { assert_eq!(add(2, 3), 5); }
52
+ }
53
+ // cargo test → panics at 'not yet implemented'
54
+ ```
55
+
56
+ ```rust
57
+ // GREEN: Replace todo!() with minimal implementation
58
+ pub fn add(a: i32, b: i32) -> i32 { a + b }
59
+ // cargo test → PASS, then REFACTOR while keeping tests green
60
+ ```
61
+
62
+ ## Unit Tests
63
+
64
+ ### Module-Level Test Organization
65
+
66
+ ```rust
67
+ // src/user.rs
68
+ pub struct User {
69
+ pub name: String,
70
+ pub email: String,
71
+ }
72
+
73
+ impl User {
74
+ pub fn new(name: impl Into<String>, email: impl Into<String>) -> Result<Self, String> {
75
+ let email = email.into();
76
+ if !email.contains('@') {
77
+ return Err(format!("invalid email: {email}"));
78
+ }
79
+ Ok(Self { name: name.into(), email })
80
+ }
81
+
82
+ pub fn display_name(&self) -> &str {
83
+ &self.name
84
+ }
85
+ }
86
+
87
+ #[cfg(test)]
88
+ mod tests {
89
+ use super::*;
90
+
91
+ #[test]
92
+ fn creates_user_with_valid_email() {
93
+ let user = User::new("Alice", "alice@example.com").unwrap();
94
+ assert_eq!(user.display_name(), "Alice");
95
+ assert_eq!(user.email, "alice@example.com");
96
+ }
97
+
98
+ #[test]
99
+ fn rejects_invalid_email() {
100
+ let result = User::new("Bob", "not-an-email");
101
+ assert!(result.is_err());
102
+ assert!(result.unwrap_err().contains("invalid email"));
103
+ }
104
+ }
105
+ ```
106
+
107
+ ### Assertion Macros
108
+
109
+ ```rust
110
+ assert_eq!(2 + 2, 4); // Equality
111
+ assert_ne!(2 + 2, 5); // Inequality
112
+ assert!(vec![1, 2, 3].contains(&2)); // Boolean
113
+ assert_eq!(value, 42, "expected 42 but got {value}"); // Custom message
114
+ assert!((0.1_f64 + 0.2 - 0.3).abs() < f64::EPSILON); // Float comparison
115
+ ```
116
+
117
+ ## Error and Panic Testing
118
+
119
+ ### Testing `Result` Returns
120
+
121
+ ```rust
122
+ #[test]
123
+ fn parse_returns_error_for_invalid_input() {
124
+ let result = parse_config("}{invalid");
125
+ assert!(result.is_err());
126
+
127
+ // Assert specific error variant
128
+ let err = result.unwrap_err();
129
+ assert!(matches!(err, ConfigError::ParseError(_)));
130
+ }
131
+
132
+ #[test]
133
+ fn parse_succeeds_for_valid_input() -> Result<(), Box<dyn std::error::Error>> {
134
+ let config = parse_config(r#"{"port": 8080}"#)?;
135
+ assert_eq!(config.port, 8080);
136
+ Ok(()) // Test fails if any ? returns Err
137
+ }
138
+ ```
139
+
140
+ ### Testing Panics
141
+
142
+ ```rust
143
+ #[test]
144
+ #[should_panic]
145
+ fn panics_on_empty_input() {
146
+ process(&[]);
147
+ }
148
+
149
+ #[test]
150
+ #[should_panic(expected = "index out of bounds")]
151
+ fn panics_with_specific_message() {
152
+ let v: Vec<i32> = vec![];
153
+ let _ = v[0];
154
+ }
155
+ ```
156
+
157
+ ## Integration Tests
158
+
159
+ ### File Structure
160
+
161
+ ```text
162
+ my_crate/
163
+ ├── src/
164
+ │ └── lib.rs
165
+ ├── tests/ # Integration tests
166
+ │ ├── api_test.rs # Each file is a separate test binary
167
+ │ ├── db_test.rs
168
+ │ └── common/ # Shared test utilities
169
+ │ └── mod.rs
170
+ ```
171
+
172
+ ### Writing Integration Tests
173
+
174
+ ```rust
175
+ // tests/api_test.rs
176
+ use my_crate::{App, Config};
177
+
178
+ #[test]
179
+ fn full_request_lifecycle() {
180
+ let config = Config::test_default();
181
+ let app = App::new(config);
182
+
183
+ let response = app.handle_request("/health");
184
+ assert_eq!(response.status, 200);
185
+ assert_eq!(response.body, "OK");
186
+ }
187
+ ```
188
+
189
+ ## Async Tests
190
+
191
+ ### With Tokio
192
+
193
+ ```rust
194
+ #[tokio::test]
195
+ async fn fetches_data_successfully() {
196
+ let client = TestClient::new().await;
197
+ let result = client.get("/data").await;
198
+ assert!(result.is_ok());
199
+ assert_eq!(result.unwrap().items.len(), 3);
200
+ }
201
+
202
+ #[tokio::test]
203
+ async fn handles_timeout() {
204
+ use std::time::Duration;
205
+ let result = tokio::time::timeout(
206
+ Duration::from_millis(100),
207
+ slow_operation(),
208
+ ).await;
209
+
210
+ assert!(result.is_err(), "should have timed out");
211
+ }
212
+ ```
213
+
214
+ ## Test Organization Patterns
215
+
216
+ ### Parameterized Tests with `rstest`
217
+
218
+ ```rust
219
+ use rstest::{rstest, fixture};
220
+
221
+ #[rstest]
222
+ #[case("hello", 5)]
223
+ #[case("", 0)]
224
+ #[case("rust", 4)]
225
+ fn test_string_length(#[case] input: &str, #[case] expected: usize) {
226
+ assert_eq!(input.len(), expected);
227
+ }
228
+
229
+ // Fixtures
230
+ #[fixture]
231
+ fn test_db() -> TestDb {
232
+ TestDb::new_in_memory()
233
+ }
234
+
235
+ #[rstest]
236
+ fn test_insert(test_db: TestDb) {
237
+ test_db.insert("key", "value");
238
+ assert_eq!(test_db.get("key"), Some("value".into()));
239
+ }
240
+ ```
241
+
242
+ ### Test Helpers
243
+
244
+ ```rust
245
+ #[cfg(test)]
246
+ mod tests {
247
+ use super::*;
248
+
249
+ /// Creates a test user with sensible defaults.
250
+ fn make_user(name: &str) -> User {
251
+ User::new(name, &format!("{name}@test.com")).unwrap()
252
+ }
253
+
254
+ #[test]
255
+ fn user_display() {
256
+ let user = make_user("alice");
257
+ assert_eq!(user.display_name(), "alice");
258
+ }
259
+ }
260
+ ```
261
+
262
+ ## Property-Based Testing with `proptest`
263
+
264
+ ### Basic Property Tests
265
+
266
+ ```rust
267
+ use proptest::prelude::*;
268
+
269
+ proptest! {
270
+ #[test]
271
+ fn encode_decode_roundtrip(input in ".*") {
272
+ let encoded = encode(&input);
273
+ let decoded = decode(&encoded).unwrap();
274
+ assert_eq!(input, decoded);
275
+ }
276
+
277
+ #[test]
278
+ fn sort_preserves_length(mut vec in prop::collection::vec(any::<i32>(), 0..100)) {
279
+ let original_len = vec.len();
280
+ vec.sort();
281
+ assert_eq!(vec.len(), original_len);
282
+ }
283
+
284
+ #[test]
285
+ fn sort_produces_ordered_output(mut vec in prop::collection::vec(any::<i32>(), 0..100)) {
286
+ vec.sort();
287
+ for window in vec.windows(2) {
288
+ assert!(window[0] <= window[1]);
289
+ }
290
+ }
291
+ }
292
+ ```
293
+
294
+ ### Custom Strategies
295
+
296
+ ```rust
297
+ use proptest::prelude::*;
298
+
299
+ fn valid_email() -> impl Strategy<Value = String> {
300
+ ("[a-z]{1,10}", "[a-z]{1,5}")
301
+ .prop_map(|(user, domain)| format!("{user}@{domain}.com"))
302
+ }
303
+
304
+ proptest! {
305
+ #[test]
306
+ fn accepts_valid_emails(email in valid_email()) {
307
+ assert!(User::new("Test", &email).is_ok());
308
+ }
309
+ }
310
+ ```
311
+
312
+ ## Mocking with `mockall`
313
+
314
+ ### Trait-Based Mocking
315
+
316
+ ```rust
317
+ use mockall::{automock, predicate::eq};
318
+
319
+ #[automock]
320
+ trait UserRepository {
321
+ fn find_by_id(&self, id: u64) -> Option<User>;
322
+ fn save(&self, user: &User) -> Result<(), StorageError>;
323
+ }
324
+
325
+ #[test]
326
+ fn service_returns_user_when_found() {
327
+ let mut mock = MockUserRepository::new();
328
+ mock.expect_find_by_id()
329
+ .with(eq(42))
330
+ .times(1)
331
+ .returning(|_| Some(User { id: 42, name: "Alice".into() }));
332
+
333
+ let service = UserService::new(Box::new(mock));
334
+ let user = service.get_user(42).unwrap();
335
+ assert_eq!(user.name, "Alice");
336
+ }
337
+
338
+ #[test]
339
+ fn service_returns_none_when_not_found() {
340
+ let mut mock = MockUserRepository::new();
341
+ mock.expect_find_by_id()
342
+ .returning(|_| None);
343
+
344
+ let service = UserService::new(Box::new(mock));
345
+ assert!(service.get_user(99).is_none());
346
+ }
347
+ ```
348
+
349
+ ## Doc Tests
350
+
351
+ ### Executable Documentation
352
+
353
+ ```rust
354
+ /// Adds two numbers together.
355
+ ///
356
+ /// # Examples
357
+ ///
358
+ /// ```
359
+ /// use my_crate::add;
360
+ ///
361
+ /// assert_eq!(add(2, 3), 5);
362
+ /// assert_eq!(add(-1, 1), 0);
363
+ /// ```
364
+ pub fn add(a: i32, b: i32) -> i32 {
365
+ a + b
366
+ }
367
+
368
+ /// Parses a config string.
369
+ ///
370
+ /// # Errors
371
+ ///
372
+ /// Returns `Err` if the input is not valid TOML.
373
+ ///
374
+ /// ```no_run
375
+ /// use my_crate::parse_config;
376
+ ///
377
+ /// let config = parse_config(r#"port = 8080"#).unwrap();
378
+ /// assert_eq!(config.port, 8080);
379
+ /// ```
380
+ ///
381
+ /// ```no_run
382
+ /// use my_crate::parse_config;
383
+ ///
384
+ /// assert!(parse_config("}{invalid").is_err());
385
+ /// ```
386
+ pub fn parse_config(input: &str) -> Result<Config, ParseError> {
387
+ todo!()
388
+ }
389
+ ```
390
+
391
+ ## Benchmarking with Criterion
392
+
393
+ ```toml
394
+ # Cargo.toml
395
+ [dev-dependencies]
396
+ criterion = { version = "0.5", features = ["html_reports"] }
397
+
398
+ [[bench]]
399
+ name = "benchmark"
400
+ harness = false
401
+ ```
402
+
403
+ ```rust
404
+ // benches/benchmark.rs
405
+ use criterion::{black_box, criterion_group, criterion_main, Criterion};
406
+
407
+ fn fibonacci(n: u64) -> u64 {
408
+ match n {
409
+ 0 | 1 => n,
410
+ _ => fibonacci(n - 1) + fibonacci(n - 2),
411
+ }
412
+ }
413
+
414
+ fn bench_fibonacci(c: &mut Criterion) {
415
+ c.bench_function("fib 20", |b| b.iter(|| fibonacci(black_box(20))));
416
+ }
417
+
418
+ criterion_group!(benches, bench_fibonacci);
419
+ criterion_main!(benches);
420
+ ```
421
+
422
+ ## Test Coverage
423
+
424
+ ### Running Coverage
425
+
426
+ ```bash
427
+ # Install: cargo install cargo-llvm-cov (or use taiki-e/install-action in CI)
428
+ cargo llvm-cov # Summary
429
+ cargo llvm-cov --html # HTML report
430
+ cargo llvm-cov --lcov > lcov.info # LCOV format for CI
431
+ cargo llvm-cov --fail-under-lines 80 # Fail if below threshold
432
+ ```
433
+
434
+ ### Coverage Targets
435
+
436
+ | Code Type | Target |
437
+ |-----------|--------|
438
+ | Critical business logic | 100% |
439
+ | Public API | 90%+ |
440
+ | General code | 80%+ |
441
+ | Generated / FFI bindings | Exclude |
442
+
443
+ ## Testing Commands
444
+
445
+ ```bash
446
+ cargo test # Run all tests
447
+ cargo test -- --nocapture # Show println output
448
+ cargo test test_name # Run tests matching pattern
449
+ cargo test --lib # Unit tests only
450
+ cargo test --test api_test # Integration tests only
451
+ cargo test --doc # Doc tests only
452
+ cargo test --no-fail-fast # Don't stop on first failure
453
+ cargo test -- --ignored # Run ignored tests
454
+ ```
455
+
456
+ ## Best Practices
457
+
458
+ **DO:**
459
+ - Write tests FIRST (TDD)
460
+ - Use `#[cfg(test)]` modules for unit tests
461
+ - Test behavior, not implementation
462
+ - Use descriptive test names that explain the scenario
463
+ - Prefer `assert_eq!` over `assert!` for better error messages
464
+ - Use `?` in tests that return `Result` for cleaner error output
465
+ - Keep tests independent — no shared mutable state
466
+
467
+ **DON'T:**
468
+ - Use `#[should_panic]` when you can test `Result::is_err()` instead
469
+ - Mock everything — prefer integration tests when feasible
470
+ - Ignore flaky tests — fix or quarantine them
471
+ - Use `sleep()` in tests — use channels, barriers, or `tokio::time::pause()`
472
+ - Skip error path testing
473
+
474
+ ## CI Integration
475
+
476
+ ```yaml
477
+ # GitHub Actions
478
+ test:
479
+ runs-on: ubuntu-latest
480
+ steps:
481
+ - uses: actions/checkout@v4
482
+ - uses: dtolnay/rust-toolchain@stable
483
+ with:
484
+ components: clippy, rustfmt
485
+
486
+ - name: Check formatting
487
+ run: cargo fmt --check
488
+
489
+ - name: Clippy
490
+ run: cargo clippy -- -D warnings
491
+
492
+ - name: Run tests
493
+ run: cargo test
494
+
495
+ - uses: taiki-e/install-action@cargo-llvm-cov
496
+
497
+ - name: Coverage
498
+ run: cargo llvm-cov --fail-under-lines 80
499
+ ```
500
+
501
+ **Remember**: Tests are documentation. They show how your code is meant to be used. Write them clearly and keep them up to date.