prjct-cli 1.20.0 → 1.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,463 @@
1
+ /**
2
+ * State Storage Task Feedback Tests (PRJ-272)
3
+ *
4
+ * Tests for the task-to-analysis feedback loop:
5
+ * - Feedback schema validation
6
+ * - Feedback persistence in task history
7
+ * - Feedback aggregation across tasks
8
+ * - Known gotchas promotion (2+ occurrences)
9
+ * - Backward compatibility (tasks without feedback)
10
+ * - Context injection (markdown with feedback)
11
+ */
12
+
13
+ import { afterEach, beforeEach, describe, expect, it } from 'bun:test'
14
+ import fs from 'node:fs/promises'
15
+ import os from 'node:os'
16
+ import path from 'node:path'
17
+ import pathManager from '../../infrastructure/path-manager'
18
+ import type { CurrentTask, StateJson, TaskFeedback } from '../../schemas/state'
19
+ import { TaskFeedbackSchema } from '../../schemas/state'
20
+ import { prjctDb } from '../../storage/database'
21
+ import { stateStorage } from '../../storage/state-storage'
22
+
23
+ // =============================================================================
24
+ // Test Setup
25
+ // =============================================================================
26
+
27
+ let tmpRoot: string | null = null
28
+ let testProjectId: string
29
+
30
+ const originalGetGlobalProjectPath = pathManager.getGlobalProjectPath.bind(pathManager)
31
+ const originalGetStoragePath = pathManager.getStoragePath.bind(pathManager)
32
+ const originalGetFilePath = pathManager.getFilePath.bind(pathManager)
33
+
34
+ beforeEach(async () => {
35
+ tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'prjct-feedback-test-'))
36
+ testProjectId = `test-feedback-${Date.now()}`
37
+
38
+ pathManager.getGlobalProjectPath = (projectId: string) => {
39
+ return path.join(tmpRoot!, projectId)
40
+ }
41
+
42
+ pathManager.getStoragePath = (projectId: string, filename: string) => {
43
+ return path.join(tmpRoot!, projectId, 'storage', filename)
44
+ }
45
+
46
+ pathManager.getFilePath = (projectId: string, layer: string, filename: string) => {
47
+ return path.join(tmpRoot!, projectId, layer, filename)
48
+ }
49
+
50
+ const storagePath = pathManager.getStoragePath(testProjectId, '')
51
+ await fs.mkdir(storagePath, { recursive: true })
52
+
53
+ const syncPath = path.join(tmpRoot!, testProjectId, 'sync')
54
+ await fs.mkdir(syncPath, { recursive: true })
55
+ })
56
+
57
+ afterEach(async () => {
58
+ prjctDb.close()
59
+
60
+ pathManager.getGlobalProjectPath = originalGetGlobalProjectPath
61
+ pathManager.getStoragePath = originalGetStoragePath
62
+ pathManager.getFilePath = originalGetFilePath
63
+
64
+ if (tmpRoot) {
65
+ await fs.rm(tmpRoot, { recursive: true, force: true })
66
+ tmpRoot = null
67
+ }
68
+ })
69
+
70
+ // =============================================================================
71
+ // Helper Functions
72
+ // =============================================================================
73
+
74
+ function createMockTask(
75
+ overrides: Partial<CurrentTask> & Record<string, unknown> = {}
76
+ ): CurrentTask {
77
+ return {
78
+ id: `task-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`,
79
+ description: 'Test task',
80
+ startedAt: new Date().toISOString(),
81
+ sessionId: `session-${Date.now()}`,
82
+ ...overrides,
83
+ } as CurrentTask
84
+ }
85
+
86
+ async function startAndCompleteWithFeedback(
87
+ projectId: string,
88
+ task: CurrentTask,
89
+ feedback?: TaskFeedback
90
+ ): Promise<StateJson> {
91
+ await stateStorage.startTask(projectId, task)
92
+ await stateStorage.completeTask(projectId, feedback)
93
+ return await stateStorage.read(projectId)
94
+ }
95
+
96
+ // =============================================================================
97
+ // Tests: TaskFeedback Schema Validation
98
+ // =============================================================================
99
+
100
+ describe('TaskFeedback Schema', () => {
101
+ it('should validate a complete feedback object', () => {
102
+ const feedback: TaskFeedback = {
103
+ stackConfirmed: ['React 18', 'TypeScript strict mode'],
104
+ patternsDiscovered: ['API routes follow /api/v1/{resource} pattern'],
105
+ agentAccuracy: [{ agent: 'backend.md', rating: 'helpful', note: 'Good API patterns' }],
106
+ issuesEncountered: ['ESLint conflicts with Prettier'],
107
+ }
108
+
109
+ const result = TaskFeedbackSchema.safeParse(feedback)
110
+ expect(result.success).toBe(true)
111
+ })
112
+
113
+ it('should validate an empty feedback object', () => {
114
+ const feedback: TaskFeedback = {}
115
+
116
+ const result = TaskFeedbackSchema.safeParse(feedback)
117
+ expect(result.success).toBe(true)
118
+ })
119
+
120
+ it('should validate feedback with only patterns', () => {
121
+ const feedback: TaskFeedback = {
122
+ patternsDiscovered: ['Components use barrel exports'],
123
+ }
124
+
125
+ const result = TaskFeedbackSchema.safeParse(feedback)
126
+ expect(result.success).toBe(true)
127
+ })
128
+
129
+ it('should reject invalid agent accuracy rating', () => {
130
+ const feedback = {
131
+ agentAccuracy: [{ agent: 'backend.md', rating: 'invalid_rating' }],
132
+ }
133
+
134
+ const result = TaskFeedbackSchema.safeParse(feedback)
135
+ expect(result.success).toBe(false)
136
+ })
137
+
138
+ it('should validate all agent accuracy rating values', () => {
139
+ for (const rating of ['helpful', 'neutral', 'inaccurate'] as const) {
140
+ const feedback: TaskFeedback = {
141
+ agentAccuracy: [{ agent: 'test.md', rating }],
142
+ }
143
+ const result = TaskFeedbackSchema.safeParse(feedback)
144
+ expect(result.success).toBe(true)
145
+ }
146
+ })
147
+ })
148
+
149
+ // =============================================================================
150
+ // Tests: Feedback Persistence in Task History
151
+ // =============================================================================
152
+
153
+ describe('Feedback Persistence', () => {
154
+ it('should store feedback in task history entry', async () => {
155
+ const task = createMockTask({ description: 'Task with feedback' })
156
+ const feedback: TaskFeedback = {
157
+ stackConfirmed: ['TypeScript'],
158
+ patternsDiscovered: ['Uses Hono framework'],
159
+ }
160
+
161
+ const state = await startAndCompleteWithFeedback(testProjectId, task, feedback)
162
+
163
+ expect(state.taskHistory).toBeDefined()
164
+ expect(state.taskHistory!.length).toBe(1)
165
+ expect(state.taskHistory![0].feedback).toBeDefined()
166
+ expect(state.taskHistory![0].feedback?.stackConfirmed).toEqual(['TypeScript'])
167
+ expect(state.taskHistory![0].feedback?.patternsDiscovered).toEqual(['Uses Hono framework'])
168
+ })
169
+
170
+ it('should store task without feedback (backward compatible)', async () => {
171
+ const task = createMockTask({ description: 'Task without feedback' })
172
+
173
+ const state = await startAndCompleteWithFeedback(testProjectId, task)
174
+
175
+ expect(state.taskHistory).toBeDefined()
176
+ expect(state.taskHistory!.length).toBe(1)
177
+ expect(state.taskHistory![0].feedback).toBeUndefined()
178
+ })
179
+
180
+ it('should preserve feedback through FIFO eviction', async () => {
181
+ // Complete first task with feedback
182
+ const task1 = createMockTask({ description: 'Task 1' })
183
+ await stateStorage.startTask(testProjectId, task1)
184
+ await stateStorage.completeTask(testProjectId, {
185
+ patternsDiscovered: ['Pattern from task 1'],
186
+ })
187
+
188
+ // Complete second task with feedback
189
+ const task2 = createMockTask({ description: 'Task 2' })
190
+ await stateStorage.startTask(testProjectId, task2)
191
+ await stateStorage.completeTask(testProjectId, {
192
+ patternsDiscovered: ['Pattern from task 2'],
193
+ })
194
+
195
+ const state = await stateStorage.read(testProjectId)
196
+ expect(state.taskHistory!.length).toBe(2)
197
+ // Most recent first (FIFO)
198
+ expect(state.taskHistory![0].feedback?.patternsDiscovered).toEqual(['Pattern from task 2'])
199
+ expect(state.taskHistory![1].feedback?.patternsDiscovered).toEqual(['Pattern from task 1'])
200
+ })
201
+
202
+ it('should store full feedback with all fields', async () => {
203
+ const task = createMockTask({ description: 'Full feedback task' })
204
+ const feedback: TaskFeedback = {
205
+ stackConfirmed: ['React 18', 'TypeScript'],
206
+ patternsDiscovered: ['API routes use /api/v1/{resource}', 'Barrel exports'],
207
+ agentAccuracy: [
208
+ { agent: 'backend.md', rating: 'helpful', note: 'Good patterns' },
209
+ { agent: 'frontend.md', rating: 'inaccurate', note: 'Missing Tailwind' },
210
+ ],
211
+ issuesEncountered: ['ESLint conflicts with Prettier'],
212
+ }
213
+
214
+ const state = await startAndCompleteWithFeedback(testProjectId, task, feedback)
215
+
216
+ const stored = state.taskHistory![0].feedback!
217
+ expect(stored.stackConfirmed).toEqual(['React 18', 'TypeScript'])
218
+ expect(stored.patternsDiscovered).toEqual([
219
+ 'API routes use /api/v1/{resource}',
220
+ 'Barrel exports',
221
+ ])
222
+ expect(stored.agentAccuracy).toHaveLength(2)
223
+ expect(stored.agentAccuracy![0].rating).toBe('helpful')
224
+ expect(stored.agentAccuracy![1].rating).toBe('inaccurate')
225
+ expect(stored.issuesEncountered).toEqual(['ESLint conflicts with Prettier'])
226
+ })
227
+ })
228
+
229
+ // =============================================================================
230
+ // Tests: Feedback Aggregation
231
+ // =============================================================================
232
+
233
+ describe('Feedback Aggregation', () => {
234
+ it('should aggregate patterns from multiple tasks', async () => {
235
+ // Task 1
236
+ const task1 = createMockTask({ description: 'Task 1' })
237
+ await stateStorage.startTask(testProjectId, task1)
238
+ await stateStorage.completeTask(testProjectId, {
239
+ patternsDiscovered: ['Pattern A'],
240
+ })
241
+
242
+ // Task 2
243
+ const task2 = createMockTask({ description: 'Task 2' })
244
+ await stateStorage.startTask(testProjectId, task2)
245
+ await stateStorage.completeTask(testProjectId, {
246
+ patternsDiscovered: ['Pattern B'],
247
+ })
248
+
249
+ const aggregated = await stateStorage.getAggregatedFeedback(testProjectId)
250
+ expect(aggregated.patternsDiscovered).toContain('Pattern A')
251
+ expect(aggregated.patternsDiscovered).toContain('Pattern B')
252
+ })
253
+
254
+ it('should deduplicate patterns', async () => {
255
+ // Both tasks discover the same pattern
256
+ const task1 = createMockTask({ description: 'Task 1' })
257
+ await stateStorage.startTask(testProjectId, task1)
258
+ await stateStorage.completeTask(testProjectId, {
259
+ patternsDiscovered: ['Same pattern'],
260
+ })
261
+
262
+ const task2 = createMockTask({ description: 'Task 2' })
263
+ await stateStorage.startTask(testProjectId, task2)
264
+ await stateStorage.completeTask(testProjectId, {
265
+ patternsDiscovered: ['Same pattern'],
266
+ })
267
+
268
+ const aggregated = await stateStorage.getAggregatedFeedback(testProjectId)
269
+ expect(aggregated.patternsDiscovered).toEqual(['Same pattern'])
270
+ })
271
+
272
+ it('should deduplicate stack confirmations', async () => {
273
+ const task1 = createMockTask({ description: 'Task 1' })
274
+ await stateStorage.startTask(testProjectId, task1)
275
+ await stateStorage.completeTask(testProjectId, {
276
+ stackConfirmed: ['TypeScript', 'React'],
277
+ })
278
+
279
+ const task2 = createMockTask({ description: 'Task 2' })
280
+ await stateStorage.startTask(testProjectId, task2)
281
+ await stateStorage.completeTask(testProjectId, {
282
+ stackConfirmed: ['TypeScript', 'Next.js'],
283
+ })
284
+
285
+ const aggregated = await stateStorage.getAggregatedFeedback(testProjectId)
286
+ expect(aggregated.stackConfirmed).toContain('TypeScript')
287
+ expect(aggregated.stackConfirmed).toContain('React')
288
+ expect(aggregated.stackConfirmed).toContain('Next.js')
289
+ // TypeScript should not be duplicated
290
+ expect(aggregated.stackConfirmed.filter((s) => s === 'TypeScript')).toHaveLength(1)
291
+ })
292
+
293
+ it('should promote recurring issues to known gotchas', async () => {
294
+ // Same issue encountered twice
295
+ const task1 = createMockTask({ description: 'Task 1' })
296
+ await stateStorage.startTask(testProjectId, task1)
297
+ await stateStorage.completeTask(testProjectId, {
298
+ issuesEncountered: ['ESLint conflicts with Prettier'],
299
+ })
300
+
301
+ const task2 = createMockTask({ description: 'Task 2' })
302
+ await stateStorage.startTask(testProjectId, task2)
303
+ await stateStorage.completeTask(testProjectId, {
304
+ issuesEncountered: ['ESLint conflicts with Prettier'],
305
+ })
306
+
307
+ const aggregated = await stateStorage.getAggregatedFeedback(testProjectId)
308
+ expect(aggregated.knownGotchas).toContain('ESLint conflicts with Prettier')
309
+ })
310
+
311
+ it('should NOT promote single-occurrence issues to gotchas', async () => {
312
+ const task1 = createMockTask({ description: 'Task 1' })
313
+ await stateStorage.startTask(testProjectId, task1)
314
+ await stateStorage.completeTask(testProjectId, {
315
+ issuesEncountered: ['One-time issue'],
316
+ })
317
+
318
+ const aggregated = await stateStorage.getAggregatedFeedback(testProjectId)
319
+ expect(aggregated.issuesEncountered).toContain('One-time issue')
320
+ expect(aggregated.knownGotchas).not.toContain('One-time issue')
321
+ })
322
+
323
+ it('should aggregate agent accuracy across tasks', async () => {
324
+ const task1 = createMockTask({ description: 'Task 1' })
325
+ await stateStorage.startTask(testProjectId, task1)
326
+ await stateStorage.completeTask(testProjectId, {
327
+ agentAccuracy: [{ agent: 'backend.md', rating: 'helpful' }],
328
+ })
329
+
330
+ const task2 = createMockTask({ description: 'Task 2' })
331
+ await stateStorage.startTask(testProjectId, task2)
332
+ await stateStorage.completeTask(testProjectId, {
333
+ agentAccuracy: [{ agent: 'backend.md', rating: 'inaccurate', note: 'Missing Hono context' }],
334
+ })
335
+
336
+ const aggregated = await stateStorage.getAggregatedFeedback(testProjectId)
337
+ expect(aggregated.agentAccuracy).toHaveLength(2)
338
+ expect(aggregated.agentAccuracy[0].agent).toBe('backend.md')
339
+ expect(aggregated.agentAccuracy[1].agent).toBe('backend.md')
340
+ })
341
+
342
+ it('should return empty aggregation when no feedback exists', async () => {
343
+ // Complete task without feedback
344
+ const task = createMockTask({ description: 'No feedback' })
345
+ await stateStorage.startTask(testProjectId, task)
346
+ await stateStorage.completeTask(testProjectId)
347
+
348
+ const aggregated = await stateStorage.getAggregatedFeedback(testProjectId)
349
+ expect(aggregated.stackConfirmed).toEqual([])
350
+ expect(aggregated.patternsDiscovered).toEqual([])
351
+ expect(aggregated.agentAccuracy).toEqual([])
352
+ expect(aggregated.issuesEncountered).toEqual([])
353
+ expect(aggregated.knownGotchas).toEqual([])
354
+ })
355
+
356
+ it('should return empty aggregation when no tasks exist', async () => {
357
+ const aggregated = await stateStorage.getAggregatedFeedback(testProjectId)
358
+ expect(aggregated.stackConfirmed).toEqual([])
359
+ expect(aggregated.patternsDiscovered).toEqual([])
360
+ expect(aggregated.knownGotchas).toEqual([])
361
+ })
362
+ })
363
+
364
+ // =============================================================================
365
+ // Tests: Context Injection (Markdown Generation)
366
+ // =============================================================================
367
+
368
+ describe('Feedback in Markdown Context', () => {
369
+ it('should include patterns in task history markdown', async () => {
370
+ const task = createMockTask({ description: 'Task with patterns' })
371
+ await stateStorage.startTask(testProjectId, task)
372
+ await stateStorage.completeTask(testProjectId, {
373
+ patternsDiscovered: ['Uses barrel exports'],
374
+ })
375
+
376
+ const state = await stateStorage.read(testProjectId)
377
+ // Access the private toMarkdown via the generated context
378
+ const md = (stateStorage as any).toMarkdown(state)
379
+
380
+ expect(md).toContain('Patterns: Uses barrel exports')
381
+ })
382
+
383
+ it('should include gotchas in task history markdown', async () => {
384
+ const task = createMockTask({ description: 'Task with gotchas' })
385
+ await stateStorage.startTask(testProjectId, task)
386
+ await stateStorage.completeTask(testProjectId, {
387
+ issuesEncountered: ['Port 3000 already in use'],
388
+ })
389
+
390
+ const state = await stateStorage.read(testProjectId)
391
+ const md = (stateStorage as any).toMarkdown(state)
392
+
393
+ expect(md).toContain('Gotchas: Port 3000 already in use')
394
+ })
395
+
396
+ it('should NOT show feedback section when feedback is empty', async () => {
397
+ const task = createMockTask({ description: 'Task without feedback' })
398
+ await stateStorage.startTask(testProjectId, task)
399
+ await stateStorage.completeTask(testProjectId)
400
+
401
+ const state = await stateStorage.read(testProjectId)
402
+ const md = (stateStorage as any).toMarkdown(state)
403
+
404
+ expect(md).not.toContain('Patterns:')
405
+ expect(md).not.toContain('Gotchas:')
406
+ })
407
+ })
408
+
409
+ // =============================================================================
410
+ // Tests: Mixed Feedback and Non-Feedback Tasks
411
+ // =============================================================================
412
+
413
+ describe('Mixed Tasks (with and without feedback)', () => {
414
+ it('should handle mix of tasks with and without feedback', async () => {
415
+ // Task 1: with feedback
416
+ const task1 = createMockTask({ description: 'With feedback' })
417
+ await stateStorage.startTask(testProjectId, task1)
418
+ await stateStorage.completeTask(testProjectId, {
419
+ patternsDiscovered: ['Pattern from task 1'],
420
+ })
421
+
422
+ // Task 2: without feedback
423
+ const task2 = createMockTask({ description: 'Without feedback' })
424
+ await stateStorage.startTask(testProjectId, task2)
425
+ await stateStorage.completeTask(testProjectId)
426
+
427
+ // Task 3: with feedback
428
+ const task3 = createMockTask({ description: 'With feedback again' })
429
+ await stateStorage.startTask(testProjectId, task3)
430
+ await stateStorage.completeTask(testProjectId, {
431
+ patternsDiscovered: ['Pattern from task 3'],
432
+ })
433
+
434
+ const aggregated = await stateStorage.getAggregatedFeedback(testProjectId)
435
+ expect(aggregated.patternsDiscovered).toContain('Pattern from task 1')
436
+ expect(aggregated.patternsDiscovered).toContain('Pattern from task 3')
437
+ expect(aggregated.patternsDiscovered).toHaveLength(2)
438
+ })
439
+
440
+ it('should correctly count occurrences for gotcha promotion with mixed tasks', async () => {
441
+ // Task 1: encounters issue
442
+ const task1 = createMockTask({ description: 'Task 1' })
443
+ await stateStorage.startTask(testProjectId, task1)
444
+ await stateStorage.completeTask(testProjectId, {
445
+ issuesEncountered: ['Build fails on M1'],
446
+ })
447
+
448
+ // Task 2: no feedback
449
+ const task2 = createMockTask({ description: 'Task 2' })
450
+ await stateStorage.startTask(testProjectId, task2)
451
+ await stateStorage.completeTask(testProjectId)
452
+
453
+ // Task 3: encounters same issue
454
+ const task3 = createMockTask({ description: 'Task 3' })
455
+ await stateStorage.startTask(testProjectId, task3)
456
+ await stateStorage.completeTask(testProjectId, {
457
+ issuesEncountered: ['Build fails on M1'],
458
+ })
459
+
460
+ const aggregated = await stateStorage.getAggregatedFeedback(testProjectId)
461
+ expect(aggregated.knownGotchas).toContain('Build fails on M1')
462
+ })
463
+ })