|
| 1 | +const fs = require('fs-extra'); |
| 2 | +const path = require('path'); |
| 3 | +const os = require('os'); |
| 4 | +const { execSync } = require('child_process'); |
| 5 | + |
| 6 | +let tmpDir; |
| 7 | +let originalCwd; |
| 8 | +let hookPath; |
| 9 | + |
| 10 | +beforeEach(async () => { |
| 11 | + tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'ck-commit-msg-')); |
| 12 | + originalCwd = process.cwd(); |
| 13 | + process.chdir(tmpDir); |
| 14 | + |
| 15 | + // Initialize a real git repo |
| 16 | + execSync('git init', { stdio: 'pipe' }); |
| 17 | + execSync('git config user.email "test@example.com"', { stdio: 'pipe' }); |
| 18 | + execSync('git config user.name "Test User"', { stdio: 'pipe' }); |
| 19 | + |
| 20 | + // Copy the actual commit-msg hook from the project |
| 21 | + hookPath = path.join(tmpDir, '.contextkit', 'hooks'); |
| 22 | + await fs.ensureDir(hookPath); |
| 23 | + const sourceHook = path.join(originalCwd, '.contextkit', 'hooks', 'commit-msg'); |
| 24 | + await fs.copy(sourceHook, path.join(hookPath, 'commit-msg')); |
| 25 | + await fs.chmod(path.join(hookPath, 'commit-msg'), 0o755); |
| 26 | + |
| 27 | + // Set the hook path |
| 28 | + execSync('git config core.hooksPath .contextkit/hooks', { stdio: 'pipe' }); |
| 29 | +}); |
| 30 | + |
| 31 | +afterEach(async () => { |
| 32 | + process.chdir(originalCwd); |
| 33 | + await fs.remove(tmpDir); |
| 34 | +}); |
| 35 | + |
| 36 | +function runCommitMsgHook(message) { |
| 37 | + const msgFile = path.join(tmpDir, '.git', 'COMMIT_EDITMSG'); |
| 38 | + fs.writeFileSync(msgFile, message); |
| 39 | + |
| 40 | + try { |
| 41 | + execSync(path.join(hookPath, 'commit-msg') + ` "${msgFile}"`, { |
| 42 | + stdio: 'pipe', |
| 43 | + encoding: 'utf8' |
| 44 | + }); |
| 45 | + return { success: true, output: '' }; |
| 46 | + } catch (error) { |
| 47 | + return { success: false, output: error.stderr || error.stdout || error.message }; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +describe('commit-msg hook', () => { |
| 52 | + describe('PO Spec: Subject Line Length Check', () => { |
| 53 | + it('1. accepts subject with exactly 10 characters (boundary test)', () => { |
| 54 | + const result = runCommitMsgHook('feat: 12345'); |
| 55 | + expect(result.success).toBe(true); |
| 56 | + }); |
| 57 | + |
| 58 | + it('2. rejects subject with less than 10 characters', () => { |
| 59 | + const result = runCommitMsgHook('feat: 123'); |
| 60 | + expect(result.success).toBe(false); |
| 61 | + expect(result.output).toContain('too short'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('3. accepts valid subject with no body', () => { |
| 65 | + const result = runCommitMsgHook('feat(auth): implement login validation'); |
| 66 | + expect(result.success).toBe(true); |
| 67 | + }); |
| 68 | + |
| 69 | + it('4. accepts valid subject with a long body (body is not measured)', () => { |
| 70 | + const message = `feat(core): add new validation logic |
| 71 | +
|
| 72 | +This is a much longer body that contains detailed implementation notes. |
| 73 | +It goes over many lines and has lots of extra content. |
| 74 | +The length check should only measure the subject line, not this body. |
| 75 | +So even if the body pushes the total message length to hundreds of characters, |
| 76 | +as long as the subject line is at least 10 characters, it should pass.`; |
| 77 | + const result = runCommitMsgHook(message); |
| 78 | + expect(result.success).toBe(true); |
| 79 | + }); |
| 80 | + |
| 81 | + it('5. rejects short subject with a long body (body does not count toward length)', () => { |
| 82 | + const message = `fix: bug |
| 83 | +
|
| 84 | +This is a long body that would normally count toward the total message length. |
| 85 | +But since we fixed the length check to only measure the subject line, |
| 86 | +this should fail because the subject "fix: bug" is only 8 characters.`; |
| 87 | + const result = runCommitMsgHook(message); |
| 88 | + expect(result.success).toBe(false); |
| 89 | + expect(result.output).toContain('too short'); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe('PO Spec: Check Order (Length Before Format)', () => { |
| 94 | + it('6. returns length error before format error for short subject', () => { |
| 95 | + const result = runCommitMsgHook('abc'); |
| 96 | + // Should fail on length check first, not format |
| 97 | + expect(result.success).toBe(false); |
| 98 | + expect(result.output).toContain('too short'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('7. returns format error when subject is long enough but malformed', () => { |
| 102 | + const result = runCommitMsgHook('this is a long subject but no colon'); |
| 103 | + expect(result.success).toBe(false); |
| 104 | + expect(result.output).toContain('conventional format'); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe('PO Spec: Conventional Format Check (Subject Line Only)', () => { |
| 109 | + it('8. accepts feat type with scope', () => { |
| 110 | + const result = runCommitMsgHook('feat(api): add new endpoint'); |
| 111 | + expect(result.success).toBe(true); |
| 112 | + }); |
| 113 | + |
| 114 | + it('9. accepts fix type with scope', () => { |
| 115 | + const result = runCommitMsgHook('fix(hooks): skip merge commits'); |
| 116 | + expect(result.success).toBe(true); |
| 117 | + }); |
| 118 | + |
| 119 | + it('10. accepts docs type without scope', () => { |
| 120 | + const result = runCommitMsgHook('docs: update README'); |
| 121 | + expect(result.success).toBe(true); |
| 122 | + }); |
| 123 | + |
| 124 | + it('11. accepts improve type', () => { |
| 125 | + const result = runCommitMsgHook('improve(cli): better error handling'); |
| 126 | + expect(result.success).toBe(true); |
| 127 | + }); |
| 128 | + |
| 129 | + it('12. accepts refactor, test, chore types', () => { |
| 130 | + const validTypes = [ |
| 131 | + 'refactor(utils): simplify logic', |
| 132 | + 'test(integration): add new test case', |
| 133 | + 'chore(deps): update dependencies' |
| 134 | + ]; |
| 135 | + validTypes.forEach(msg => { |
| 136 | + const result = runCommitMsgHook(msg); |
| 137 | + expect(result.success).toBe(true); |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + it('13. rejects style type (not allowed in this project)', () => { |
| 142 | + const result = runCommitMsgHook('style(format): adjust spacing'); |
| 143 | + expect(result.success).toBe(false); |
| 144 | + expect(result.output).toContain('conventional format'); |
| 145 | + }); |
| 146 | + |
| 147 | + it('14. rejects unknown type', () => { |
| 148 | + const result = runCommitMsgHook('random: some change'); |
| 149 | + expect(result.success).toBe(false); |
| 150 | + expect(result.output).toContain('conventional format'); |
| 151 | + }); |
| 152 | + |
| 153 | + it('15. rejects missing description after colon', () => { |
| 154 | + const result = runCommitMsgHook('feat(api):'); |
| 155 | + expect(result.success).toBe(false); |
| 156 | + expect(result.output).toContain('conventional format'); |
| 157 | + }); |
| 158 | + |
| 159 | + it('16. allows body lines that do not match the pattern', () => { |
| 160 | + // Body line that looks like it could match the pattern should be ignored |
| 161 | + const message = `feat(api): main implementation |
| 162 | +
|
| 163 | +Some random text in the body that says feat: something else. |
| 164 | +This should not cause a format error.`; |
| 165 | + const result = runCommitMsgHook(message); |
| 166 | + expect(result.success).toBe(true); |
| 167 | + }); |
| 168 | + }); |
| 169 | + |
| 170 | + describe('PO Spec: Edge Cases', () => { |
| 171 | + it('17. skips validation for Merge commits', () => { |
| 172 | + const result = runCommitMsgHook('Merge branch main into feature'); |
| 173 | + expect(result.success).toBe(true); |
| 174 | + }); |
| 175 | + |
| 176 | + it('18. skips validation for Revert commits', () => { |
| 177 | + const result = runCommitMsgHook('Revert "some previous commit"'); |
| 178 | + expect(result.success).toBe(true); |
| 179 | + }); |
| 180 | + |
| 181 | + it('19. skips validation for fixup commits', () => { |
| 182 | + const result = runCommitMsgHook('fixup! previous commit message'); |
| 183 | + expect(result.success).toBe(true); |
| 184 | + }); |
| 185 | + |
| 186 | + it('20. skips validation for squash commits', () => { |
| 187 | + const result = runCommitMsgHook('squash! previous commit message'); |
| 188 | + expect(result.success).toBe(true); |
| 189 | + }); |
| 190 | + |
| 191 | + it('21. multi-line message from -m "subject" -m "body" format', () => { |
| 192 | + // When git writes with multiple -m flags, it creates newlines in the file |
| 193 | + const message = `feat(core): new feature here |
| 194 | +
|
| 195 | +Body content on another line.`; |
| 196 | + const result = runCommitMsgHook(message); |
| 197 | + expect(result.success).toBe(true); |
| 198 | + }); |
| 199 | + }); |
| 200 | + |
| 201 | + describe('Additional Integration Tests', () => { |
| 202 | + it('22. message with scope containing parentheses passes', () => { |
| 203 | + const result = runCommitMsgHook('feat(api-v1): add endpoint'); |
| 204 | + expect(result.success).toBe(true); |
| 205 | + }); |
| 206 | + |
| 207 | + it('23. message with special characters in description passes', () => { |
| 208 | + const result = runCommitMsgHook('feat: add feature (with notes)'); |
| 209 | + expect(result.success).toBe(true); |
| 210 | + }); |
| 211 | + |
| 212 | + it('24. exactly 10 character subject with scope and type', () => { |
| 213 | + // "fix(x): ab" = 10 characters |
| 214 | + const result = runCommitMsgHook('fix(x): ab'); |
| 215 | + expect(result.success).toBe(true); |
| 216 | + }); |
| 217 | + |
| 218 | + it('25. exactly 9 character subject should fail', () => { |
| 219 | + // "fix(x): a" = 9 characters |
| 220 | + const result = runCommitMsgHook('fix(x): a'); |
| 221 | + expect(result.success).toBe(false); |
| 222 | + expect(result.output).toContain('too short'); |
| 223 | + }); |
| 224 | + }); |
| 225 | +}); |
0 commit comments