opencode-swarm-plugin 0.43.0 → 0.44.1

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 (208) hide show
  1. package/bin/cass.characterization.test.ts +422 -0
  2. package/bin/swarm.serve.test.ts +6 -4
  3. package/bin/swarm.test.ts +68 -0
  4. package/bin/swarm.ts +81 -8
  5. package/dist/compaction-prompt-scoring.js +139 -0
  6. package/dist/contributor-tools.d.ts +42 -0
  7. package/dist/contributor-tools.d.ts.map +1 -0
  8. package/dist/eval-capture.js +12811 -0
  9. package/dist/hive.d.ts.map +1 -1
  10. package/dist/index.d.ts +12 -0
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +7728 -62590
  13. package/dist/plugin.js +23833 -78695
  14. package/dist/sessions/agent-discovery.d.ts +59 -0
  15. package/dist/sessions/agent-discovery.d.ts.map +1 -0
  16. package/dist/sessions/index.d.ts +10 -0
  17. package/dist/sessions/index.d.ts.map +1 -0
  18. package/dist/swarm-orchestrate.d.ts.map +1 -1
  19. package/dist/swarm-prompts.d.ts.map +1 -1
  20. package/dist/swarm-review.d.ts.map +1 -1
  21. package/package.json +17 -5
  22. package/.changeset/swarm-insights-data-layer.md +0 -63
  23. package/.hive/analysis/eval-failure-analysis-2025-12-25.md +0 -331
  24. package/.hive/analysis/session-data-quality-audit.md +0 -320
  25. package/.hive/eval-results.json +0 -483
  26. package/.hive/issues.jsonl +0 -138
  27. package/.hive/memories.jsonl +0 -729
  28. package/.opencode/eval-history.jsonl +0 -327
  29. package/.turbo/turbo-build.log +0 -9
  30. package/CHANGELOG.md +0 -2255
  31. package/SCORER-ANALYSIS.md +0 -598
  32. package/docs/analysis/subagent-coordination-patterns.md +0 -902
  33. package/docs/analysis-socratic-planner-pattern.md +0 -504
  34. package/docs/planning/ADR-001-monorepo-structure.md +0 -171
  35. package/docs/planning/ADR-002-package-extraction.md +0 -393
  36. package/docs/planning/ADR-003-performance-improvements.md +0 -451
  37. package/docs/planning/ADR-004-message-queue-features.md +0 -187
  38. package/docs/planning/ADR-005-devtools-observability.md +0 -202
  39. package/docs/planning/ADR-007-swarm-enhancements-worktree-review.md +0 -168
  40. package/docs/planning/ADR-008-worker-handoff-protocol.md +0 -293
  41. package/docs/planning/ADR-009-oh-my-opencode-patterns.md +0 -353
  42. package/docs/planning/ROADMAP.md +0 -368
  43. package/docs/semantic-memory-cli-syntax.md +0 -123
  44. package/docs/swarm-mail-architecture.md +0 -1147
  45. package/docs/testing/context-recovery-test.md +0 -470
  46. package/evals/ARCHITECTURE.md +0 -1189
  47. package/evals/README.md +0 -768
  48. package/evals/compaction-prompt.eval.ts +0 -149
  49. package/evals/compaction-resumption.eval.ts +0 -289
  50. package/evals/coordinator-behavior.eval.ts +0 -307
  51. package/evals/coordinator-session.eval.ts +0 -154
  52. package/evals/evalite.config.ts.bak +0 -15
  53. package/evals/example.eval.ts +0 -31
  54. package/evals/fixtures/compaction-cases.ts +0 -350
  55. package/evals/fixtures/compaction-prompt-cases.ts +0 -311
  56. package/evals/fixtures/coordinator-sessions.ts +0 -328
  57. package/evals/fixtures/decomposition-cases.ts +0 -105
  58. package/evals/lib/compaction-loader.test.ts +0 -248
  59. package/evals/lib/compaction-loader.ts +0 -320
  60. package/evals/lib/data-loader.evalite-test.ts +0 -289
  61. package/evals/lib/data-loader.test.ts +0 -345
  62. package/evals/lib/data-loader.ts +0 -281
  63. package/evals/lib/llm.ts +0 -115
  64. package/evals/scorers/compaction-prompt-scorers.ts +0 -145
  65. package/evals/scorers/compaction-scorers.ts +0 -305
  66. package/evals/scorers/coordinator-discipline.evalite-test.ts +0 -539
  67. package/evals/scorers/coordinator-discipline.ts +0 -325
  68. package/evals/scorers/index.test.ts +0 -146
  69. package/evals/scorers/index.ts +0 -328
  70. package/evals/scorers/outcome-scorers.evalite-test.ts +0 -27
  71. package/evals/scorers/outcome-scorers.ts +0 -349
  72. package/evals/swarm-decomposition.eval.ts +0 -121
  73. package/examples/commands/swarm.md +0 -745
  74. package/examples/plugin-wrapper-template.ts +0 -2426
  75. package/examples/skills/hive-workflow/SKILL.md +0 -212
  76. package/examples/skills/skill-creator/SKILL.md +0 -223
  77. package/examples/skills/swarm-coordination/SKILL.md +0 -292
  78. package/global-skills/cli-builder/SKILL.md +0 -344
  79. package/global-skills/cli-builder/references/advanced-patterns.md +0 -244
  80. package/global-skills/learning-systems/SKILL.md +0 -644
  81. package/global-skills/skill-creator/LICENSE.txt +0 -202
  82. package/global-skills/skill-creator/SKILL.md +0 -352
  83. package/global-skills/skill-creator/references/output-patterns.md +0 -82
  84. package/global-skills/skill-creator/references/workflows.md +0 -28
  85. package/global-skills/swarm-coordination/SKILL.md +0 -995
  86. package/global-skills/swarm-coordination/references/coordinator-patterns.md +0 -235
  87. package/global-skills/swarm-coordination/references/strategies.md +0 -138
  88. package/global-skills/system-design/SKILL.md +0 -213
  89. package/global-skills/testing-patterns/SKILL.md +0 -430
  90. package/global-skills/testing-patterns/references/dependency-breaking-catalog.md +0 -586
  91. package/opencode-swarm-plugin-0.30.7.tgz +0 -0
  92. package/opencode-swarm-plugin-0.31.0.tgz +0 -0
  93. package/scripts/cleanup-test-memories.ts +0 -346
  94. package/scripts/init-skill.ts +0 -222
  95. package/scripts/migrate-unknown-sessions.ts +0 -349
  96. package/scripts/validate-skill.ts +0 -204
  97. package/src/agent-mail.ts +0 -1724
  98. package/src/anti-patterns.test.ts +0 -1167
  99. package/src/anti-patterns.ts +0 -448
  100. package/src/compaction-capture.integration.test.ts +0 -257
  101. package/src/compaction-hook.test.ts +0 -838
  102. package/src/compaction-hook.ts +0 -1204
  103. package/src/compaction-observability.integration.test.ts +0 -139
  104. package/src/compaction-observability.test.ts +0 -187
  105. package/src/compaction-observability.ts +0 -324
  106. package/src/compaction-prompt-scorers.test.ts +0 -475
  107. package/src/compaction-prompt-scoring.ts +0 -300
  108. package/src/dashboard.test.ts +0 -611
  109. package/src/dashboard.ts +0 -462
  110. package/src/error-enrichment.test.ts +0 -403
  111. package/src/error-enrichment.ts +0 -219
  112. package/src/eval-capture.test.ts +0 -1015
  113. package/src/eval-capture.ts +0 -929
  114. package/src/eval-gates.test.ts +0 -306
  115. package/src/eval-gates.ts +0 -218
  116. package/src/eval-history.test.ts +0 -508
  117. package/src/eval-history.ts +0 -214
  118. package/src/eval-learning.test.ts +0 -378
  119. package/src/eval-learning.ts +0 -360
  120. package/src/eval-runner.test.ts +0 -223
  121. package/src/eval-runner.ts +0 -402
  122. package/src/export-tools.test.ts +0 -476
  123. package/src/export-tools.ts +0 -257
  124. package/src/hive.integration.test.ts +0 -2241
  125. package/src/hive.ts +0 -1628
  126. package/src/index.ts +0 -935
  127. package/src/learning.integration.test.ts +0 -1815
  128. package/src/learning.ts +0 -1079
  129. package/src/logger.test.ts +0 -189
  130. package/src/logger.ts +0 -135
  131. package/src/mandate-promotion.test.ts +0 -473
  132. package/src/mandate-promotion.ts +0 -239
  133. package/src/mandate-storage.integration.test.ts +0 -601
  134. package/src/mandate-storage.test.ts +0 -578
  135. package/src/mandate-storage.ts +0 -794
  136. package/src/mandates.ts +0 -540
  137. package/src/memory-tools.test.ts +0 -195
  138. package/src/memory-tools.ts +0 -344
  139. package/src/memory.integration.test.ts +0 -334
  140. package/src/memory.test.ts +0 -158
  141. package/src/memory.ts +0 -527
  142. package/src/model-selection.test.ts +0 -188
  143. package/src/model-selection.ts +0 -68
  144. package/src/observability-tools.test.ts +0 -359
  145. package/src/observability-tools.ts +0 -871
  146. package/src/output-guardrails.test.ts +0 -438
  147. package/src/output-guardrails.ts +0 -381
  148. package/src/pattern-maturity.test.ts +0 -1160
  149. package/src/pattern-maturity.ts +0 -525
  150. package/src/planning-guardrails.test.ts +0 -491
  151. package/src/planning-guardrails.ts +0 -438
  152. package/src/plugin.ts +0 -23
  153. package/src/post-compaction-tracker.test.ts +0 -251
  154. package/src/post-compaction-tracker.ts +0 -237
  155. package/src/query-tools.test.ts +0 -636
  156. package/src/query-tools.ts +0 -324
  157. package/src/rate-limiter.integration.test.ts +0 -466
  158. package/src/rate-limiter.ts +0 -774
  159. package/src/replay-tools.test.ts +0 -496
  160. package/src/replay-tools.ts +0 -240
  161. package/src/repo-crawl.integration.test.ts +0 -441
  162. package/src/repo-crawl.ts +0 -610
  163. package/src/schemas/cell-events.test.ts +0 -347
  164. package/src/schemas/cell-events.ts +0 -807
  165. package/src/schemas/cell.ts +0 -257
  166. package/src/schemas/evaluation.ts +0 -166
  167. package/src/schemas/index.test.ts +0 -199
  168. package/src/schemas/index.ts +0 -286
  169. package/src/schemas/mandate.ts +0 -232
  170. package/src/schemas/swarm-context.ts +0 -115
  171. package/src/schemas/task.ts +0 -161
  172. package/src/schemas/worker-handoff.test.ts +0 -302
  173. package/src/schemas/worker-handoff.ts +0 -131
  174. package/src/skills.integration.test.ts +0 -1192
  175. package/src/skills.test.ts +0 -643
  176. package/src/skills.ts +0 -1549
  177. package/src/storage.integration.test.ts +0 -341
  178. package/src/storage.ts +0 -884
  179. package/src/structured.integration.test.ts +0 -817
  180. package/src/structured.test.ts +0 -1046
  181. package/src/structured.ts +0 -762
  182. package/src/swarm-decompose.test.ts +0 -188
  183. package/src/swarm-decompose.ts +0 -1302
  184. package/src/swarm-deferred.integration.test.ts +0 -157
  185. package/src/swarm-deferred.test.ts +0 -38
  186. package/src/swarm-insights.test.ts +0 -214
  187. package/src/swarm-insights.ts +0 -459
  188. package/src/swarm-mail.integration.test.ts +0 -970
  189. package/src/swarm-mail.ts +0 -739
  190. package/src/swarm-orchestrate.integration.test.ts +0 -282
  191. package/src/swarm-orchestrate.test.ts +0 -548
  192. package/src/swarm-orchestrate.ts +0 -3084
  193. package/src/swarm-prompts.test.ts +0 -1270
  194. package/src/swarm-prompts.ts +0 -2077
  195. package/src/swarm-research.integration.test.ts +0 -701
  196. package/src/swarm-research.test.ts +0 -698
  197. package/src/swarm-research.ts +0 -472
  198. package/src/swarm-review.integration.test.ts +0 -285
  199. package/src/swarm-review.test.ts +0 -879
  200. package/src/swarm-review.ts +0 -709
  201. package/src/swarm-strategies.ts +0 -407
  202. package/src/swarm-worktree.test.ts +0 -501
  203. package/src/swarm-worktree.ts +0 -575
  204. package/src/swarm.integration.test.ts +0 -2377
  205. package/src/swarm.ts +0 -38
  206. package/src/tool-adapter.integration.test.ts +0 -1221
  207. package/src/tool-availability.ts +0 -461
  208. package/tsconfig.json +0 -28
@@ -1,1167 +0,0 @@
1
- /**
2
- * Tests for anti-pattern learning module
3
- */
4
- import { describe, expect, it } from "vitest";
5
- import {
6
- DEFAULT_ANTI_PATTERN_CONFIG,
7
- DecompositionPatternSchema,
8
- InMemoryPatternStorage,
9
- PatternInversionResultSchema,
10
- PatternKindSchema,
11
- createPattern,
12
- extractPatternsFromDescription,
13
- formatAntiPatternsForPrompt,
14
- formatSuccessfulPatternsForPrompt,
15
- invertToAntiPattern,
16
- recordPatternObservation,
17
- shouldInvertPattern,
18
- type AntiPatternConfig,
19
- type DecompositionPattern,
20
- } from "./anti-patterns";
21
-
22
- // ============================================================================
23
- // Schema Validation Tests
24
- // ============================================================================
25
-
26
- describe("PatternKindSchema", () => {
27
- it("validates 'pattern' kind", () => {
28
- expect(() => PatternKindSchema.parse("pattern")).not.toThrow();
29
- });
30
-
31
- it("validates 'anti_pattern' kind", () => {
32
- expect(() => PatternKindSchema.parse("anti_pattern")).not.toThrow();
33
- });
34
-
35
- it("rejects invalid kind", () => {
36
- expect(() => PatternKindSchema.parse("invalid")).toThrow();
37
- });
38
- });
39
-
40
- describe("DecompositionPatternSchema", () => {
41
- it("validates a complete valid pattern", () => {
42
- const pattern = {
43
- id: "pattern-123",
44
- content: "Split by file type",
45
- kind: "pattern",
46
- is_negative: false,
47
- success_count: 5,
48
- failure_count: 2,
49
- created_at: new Date().toISOString(),
50
- updated_at: new Date().toISOString(),
51
- example_beads: ["bd-123", "bd-456"],
52
- tags: ["file-splitting"],
53
- reason: "Test pattern",
54
- };
55
- expect(() => DecompositionPatternSchema.parse(pattern)).not.toThrow();
56
- });
57
-
58
- it("validates a valid anti-pattern", () => {
59
- const antiPattern = {
60
- id: "anti-pattern-123",
61
- content: "AVOID: Split by file type",
62
- kind: "anti_pattern",
63
- is_negative: true,
64
- success_count: 2,
65
- failure_count: 8,
66
- created_at: new Date().toISOString(),
67
- updated_at: new Date().toISOString(),
68
- example_beads: [],
69
- tags: [],
70
- };
71
- expect(() => DecompositionPatternSchema.parse(antiPattern)).not.toThrow();
72
- });
73
-
74
- it("applies default values for optional fields", () => {
75
- const minimal = {
76
- id: "pattern-minimal",
77
- content: "Test pattern",
78
- kind: "pattern",
79
- is_negative: false,
80
- created_at: new Date().toISOString(),
81
- updated_at: new Date().toISOString(),
82
- };
83
- const parsed = DecompositionPatternSchema.parse(minimal);
84
- expect(parsed.success_count).toBe(0);
85
- expect(parsed.failure_count).toBe(0);
86
- expect(parsed.tags).toEqual([]);
87
- expect(parsed.example_beads).toEqual([]);
88
- });
89
-
90
- it("rejects negative success_count", () => {
91
- const pattern = {
92
- id: "pattern-invalid",
93
- content: "Test",
94
- kind: "pattern",
95
- is_negative: false,
96
- success_count: -1,
97
- failure_count: 0,
98
- created_at: new Date().toISOString(),
99
- updated_at: new Date().toISOString(),
100
- };
101
- expect(() => DecompositionPatternSchema.parse(pattern)).toThrow();
102
- });
103
-
104
- it("rejects negative failure_count", () => {
105
- const pattern = {
106
- id: "pattern-invalid",
107
- content: "Test",
108
- kind: "pattern",
109
- is_negative: false,
110
- success_count: 0,
111
- failure_count: -1,
112
- created_at: new Date().toISOString(),
113
- updated_at: new Date().toISOString(),
114
- };
115
- expect(() => DecompositionPatternSchema.parse(pattern)).toThrow();
116
- });
117
-
118
- it("rejects invalid kind", () => {
119
- const pattern = {
120
- id: "pattern-invalid",
121
- content: "Test",
122
- kind: "invalid_kind",
123
- is_negative: false,
124
- success_count: 0,
125
- failure_count: 0,
126
- created_at: new Date().toISOString(),
127
- updated_at: new Date().toISOString(),
128
- };
129
- expect(() => DecompositionPatternSchema.parse(pattern)).toThrow();
130
- });
131
- });
132
-
133
- describe("PatternInversionResultSchema", () => {
134
- it("validates a complete inversion result", () => {
135
- const now = new Date().toISOString();
136
- const result = {
137
- original: {
138
- id: "pattern-123",
139
- content: "Split by file type",
140
- kind: "pattern",
141
- is_negative: false,
142
- success_count: 2,
143
- failure_count: 8,
144
- created_at: now,
145
- updated_at: now,
146
- tags: [],
147
- example_beads: [],
148
- },
149
- inverted: {
150
- id: "anti-pattern-123",
151
- content: "AVOID: Split by file type",
152
- kind: "anti_pattern",
153
- is_negative: true,
154
- success_count: 2,
155
- failure_count: 8,
156
- created_at: now,
157
- updated_at: now,
158
- tags: [],
159
- example_beads: [],
160
- },
161
- reason: "Failed 8/10 times (80% failure rate)",
162
- };
163
- expect(() => PatternInversionResultSchema.parse(result)).not.toThrow();
164
- });
165
- });
166
-
167
- // ============================================================================
168
- // shouldInvertPattern Tests
169
- // ============================================================================
170
-
171
- describe("shouldInvertPattern", () => {
172
- const basePattern: DecompositionPattern = {
173
- id: "pattern-test",
174
- content: "Test pattern",
175
- kind: "pattern",
176
- is_negative: false,
177
- success_count: 0,
178
- failure_count: 0,
179
- created_at: new Date().toISOString(),
180
- updated_at: new Date().toISOString(),
181
- tags: [],
182
- example_beads: [],
183
- };
184
-
185
- it("returns true when failure rate exceeds 60%", () => {
186
- const pattern = {
187
- ...basePattern,
188
- success_count: 3,
189
- failure_count: 7, // 70% failure rate
190
- };
191
- expect(shouldInvertPattern(pattern)).toBe(true);
192
- });
193
-
194
- it("returns true when failure rate equals 60%", () => {
195
- const pattern = {
196
- ...basePattern,
197
- success_count: 4,
198
- failure_count: 6, // Exactly 60% failure rate
199
- };
200
- expect(shouldInvertPattern(pattern)).toBe(true);
201
- });
202
-
203
- it("returns false when failure rate is below 60%", () => {
204
- const pattern = {
205
- ...basePattern,
206
- success_count: 6,
207
- failure_count: 4, // 40% failure rate
208
- };
209
- expect(shouldInvertPattern(pattern)).toBe(false);
210
- });
211
-
212
- it("returns false when failure rate is just below threshold", () => {
213
- const pattern = {
214
- ...basePattern,
215
- success_count: 41,
216
- failure_count: 59, // 59% failure rate (just below 60%)
217
- };
218
- expect(shouldInvertPattern(pattern)).toBe(false);
219
- });
220
-
221
- it("returns false with insufficient observations (< minObservations)", () => {
222
- const pattern = {
223
- ...basePattern,
224
- success_count: 0,
225
- failure_count: 2, // Only 2 observations, need 3
226
- };
227
- expect(shouldInvertPattern(pattern)).toBe(false);
228
- });
229
-
230
- it("returns false when exactly at minObservations but low failure rate", () => {
231
- const pattern = {
232
- ...basePattern,
233
- success_count: 2,
234
- failure_count: 1, // Exactly 3 observations, 33% failure
235
- };
236
- expect(shouldInvertPattern(pattern)).toBe(false);
237
- });
238
-
239
- it("returns true when at minObservations with high failure rate", () => {
240
- const pattern = {
241
- ...basePattern,
242
- success_count: 1,
243
- failure_count: 2, // Exactly 3 observations, 67% failure
244
- };
245
- expect(shouldInvertPattern(pattern)).toBe(true);
246
- });
247
-
248
- it("returns false when already an anti-pattern", () => {
249
- const antiPattern = {
250
- ...basePattern,
251
- kind: "anti_pattern" as const,
252
- is_negative: true,
253
- success_count: 0,
254
- failure_count: 10, // 100% failure but already anti-pattern
255
- };
256
- expect(shouldInvertPattern(antiPattern)).toBe(false);
257
- });
258
-
259
- it("returns false with zero observations", () => {
260
- const pattern = {
261
- ...basePattern,
262
- success_count: 0,
263
- failure_count: 0,
264
- };
265
- expect(shouldInvertPattern(pattern)).toBe(false);
266
- });
267
-
268
- it("respects custom config minObservations", () => {
269
- const pattern = {
270
- ...basePattern,
271
- success_count: 1,
272
- failure_count: 4, // 80% failure
273
- };
274
- const config: AntiPatternConfig = {
275
- ...DEFAULT_ANTI_PATTERN_CONFIG,
276
- minObservations: 5,
277
- };
278
- expect(shouldInvertPattern(pattern, config)).toBe(true);
279
- });
280
-
281
- it("respects custom config failureRatioThreshold", () => {
282
- const pattern = {
283
- ...basePattern,
284
- success_count: 3,
285
- failure_count: 7, // 70% failure
286
- };
287
- const config: AntiPatternConfig = {
288
- ...DEFAULT_ANTI_PATTERN_CONFIG,
289
- failureRatioThreshold: 0.8, // Need 80% failure
290
- };
291
- expect(shouldInvertPattern(pattern, config)).toBe(false);
292
- });
293
- });
294
-
295
- // ============================================================================
296
- // invertToAntiPattern Tests
297
- // ============================================================================
298
-
299
- describe("invertToAntiPattern", () => {
300
- const basePattern: DecompositionPattern = {
301
- id: "pattern-123",
302
- content: "Split by file type",
303
- kind: "pattern",
304
- is_negative: false,
305
- success_count: 2,
306
- failure_count: 8,
307
- created_at: new Date().toISOString(),
308
- updated_at: new Date().toISOString(),
309
- tags: ["file-splitting"],
310
- example_beads: ["bd-123", "bd-456"],
311
- };
312
-
313
- it("converts pattern to anti-pattern with correct kind", () => {
314
- const result = invertToAntiPattern(basePattern, "Test reason");
315
- expect(result.inverted.kind).toBe("anti_pattern");
316
- expect(result.inverted.is_negative).toBe(true);
317
- });
318
-
319
- it("prefixes content with AVOID:", () => {
320
- const result = invertToAntiPattern(basePattern, "Test reason");
321
- expect(result.inverted.content).toContain("AVOID:");
322
- expect(result.inverted.content).toContain("Split by file type");
323
- });
324
-
325
- it("appends reason to content", () => {
326
- const result = invertToAntiPattern(basePattern, "Failed too many times");
327
- expect(result.inverted.content).toContain("Failed too many times");
328
- });
329
-
330
- it("preserves success and failure counts", () => {
331
- const result = invertToAntiPattern(basePattern, "Test");
332
- expect(result.inverted.success_count).toBe(basePattern.success_count);
333
- expect(result.inverted.failure_count).toBe(basePattern.failure_count);
334
- });
335
-
336
- it("preserves example_beads", () => {
337
- const result = invertToAntiPattern(basePattern, "Test");
338
- expect(result.inverted.example_beads).toEqual(["bd-123", "bd-456"]);
339
- });
340
-
341
- it("preserves tags", () => {
342
- const result = invertToAntiPattern(basePattern, "Test");
343
- expect(result.inverted.tags).toEqual(["file-splitting"]);
344
- });
345
-
346
- it("generates new ID with 'anti-' prefix", () => {
347
- const result = invertToAntiPattern(basePattern, "Test");
348
- expect(result.inverted.id).toBe("anti-pattern-123");
349
- });
350
-
351
- it("stores reason in inverted pattern", () => {
352
- const result = invertToAntiPattern(basePattern, "Custom reason");
353
- expect(result.inverted.reason).toBe("Custom reason");
354
- });
355
-
356
- it("updates updated_at timestamp", () => {
357
- const before = new Date();
358
- const result = invertToAntiPattern(basePattern, "Test");
359
- const after = new Date();
360
- const updatedAt = new Date(result.inverted.updated_at);
361
- expect(updatedAt.getTime()).toBeGreaterThanOrEqual(before.getTime());
362
- expect(updatedAt.getTime()).toBeLessThanOrEqual(after.getTime());
363
- });
364
-
365
- it("returns original pattern in result", () => {
366
- const result = invertToAntiPattern(basePattern, "Test");
367
- expect(result.original).toEqual(basePattern);
368
- });
369
-
370
- it("returns reason in result", () => {
371
- const result = invertToAntiPattern(basePattern, "Test reason");
372
- expect(result.reason).toBe("Test reason");
373
- });
374
-
375
- it("cleans existing AVOID: prefix", () => {
376
- const pattern = {
377
- ...basePattern,
378
- content: "AVOID: Split by file type",
379
- };
380
- const result = invertToAntiPattern(pattern, "Test");
381
- // Should not have double prefix
382
- expect(result.inverted.content).toMatch(/^AVOID: Split by file type\./);
383
- expect(result.inverted.content).not.toMatch(/AVOID:.*AVOID:/);
384
- });
385
-
386
- it("cleans existing DO NOT: prefix", () => {
387
- const pattern = {
388
- ...basePattern,
389
- content: "DO NOT: Split by file type",
390
- };
391
- const result = invertToAntiPattern(pattern, "Test");
392
- expect(result.inverted.content).toMatch(/^AVOID: Split by file type\./);
393
- });
394
-
395
- it("cleans existing NEVER: prefix", () => {
396
- const pattern = {
397
- ...basePattern,
398
- content: "NEVER: Split by file type",
399
- };
400
- const result = invertToAntiPattern(pattern, "Test");
401
- expect(result.inverted.content).toMatch(/^AVOID: Split by file type\./);
402
- });
403
-
404
- it("respects custom antiPatternPrefix", () => {
405
- const config: AntiPatternConfig = {
406
- ...DEFAULT_ANTI_PATTERN_CONFIG,
407
- antiPatternPrefix: "DO NOT: ",
408
- };
409
- const result = invertToAntiPattern(basePattern, "Test", config);
410
- expect(result.inverted.content).toContain("DO NOT:");
411
- });
412
- });
413
-
414
- // ============================================================================
415
- // recordPatternObservation Tests
416
- // ============================================================================
417
-
418
- describe("recordPatternObservation", () => {
419
- const basePattern: DecompositionPattern = {
420
- id: "pattern-test",
421
- content: "Test pattern",
422
- kind: "pattern",
423
- is_negative: false,
424
- success_count: 5,
425
- failure_count: 2,
426
- created_at: new Date().toISOString(),
427
- updated_at: new Date().toISOString(),
428
- tags: [],
429
- example_beads: [],
430
- };
431
-
432
- it("increments success count on success", () => {
433
- const result = recordPatternObservation(basePattern, true);
434
- expect(result.pattern.success_count).toBe(6);
435
- expect(result.pattern.failure_count).toBe(2);
436
- });
437
-
438
- it("increments failure count on failure", () => {
439
- const result = recordPatternObservation(basePattern, false);
440
- expect(result.pattern.success_count).toBe(5);
441
- expect(result.pattern.failure_count).toBe(3);
442
- });
443
-
444
- it("adds bead to example_beads when provided", () => {
445
- const result = recordPatternObservation(basePattern, true, "bd-789");
446
- expect(result.pattern.example_beads).toContain("bd-789");
447
- });
448
-
449
- it("does not modify example_beads when beadId not provided", () => {
450
- const result = recordPatternObservation(basePattern, true);
451
- expect(result.pattern.example_beads).toEqual([]);
452
- });
453
-
454
- it("limits example_beads to MAX_EXAMPLE_BEADS (10)", () => {
455
- const pattern = {
456
- ...basePattern,
457
- example_beads: Array(10)
458
- .fill(0)
459
- .map((_, i) => `bd-${i}`),
460
- };
461
- const result = recordPatternObservation(pattern, true, "bd-new");
462
- expect(result.pattern.example_beads.length).toBe(10);
463
- expect(result.pattern.example_beads).toContain("bd-new");
464
- expect(result.pattern.example_beads).not.toContain("bd-0"); // Oldest removed
465
- });
466
-
467
- it("keeps newest beads when trimming example_beads", () => {
468
- const pattern = {
469
- ...basePattern,
470
- example_beads: [
471
- "bd-1",
472
- "bd-2",
473
- "bd-3",
474
- "bd-4",
475
- "bd-5",
476
- "bd-6",
477
- "bd-7",
478
- "bd-8",
479
- "bd-9",
480
- "bd-10",
481
- ],
482
- };
483
- const result = recordPatternObservation(pattern, true, "bd-new");
484
- expect(result.pattern.example_beads[0]).toBe("bd-2"); // First one removed
485
- expect(result.pattern.example_beads[9]).toBe("bd-new"); // New one added
486
- });
487
-
488
- it("updates updated_at timestamp", () => {
489
- const before = new Date();
490
- const result = recordPatternObservation(basePattern, true);
491
- const after = new Date();
492
- const updatedAt = new Date(result.pattern.updated_at);
493
- expect(updatedAt.getTime()).toBeGreaterThanOrEqual(before.getTime());
494
- expect(updatedAt.getTime()).toBeLessThanOrEqual(after.getTime());
495
- });
496
-
497
- it("does not invert when below threshold", () => {
498
- const result = recordPatternObservation(basePattern, false); // 5 success, 3 failure = 37.5%
499
- expect(result.inversion).toBeUndefined();
500
- });
501
-
502
- it("inverts when crossing threshold", () => {
503
- const pattern = {
504
- ...basePattern,
505
- success_count: 2,
506
- failure_count: 4, // Currently 66% failure
507
- };
508
- const result = recordPatternObservation(pattern, false); // Now 2/7 = 71% failure
509
- expect(result.inversion).toBeDefined();
510
- if (result.inversion) {
511
- expect(result.inversion.inverted.kind).toBe("anti_pattern");
512
- }
513
- });
514
-
515
- it("includes failure statistics in inversion reason", () => {
516
- const pattern = {
517
- ...basePattern,
518
- success_count: 3,
519
- failure_count: 6, // 66% failure
520
- };
521
- const result = recordPatternObservation(pattern, false); // 70% failure
522
- expect(result.inversion).toBeDefined();
523
- if (result.inversion) {
524
- expect(result.inversion.reason).toContain("7/10");
525
- expect(result.inversion.reason).toContain("70%");
526
- }
527
- });
528
-
529
- it("does not invert already-inverted anti-patterns", () => {
530
- const antiPattern: DecompositionPattern = {
531
- ...basePattern,
532
- kind: "anti_pattern",
533
- is_negative: true,
534
- success_count: 0,
535
- failure_count: 10,
536
- };
537
- const result = recordPatternObservation(antiPattern, false);
538
- expect(result.inversion).toBeUndefined();
539
- });
540
-
541
- it("respects custom config for inversion", () => {
542
- const pattern = {
543
- ...basePattern,
544
- success_count: 2,
545
- failure_count: 3, // 60% failure
546
- };
547
- const config: AntiPatternConfig = {
548
- ...DEFAULT_ANTI_PATTERN_CONFIG,
549
- failureRatioThreshold: 0.7, // Need 70%
550
- };
551
- const result = recordPatternObservation(pattern, false, undefined, config);
552
- expect(result.inversion).toBeUndefined(); // 66% not enough
553
- });
554
-
555
- it("preserves original pattern fields", () => {
556
- const result = recordPatternObservation(basePattern, true);
557
- expect(result.pattern.id).toBe(basePattern.id);
558
- expect(result.pattern.content).toBe(basePattern.content);
559
- expect(result.pattern.kind).toBe(basePattern.kind);
560
- expect(result.pattern.tags).toEqual(basePattern.tags);
561
- });
562
- });
563
-
564
- // ============================================================================
565
- // extractPatternsFromDescription Tests
566
- // ============================================================================
567
-
568
- describe("extractPatternsFromDescription", () => {
569
- it("detects 'split by file type' pattern", () => {
570
- const patterns = extractPatternsFromDescription("Split by file type");
571
- expect(patterns).toContain("Split by file type");
572
- });
573
-
574
- it("detects 'splitting by file type' variant", () => {
575
- const patterns = extractPatternsFromDescription("Splitting by file type");
576
- expect(patterns).toContain("Split by file type");
577
- });
578
-
579
- it("detects 'split by component' pattern", () => {
580
- const patterns = extractPatternsFromDescription("Split by component");
581
- expect(patterns).toContain("Split by component");
582
- });
583
-
584
- it("detects 'split by layer' pattern", () => {
585
- const patterns = extractPatternsFromDescription("Split by layer");
586
- expect(patterns).toContain("Split by layer (UI/logic/data)");
587
- });
588
-
589
- it("detects 'split by feature' pattern", () => {
590
- const patterns = extractPatternsFromDescription("Split by feature");
591
- expect(patterns).toContain("Split by feature");
592
- });
593
-
594
- it("detects 'one file per task' pattern", () => {
595
- const patterns = extractPatternsFromDescription("One file per task");
596
- expect(patterns).toContain("One file per subtask");
597
- });
598
-
599
- it("detects 'shared types first' pattern", () => {
600
- const patterns = extractPatternsFromDescription("shared types first");
601
- expect(patterns).toContain("Handle shared types first");
602
- });
603
-
604
- it("detects 'API routes separate' pattern", () => {
605
- const patterns = extractPatternsFromDescription("API routes separate");
606
- expect(patterns).toContain("Separate API routes");
607
- });
608
-
609
- it("detects 'tests with code' pattern", () => {
610
- const patterns = extractPatternsFromDescription("tests with code");
611
- expect(patterns).toContain("Tests alongside implementation");
612
- });
613
-
614
- it("detects 'tests in separate subtask' pattern", () => {
615
- const patterns = extractPatternsFromDescription(
616
- "tests in separate subtask",
617
- );
618
- expect(patterns).toContain("Tests in separate subtask");
619
- });
620
-
621
- it("detects 'parallelize all' pattern", () => {
622
- const patterns = extractPatternsFromDescription("Parallelize everything");
623
- expect(patterns).toContain("Maximize parallelization");
624
- });
625
-
626
- it("detects 'sequential order' pattern", () => {
627
- const patterns = extractPatternsFromDescription("Sequential execution");
628
- expect(patterns).toContain("Sequential execution order");
629
- });
630
-
631
- it("detects 'dependency chain' pattern", () => {
632
- const patterns = extractPatternsFromDescription("dependency chain");
633
- expect(patterns).toContain("Respect dependency chain");
634
- });
635
-
636
- it("returns empty array for unrecognized descriptions", () => {
637
- const patterns = extractPatternsFromDescription("random gibberish text");
638
- expect(patterns).toEqual([]);
639
- });
640
-
641
- it("detects multiple patterns in one description", () => {
642
- const patterns = extractPatternsFromDescription(
643
- "Split by file type and handle shared types first",
644
- );
645
- expect(patterns).toContain("Split by file type");
646
- expect(patterns).toContain("Handle shared types first");
647
- expect(patterns.length).toBeGreaterThanOrEqual(2);
648
- });
649
-
650
- it("is case-insensitive", () => {
651
- const patterns = extractPatternsFromDescription("SPLIT BY FILE TYPE");
652
- expect(patterns).toContain("Split by file type");
653
- });
654
-
655
- it("handles partial matches in longer sentences", () => {
656
- const patterns = extractPatternsFromDescription(
657
- "We should split by component for this refactor",
658
- );
659
- expect(patterns).toContain("Split by component");
660
- });
661
- });
662
-
663
- // ============================================================================
664
- // createPattern Tests
665
- // ============================================================================
666
-
667
- describe("createPattern", () => {
668
- it("creates pattern with provided content", () => {
669
- const pattern = createPattern("Test pattern");
670
- expect(pattern.content).toBe("Test pattern");
671
- });
672
-
673
- it("creates pattern with kind='pattern'", () => {
674
- const pattern = createPattern("Test");
675
- expect(pattern.kind).toBe("pattern");
676
- });
677
-
678
- it("creates pattern with is_negative=false", () => {
679
- const pattern = createPattern("Test");
680
- expect(pattern.is_negative).toBe(false);
681
- });
682
-
683
- it("initializes counts to zero", () => {
684
- const pattern = createPattern("Test");
685
- expect(pattern.success_count).toBe(0);
686
- expect(pattern.failure_count).toBe(0);
687
- });
688
-
689
- it("includes provided tags", () => {
690
- const pattern = createPattern("Test", ["tag1", "tag2"]);
691
- expect(pattern.tags).toEqual(["tag1", "tag2"]);
692
- });
693
-
694
- it("defaults to empty tags array", () => {
695
- const pattern = createPattern("Test");
696
- expect(pattern.tags).toEqual([]);
697
- });
698
-
699
- it("generates unique ID", () => {
700
- const p1 = createPattern("Test");
701
- const p2 = createPattern("Test");
702
- expect(p1.id).not.toBe(p2.id);
703
- });
704
-
705
- it("sets created_at timestamp", () => {
706
- const before = new Date();
707
- const pattern = createPattern("Test");
708
- const after = new Date();
709
- const createdAt = new Date(pattern.created_at);
710
- expect(createdAt.getTime()).toBeGreaterThanOrEqual(before.getTime());
711
- expect(createdAt.getTime()).toBeLessThanOrEqual(after.getTime());
712
- });
713
-
714
- it("sets updated_at equal to created_at", () => {
715
- const pattern = createPattern("Test");
716
- expect(pattern.updated_at).toBe(pattern.created_at);
717
- });
718
-
719
- it("initializes example_beads to empty array", () => {
720
- const pattern = createPattern("Test");
721
- expect(pattern.example_beads).toEqual([]);
722
- });
723
- });
724
-
725
- // ============================================================================
726
- // formatAntiPatternsForPrompt Tests
727
- // ============================================================================
728
-
729
- describe("formatAntiPatternsForPrompt", () => {
730
- it("formats anti-patterns with header", () => {
731
- const patterns: DecompositionPattern[] = [
732
- {
733
- id: "anti-1",
734
- content: "AVOID: Split by file type",
735
- kind: "anti_pattern",
736
- is_negative: true,
737
- success_count: 0,
738
- failure_count: 10,
739
- created_at: new Date().toISOString(),
740
- updated_at: new Date().toISOString(),
741
- tags: [],
742
- example_beads: [],
743
- },
744
- ];
745
- const formatted = formatAntiPatternsForPrompt(patterns);
746
- expect(formatted).toContain("## Anti-Patterns to Avoid");
747
- expect(formatted).toContain("AVOID: Split by file type");
748
- });
749
-
750
- it("filters out non-anti-patterns", () => {
751
- const patterns: DecompositionPattern[] = [
752
- {
753
- id: "pattern-1",
754
- content: "Good pattern",
755
- kind: "pattern",
756
- is_negative: false,
757
- success_count: 10,
758
- failure_count: 0,
759
- created_at: new Date().toISOString(),
760
- updated_at: new Date().toISOString(),
761
- tags: [],
762
- example_beads: [],
763
- },
764
- {
765
- id: "anti-1",
766
- content: "AVOID: Bad pattern",
767
- kind: "anti_pattern",
768
- is_negative: true,
769
- success_count: 0,
770
- failure_count: 10,
771
- created_at: new Date().toISOString(),
772
- updated_at: new Date().toISOString(),
773
- tags: [],
774
- example_beads: [],
775
- },
776
- ];
777
- const formatted = formatAntiPatternsForPrompt(patterns);
778
- expect(formatted).toContain("AVOID: Bad pattern");
779
- expect(formatted).not.toContain("Good pattern");
780
- });
781
-
782
- it("returns empty string when no anti-patterns", () => {
783
- const patterns: DecompositionPattern[] = [
784
- {
785
- id: "pattern-1",
786
- content: "Good pattern",
787
- kind: "pattern",
788
- is_negative: false,
789
- success_count: 10,
790
- failure_count: 0,
791
- created_at: new Date().toISOString(),
792
- updated_at: new Date().toISOString(),
793
- tags: [],
794
- example_beads: [],
795
- },
796
- ];
797
- const formatted = formatAntiPatternsForPrompt(patterns);
798
- expect(formatted).toBe("");
799
- });
800
-
801
- it("returns empty string for empty array", () => {
802
- const formatted = formatAntiPatternsForPrompt([]);
803
- expect(formatted).toBe("");
804
- });
805
-
806
- it("formats multiple anti-patterns", () => {
807
- const patterns: DecompositionPattern[] = [
808
- {
809
- id: "anti-1",
810
- content: "AVOID: Pattern 1",
811
- kind: "anti_pattern",
812
- is_negative: true,
813
- success_count: 0,
814
- failure_count: 5,
815
- created_at: new Date().toISOString(),
816
- updated_at: new Date().toISOString(),
817
- tags: [],
818
- example_beads: [],
819
- },
820
- {
821
- id: "anti-2",
822
- content: "AVOID: Pattern 2",
823
- kind: "anti_pattern",
824
- is_negative: true,
825
- success_count: 1,
826
- failure_count: 9,
827
- created_at: new Date().toISOString(),
828
- updated_at: new Date().toISOString(),
829
- tags: [],
830
- example_beads: [],
831
- },
832
- ];
833
- const formatted = formatAntiPatternsForPrompt(patterns);
834
- expect(formatted).toContain("AVOID: Pattern 1");
835
- expect(formatted).toContain("AVOID: Pattern 2");
836
- });
837
- });
838
-
839
- // ============================================================================
840
- // formatSuccessfulPatternsForPrompt Tests
841
- // ============================================================================
842
-
843
- describe("formatSuccessfulPatternsForPrompt", () => {
844
- it("filters patterns below minSuccessRate", () => {
845
- const patterns: DecompositionPattern[] = [
846
- {
847
- id: "pattern-1",
848
- content: "Good pattern",
849
- kind: "pattern",
850
- is_negative: false,
851
- success_count: 8,
852
- failure_count: 2, // 80% success
853
- created_at: new Date().toISOString(),
854
- updated_at: new Date().toISOString(),
855
- tags: [],
856
- example_beads: [],
857
- },
858
- {
859
- id: "pattern-2",
860
- content: "Bad pattern",
861
- kind: "pattern",
862
- is_negative: false,
863
- success_count: 5,
864
- failure_count: 5, // 50% success
865
- created_at: new Date().toISOString(),
866
- updated_at: new Date().toISOString(),
867
- tags: [],
868
- example_beads: [],
869
- },
870
- ];
871
- const formatted = formatSuccessfulPatternsForPrompt(patterns, 0.7);
872
- expect(formatted).toContain("Good pattern");
873
- expect(formatted).not.toContain("Bad pattern");
874
- });
875
-
876
- it("includes success rate percentage in output", () => {
877
- const patterns: DecompositionPattern[] = [
878
- {
879
- id: "pattern-1",
880
- content: "Test pattern",
881
- kind: "pattern",
882
- is_negative: false,
883
- success_count: 8,
884
- failure_count: 2, // 80%
885
- created_at: new Date().toISOString(),
886
- updated_at: new Date().toISOString(),
887
- tags: [],
888
- example_beads: [],
889
- },
890
- ];
891
- const formatted = formatSuccessfulPatternsForPrompt(patterns);
892
- expect(formatted).toContain("80% success rate");
893
- });
894
-
895
- it("filters out anti-patterns", () => {
896
- const patterns: DecompositionPattern[] = [
897
- {
898
- id: "anti-1",
899
- content: "AVOID: Bad",
900
- kind: "anti_pattern",
901
- is_negative: true,
902
- success_count: 10,
903
- failure_count: 0,
904
- created_at: new Date().toISOString(),
905
- updated_at: new Date().toISOString(),
906
- tags: [],
907
- example_beads: [],
908
- },
909
- ];
910
- const formatted = formatSuccessfulPatternsForPrompt(patterns);
911
- expect(formatted).toBe("");
912
- });
913
-
914
- it("filters out patterns with < 2 total observations", () => {
915
- const patterns: DecompositionPattern[] = [
916
- {
917
- id: "pattern-1",
918
- content: "Not enough data",
919
- kind: "pattern",
920
- is_negative: false,
921
- success_count: 1,
922
- failure_count: 0, // Only 1 observation
923
- created_at: new Date().toISOString(),
924
- updated_at: new Date().toISOString(),
925
- tags: [],
926
- example_beads: [],
927
- },
928
- ];
929
- const formatted = formatSuccessfulPatternsForPrompt(patterns, 0.7);
930
- expect(formatted).toBe("");
931
- });
932
-
933
- it("returns empty string when no qualifying patterns", () => {
934
- const patterns: DecompositionPattern[] = [
935
- {
936
- id: "pattern-1",
937
- content: "Low success",
938
- kind: "pattern",
939
- is_negative: false,
940
- success_count: 1,
941
- failure_count: 9, // 10% success
942
- created_at: new Date().toISOString(),
943
- updated_at: new Date().toISOString(),
944
- tags: [],
945
- example_beads: [],
946
- },
947
- ];
948
- const formatted = formatSuccessfulPatternsForPrompt(patterns, 0.7);
949
- expect(formatted).toBe("");
950
- });
951
-
952
- it("returns empty string for empty array", () => {
953
- const formatted = formatSuccessfulPatternsForPrompt([]);
954
- expect(formatted).toBe("");
955
- });
956
-
957
- it("uses default minSuccessRate of 0.7", () => {
958
- const patterns: DecompositionPattern[] = [
959
- {
960
- id: "pattern-1",
961
- content: "69% success",
962
- kind: "pattern",
963
- is_negative: false,
964
- success_count: 69,
965
- failure_count: 31, // Just below 70%
966
- created_at: new Date().toISOString(),
967
- updated_at: new Date().toISOString(),
968
- tags: [],
969
- example_beads: [],
970
- },
971
- ];
972
- const formatted = formatSuccessfulPatternsForPrompt(patterns);
973
- expect(formatted).toBe(""); // Should be filtered out
974
- });
975
-
976
- it("respects custom minSuccessRate", () => {
977
- const patterns: DecompositionPattern[] = [
978
- {
979
- id: "pattern-1",
980
- content: "60% success",
981
- kind: "pattern",
982
- is_negative: false,
983
- success_count: 6,
984
- failure_count: 4,
985
- created_at: new Date().toISOString(),
986
- updated_at: new Date().toISOString(),
987
- tags: [],
988
- example_beads: [],
989
- },
990
- ];
991
- const formatted = formatSuccessfulPatternsForPrompt(patterns, 0.5);
992
- expect(formatted).toContain("60% success");
993
- });
994
-
995
- it("formats multiple successful patterns", () => {
996
- const patterns: DecompositionPattern[] = [
997
- {
998
- id: "pattern-1",
999
- content: "Pattern A",
1000
- kind: "pattern",
1001
- is_negative: false,
1002
- success_count: 8,
1003
- failure_count: 2,
1004
- created_at: new Date().toISOString(),
1005
- updated_at: new Date().toISOString(),
1006
- tags: [],
1007
- example_beads: [],
1008
- },
1009
- {
1010
- id: "pattern-2",
1011
- content: "Pattern B",
1012
- kind: "pattern",
1013
- is_negative: false,
1014
- success_count: 7,
1015
- failure_count: 3,
1016
- created_at: new Date().toISOString(),
1017
- updated_at: new Date().toISOString(),
1018
- tags: [],
1019
- example_beads: [],
1020
- },
1021
- ];
1022
- const formatted = formatSuccessfulPatternsForPrompt(patterns);
1023
- expect(formatted).toContain("Pattern A");
1024
- expect(formatted).toContain("Pattern B");
1025
- });
1026
-
1027
- it("includes header when patterns exist", () => {
1028
- const patterns: DecompositionPattern[] = [
1029
- {
1030
- id: "pattern-1",
1031
- content: "Test",
1032
- kind: "pattern",
1033
- is_negative: false,
1034
- success_count: 8,
1035
- failure_count: 2,
1036
- created_at: new Date().toISOString(),
1037
- updated_at: new Date().toISOString(),
1038
- tags: [],
1039
- example_beads: [],
1040
- },
1041
- ];
1042
- const formatted = formatSuccessfulPatternsForPrompt(patterns);
1043
- expect(formatted).toContain("## Successful Patterns");
1044
- });
1045
- });
1046
-
1047
- // ============================================================================
1048
- // InMemoryPatternStorage Tests
1049
- // ============================================================================
1050
-
1051
- describe("InMemoryPatternStorage", () => {
1052
- it("stores and retrieves a pattern", async () => {
1053
- const storage = new InMemoryPatternStorage();
1054
- const pattern = createPattern("Test pattern");
1055
- await storage.store(pattern);
1056
- const retrieved = await storage.get(pattern.id);
1057
- expect(retrieved).toEqual(pattern);
1058
- });
1059
-
1060
- it("returns null for non-existent pattern", async () => {
1061
- const storage = new InMemoryPatternStorage();
1062
- const retrieved = await storage.get("non-existent");
1063
- expect(retrieved).toBeNull();
1064
- });
1065
-
1066
- it("updates existing pattern on store", async () => {
1067
- const storage = new InMemoryPatternStorage();
1068
- const pattern = createPattern("Original");
1069
- await storage.store(pattern);
1070
-
1071
- const updated = { ...pattern, content: "Updated" };
1072
- await storage.store(updated);
1073
-
1074
- const retrieved = await storage.get(pattern.id);
1075
- expect(retrieved?.content).toBe("Updated");
1076
- });
1077
-
1078
- it("getAll returns all stored patterns", async () => {
1079
- const storage = new InMemoryPatternStorage();
1080
- const p1 = createPattern("Pattern 1");
1081
- const p2 = createPattern("Pattern 2");
1082
- await storage.store(p1);
1083
- await storage.store(p2);
1084
-
1085
- const all = await storage.getAll();
1086
- expect(all).toHaveLength(2);
1087
- expect(all).toContainEqual(p1);
1088
- expect(all).toContainEqual(p2);
1089
- });
1090
-
1091
- it("getAll returns empty array when no patterns", async () => {
1092
- const storage = new InMemoryPatternStorage();
1093
- const all = await storage.getAll();
1094
- expect(all).toEqual([]);
1095
- });
1096
-
1097
- it("getAntiPatterns filters by kind", async () => {
1098
- const storage = new InMemoryPatternStorage();
1099
- const pattern = createPattern("Good pattern");
1100
- const antiPattern: DecompositionPattern = {
1101
- ...createPattern("Bad pattern"),
1102
- kind: "anti_pattern",
1103
- is_negative: true,
1104
- };
1105
- await storage.store(pattern);
1106
- await storage.store(antiPattern);
1107
-
1108
- const antiPatterns = await storage.getAntiPatterns();
1109
- expect(antiPatterns).toHaveLength(1);
1110
- expect(antiPatterns[0].kind).toBe("anti_pattern");
1111
- });
1112
-
1113
- it("getByTag filters by tag", async () => {
1114
- const storage = new InMemoryPatternStorage();
1115
- const p1 = createPattern("Pattern 1", ["tag1", "tag2"]);
1116
- const p2 = createPattern("Pattern 2", ["tag2", "tag3"]);
1117
- const p3 = createPattern("Pattern 3", ["tag3"]);
1118
- await storage.store(p1);
1119
- await storage.store(p2);
1120
- await storage.store(p3);
1121
-
1122
- const tagged = await storage.getByTag("tag2");
1123
- expect(tagged).toHaveLength(2);
1124
- expect(tagged.map((p) => p.id)).toContain(p1.id);
1125
- expect(tagged.map((p) => p.id)).toContain(p2.id);
1126
- });
1127
-
1128
- it("getByTag returns empty for non-existent tag", async () => {
1129
- const storage = new InMemoryPatternStorage();
1130
- const pattern = createPattern("Test", ["tag1"]);
1131
- await storage.store(pattern);
1132
-
1133
- const tagged = await storage.getByTag("tag2");
1134
- expect(tagged).toEqual([]);
1135
- });
1136
-
1137
- it("findByContent finds patterns by substring", async () => {
1138
- const storage = new InMemoryPatternStorage();
1139
- const p1 = createPattern("Split by file type");
1140
- const p2 = createPattern("Split by component");
1141
- const p3 = createPattern("Maximize parallelization");
1142
- await storage.store(p1);
1143
- await storage.store(p2);
1144
- await storage.store(p3);
1145
-
1146
- const found = await storage.findByContent("split");
1147
- expect(found).toHaveLength(2);
1148
- });
1149
-
1150
- it("findByContent is case-insensitive", async () => {
1151
- const storage = new InMemoryPatternStorage();
1152
- const pattern = createPattern("Split by file type");
1153
- await storage.store(pattern);
1154
-
1155
- const found = await storage.findByContent("SPLIT");
1156
- expect(found).toHaveLength(1);
1157
- });
1158
-
1159
- it("findByContent returns empty for no matches", async () => {
1160
- const storage = new InMemoryPatternStorage();
1161
- const pattern = createPattern("Test pattern");
1162
- await storage.store(pattern);
1163
-
1164
- const found = await storage.findByContent("nonexistent");
1165
- expect(found).toEqual([]);
1166
- });
1167
- });