opencode-swarm-plugin 0.44.0 → 0.44.2

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 (215) hide show
  1. package/bin/swarm.serve.test.ts +6 -4
  2. package/bin/swarm.ts +18 -12
  3. package/dist/compaction-prompt-scoring.js +139 -0
  4. package/dist/eval-capture.js +12811 -0
  5. package/dist/hive.d.ts.map +1 -1
  6. package/dist/hive.js +14834 -0
  7. package/dist/index.d.ts +18 -0
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +7743 -62593
  10. package/dist/plugin.js +24052 -78907
  11. package/dist/swarm-orchestrate.d.ts.map +1 -1
  12. package/dist/swarm-prompts.d.ts.map +1 -1
  13. package/dist/swarm-prompts.js +39407 -0
  14. package/dist/swarm-review.d.ts.map +1 -1
  15. package/dist/swarm-validation.d.ts +127 -0
  16. package/dist/swarm-validation.d.ts.map +1 -0
  17. package/dist/validators/index.d.ts +7 -0
  18. package/dist/validators/index.d.ts.map +1 -0
  19. package/dist/validators/schema-validator.d.ts +58 -0
  20. package/dist/validators/schema-validator.d.ts.map +1 -0
  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 -2286
  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/ADR-010-cass-inhousing.md +0 -1215
  43. package/docs/planning/ROADMAP.md +0 -368
  44. package/docs/semantic-memory-cli-syntax.md +0 -123
  45. package/docs/swarm-mail-architecture.md +0 -1147
  46. package/docs/testing/context-recovery-test.md +0 -470
  47. package/evals/ARCHITECTURE.md +0 -1189
  48. package/evals/README.md +0 -768
  49. package/evals/compaction-prompt.eval.ts +0 -149
  50. package/evals/compaction-resumption.eval.ts +0 -289
  51. package/evals/coordinator-behavior.eval.ts +0 -307
  52. package/evals/coordinator-session.eval.ts +0 -154
  53. package/evals/evalite.config.ts.bak +0 -15
  54. package/evals/example.eval.ts +0 -31
  55. package/evals/fixtures/cass-baseline.ts +0 -217
  56. package/evals/fixtures/compaction-cases.ts +0 -350
  57. package/evals/fixtures/compaction-prompt-cases.ts +0 -311
  58. package/evals/fixtures/coordinator-sessions.ts +0 -328
  59. package/evals/fixtures/decomposition-cases.ts +0 -105
  60. package/evals/lib/compaction-loader.test.ts +0 -248
  61. package/evals/lib/compaction-loader.ts +0 -320
  62. package/evals/lib/data-loader.evalite-test.ts +0 -289
  63. package/evals/lib/data-loader.test.ts +0 -345
  64. package/evals/lib/data-loader.ts +0 -281
  65. package/evals/lib/llm.ts +0 -115
  66. package/evals/scorers/compaction-prompt-scorers.ts +0 -145
  67. package/evals/scorers/compaction-scorers.ts +0 -305
  68. package/evals/scorers/coordinator-discipline.evalite-test.ts +0 -539
  69. package/evals/scorers/coordinator-discipline.ts +0 -325
  70. package/evals/scorers/index.test.ts +0 -146
  71. package/evals/scorers/index.ts +0 -328
  72. package/evals/scorers/outcome-scorers.evalite-test.ts +0 -27
  73. package/evals/scorers/outcome-scorers.ts +0 -349
  74. package/evals/swarm-decomposition.eval.ts +0 -121
  75. package/examples/commands/swarm.md +0 -745
  76. package/examples/plugin-wrapper-template.ts +0 -2515
  77. package/examples/skills/hive-workflow/SKILL.md +0 -212
  78. package/examples/skills/skill-creator/SKILL.md +0 -223
  79. package/examples/skills/swarm-coordination/SKILL.md +0 -292
  80. package/global-skills/cli-builder/SKILL.md +0 -344
  81. package/global-skills/cli-builder/references/advanced-patterns.md +0 -244
  82. package/global-skills/learning-systems/SKILL.md +0 -644
  83. package/global-skills/skill-creator/LICENSE.txt +0 -202
  84. package/global-skills/skill-creator/SKILL.md +0 -352
  85. package/global-skills/skill-creator/references/output-patterns.md +0 -82
  86. package/global-skills/skill-creator/references/workflows.md +0 -28
  87. package/global-skills/swarm-coordination/SKILL.md +0 -995
  88. package/global-skills/swarm-coordination/references/coordinator-patterns.md +0 -235
  89. package/global-skills/swarm-coordination/references/strategies.md +0 -138
  90. package/global-skills/system-design/SKILL.md +0 -213
  91. package/global-skills/testing-patterns/SKILL.md +0 -430
  92. package/global-skills/testing-patterns/references/dependency-breaking-catalog.md +0 -586
  93. package/opencode-swarm-plugin-0.30.7.tgz +0 -0
  94. package/opencode-swarm-plugin-0.31.0.tgz +0 -0
  95. package/scripts/cleanup-test-memories.ts +0 -346
  96. package/scripts/init-skill.ts +0 -222
  97. package/scripts/migrate-unknown-sessions.ts +0 -349
  98. package/scripts/validate-skill.ts +0 -204
  99. package/src/agent-mail.ts +0 -1724
  100. package/src/anti-patterns.test.ts +0 -1167
  101. package/src/anti-patterns.ts +0 -448
  102. package/src/compaction-capture.integration.test.ts +0 -257
  103. package/src/compaction-hook.test.ts +0 -838
  104. package/src/compaction-hook.ts +0 -1204
  105. package/src/compaction-observability.integration.test.ts +0 -139
  106. package/src/compaction-observability.test.ts +0 -187
  107. package/src/compaction-observability.ts +0 -324
  108. package/src/compaction-prompt-scorers.test.ts +0 -475
  109. package/src/compaction-prompt-scoring.ts +0 -300
  110. package/src/contributor-tools.test.ts +0 -133
  111. package/src/contributor-tools.ts +0 -201
  112. package/src/dashboard.test.ts +0 -611
  113. package/src/dashboard.ts +0 -462
  114. package/src/error-enrichment.test.ts +0 -403
  115. package/src/error-enrichment.ts +0 -219
  116. package/src/eval-capture.test.ts +0 -1015
  117. package/src/eval-capture.ts +0 -929
  118. package/src/eval-gates.test.ts +0 -306
  119. package/src/eval-gates.ts +0 -218
  120. package/src/eval-history.test.ts +0 -508
  121. package/src/eval-history.ts +0 -214
  122. package/src/eval-learning.test.ts +0 -378
  123. package/src/eval-learning.ts +0 -360
  124. package/src/eval-runner.test.ts +0 -223
  125. package/src/eval-runner.ts +0 -402
  126. package/src/export-tools.test.ts +0 -476
  127. package/src/export-tools.ts +0 -257
  128. package/src/hive.integration.test.ts +0 -2241
  129. package/src/hive.ts +0 -1628
  130. package/src/index.ts +0 -940
  131. package/src/learning.integration.test.ts +0 -1815
  132. package/src/learning.ts +0 -1079
  133. package/src/logger.test.ts +0 -189
  134. package/src/logger.ts +0 -135
  135. package/src/mandate-promotion.test.ts +0 -473
  136. package/src/mandate-promotion.ts +0 -239
  137. package/src/mandate-storage.integration.test.ts +0 -601
  138. package/src/mandate-storage.test.ts +0 -578
  139. package/src/mandate-storage.ts +0 -794
  140. package/src/mandates.ts +0 -540
  141. package/src/memory-tools.test.ts +0 -195
  142. package/src/memory-tools.ts +0 -344
  143. package/src/memory.integration.test.ts +0 -334
  144. package/src/memory.test.ts +0 -158
  145. package/src/memory.ts +0 -527
  146. package/src/model-selection.test.ts +0 -188
  147. package/src/model-selection.ts +0 -68
  148. package/src/observability-tools.test.ts +0 -359
  149. package/src/observability-tools.ts +0 -871
  150. package/src/output-guardrails.test.ts +0 -438
  151. package/src/output-guardrails.ts +0 -381
  152. package/src/pattern-maturity.test.ts +0 -1160
  153. package/src/pattern-maturity.ts +0 -525
  154. package/src/planning-guardrails.test.ts +0 -491
  155. package/src/planning-guardrails.ts +0 -438
  156. package/src/plugin.ts +0 -23
  157. package/src/post-compaction-tracker.test.ts +0 -251
  158. package/src/post-compaction-tracker.ts +0 -237
  159. package/src/query-tools.test.ts +0 -636
  160. package/src/query-tools.ts +0 -324
  161. package/src/rate-limiter.integration.test.ts +0 -466
  162. package/src/rate-limiter.ts +0 -774
  163. package/src/replay-tools.test.ts +0 -496
  164. package/src/replay-tools.ts +0 -240
  165. package/src/repo-crawl.integration.test.ts +0 -441
  166. package/src/repo-crawl.ts +0 -610
  167. package/src/schemas/cell-events.test.ts +0 -347
  168. package/src/schemas/cell-events.ts +0 -807
  169. package/src/schemas/cell.ts +0 -257
  170. package/src/schemas/evaluation.ts +0 -166
  171. package/src/schemas/index.test.ts +0 -199
  172. package/src/schemas/index.ts +0 -286
  173. package/src/schemas/mandate.ts +0 -232
  174. package/src/schemas/swarm-context.ts +0 -115
  175. package/src/schemas/task.ts +0 -161
  176. package/src/schemas/worker-handoff.test.ts +0 -302
  177. package/src/schemas/worker-handoff.ts +0 -131
  178. package/src/sessions/agent-discovery.test.ts +0 -137
  179. package/src/sessions/agent-discovery.ts +0 -112
  180. package/src/sessions/index.ts +0 -15
  181. package/src/skills.integration.test.ts +0 -1192
  182. package/src/skills.test.ts +0 -643
  183. package/src/skills.ts +0 -1549
  184. package/src/storage.integration.test.ts +0 -341
  185. package/src/storage.ts +0 -884
  186. package/src/structured.integration.test.ts +0 -817
  187. package/src/structured.test.ts +0 -1046
  188. package/src/structured.ts +0 -762
  189. package/src/swarm-decompose.test.ts +0 -188
  190. package/src/swarm-decompose.ts +0 -1302
  191. package/src/swarm-deferred.integration.test.ts +0 -157
  192. package/src/swarm-deferred.test.ts +0 -38
  193. package/src/swarm-insights.test.ts +0 -214
  194. package/src/swarm-insights.ts +0 -459
  195. package/src/swarm-mail.integration.test.ts +0 -970
  196. package/src/swarm-mail.ts +0 -739
  197. package/src/swarm-orchestrate.integration.test.ts +0 -282
  198. package/src/swarm-orchestrate.test.ts +0 -548
  199. package/src/swarm-orchestrate.ts +0 -3084
  200. package/src/swarm-prompts.test.ts +0 -1270
  201. package/src/swarm-prompts.ts +0 -2077
  202. package/src/swarm-research.integration.test.ts +0 -701
  203. package/src/swarm-research.test.ts +0 -698
  204. package/src/swarm-research.ts +0 -472
  205. package/src/swarm-review.integration.test.ts +0 -285
  206. package/src/swarm-review.test.ts +0 -879
  207. package/src/swarm-review.ts +0 -709
  208. package/src/swarm-strategies.ts +0 -407
  209. package/src/swarm-worktree.test.ts +0 -501
  210. package/src/swarm-worktree.ts +0 -575
  211. package/src/swarm.integration.test.ts +0 -2377
  212. package/src/swarm.ts +0 -38
  213. package/src/tool-adapter.integration.test.ts +0 -1221
  214. package/src/tool-availability.ts +0 -461
  215. package/tsconfig.json +0 -28
@@ -1,1160 +0,0 @@
1
- /**
2
- * Comprehensive tests for pattern-maturity.ts
3
- *
4
- * Tests behavior of maturity state transitions, decay calculations,
5
- * and storage operations. Focuses on observable behavior over internal state.
6
- */
7
- import { describe, test, expect, beforeEach } from "vitest";
8
- import {
9
- calculateDecayedCounts,
10
- calculateMaturityState,
11
- createPatternMaturity,
12
- updatePatternMaturity,
13
- promotePattern,
14
- deprecatePattern,
15
- getMaturityMultiplier,
16
- formatMaturityForPrompt,
17
- formatPatternsWithMaturityForPrompt,
18
- InMemoryMaturityStorage,
19
- type MaturityFeedback,
20
- type PatternMaturity,
21
- DEFAULT_MATURITY_CONFIG,
22
- } from "./pattern-maturity";
23
-
24
- // ============================================================================
25
- // Test Fixtures
26
- // ============================================================================
27
-
28
- /**
29
- * Create a feedback event with defaults
30
- */
31
- function createFeedback(
32
- overrides: Partial<MaturityFeedback> = {},
33
- ): MaturityFeedback {
34
- return {
35
- pattern_id: "test-pattern",
36
- type: "helpful",
37
- timestamp: new Date().toISOString(),
38
- weight: 1,
39
- ...overrides,
40
- };
41
- }
42
-
43
- /**
44
- * Create feedback events at specific days ago
45
- */
46
- function createFeedbackAt(
47
- daysAgo: number,
48
- type: "helpful" | "harmful",
49
- weight = 1,
50
- ): MaturityFeedback {
51
- const timestamp = new Date();
52
- timestamp.setDate(timestamp.getDate() - daysAgo);
53
- return createFeedback({ type, timestamp: timestamp.toISOString(), weight });
54
- }
55
-
56
- // ============================================================================
57
- // calculateDecayedCounts Tests
58
- // ============================================================================
59
-
60
- describe("calculateDecayedCounts", () => {
61
- test("returns zero counts for empty feedback", () => {
62
- const result = calculateDecayedCounts([]);
63
- expect(result).toEqual({ decayedHelpful: 0, decayedHarmful: 0 });
64
- });
65
-
66
- test("counts recent helpful feedback at full weight", () => {
67
- const events = [createFeedback({ type: "helpful" })];
68
- const result = calculateDecayedCounts(events);
69
- // Recent feedback should be ~1 (minor decay allowed)
70
- expect(result.decayedHelpful).toBeGreaterThan(0.99);
71
- expect(result.decayedHarmful).toBe(0);
72
- });
73
-
74
- test("counts recent harmful feedback at full weight", () => {
75
- const events = [createFeedback({ type: "harmful" })];
76
- const result = calculateDecayedCounts(events);
77
- expect(result.decayedHelpful).toBe(0);
78
- expect(result.decayedHarmful).toBeGreaterThan(0.99);
79
- });
80
-
81
- test("applies decay to old feedback", () => {
82
- const oldEvent = createFeedbackAt(90, "helpful"); // one half-life
83
- const recentEvent = createFeedbackAt(0, "helpful");
84
-
85
- const result = calculateDecayedCounts([oldEvent, recentEvent]);
86
-
87
- // 90-day old feedback should be ~0.5x (one half-life)
88
- // Recent feedback should be ~1.0x
89
- // Total should be ~1.5
90
- expect(result.decayedHelpful).toBeGreaterThan(1.4);
91
- expect(result.decayedHelpful).toBeLessThan(1.6);
92
- });
93
-
94
- test("handles mixed feedback types", () => {
95
- const events = [
96
- createFeedbackAt(0, "helpful"),
97
- createFeedbackAt(0, "helpful"),
98
- createFeedbackAt(0, "harmful"),
99
- ];
100
-
101
- const result = calculateDecayedCounts(events);
102
- expect(result.decayedHelpful).toBeGreaterThan(1.9);
103
- expect(result.decayedHarmful).toBeGreaterThan(0.99);
104
- });
105
-
106
- test("respects weight parameter", () => {
107
- const fullWeight = createFeedback({ type: "helpful", weight: 1 });
108
- const halfWeight = createFeedback({ type: "helpful", weight: 0.5 });
109
-
110
- const result = calculateDecayedCounts([fullWeight, halfWeight]);
111
-
112
- // ~1.0 + ~0.5 = ~1.5
113
- expect(result.decayedHelpful).toBeGreaterThan(1.4);
114
- expect(result.decayedHelpful).toBeLessThan(1.6);
115
- });
116
-
117
- test("uses custom config half-life", () => {
118
- const event = createFeedbackAt(45, "helpful"); // half of 90-day half-life
119
- const config = { ...DEFAULT_MATURITY_CONFIG, halfLifeDays: 45 };
120
-
121
- const result = calculateDecayedCounts([event], config);
122
-
123
- // At custom half-life, should be ~0.5
124
- expect(result.decayedHelpful).toBeGreaterThan(0.4);
125
- expect(result.decayedHelpful).toBeLessThan(0.6);
126
- });
127
-
128
- test("uses custom now parameter for decay calculation", () => {
129
- const event = createFeedback({
130
- type: "helpful",
131
- timestamp: "2024-01-01T00:00:00Z",
132
- });
133
- const now = new Date("2024-04-01T00:00:00Z"); // 90 days later
134
-
135
- const result = calculateDecayedCounts(
136
- [event],
137
- DEFAULT_MATURITY_CONFIG,
138
- now,
139
- );
140
-
141
- // Should be decayed by one half-life
142
- expect(result.decayedHelpful).toBeGreaterThan(0.4);
143
- expect(result.decayedHelpful).toBeLessThan(0.6);
144
- });
145
- });
146
-
147
- // ============================================================================
148
- // calculateMaturityState Tests
149
- // ============================================================================
150
-
151
- describe("calculateMaturityState", () => {
152
- test("returns candidate with no feedback", () => {
153
- const state = calculateMaturityState([]);
154
- expect(state).toBe("candidate");
155
- });
156
-
157
- test("returns candidate with insufficient feedback", () => {
158
- // minFeedback = 3, so 2 events should be candidate
159
- const events = [
160
- createFeedbackAt(0, "helpful"),
161
- createFeedbackAt(0, "helpful"),
162
- ];
163
- const state = calculateMaturityState(events);
164
- expect(state).toBe("candidate");
165
- });
166
-
167
- test("returns established with enough neutral feedback", () => {
168
- // 3 helpful, 0 harmful = established (not enough for proven)
169
- const events = [
170
- createFeedbackAt(0, "helpful"),
171
- createFeedbackAt(0, "helpful"),
172
- createFeedbackAt(0, "helpful"),
173
- ];
174
- const state = calculateMaturityState(events);
175
- expect(state).toBe("established");
176
- });
177
-
178
- test("returns proven with strong positive feedback", () => {
179
- // minHelpful = 5, maxHarmful = 15%
180
- // 6 helpful, 1 harmful = 14% harmful, should be proven
181
- const events = [
182
- ...Array(6)
183
- .fill(null)
184
- .map(() => createFeedbackAt(0, "helpful")),
185
- createFeedbackAt(0, "harmful"),
186
- ];
187
- const state = calculateMaturityState(events);
188
- expect(state).toBe("proven");
189
- });
190
-
191
- test("returns deprecated with high harmful ratio", () => {
192
- // deprecationThreshold = 30%
193
- // 2 helpful, 3 harmful = 60% harmful
194
- const events = [
195
- createFeedbackAt(0, "helpful"),
196
- createFeedbackAt(0, "helpful"),
197
- createFeedbackAt(0, "harmful"),
198
- createFeedbackAt(0, "harmful"),
199
- createFeedbackAt(0, "harmful"),
200
- ];
201
- const state = calculateMaturityState(events);
202
- expect(state).toBe("deprecated");
203
- });
204
-
205
- test("proven requires minimum helpful count", () => {
206
- // 4 helpful is below minHelpful (5), even with low harmful ratio
207
- const events = [
208
- ...Array(4)
209
- .fill(null)
210
- .map(() => createFeedbackAt(0, "helpful")),
211
- createFeedbackAt(0, "harmful"),
212
- ];
213
- const state = calculateMaturityState(events);
214
- expect(state).toBe("established"); // not proven
215
- });
216
-
217
- test("proven requires low harmful ratio", () => {
218
- // 5 helpful, 2 harmful = 28% harmful (above maxHarmful of 15%)
219
- const events = [
220
- ...Array(5)
221
- .fill(null)
222
- .map(() => createFeedbackAt(0, "helpful")),
223
- createFeedbackAt(0, "harmful"),
224
- createFeedbackAt(0, "harmful"),
225
- ];
226
- const state = calculateMaturityState(events);
227
- expect(state).toBe("established"); // not proven due to harmful ratio
228
- });
229
-
230
- test("deprecation takes priority over proven", () => {
231
- // Even with high helpful count, high harmful ratio = deprecated
232
- const events = [
233
- ...Array(10)
234
- .fill(null)
235
- .map(() => createFeedbackAt(0, "helpful")),
236
- ...Array(8)
237
- .fill(null)
238
- .map(() => createFeedbackAt(0, "harmful")), // 44% harmful
239
- ];
240
- const state = calculateMaturityState(events);
241
- expect(state).toBe("deprecated");
242
- });
243
-
244
- test("uses custom config thresholds", () => {
245
- const config = {
246
- ...DEFAULT_MATURITY_CONFIG,
247
- minFeedback: 2,
248
- minHelpful: 3,
249
- maxHarmful: 0.2,
250
- deprecationThreshold: 0.4,
251
- };
252
-
253
- // 3 helpful, 0 harmful = proven with custom config
254
- const events = [
255
- createFeedbackAt(0, "helpful"),
256
- createFeedbackAt(0, "helpful"),
257
- createFeedbackAt(0, "helpful"),
258
- ];
259
- const state = calculateMaturityState(events, config);
260
- expect(state).toBe("proven");
261
- });
262
-
263
- test("accounts for decay in state calculation", () => {
264
- // Old helpful feedback decays, shifting ratios
265
- // 3 events at 180 days = 2 half-lives = ~0.25x each = ~0.75 total helpful
266
- // 1 event at 0 days = ~1.0x harmful
267
- // Total = ~1.75, harmful ratio = 1.0/1.75 = ~57%
268
- // BUT total < minFeedback (3), so state should be candidate
269
- const events = [
270
- createFeedbackAt(180, "helpful"), // heavily decayed (~0.25x)
271
- createFeedbackAt(180, "helpful"), // heavily decayed (~0.25x)
272
- createFeedbackAt(180, "helpful"), // heavily decayed (~0.25x)
273
- createFeedbackAt(0, "harmful"), // recent, full weight (~1.0x)
274
- ];
275
-
276
- const state = calculateMaturityState(events);
277
- // Total decayed feedback < minFeedback threshold
278
- expect(state).toBe("candidate");
279
- });
280
- });
281
-
282
- // ============================================================================
283
- // createPatternMaturity Tests
284
- // ============================================================================
285
-
286
- describe("createPatternMaturity", () => {
287
- test("creates initial maturity in candidate state", () => {
288
- const maturity = createPatternMaturity("test-pattern");
289
- expect(maturity.pattern_id).toBe("test-pattern");
290
- expect(maturity.state).toBe("candidate");
291
- expect(maturity.helpful_count).toBe(0);
292
- expect(maturity.harmful_count).toBe(0);
293
- });
294
-
295
- test("sets last_validated timestamp", () => {
296
- const before = new Date();
297
- const maturity = createPatternMaturity("test-pattern");
298
- const after = new Date();
299
-
300
- const validated = new Date(maturity.last_validated);
301
- expect(validated.getTime()).toBeGreaterThanOrEqual(before.getTime());
302
- expect(validated.getTime()).toBeLessThanOrEqual(after.getTime());
303
- });
304
-
305
- test("does not set promoted_at or deprecated_at initially", () => {
306
- const maturity = createPatternMaturity("test-pattern");
307
- expect(maturity.promoted_at).toBeUndefined();
308
- expect(maturity.deprecated_at).toBeUndefined();
309
- });
310
- });
311
-
312
- // ============================================================================
313
- // updatePatternMaturity Tests
314
- // ============================================================================
315
-
316
- describe("updatePatternMaturity", () => {
317
- test("updates state based on feedback", () => {
318
- const maturity = createPatternMaturity("test-pattern");
319
- const events = [
320
- ...Array(6)
321
- .fill(null)
322
- .map(() => createFeedbackAt(0, "helpful")),
323
- createFeedbackAt(0, "harmful"),
324
- ];
325
-
326
- const updated = updatePatternMaturity(maturity, events);
327
- expect(updated.state).toBe("proven");
328
- });
329
-
330
- test("updates helpful and harmful counts", () => {
331
- const maturity = createPatternMaturity("test-pattern");
332
- const events = [
333
- createFeedbackAt(0, "helpful"),
334
- createFeedbackAt(0, "helpful"),
335
- createFeedbackAt(0, "harmful"),
336
- ];
337
-
338
- const updated = updatePatternMaturity(maturity, events);
339
- expect(updated.helpful_count).toBe(2);
340
- expect(updated.harmful_count).toBe(1);
341
- });
342
-
343
- test("sets promoted_at on first transition to proven", () => {
344
- const maturity = createPatternMaturity("test-pattern");
345
- const events = [
346
- ...Array(6)
347
- .fill(null)
348
- .map(() => createFeedbackAt(0, "helpful")),
349
- ];
350
-
351
- const updated = updatePatternMaturity(maturity, events);
352
- expect(updated.promoted_at).toBeDefined();
353
- expect(new Date(updated.promoted_at!).getTime()).toBeLessThanOrEqual(
354
- Date.now(),
355
- );
356
- });
357
-
358
- test("does not update promoted_at if already proven", () => {
359
- const maturity = createPatternMaturity("test-pattern");
360
- const events = [
361
- ...Array(6)
362
- .fill(null)
363
- .map(() => createFeedbackAt(0, "helpful")),
364
- ];
365
-
366
- const first = updatePatternMaturity(maturity, events);
367
- const promotedAt = first.promoted_at;
368
-
369
- // Add more helpful feedback
370
- const moreEvents = [
371
- ...events,
372
- createFeedbackAt(0, "helpful"),
373
- createFeedbackAt(0, "helpful"),
374
- ];
375
- const second = updatePatternMaturity(first, moreEvents);
376
-
377
- expect(second.promoted_at).toBe(promotedAt); // unchanged
378
- });
379
-
380
- test("sets deprecated_at on first transition to deprecated", () => {
381
- const maturity = createPatternMaturity("test-pattern");
382
- const events = [
383
- createFeedbackAt(0, "helpful"),
384
- createFeedbackAt(0, "harmful"),
385
- createFeedbackAt(0, "harmful"),
386
- createFeedbackAt(0, "harmful"),
387
- ];
388
-
389
- const updated = updatePatternMaturity(maturity, events);
390
- expect(updated.deprecated_at).toBeDefined();
391
- });
392
-
393
- test("does not update deprecated_at if already deprecated", () => {
394
- const maturity = createPatternMaturity("test-pattern");
395
- const events = [
396
- createFeedbackAt(0, "helpful"),
397
- createFeedbackAt(0, "harmful"),
398
- createFeedbackAt(0, "harmful"),
399
- createFeedbackAt(0, "harmful"),
400
- ];
401
-
402
- const first = updatePatternMaturity(maturity, events);
403
- const deprecatedAt = first.deprecated_at;
404
-
405
- const moreEvents = [...events, createFeedbackAt(0, "harmful")];
406
- const second = updatePatternMaturity(first, moreEvents);
407
-
408
- expect(second.deprecated_at).toBe(deprecatedAt); // unchanged
409
- });
410
-
411
- test("updates last_validated timestamp", () => {
412
- const maturity = createPatternMaturity("test-pattern");
413
- const events = [createFeedbackAt(0, "helpful")];
414
-
415
- const before = new Date();
416
- const updated = updatePatternMaturity(maturity, events);
417
- const after = new Date();
418
-
419
- const validated = new Date(updated.last_validated);
420
- expect(validated.getTime()).toBeGreaterThanOrEqual(before.getTime());
421
- expect(validated.getTime()).toBeLessThanOrEqual(after.getTime());
422
- });
423
-
424
- test("handles state transitions: candidate -> established", () => {
425
- const maturity = createPatternMaturity("test-pattern");
426
- const events = [
427
- createFeedbackAt(0, "helpful"),
428
- createFeedbackAt(0, "helpful"),
429
- createFeedbackAt(0, "helpful"),
430
- ];
431
-
432
- const updated = updatePatternMaturity(maturity, events);
433
- expect(maturity.state).toBe("candidate");
434
- expect(updated.state).toBe("established");
435
- });
436
-
437
- test("handles state transitions: established -> proven", () => {
438
- let maturity = createPatternMaturity("test-pattern");
439
- // First get to established
440
- maturity = updatePatternMaturity(maturity, [
441
- createFeedbackAt(0, "helpful"),
442
- createFeedbackAt(0, "helpful"),
443
- createFeedbackAt(0, "helpful"),
444
- ]);
445
- expect(maturity.state).toBe("established");
446
-
447
- // Then add enough for proven
448
- const provenEvents = [
449
- ...Array(6)
450
- .fill(null)
451
- .map(() => createFeedbackAt(0, "helpful")),
452
- ];
453
- const updated = updatePatternMaturity(maturity, provenEvents);
454
- expect(updated.state).toBe("proven");
455
- });
456
-
457
- test("handles state transitions: proven -> deprecated", () => {
458
- let maturity = createPatternMaturity("test-pattern");
459
- // First get to proven
460
- maturity = updatePatternMaturity(maturity, [
461
- ...Array(6)
462
- .fill(null)
463
- .map(() => createFeedbackAt(0, "helpful")),
464
- ]);
465
- expect(maturity.state).toBe("proven");
466
-
467
- // Then add lots of harmful feedback
468
- const deprecatedEvents = [
469
- ...Array(6)
470
- .fill(null)
471
- .map(() => createFeedbackAt(0, "helpful")),
472
- ...Array(8)
473
- .fill(null)
474
- .map(() => createFeedbackAt(0, "harmful")),
475
- ];
476
- const updated = updatePatternMaturity(maturity, deprecatedEvents);
477
- expect(updated.state).toBe("deprecated");
478
- });
479
-
480
- test("handles empty feedback array", () => {
481
- const maturity = createPatternMaturity("test-pattern");
482
- const updated = updatePatternMaturity(maturity, []);
483
- expect(updated.state).toBe("candidate");
484
- expect(updated.helpful_count).toBe(0);
485
- expect(updated.harmful_count).toBe(0);
486
- });
487
- });
488
-
489
- // ============================================================================
490
- // promotePattern Tests
491
- // ============================================================================
492
-
493
- describe("promotePattern", () => {
494
- test("promotes candidate to proven", () => {
495
- const maturity = createPatternMaturity("test-pattern");
496
- const promoted = promotePattern(maturity);
497
- expect(promoted.state).toBe("proven");
498
- });
499
-
500
- test("promotes established to proven", () => {
501
- const maturity: PatternMaturity = {
502
- pattern_id: "test-pattern",
503
- state: "established",
504
- helpful_count: 3,
505
- harmful_count: 0,
506
- last_validated: new Date().toISOString(),
507
- };
508
- const promoted = promotePattern(maturity);
509
- expect(promoted.state).toBe("proven");
510
- });
511
-
512
- test("sets promoted_at timestamp", () => {
513
- const maturity = createPatternMaturity("test-pattern");
514
- const before = new Date();
515
- const promoted = promotePattern(maturity);
516
- const after = new Date();
517
-
518
- expect(promoted.promoted_at).toBeDefined();
519
- const promotedAt = new Date(promoted.promoted_at!);
520
- expect(promotedAt.getTime()).toBeGreaterThanOrEqual(before.getTime());
521
- expect(promotedAt.getTime()).toBeLessThanOrEqual(after.getTime());
522
- });
523
-
524
- test("updates last_validated timestamp", () => {
525
- const maturity = createPatternMaturity("test-pattern");
526
- const before = new Date();
527
- const promoted = promotePattern(maturity);
528
- const after = new Date();
529
-
530
- const validated = new Date(promoted.last_validated);
531
- expect(validated.getTime()).toBeGreaterThanOrEqual(before.getTime());
532
- expect(validated.getTime()).toBeLessThanOrEqual(after.getTime());
533
- });
534
-
535
- test("throws error when promoting deprecated pattern", () => {
536
- const maturity: PatternMaturity = {
537
- pattern_id: "test-pattern",
538
- state: "deprecated",
539
- helpful_count: 1,
540
- harmful_count: 3,
541
- last_validated: new Date().toISOString(),
542
- deprecated_at: new Date().toISOString(),
543
- };
544
-
545
- expect(() => promotePattern(maturity)).toThrow(
546
- "Cannot promote a deprecated pattern",
547
- );
548
- });
549
-
550
- test("returns unchanged maturity when already proven", () => {
551
- const maturity: PatternMaturity = {
552
- pattern_id: "test-pattern",
553
- state: "proven",
554
- helpful_count: 10,
555
- harmful_count: 1,
556
- last_validated: new Date().toISOString(),
557
- promoted_at: "2024-01-01T00:00:00Z",
558
- };
559
-
560
- const promoted = promotePattern(maturity);
561
- expect(promoted).toBe(maturity); // same reference
562
- expect(promoted.state).toBe("proven");
563
- });
564
- });
565
-
566
- // ============================================================================
567
- // deprecatePattern Tests
568
- // ============================================================================
569
-
570
- describe("deprecatePattern", () => {
571
- test("deprecates candidate pattern", () => {
572
- const maturity = createPatternMaturity("test-pattern");
573
- const deprecated = deprecatePattern(maturity);
574
- expect(deprecated.state).toBe("deprecated");
575
- });
576
-
577
- test("deprecates established pattern", () => {
578
- const maturity: PatternMaturity = {
579
- pattern_id: "test-pattern",
580
- state: "established",
581
- helpful_count: 3,
582
- harmful_count: 1,
583
- last_validated: new Date().toISOString(),
584
- };
585
- const deprecated = deprecatePattern(maturity);
586
- expect(deprecated.state).toBe("deprecated");
587
- });
588
-
589
- test("deprecates proven pattern", () => {
590
- const maturity: PatternMaturity = {
591
- pattern_id: "test-pattern",
592
- state: "proven",
593
- helpful_count: 10,
594
- harmful_count: 1,
595
- last_validated: new Date().toISOString(),
596
- promoted_at: new Date().toISOString(),
597
- };
598
- const deprecated = deprecatePattern(maturity);
599
- expect(deprecated.state).toBe("deprecated");
600
- });
601
-
602
- test("sets deprecated_at timestamp", () => {
603
- const maturity = createPatternMaturity("test-pattern");
604
- const before = new Date();
605
- const deprecated = deprecatePattern(maturity);
606
- const after = new Date();
607
-
608
- expect(deprecated.deprecated_at).toBeDefined();
609
- const deprecatedAt = new Date(deprecated.deprecated_at!);
610
- expect(deprecatedAt.getTime()).toBeGreaterThanOrEqual(before.getTime());
611
- expect(deprecatedAt.getTime()).toBeLessThanOrEqual(after.getTime());
612
- });
613
-
614
- test("updates last_validated timestamp", () => {
615
- const maturity = createPatternMaturity("test-pattern");
616
- const before = new Date();
617
- const deprecated = deprecatePattern(maturity);
618
- const after = new Date();
619
-
620
- const validated = new Date(deprecated.last_validated);
621
- expect(validated.getTime()).toBeGreaterThanOrEqual(before.getTime());
622
- expect(validated.getTime()).toBeLessThanOrEqual(after.getTime());
623
- });
624
-
625
- test("returns unchanged maturity when already deprecated", () => {
626
- const maturity: PatternMaturity = {
627
- pattern_id: "test-pattern",
628
- state: "deprecated",
629
- helpful_count: 1,
630
- harmful_count: 3,
631
- last_validated: new Date().toISOString(),
632
- deprecated_at: "2024-01-01T00:00:00Z",
633
- };
634
-
635
- const deprecated = deprecatePattern(maturity);
636
- expect(deprecated).toBe(maturity); // same reference
637
- expect(deprecated.state).toBe("deprecated");
638
- });
639
-
640
- test("accepts optional reason parameter", () => {
641
- const maturity = createPatternMaturity("test-pattern");
642
- // Reason is accepted but not stored (parameter prefixed with _)
643
- const deprecated = deprecatePattern(maturity, "test reason");
644
- expect(deprecated.state).toBe("deprecated");
645
- });
646
- });
647
-
648
- // ============================================================================
649
- // getMaturityMultiplier Tests
650
- // ============================================================================
651
-
652
- describe("getMaturityMultiplier", () => {
653
- test("returns 0.5 for candidate", () => {
654
- expect(getMaturityMultiplier("candidate")).toBe(0.5);
655
- });
656
-
657
- test("returns 1.0 for established", () => {
658
- expect(getMaturityMultiplier("established")).toBe(1.0);
659
- });
660
-
661
- test("returns 1.5 for proven", () => {
662
- expect(getMaturityMultiplier("proven")).toBe(1.5);
663
- });
664
-
665
- test("returns 0 for deprecated", () => {
666
- expect(getMaturityMultiplier("deprecated")).toBe(0);
667
- });
668
- });
669
-
670
- // ============================================================================
671
- // formatMaturityForPrompt Tests
672
- // ============================================================================
673
-
674
- describe("formatMaturityForPrompt", () => {
675
- test("shows limited data for insufficient observations", () => {
676
- const maturity: PatternMaturity = {
677
- pattern_id: "test",
678
- state: "candidate",
679
- helpful_count: 2,
680
- harmful_count: 0,
681
- last_validated: new Date().toISOString(),
682
- };
683
-
684
- const formatted = formatMaturityForPrompt(maturity);
685
- expect(formatted).toBe("[LIMITED DATA - 2 observations]");
686
- });
687
-
688
- test("shows singular observation for count of 1", () => {
689
- const maturity: PatternMaturity = {
690
- pattern_id: "test",
691
- state: "candidate",
692
- helpful_count: 1,
693
- harmful_count: 0,
694
- last_validated: new Date().toISOString(),
695
- };
696
-
697
- const formatted = formatMaturityForPrompt(maturity);
698
- expect(formatted).toBe("[LIMITED DATA - 1 observation]");
699
- });
700
-
701
- test("shows candidate with observation count when >= 3", () => {
702
- const maturity: PatternMaturity = {
703
- pattern_id: "test",
704
- state: "candidate",
705
- helpful_count: 3,
706
- harmful_count: 0,
707
- last_validated: new Date().toISOString(),
708
- };
709
-
710
- const formatted = formatMaturityForPrompt(maturity);
711
- expect(formatted).toBe("[CANDIDATE - 3 observations, needs more data]");
712
- });
713
-
714
- test("shows established with percentages", () => {
715
- const maturity: PatternMaturity = {
716
- pattern_id: "test",
717
- state: "established",
718
- helpful_count: 7,
719
- harmful_count: 3,
720
- last_validated: new Date().toISOString(),
721
- };
722
-
723
- const formatted = formatMaturityForPrompt(maturity);
724
- expect(formatted).toBe(
725
- "[ESTABLISHED - 70% helpful, 30% harmful from 10 observations]",
726
- );
727
- });
728
-
729
- test("shows proven with helpful percentage", () => {
730
- const maturity: PatternMaturity = {
731
- pattern_id: "test",
732
- state: "proven",
733
- helpful_count: 9,
734
- harmful_count: 1,
735
- last_validated: new Date().toISOString(),
736
- promoted_at: new Date().toISOString(),
737
- };
738
-
739
- const formatted = formatMaturityForPrompt(maturity);
740
- expect(formatted).toBe("[PROVEN - 90% helpful from 10 observations]");
741
- });
742
-
743
- test("shows deprecated with harmful percentage", () => {
744
- const maturity: PatternMaturity = {
745
- pattern_id: "test",
746
- state: "deprecated",
747
- helpful_count: 2,
748
- harmful_count: 8,
749
- last_validated: new Date().toISOString(),
750
- deprecated_at: new Date().toISOString(),
751
- };
752
-
753
- const formatted = formatMaturityForPrompt(maturity);
754
- expect(formatted).toBe("[DEPRECATED - 80% harmful, avoid using]");
755
- });
756
-
757
- test("rounds percentages correctly", () => {
758
- const maturity: PatternMaturity = {
759
- pattern_id: "test",
760
- state: "established",
761
- helpful_count: 2,
762
- harmful_count: 1,
763
- last_validated: new Date().toISOString(),
764
- };
765
-
766
- const formatted = formatMaturityForPrompt(maturity);
767
- // 2/3 = 66.666... rounds to 67%, 1/3 = 33.333... rounds to 33%
768
- expect(formatted).toBe(
769
- "[ESTABLISHED - 67% helpful, 33% harmful from 3 observations]",
770
- );
771
- });
772
-
773
- test("handles zero counts edge case", () => {
774
- const maturity: PatternMaturity = {
775
- pattern_id: "test",
776
- state: "candidate",
777
- helpful_count: 0,
778
- harmful_count: 0,
779
- last_validated: new Date().toISOString(),
780
- };
781
-
782
- const formatted = formatMaturityForPrompt(maturity);
783
- expect(formatted).toBe("[LIMITED DATA - 0 observations]");
784
- });
785
- });
786
-
787
- // ============================================================================
788
- // formatPatternsWithMaturityForPrompt Tests
789
- // ============================================================================
790
-
791
- describe("formatPatternsWithMaturityForPrompt", () => {
792
- test("formats empty map", () => {
793
- const patterns = new Map<string, PatternMaturity>();
794
- const formatted = formatPatternsWithMaturityForPrompt(patterns);
795
- expect(formatted).toBe("");
796
- });
797
-
798
- test("groups patterns by maturity state", () => {
799
- const patterns = new Map<string, PatternMaturity>();
800
-
801
- patterns.set("Proven pattern", {
802
- pattern_id: "p1",
803
- state: "proven",
804
- helpful_count: 9,
805
- harmful_count: 1,
806
- last_validated: new Date().toISOString(),
807
- promoted_at: new Date().toISOString(),
808
- });
809
-
810
- patterns.set("Established pattern", {
811
- pattern_id: "p2",
812
- state: "established",
813
- helpful_count: 5,
814
- harmful_count: 2,
815
- last_validated: new Date().toISOString(),
816
- });
817
-
818
- patterns.set("Candidate pattern", {
819
- pattern_id: "p3",
820
- state: "candidate",
821
- helpful_count: 3,
822
- harmful_count: 0,
823
- last_validated: new Date().toISOString(),
824
- });
825
-
826
- patterns.set("Deprecated pattern", {
827
- pattern_id: "p4",
828
- state: "deprecated",
829
- helpful_count: 1,
830
- harmful_count: 5,
831
- last_validated: new Date().toISOString(),
832
- deprecated_at: new Date().toISOString(),
833
- });
834
-
835
- const formatted = formatPatternsWithMaturityForPrompt(patterns);
836
-
837
- expect(formatted).toContain("## Proven Patterns");
838
- expect(formatted).toContain("- Proven pattern");
839
- expect(formatted).toContain("## Established Patterns");
840
- expect(formatted).toContain("- Established pattern");
841
- expect(formatted).toContain("## Candidate Patterns");
842
- expect(formatted).toContain("- Candidate pattern");
843
- expect(formatted).toContain("## Deprecated Patterns");
844
- expect(formatted).toContain("- Deprecated pattern");
845
- });
846
-
847
- test("omits sections with no patterns", () => {
848
- const patterns = new Map<string, PatternMaturity>();
849
-
850
- patterns.set("Only proven", {
851
- pattern_id: "p1",
852
- state: "proven",
853
- helpful_count: 10,
854
- harmful_count: 0,
855
- last_validated: new Date().toISOString(),
856
- promoted_at: new Date().toISOString(),
857
- });
858
-
859
- const formatted = formatPatternsWithMaturityForPrompt(patterns);
860
-
861
- expect(formatted).toContain("## Proven Patterns");
862
- expect(formatted).not.toContain("## Established Patterns");
863
- expect(formatted).not.toContain("## Candidate Patterns");
864
- expect(formatted).not.toContain("## Deprecated Patterns");
865
- });
866
-
867
- test("includes pattern maturity labels", () => {
868
- const patterns = new Map<string, PatternMaturity>();
869
-
870
- patterns.set("Test pattern", {
871
- pattern_id: "p1",
872
- state: "proven",
873
- helpful_count: 9,
874
- harmful_count: 1,
875
- last_validated: new Date().toISOString(),
876
- promoted_at: new Date().toISOString(),
877
- });
878
-
879
- const formatted = formatPatternsWithMaturityForPrompt(patterns);
880
- expect(formatted).toContain("[PROVEN - 90% helpful from 10 observations]");
881
- });
882
-
883
- test("maintains multiple patterns in same section", () => {
884
- const patterns = new Map<string, PatternMaturity>();
885
-
886
- patterns.set("Pattern A", {
887
- pattern_id: "p1",
888
- state: "proven",
889
- helpful_count: 10,
890
- harmful_count: 0,
891
- last_validated: new Date().toISOString(),
892
- promoted_at: new Date().toISOString(),
893
- });
894
-
895
- patterns.set("Pattern B", {
896
- pattern_id: "p2",
897
- state: "proven",
898
- helpful_count: 8,
899
- harmful_count: 1,
900
- last_validated: new Date().toISOString(),
901
- promoted_at: new Date().toISOString(),
902
- });
903
-
904
- const formatted = formatPatternsWithMaturityForPrompt(patterns);
905
- expect(formatted).toContain("- Pattern A");
906
- expect(formatted).toContain("- Pattern B");
907
- });
908
-
909
- test("formats section headers correctly", () => {
910
- const patterns = new Map<string, PatternMaturity>();
911
-
912
- patterns.set("Test", {
913
- pattern_id: "p1",
914
- state: "deprecated",
915
- helpful_count: 0,
916
- harmful_count: 5,
917
- last_validated: new Date().toISOString(),
918
- deprecated_at: new Date().toISOString(),
919
- });
920
-
921
- const formatted = formatPatternsWithMaturityForPrompt(patterns);
922
- expect(formatted).toContain(
923
- "## Deprecated Patterns\n\nAVOID these patterns - they have poor track records:",
924
- );
925
- });
926
- });
927
-
928
- // ============================================================================
929
- // InMemoryMaturityStorage Tests
930
- // ============================================================================
931
-
932
- describe("InMemoryMaturityStorage", () => {
933
- let storage: InMemoryMaturityStorage;
934
-
935
- beforeEach(() => {
936
- storage = new InMemoryMaturityStorage();
937
- });
938
-
939
- describe("store and get", () => {
940
- test("stores and retrieves maturity by pattern ID", async () => {
941
- const maturity = createPatternMaturity("test-pattern");
942
- await storage.store(maturity);
943
-
944
- const retrieved = await storage.get("test-pattern");
945
- expect(retrieved).toEqual(maturity);
946
- });
947
-
948
- test("returns null for non-existent pattern", async () => {
949
- const retrieved = await storage.get("non-existent");
950
- expect(retrieved).toBeNull();
951
- });
952
-
953
- test("overwrites existing maturity on store", async () => {
954
- const maturity1 = createPatternMaturity("test-pattern");
955
- await storage.store(maturity1);
956
-
957
- const maturity2 = { ...maturity1, helpful_count: 5 };
958
- await storage.store(maturity2);
959
-
960
- const retrieved = await storage.get("test-pattern");
961
- expect(retrieved?.helpful_count).toBe(5);
962
- });
963
- });
964
-
965
- describe("getAll", () => {
966
- test("returns empty array when no maturities stored", async () => {
967
- const all = await storage.getAll();
968
- expect(all).toEqual([]);
969
- });
970
-
971
- test("returns all stored maturities", async () => {
972
- const m1 = createPatternMaturity("pattern-1");
973
- const m2 = createPatternMaturity("pattern-2");
974
- const m3 = createPatternMaturity("pattern-3");
975
-
976
- await storage.store(m1);
977
- await storage.store(m2);
978
- await storage.store(m3);
979
-
980
- const all = await storage.getAll();
981
- expect(all).toHaveLength(3);
982
- expect(all).toContainEqual(m1);
983
- expect(all).toContainEqual(m2);
984
- expect(all).toContainEqual(m3);
985
- });
986
- });
987
-
988
- describe("getByState", () => {
989
- test("returns empty array when no patterns match state", async () => {
990
- const maturity = createPatternMaturity("test-pattern");
991
- await storage.store(maturity);
992
-
993
- const proven = await storage.getByState("proven");
994
- expect(proven).toEqual([]);
995
- });
996
-
997
- test("returns only patterns matching state", async () => {
998
- const candidate: PatternMaturity = {
999
- pattern_id: "p1",
1000
- state: "candidate",
1001
- helpful_count: 1,
1002
- harmful_count: 0,
1003
- last_validated: new Date().toISOString(),
1004
- };
1005
-
1006
- const proven: PatternMaturity = {
1007
- pattern_id: "p2",
1008
- state: "proven",
1009
- helpful_count: 10,
1010
- harmful_count: 0,
1011
- last_validated: new Date().toISOString(),
1012
- promoted_at: new Date().toISOString(),
1013
- };
1014
-
1015
- const deprecated: PatternMaturity = {
1016
- pattern_id: "p3",
1017
- state: "deprecated",
1018
- helpful_count: 1,
1019
- harmful_count: 5,
1020
- last_validated: new Date().toISOString(),
1021
- deprecated_at: new Date().toISOString(),
1022
- };
1023
-
1024
- await storage.store(candidate);
1025
- await storage.store(proven);
1026
- await storage.store(deprecated);
1027
-
1028
- const provenResults = await storage.getByState("proven");
1029
- expect(provenResults).toHaveLength(1);
1030
- expect(provenResults[0]).toEqual(proven);
1031
-
1032
- const candidateResults = await storage.getByState("candidate");
1033
- expect(candidateResults).toHaveLength(1);
1034
- expect(candidateResults[0]).toEqual(candidate);
1035
- });
1036
-
1037
- test("handles multiple patterns with same state", async () => {
1038
- const p1: PatternMaturity = {
1039
- pattern_id: "p1",
1040
- state: "proven",
1041
- helpful_count: 10,
1042
- harmful_count: 0,
1043
- last_validated: new Date().toISOString(),
1044
- promoted_at: new Date().toISOString(),
1045
- };
1046
-
1047
- const p2: PatternMaturity = {
1048
- pattern_id: "p2",
1049
- state: "proven",
1050
- helpful_count: 8,
1051
- harmful_count: 1,
1052
- last_validated: new Date().toISOString(),
1053
- promoted_at: new Date().toISOString(),
1054
- };
1055
-
1056
- await storage.store(p1);
1057
- await storage.store(p2);
1058
-
1059
- const proven = await storage.getByState("proven");
1060
- expect(proven).toHaveLength(2);
1061
- });
1062
- });
1063
-
1064
- describe("storeFeedback and getFeedback", () => {
1065
- test("stores and retrieves feedback for pattern", async () => {
1066
- const feedback = createFeedback({ pattern_id: "test-pattern" });
1067
- await storage.storeFeedback(feedback);
1068
-
1069
- const retrieved = await storage.getFeedback("test-pattern");
1070
- expect(retrieved).toHaveLength(1);
1071
- expect(retrieved[0]).toEqual(feedback);
1072
- });
1073
-
1074
- test("returns empty array for pattern with no feedback", async () => {
1075
- const feedback = await storage.getFeedback("non-existent");
1076
- expect(feedback).toEqual([]);
1077
- });
1078
-
1079
- test("stores multiple feedback events for same pattern", async () => {
1080
- const f1 = createFeedback({
1081
- pattern_id: "test-pattern",
1082
- type: "helpful",
1083
- });
1084
- const f2 = createFeedback({
1085
- pattern_id: "test-pattern",
1086
- type: "harmful",
1087
- });
1088
- const f3 = createFeedback({
1089
- pattern_id: "test-pattern",
1090
- type: "helpful",
1091
- });
1092
-
1093
- await storage.storeFeedback(f1);
1094
- await storage.storeFeedback(f2);
1095
- await storage.storeFeedback(f3);
1096
-
1097
- const retrieved = await storage.getFeedback("test-pattern");
1098
- expect(retrieved).toHaveLength(3);
1099
- });
1100
-
1101
- test("filters feedback by pattern ID", async () => {
1102
- const f1 = createFeedback({ pattern_id: "pattern-1" });
1103
- const f2 = createFeedback({ pattern_id: "pattern-2" });
1104
- const f3 = createFeedback({ pattern_id: "pattern-1" });
1105
-
1106
- await storage.storeFeedback(f1);
1107
- await storage.storeFeedback(f2);
1108
- await storage.storeFeedback(f3);
1109
-
1110
- const pattern1Feedback = await storage.getFeedback("pattern-1");
1111
- expect(pattern1Feedback).toHaveLength(2);
1112
-
1113
- const pattern2Feedback = await storage.getFeedback("pattern-2");
1114
- expect(pattern2Feedback).toHaveLength(1);
1115
- });
1116
-
1117
- test("preserves feedback event data", async () => {
1118
- const feedback = createFeedback({
1119
- pattern_id: "test-pattern",
1120
- type: "helpful",
1121
- weight: 0.75,
1122
- timestamp: "2024-01-01T00:00:00Z",
1123
- });
1124
-
1125
- await storage.storeFeedback(feedback);
1126
- const retrieved = await storage.getFeedback("test-pattern");
1127
-
1128
- expect(retrieved[0].type).toBe("helpful");
1129
- expect(retrieved[0].weight).toBe(0.75);
1130
- expect(retrieved[0].timestamp).toBe("2024-01-01T00:00:00Z");
1131
- });
1132
- });
1133
-
1134
- describe("integration: full workflow", () => {
1135
- test("supports complete maturity tracking workflow", async () => {
1136
- // Create and store initial maturity
1137
- const maturity = createPatternMaturity("test-pattern");
1138
- await storage.store(maturity);
1139
-
1140
- // Add feedback events
1141
- const feedback1 = createFeedback({ type: "helpful" });
1142
- const feedback2 = createFeedback({ type: "helpful" });
1143
- const feedback3 = createFeedback({ type: "helpful" });
1144
- await storage.storeFeedback(feedback1);
1145
- await storage.storeFeedback(feedback2);
1146
- await storage.storeFeedback(feedback3);
1147
-
1148
- // Retrieve and update maturity
1149
- const current = await storage.get("test-pattern");
1150
- const feedbackEvents = await storage.getFeedback("test-pattern");
1151
- const updated = updatePatternMaturity(current!, feedbackEvents);
1152
- await storage.store(updated);
1153
-
1154
- // Verify final state
1155
- const final = await storage.get("test-pattern");
1156
- expect(final?.state).toBe("established");
1157
- expect(final?.helpful_count).toBe(3);
1158
- });
1159
- });
1160
- });