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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/**
 * Unit tests for the UI's log-parser phase summary parsing functions.
 *
 * These tests verify the behavior of:
 * - parsePhaseSummaries(): Parses accumulated summary into individual phases
 * - extractPhaseSummary(): Extracts a specific phase's summary
 * - extractImplementationSummary(): Extracts only the implementation phase
 * - isAccumulatedSummary(): Checks if summary is in accumulated format
 *
 * The accumulated summary format uses markdown headers with `###` for phase names
 * and `---` as separators between phases.
 *
 * TODO: These test helper functions are mirrored from apps/ui/src/lib/log-parser.ts
 * because server-side tests cannot import from the UI module. If the production
 * implementation changes, these tests may pass while production fails.
 * Consider adding an integration test that validates the actual UI parsing behavior.
 */

import { describe, it, expect } from 'vitest';

// ============================================================================
// MIRRORED FUNCTIONS from apps/ui/src/lib/log-parser.ts
// ============================================================================
// NOTE: These functions are mirrored from the UI implementation because
// server-side tests cannot import from apps/ui/. Keep these in sync with the
// production implementation. The UI implementation includes additional
// handling for getPhaseSections/leadingImplementationSection for backward
// compatibility with mixed formats.

/**
 * Parses an accumulated summary string into individual phase summaries.
 */
function parsePhaseSummaries(summary: string | undefined): Map<string, string> {
  const phaseSummaries = new Map<string, string>();

  if (!summary || !summary.trim()) {
    return phaseSummaries;
  }

  // Split by the horizontal rule separator
  const sections = summary.split(/\n\n---\n\n/);

  for (const section of sections) {
    // Match the phase header pattern: ### Phase Name
    const headerMatch = section.match(/^###\s+(.+?)(?:\n|$)/);
    if (headerMatch) {
      const phaseName = headerMatch[1].trim().toLowerCase();
      // Extract content after the header (skip the header line and leading newlines)
      const content = section.substring(headerMatch[0].length).trim();
      phaseSummaries.set(phaseName, content);
    }
  }

  return phaseSummaries;
}

/**
 * Extracts a specific phase summary from an accumulated summary string.
 */
function extractPhaseSummary(summary: string | undefined, phaseName: string): string | null {
  const phaseSummaries = parsePhaseSummaries(summary);
  const normalizedPhaseName = phaseName.toLowerCase();
  return phaseSummaries.get(normalizedPhaseName) || null;
}

/**
 * Gets the implementation phase summary from an accumulated summary string.
 */
function extractImplementationSummary(summary: string | undefined): string | null {
  if (!summary || !summary.trim()) {
    return null;
  }

  const phaseSummaries = parsePhaseSummaries(summary);

  // Try exact match first
  const implementationContent = phaseSummaries.get('implementation');
  if (implementationContent) {
    return implementationContent;
  }

  // Fallback: find any phase containing "implement"
  for (const [phaseName, content] of phaseSummaries) {
    if (phaseName.includes('implement')) {
      return content;
    }
  }

  // If no phase summaries found, the summary might not be in accumulated format
  // (legacy or non-pipeline feature). In this case, return the whole summary
  // if it looks like a single summary (no phase headers).
  if (!summary.includes('### ') && !summary.includes('\n---\n')) {
    return summary;
  }

  return null;
}

/**
 * Checks if a summary string is in the accumulated multi-phase format.
 */
function isAccumulatedSummary(summary: string | undefined): boolean {
  if (!summary || !summary.trim()) {
    return false;
  }

  // Check for the presence of phase headers with separator
  const hasMultiplePhases =
    summary.includes('\n\n---\n\n') && summary.match(/###\s+.+/g)?.length > 0;

  return hasMultiplePhases;
}

describe('phase summary parser', () => {
  describe('parsePhaseSummaries', () => {
    it('should parse single phase summary', () => {
      const summary = `### Implementation

Created auth module with login functionality.`;

      const result = parsePhaseSummaries(summary);

      expect(result.size).toBe(1);
      expect(result.get('implementation')).toBe('Created auth module with login functionality.');
    });

    it('should parse multiple phase summaries', () => {
      const summary = `### Implementation

Created auth module.

---

### Testing

All tests pass.

---

### Code Review

Approved with minor suggestions.`;

      const result = parsePhaseSummaries(summary);

      expect(result.size).toBe(3);
      expect(result.get('implementation')).toBe('Created auth module.');
      expect(result.get('testing')).toBe('All tests pass.');
      expect(result.get('code review')).toBe('Approved with minor suggestions.');
    });

    it('should handle empty input', () => {
      expect(parsePhaseSummaries('').size).toBe(0);
      expect(parsePhaseSummaries(undefined).size).toBe(0);
      expect(parsePhaseSummaries('   \n\n   ').size).toBe(0);
    });

    it('should handle phase names with spaces', () => {
      const summary = `### Code Review

Review findings here.`;

      const result = parsePhaseSummaries(summary);
      expect(result.get('code review')).toBe('Review findings here.');
    });

    it('should normalize phase names to lowercase', () => {
      const summary = `### IMPLEMENTATION

Content here.`;

      const result = parsePhaseSummaries(summary);
      expect(result.get('implementation')).toBe('Content here.');
      expect(result.get('IMPLEMENTATION')).toBeUndefined();
    });

    it('should handle content with markdown', () => {
      const summary = `### Implementation

## Changes Made
- Fixed bug in parser.ts
- Added error handling

\`\`\`typescript
const x = 1;
\`\`\``;

      const result = parsePhaseSummaries(summary);
      expect(result.get('implementation')).toContain('## Changes Made');
      expect(result.get('implementation')).toContain('```typescript');
    });

    it('should return empty map for non-accumulated format', () => {
      // Legacy format without phase headers
      const summary = `## Summary

This is a simple summary without phase headers.`;

      const result = parsePhaseSummaries(summary);
      expect(result.size).toBe(0);
    });
  });

  describe('extractPhaseSummary', () => {
    it('should extract specific phase by name (case-insensitive)', () => {
      const summary = `### Implementation

Implementation content.

---

### Testing

Testing content.`;

      expect(extractPhaseSummary(summary, 'implementation')).toBe('Implementation content.');
      expect(extractPhaseSummary(summary, 'IMPLEMENTATION')).toBe('Implementation content.');
      expect(extractPhaseSummary(summary, 'Implementation')).toBe('Implementation content.');
      expect(extractPhaseSummary(summary, 'testing')).toBe('Testing content.');
    });

    it('should return null for non-existent phase', () => {
      const summary = `### Implementation

Content here.`;

      expect(extractPhaseSummary(summary, 'code review')).toBeNull();
    });

    it('should return null for empty input', () => {
      expect(extractPhaseSummary('', 'implementation')).toBeNull();
      expect(extractPhaseSummary(undefined, 'implementation')).toBeNull();
    });
  });

  describe('extractImplementationSummary', () => {
    it('should extract implementation phase from accumulated summary', () => {
      const summary = `### Implementation

Created auth module.

---

### Testing

All tests pass.

---

### Code Review

Approved.`;

      const result = extractImplementationSummary(summary);
      expect(result).toBe('Created auth module.');
      expect(result).not.toContain('Testing');
      expect(result).not.toContain('Code Review');
    });

    it('should return implementation phase even when not first', () => {
      const summary = `### Planning

Plan created.

---

### Implementation

Implemented the feature.

---

### Review

Reviewed.`;

      const result = extractImplementationSummary(summary);
      expect(result).toBe('Implemented the feature.');
    });

    it('should handle phase with "implementation" in name', () => {
      const summary = `### Feature Implementation

Built the feature.`;

      const result = extractImplementationSummary(summary);
      expect(result).toBe('Built the feature.');
    });

    it('should return full summary for non-accumulated format (legacy)', () => {
      // Non-pipeline features store summary without phase headers
      const summary = `## Changes
- Fixed bug
- Added tests`;

      const result = extractImplementationSummary(summary);
      expect(result).toBe(summary);
    });

    it('should return null for empty input', () => {
      expect(extractImplementationSummary('')).toBeNull();
      expect(extractImplementationSummary(undefined)).toBeNull();
      expect(extractImplementationSummary('   \n\n   ')).toBeNull();
    });

    it('should return null when no implementation phase in accumulated summary', () => {
      const summary = `### Testing

Tests written.

---

### Code Review

Approved.`;

      const result = extractImplementationSummary(summary);
      expect(result).toBeNull();
    });
  });

  describe('isAccumulatedSummary', () => {
    it('should return true for accumulated multi-phase summary', () => {
      const summary = `### Implementation

Content.

---

### Testing

Content.`;

      expect(isAccumulatedSummary(summary)).toBe(true);
    });

    it('should return false for single phase summary (no separator)', () => {
      const summary = `### Implementation

Content.`;

      expect(isAccumulatedSummary(summary)).toBe(false);
    });

    it('should return false for legacy non-accumulated format', () => {
      const summary = `## Summary

This is a simple summary.`;

      expect(isAccumulatedSummary(summary)).toBe(false);
    });

    it('should return false for empty input', () => {
      expect(isAccumulatedSummary('')).toBe(false);
      expect(isAccumulatedSummary(undefined)).toBe(false);
      expect(isAccumulatedSummary('   \n\n   ')).toBe(false);
    });

    it('should return true even for two phases', () => {
      const summary = `### Implementation

Content A.

---

### Code Review

Content B.`;

      expect(isAccumulatedSummary(summary)).toBe(true);
    });
  });

  describe('acceptance criteria scenarios', () => {
    it('AC1: Implementation summary preserved when Testing completes', () => {
      // Given a task card completes the Implementation phase,
      // when the Testing phase subsequently completes,
      // then the Implementation phase summary must remain stored independently
      const summary = `### Implementation

- Created auth module
- Added user service

---

### Testing

- 42 tests pass
- 98% coverage`;

      const impl = extractImplementationSummary(summary);
      const testing = extractPhaseSummary(summary, 'testing');

      expect(impl).toBe('- Created auth module\n- Added user service');
      expect(testing).toBe('- 42 tests pass\n- 98% coverage');
      expect(impl).not.toContain('Testing');
      expect(testing).not.toContain('auth module');
    });

    it('AC4: Implementation Summary tab shows only implementation phase', () => {
      // Given a task card has completed the Implementation phase
      // (regardless of how many subsequent phases have run),
      // when the user opens the "Implementation Summary" tab,
      // then it must display only the summary produced by the Implementation phase
      const summary = `### Implementation

Implementation phase output here.

---

### Testing

Testing phase output here.

---

### Code Review

Code review output here.`;

      const impl = extractImplementationSummary(summary);

      expect(impl).toBe('Implementation phase output here.');
      expect(impl).not.toContain('Testing');
      expect(impl).not.toContain('Code Review');
    });

    it('AC5: Empty state when implementation not started', () => {
      // Given a task card has not yet started the Implementation phase
      const summary = `### Planning

Planning phase complete.`;

      const impl = extractImplementationSummary(summary);

      // Should return null (UI shows "No implementation summary available")
      expect(impl).toBeNull();
    });

    it('AC6: Single phase summary displayed correctly', () => {
      // Given a task card where Implementation was the only completed phase
      const summary = `### Implementation

Only implementation was done.`;

      const impl = extractImplementationSummary(summary);

      expect(impl).toBe('Only implementation was done.');
    });

    it('AC9: Mid-progress shows only completed phases', () => {
      // Given a task card is mid-progress
      // (e.g., Implementation and Testing complete, Code Review pending)
      const summary = `### Implementation

Implementation done.

---

### Testing

Testing done.`;

      const phases = parsePhaseSummaries(summary);

      expect(phases.size).toBe(2);
      expect(phases.has('implementation')).toBe(true);
      expect(phases.has('testing')).toBe(true);
      expect(phases.has('code review')).toBe(false);
    });

    it('AC10: All phases in chronological order', () => {
      // Given all phases of a task card are complete
      const summary = `### Implementation

First phase content.

---

### Testing

Second phase content.

---

### Code Review

Third phase content.`;

      // ParsePhaseSummaries should preserve order
      const phases = parsePhaseSummaries(summary);
      const phaseNames = [...phases.keys()];

      expect(phaseNames).toEqual(['implementation', 'testing', 'code review']);
    });

    it('AC17: Retried phase shows only latest', () => {
      // Given a phase was retried, when viewing the Summary tab,
      // only one entry for the retried phase must appear (the latest retry's summary)
      //
      // Note: The server-side FeatureStateManager overwrites the phase summary
      // when the same phase runs again, so we only have one entry per phase name.
      // This test verifies that the parser correctly handles this.
      const summary = `### Implementation

First attempt content.

---

### Testing

First test run.

---

### Implementation

Retry content - fixed issues.

---

### Testing

Retry - all tests now pass.`;

      const phases = parsePhaseSummaries(summary);

      // The parser will have both entries, but Map keeps last value for same key
      expect(phases.get('implementation')).toBe('Retry content - fixed issues.');
      expect(phases.get('testing')).toBe('Retry - all tests now pass.');
    });
  });
});