|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { |
| 3 | + parseRemoteHost, |
| 4 | + buildBuiltinRules, |
| 5 | + resolveCommitLinkRules, |
| 6 | + type LinkRule, |
| 7 | +} from '../commit-link-rules'; |
| 8 | + |
| 9 | +describe('parseRemoteHost', () => { |
| 10 | + it('parses ssh shorthand', () => { |
| 11 | + expect(parseRemoteHost('git@github.com:owner/repo.git')).toEqual({ |
| 12 | + host: 'github.com', owner: 'owner', repo: 'repo', |
| 13 | + }); |
| 14 | + }); |
| 15 | + it('parses https with .git', () => { |
| 16 | + expect(parseRemoteHost('https://github.com/owner/repo.git')).toEqual({ |
| 17 | + host: 'github.com', owner: 'owner', repo: 'repo', |
| 18 | + }); |
| 19 | + }); |
| 20 | + it('parses https without .git', () => { |
| 21 | + expect(parseRemoteHost('https://gitlab.com/grp/proj')).toEqual({ |
| 22 | + host: 'gitlab.com', owner: 'grp', repo: 'proj', |
| 23 | + }); |
| 24 | + }); |
| 25 | + it('parses ssh:// url form', () => { |
| 26 | + expect(parseRemoteHost('ssh://git@github.com/owner/repo.git')).toEqual({ |
| 27 | + host: 'github.com', owner: 'owner', repo: 'repo', |
| 28 | + }); |
| 29 | + }); |
| 30 | + it('returns null for malformed url', () => { |
| 31 | + expect(parseRemoteHost('not a url')).toBeNull(); |
| 32 | + expect(parseRemoteHost('')).toBeNull(); |
| 33 | + }); |
| 34 | +}); |
| 35 | + |
| 36 | +describe('buildBuiltinRules', () => { |
| 37 | + it('builds github issue rule plus the always-on MR rule', () => { |
| 38 | + expect(buildBuiltinRules('git@github.com:owner/repo.git')).toEqual<LinkRule[]>([ |
| 39 | + { pattern: '!(\\d+)', url: 'https://github.com/owner/repo/-/merge_requests/$1' }, |
| 40 | + { pattern: '#(\\d+)', url: 'https://github.com/owner/repo/issues/$1' }, |
| 41 | + ]); |
| 42 | + }); |
| 43 | + it('builds gitlab issue + MR rules', () => { |
| 44 | + expect(buildBuiltinRules('https://gitlab.com/grp/proj.git')).toEqual<LinkRule[]>([ |
| 45 | + { pattern: '!(\\d+)', url: 'https://gitlab.com/grp/proj/-/merge_requests/$1' }, |
| 46 | + { pattern: '#(\\d+)', url: 'https://gitlab.com/grp/proj/-/issues/$1' }, |
| 47 | + ]); |
| 48 | + }); |
| 49 | + it('builds only the MR rule for a self-hosted host (! is GitLab-only)', () => { |
| 50 | + expect(buildBuiltinRules('git@git.company.com:owner/repo.git')).toEqual<LinkRule[]>([ |
| 51 | + { pattern: '!(\\d+)', url: 'https://git.company.com/owner/repo/-/merge_requests/$1' }, |
| 52 | + ]); |
| 53 | + }); |
| 54 | + it('returns empty for an unparseable remote', () => { |
| 55 | + expect(buildBuiltinRules('not a url')).toEqual([]); |
| 56 | + }); |
| 57 | + it('returns empty for null remote', () => { |
| 58 | + expect(buildBuiltinRules(null)).toEqual([]); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe('resolveCommitLinkRules', () => { |
| 63 | + const custom = [{ pattern: '([A-Z]+-\\d+)', url: 'https://jira/browse/$1' }]; |
| 64 | + |
| 65 | + it('puts custom rules before built-in rules', () => { |
| 66 | + const rules = resolveCommitLinkRules(custom, true, 'git@github.com:o/r.git'); |
| 67 | + expect(rules).toEqual<LinkRule[]>([ |
| 68 | + { pattern: '([A-Z]+-\\d+)', url: 'https://jira/browse/$1' }, |
| 69 | + { pattern: '!(\\d+)', url: 'https://github.com/o/r/-/merge_requests/$1' }, |
| 70 | + { pattern: '#(\\d+)', url: 'https://github.com/o/r/issues/$1' }, |
| 71 | + ]); |
| 72 | + }); |
| 73 | + it('omits built-in rules when autoDetect is false', () => { |
| 74 | + expect(resolveCommitLinkRules(custom, false, 'git@github.com:o/r.git')).toEqual(custom); |
| 75 | + }); |
| 76 | + it('skips invalid custom entries', () => { |
| 77 | + const raw = [ |
| 78 | + { pattern: '#(\\d+)', url: 'https://x/$1' }, |
| 79 | + { pattern: '(', url: 'https://bad' }, // invalid regex |
| 80 | + { pattern: 123, url: 'https://y' }, // non-string pattern |
| 81 | + { nope: true }, // wrong shape |
| 82 | + ]; |
| 83 | + expect(resolveCommitLinkRules(raw, false, null)).toEqual([ |
| 84 | + { pattern: '#(\\d+)', url: 'https://x/$1' }, |
| 85 | + ]); |
| 86 | + }); |
| 87 | + it('returns empty for empty config and no remote', () => { |
| 88 | + expect(resolveCommitLinkRules([], true, null)).toEqual([]); |
| 89 | + expect(resolveCommitLinkRules(undefined, true, null)).toEqual([]); |
| 90 | + }); |
| 91 | +}); |
0 commit comments