GitNexus/gitnexus/test/unit/git-ref.test.ts
Gergo Magyar 56009bc4b3 fix(analyze): escape hidden checkout names in the detect-reject warning
Keep the rejected ref visible in onLog without replaying bidi or quote characters, and document that sanitizeDetectedBranch rethrows unexpected errors.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-18 06:24:44 +00:00

46 lines
1.9 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import {
InvalidBranchError,
formatRejectedBranchForLog,
sanitizeDetectedBranch,
validateBranchName,
} from '../../src/core/git-ref.js';
describe('core/git-ref', () => {
it('throws InvalidBranchError with name "InvalidBranchError"', () => {
expect(() => validateBranchName('HEAD', 'src')).toThrow(InvalidBranchError);
try {
validateBranchName('HEAD', 'src');
throw new Error('expected InvalidBranchError');
} catch (err) {
expect(err).toBeInstanceOf(InvalidBranchError);
expect((err as Error).name).toBe('InvalidBranchError');
}
});
it('sanitizeDetectedBranch returns the trimmed name for a legal branch', () => {
expect(sanitizeDetectedBranch('develop')).toBe('develop');
expect(sanitizeDetectedBranch(' feature/foo-bar ')).toBe('feature/foo-bar');
});
it('sanitizeDetectedBranch returns undefined for null, empty, or whitespace', () => {
expect(sanitizeDetectedBranch(null)).toBeUndefined();
expect(sanitizeDetectedBranch(undefined)).toBeUndefined();
expect(sanitizeDetectedBranch('')).toBeUndefined();
expect(sanitizeDetectedBranch(' ')).toBeUndefined();
});
it('sanitizeDetectedBranch swallows InvalidBranchError and does not throw', () => {
expect(sanitizeDetectedBranch('feat`x')).toBeUndefined();
expect(sanitizeDetectedBranch('main`evil')).toBeUndefined();
expect(sanitizeDetectedBranch('HEAD')).toBeUndefined();
expect(() => sanitizeDetectedBranch('feat`x')).not.toThrow();
});
it('formatRejectedBranchForLog keeps backticks visible and escapes bidi and quotes', () => {
expect(formatRejectedBranchForLog('feat`x')).toBe('feat`x');
expect(formatRejectedBranchForLog('a"b')).toBe('a\\"b');
expect(formatRejectedBranchForLog(`ok${'\u202e'}bad`)).toBe('ok\\u202ebad');
expect(formatRejectedBranchForLog(`zw${'\u200b'}sp`)).toBe('zw\\u200bsp');
});
});