import { describe, it, expect } from 'vitest';
import {
validateWorktreeName,
validateBranchName,
validateAgentId,
validateMessage,
validateLockExpiry,
} from '../utils/validation.js';
describe('Input Validation', () => {
describe('validateWorktreeName', () => {
it('should accept valid worktree names', () => {
expect(validateWorktreeName('feature-api').valid).toBe(true);
expect(validateWorktreeName('fix-123').valid).toBe(true);
expect(validateWorktreeName('agent-1').valid).toBe(true);
expect(validateWorktreeName('feature/new-api').valid).toBe(true);
});
it('should reject empty names', () => {
const result = validateWorktreeName('');
expect(result.valid).toBe(false);
expect(result.error).toContain('empty');
});
it('should reject names with path traversal', () => {
const result = validateWorktreeName('abc..def');
expect(result.valid).toBe(false);
expect(result.error).toContain('path traversal');
});
it('should reject names starting with special characters', () => {
const result = validateWorktreeName('-invalid');
expect(result.valid).toBe(false);
});
it('should reject names with spaces', () => {
const result = validateWorktreeName('my feature');
expect(result.valid).toBe(false);
});
it('should reject overly long names', () => {
const longName = 'a'.repeat(256);
const result = validateWorktreeName(longName);
expect(result.valid).toBe(false);
expect(result.error).toContain('too long');
});
});
describe('validateBranchName', () => {
it('should accept valid branch names', () => {
expect(validateBranchName('main').valid).toBe(true);
expect(validateBranchName('feature/new-api').valid).toBe(true);
expect(validateBranchName('hotfix-123').valid).toBe(true);
});
it('should reject invalid branch names', () => {
expect(validateBranchName('').valid).toBe(false);
expect(validateBranchName('../bad').valid).toBe(false);
});
});
describe('validateAgentId', () => {
it('should accept valid agent IDs', () => {
expect(validateAgentId('agent-1').valid).toBe(true);
expect(validateAgentId('claude-opus').valid).toBe(true);
expect(validateAgentId('GPT4_turbo').valid).toBe(true);
});
it('should reject empty agent IDs', () => {
const result = validateAgentId('');
expect(result.valid).toBe(false);
});
it('should reject agent IDs with special characters', () => {
const result = validateAgentId('agent@123');
expect(result.valid).toBe(false);
});
it('should reject overly long agent IDs', () => {
const longId = 'a'.repeat(101);
const result = validateAgentId(longId);
expect(result.valid).toBe(false);
});
});
describe('validateMessage', () => {
it('should accept valid messages', () => {
expect(validateMessage('Working on API').valid).toBe(true);
expect(validateMessage(undefined).valid).toBe(true);
});
it('should reject overly long messages', () => {
const longMessage = 'a'.repeat(501);
const result = validateMessage(longMessage);
expect(result.valid).toBe(false);
});
});
describe('validateLockExpiry', () => {
it('should accept valid expiry times', () => {
expect(validateLockExpiry(60).valid).toBe(true);
expect(validateLockExpiry(120).valid).toBe(true);
expect(validateLockExpiry(undefined).valid).toBe(true);
});
it('should reject negative values', () => {
const result = validateLockExpiry(-1);
expect(result.valid).toBe(false);
});
it('should reject zero', () => {
const result = validateLockExpiry(0);
expect(result.valid).toBe(false);
});
it('should reject expiry times over 7 days', () => {
const result = validateLockExpiry(10081);
expect(result.valid).toBe(false);
});
it('should reject decimal values', () => {
const result = validateLockExpiry(60.5);
expect(result.valid).toBe(false);
});
});
});