|
| 1 | +/** |
| 2 | + * Utility functions for Acceptance Criteria change tracking |
| 3 | + */ |
| 4 | + |
| 5 | +import { DevlogNote } from '../types/index.js'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Compare two arrays of acceptance criteria and determine what changed |
| 9 | + */ |
| 10 | +export function compareAcceptanceCriteria( |
| 11 | + previous: string[], |
| 12 | + current: string[], |
| 13 | +): { |
| 14 | + changeType: 'added' | 'removed' | 'modified' | 'reordered'; |
| 15 | + addedItems: string[]; |
| 16 | + removedItems: string[]; |
| 17 | + modifiedItems: Array<{ old: string; new: string }>; |
| 18 | +} { |
| 19 | + const prevSet = new Set(previous); |
| 20 | + const currSet = new Set(current); |
| 21 | + |
| 22 | + const addedItems = current.filter((item) => !prevSet.has(item)); |
| 23 | + const removedItems = previous.filter((item) => !currSet.has(item)); |
| 24 | + |
| 25 | + // For simplicity, we'll treat any differences as modifications |
| 26 | + // More sophisticated diff logic could be added later |
| 27 | + const modifiedItems: Array<{ old: string; new: string }> = []; |
| 28 | + |
| 29 | + // Determine primary change type |
| 30 | + let changeType: 'added' | 'removed' | 'modified' | 'reordered'; |
| 31 | + if (addedItems.length > 0 && removedItems.length === 0) { |
| 32 | + changeType = 'added'; |
| 33 | + } else if (removedItems.length > 0 && addedItems.length === 0) { |
| 34 | + changeType = 'removed'; |
| 35 | + } else if (addedItems.length > 0 || removedItems.length > 0) { |
| 36 | + changeType = 'modified'; |
| 37 | + } else if (JSON.stringify(previous) !== JSON.stringify(current)) { |
| 38 | + changeType = 'reordered'; |
| 39 | + } else { |
| 40 | + changeType = 'modified'; // fallback |
| 41 | + } |
| 42 | + |
| 43 | + return { |
| 44 | + changeType, |
| 45 | + addedItems, |
| 46 | + removedItems, |
| 47 | + modifiedItems, |
| 48 | + }; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Create a formatted note content for acceptance criteria changes |
| 53 | + */ |
| 54 | +export function createAcceptanceCriteriaChangeNote( |
| 55 | + previous: string[], |
| 56 | + current: string[], |
| 57 | + changeReason?: string, |
| 58 | +): { content: string; metadata: DevlogNote['metadata'] } { |
| 59 | + const comparison = compareAcceptanceCriteria(previous, current); |
| 60 | + |
| 61 | + let content = '**Acceptance Criteria Updated**\n\n'; |
| 62 | + |
| 63 | + if (changeReason) { |
| 64 | + content += `**Reason:** ${changeReason}\n\n`; |
| 65 | + } |
| 66 | + |
| 67 | + if (comparison.addedItems.length > 0) { |
| 68 | + content += '**Added:**\n'; |
| 69 | + comparison.addedItems.forEach((item) => { |
| 70 | + content += `+ ${item}\n`; |
| 71 | + }); |
| 72 | + content += '\n'; |
| 73 | + } |
| 74 | + |
| 75 | + if (comparison.removedItems.length > 0) { |
| 76 | + content += '**Removed:**\n'; |
| 77 | + comparison.removedItems.forEach((item) => { |
| 78 | + content += `- ${item}\n`; |
| 79 | + }); |
| 80 | + content += '\n'; |
| 81 | + } |
| 82 | + |
| 83 | + if (comparison.changeType === 'reordered') { |
| 84 | + content += '**Reordered acceptance criteria**\n\n'; |
| 85 | + } |
| 86 | + |
| 87 | + content += '**Current Acceptance Criteria:**\n'; |
| 88 | + current.forEach((item, index) => { |
| 89 | + content += `${index + 1}. ${item}\n`; |
| 90 | + }); |
| 91 | + |
| 92 | + const metadata: DevlogNote['metadata'] = { |
| 93 | + previousCriteria: previous, |
| 94 | + newCriteria: current, |
| 95 | + changeType: comparison.changeType, |
| 96 | + }; |
| 97 | + |
| 98 | + return { content, metadata }; |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Helper to create a complete DevlogNote for AC changes |
| 103 | + */ |
| 104 | +export function createAcceptanceCriteriaNote( |
| 105 | + previous: string[], |
| 106 | + current: string[], |
| 107 | + changeReason?: string, |
| 108 | + changedBy?: string, |
| 109 | +): Omit<DevlogNote, 'id' | 'timestamp'> { |
| 110 | + const { content, metadata } = createAcceptanceCriteriaChangeNote(previous, current, changeReason); |
| 111 | + |
| 112 | + return { |
| 113 | + category: 'acceptance-criteria', |
| 114 | + content, |
| 115 | + metadata, |
| 116 | + }; |
| 117 | +} |
0 commit comments