import { describe, it, expect } from 'vitest'; import { normalizeSubject, normalizeMessage } from '../normalize-commit-msg.js'; describe('normalizeSubject', () => { it('passes an already-valid subject unchanged', () => { const { subject, warned } = normalizeSubject('Add feature flag support'); expect(subject).toBe('Add feature flag support'); expect(warned).toBe(false); }); it('capitalizes the first letter', () => { const { subject } = normalizeSubject('add feature flag support'); expect(subject).toBe('Add feature flag support'); }); it('strips a trailing period', () => { const { subject } = normalizeSubject('Add feature flag support.'); expect(subject).toBe('Add feature flag support'); }); it('trims leading whitespace', () => { const { subject } = normalizeSubject(' Fix the bug'); expect(subject).toBe('Fix the bug'); }); it('trims trailing whitespace', () => { const { subject } = normalizeSubject('Fix the bug '); expect(subject).toBe('Fix the bug'); }); it('capitalizes and strips period together', () => { const { subject } = normalizeSubject('fix the bug.'); expect(subject).toBe('Fix the bug'); }); it('does not strip a period that is not trailing', () => { const { subject } = normalizeSubject('Fix bug in v1.0 release'); expect(subject).toBe('Fix bug in v1.0 release'); }); it('warns when subject exceeds 72 characters', () => { const long = 'A'.repeat(73); const { warned } = normalizeSubject(long); expect(warned).toBe(true); }); it('does not warn when subject is exactly 72 characters', () => { const exact = 'A'.repeat(72); const { warned } = normalizeSubject(exact); expect(warned).toBe(false); }); it('does not warn when subject is under 72 characters', () => { const { warned } = normalizeSubject('Short message'); expect(warned).toBe(false); }); it('handles an empty subject gracefully', () => { const { subject, warned } = normalizeSubject(''); expect(subject).toBe(''); expect(warned).toBe(false); }); }); describe('normalizeMessage', () => { it('normalizes only the subject line of a multi-line message', () => { const input = 'fix the bug.\n\nThis is the body paragraph.'; const { message } = normalizeMessage(input); expect(message).toBe('Fix the bug\n\nThis is the body paragraph.'); }); it('skips comment lines when finding the subject', () => { const input = '# Comment\nfix the bug.'; const { message } = normalizeMessage(input); expect(message).toBe('# Comment\nFix the bug'); }); it('returns warned true for long subject inside full message', () => { const longSubject = 'x'.repeat(73); const input = `${longSubject}\n\nBody.`; const { warned } = normalizeMessage(input); expect(warned).toBe(true); }); it('preserves body lines exactly as-is', () => { const input = 'Fix bug\n\n - detail one\n - detail two.'; const { message } = normalizeMessage(input); expect(message).toBe('Fix bug\n\n - detail one\n - detail two.'); }); });