|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Test script to verify the regex pattern handles all changeset types |
| 5 | + */ |
| 6 | + |
| 7 | +// Test cases for different changeset formats |
| 8 | +const testCases = [ |
| 9 | + { |
| 10 | + name: 'Patch Changes with commit hash', |
| 11 | + body: `### Patch Changes |
| 12 | +
|
| 13 | +- abc1234: Fix bug in authentication module`, |
| 14 | + expected: { |
| 15 | + changeType: 'Patch', |
| 16 | + commitHash: 'abc1234', |
| 17 | + description: 'Fix bug in authentication module', |
| 18 | + }, |
| 19 | + }, |
| 20 | + { |
| 21 | + name: 'Minor Changes with commit hash', |
| 22 | + body: `### Minor Changes |
| 23 | +
|
| 24 | +- 2bcef5f: Add support for google/gemini-3-pro model alias |
| 25 | + - Added 'google/gemini-3-pro' as an alias to 'gemini-3-pro-preview' |
| 26 | + - Updated README.md with Google Gemini usage examples`, |
| 27 | + expected: { |
| 28 | + changeType: 'Minor', |
| 29 | + commitHash: '2bcef5f', |
| 30 | + description: `Add support for google/gemini-3-pro model alias |
| 31 | + - Added 'google/gemini-3-pro' as an alias to 'gemini-3-pro-preview' |
| 32 | + - Updated README.md with Google Gemini usage examples`, |
| 33 | + }, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: 'Major Changes with commit hash', |
| 37 | + body: `### Major Changes |
| 38 | +
|
| 39 | +- def5678: Breaking API changes for v2.0`, |
| 40 | + expected: { |
| 41 | + changeType: 'Major', |
| 42 | + commitHash: 'def5678', |
| 43 | + description: 'Breaking API changes for v2.0', |
| 44 | + }, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: 'Patch Changes without commit hash', |
| 48 | + body: `### Patch Changes |
| 49 | +
|
| 50 | +- Small typo fix in documentation`, |
| 51 | + expected: { |
| 52 | + changeType: 'Patch', |
| 53 | + commitHash: null, |
| 54 | + description: 'Small typo fix in documentation', |
| 55 | + }, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: 'Minor Changes without commit hash', |
| 59 | + body: `### Minor Changes |
| 60 | +
|
| 61 | +- Added new feature for user preferences`, |
| 62 | + expected: { |
| 63 | + changeType: 'Minor', |
| 64 | + commitHash: null, |
| 65 | + description: 'Added new feature for user preferences', |
| 66 | + }, |
| 67 | + }, |
| 68 | +]; |
| 69 | + |
| 70 | +// The updated regex pattern from the fix |
| 71 | +const changesPattern = |
| 72 | + /### (Major|Minor|Patch) Changes\s*\n\s*-\s+(?:([a-f0-9]+):\s+)?(.+?)$/s; |
| 73 | + |
| 74 | +console.log('🧪 Testing regex pattern for changeset detection\n'); |
| 75 | + |
| 76 | +let passedTests = 0; |
| 77 | +let failedTests = 0; |
| 78 | + |
| 79 | +for (const testCase of testCases) { |
| 80 | + console.log(`Testing: ${testCase.name}`); |
| 81 | + |
| 82 | + const changesMatch = testCase.body.match(changesPattern); |
| 83 | + |
| 84 | + if (!changesMatch) { |
| 85 | + console.log(` ❌ FAILED: Pattern did not match`); |
| 86 | + failedTests++; |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + const [, changeType, commitHash, rawDescription] = changesMatch; |
| 91 | + |
| 92 | + // Handle case where commit hash might be in the description |
| 93 | + let finalCommitHash = commitHash; |
| 94 | + let finalDescription = rawDescription; |
| 95 | + |
| 96 | + if (!finalCommitHash && rawDescription) { |
| 97 | + const descWithHashMatch = rawDescription.match(/^([a-f0-9]+):\s+(.+)$/s); |
| 98 | + if (descWithHashMatch) { |
| 99 | + [, finalCommitHash, finalDescription] = descWithHashMatch; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Verify results |
| 104 | + const errors = []; |
| 105 | + |
| 106 | + if (changeType !== testCase.expected.changeType) { |
| 107 | + errors.push( |
| 108 | + `changeType: expected "${testCase.expected.changeType}", got "${changeType}"` |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + // Compare commit hash (handle null vs undefined) |
| 113 | + const expectedHash = testCase.expected.commitHash; |
| 114 | + if ((finalCommitHash || null) !== (expectedHash || null)) { |
| 115 | + errors.push( |
| 116 | + `commitHash: expected "${expectedHash}", got "${finalCommitHash}"` |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + if (finalDescription !== testCase.expected.description) { |
| 121 | + errors.push( |
| 122 | + `description: expected "${testCase.expected.description}", got "${finalDescription}"` |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + if (errors.length > 0) { |
| 127 | + console.log(` ❌ FAILED:`); |
| 128 | + errors.forEach((error) => console.log(` - ${error}`)); |
| 129 | + failedTests++; |
| 130 | + } else { |
| 131 | + console.log(` ✅ PASSED`); |
| 132 | + passedTests++; |
| 133 | + } |
| 134 | + |
| 135 | + console.log(); |
| 136 | +} |
| 137 | + |
| 138 | +console.log('━'.repeat(60)); |
| 139 | +console.log(`Results: ${passedTests} passed, ${failedTests} failed`); |
| 140 | + |
| 141 | +if (failedTests > 0) { |
| 142 | + process.exit(1); |
| 143 | +} |
0 commit comments