136 lines
4.2 KiB
TypeScript
136 lines
4.2 KiB
TypeScript
|
|
/**
|
||
|
|
* Validation Utils Tests
|
||
|
|
*/
|
||
|
|
import { describe, it, expect } from 'vitest';
|
||
|
|
import { validateName, validateIdParam, validateSettingsKey, validateSettingsValue, MAX_NAME_LENGTH, MAX_ID_LENGTH } from '../src/utils/validation.js';
|
||
|
|
|
||
|
|
describe('Validation Utils', () => {
|
||
|
|
describe('validateName', () => {
|
||
|
|
it('should accept a valid name', () => {
|
||
|
|
expect(validateName('Test Project')).toBeNull();
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should reject undefined', () => {
|
||
|
|
expect(validateName(undefined)).toContain('required');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should reject null', () => {
|
||
|
|
expect(validateName(null)).toContain('required');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should reject non-string types', () => {
|
||
|
|
expect(validateName(123)).toContain('required');
|
||
|
|
expect(validateName(true)).toContain('required');
|
||
|
|
expect(validateName({})).toContain('required');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should reject empty string', () => {
|
||
|
|
expect(validateName('')).toContain('empty');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should reject whitespace-only string', () => {
|
||
|
|
expect(validateName(' ')).toContain('empty');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should reject string exceeding max length', () => {
|
||
|
|
const longName = 'a'.repeat(MAX_NAME_LENGTH + 1);
|
||
|
|
expect(validateName(longName)).toContain('at most');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should accept string at exactly max length', () => {
|
||
|
|
const maxName = 'a'.repeat(MAX_NAME_LENGTH);
|
||
|
|
expect(validateName(maxName)).toBeNull();
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should use custom field name in error', () => {
|
||
|
|
const err = validateName(undefined, 'project');
|
||
|
|
expect(err).toContain('Project');
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('validateIdParam', () => {
|
||
|
|
it('should accept a valid alphanumeric ID', () => {
|
||
|
|
expect(validateIdParam('abc123')).toBeNull();
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should accept ID with hyphens and underscores', () => {
|
||
|
|
expect(validateIdParam('my-id_123')).toBeNull();
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should reject undefined', () => {
|
||
|
|
expect(validateIdParam(undefined)).toContain('required');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should reject null', () => {
|
||
|
|
expect(validateIdParam(null)).toContain('required');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should reject empty string', () => {
|
||
|
|
expect(validateIdParam('')).toContain('empty');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should reject ID with special characters', () => {
|
||
|
|
expect(validateIdParam('abc!def')).toContain('invalid');
|
||
|
|
expect(validateIdParam('abc def')).toContain('invalid');
|
||
|
|
expect(validateIdParam('abc/def')).toContain('invalid');
|
||
|
|
expect(validateIdParam('abc.def')).toContain('invalid');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should reject ID exceeding max length', () => {
|
||
|
|
const longId = 'a'.repeat(MAX_ID_LENGTH + 1);
|
||
|
|
expect(validateIdParam(longId)).toContain('too long');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should accept ID at exactly max length', () => {
|
||
|
|
const maxId = 'a'.repeat(MAX_ID_LENGTH);
|
||
|
|
expect(validateIdParam(maxId)).toBeNull();
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('validateSettingsKey', () => {
|
||
|
|
it('should accept a valid key', () => {
|
||
|
|
expect(validateSettingsKey('theme.color')).toBeNull();
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should accept key with dots, hyphens, underscores', () => {
|
||
|
|
expect(validateSettingsKey('my-setting_key.value')).toBeNull();
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should reject undefined', () => {
|
||
|
|
expect(validateSettingsKey(undefined)).toContain('required');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should reject empty string', () => {
|
||
|
|
expect(validateSettingsKey('')).toContain('required');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should reject key with special characters', () => {
|
||
|
|
expect(validateSettingsKey('abc!def')).toContain('invalid');
|
||
|
|
expect(validateSettingsKey('abc def')).toContain('invalid');
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('validateSettingsValue', () => {
|
||
|
|
it('should accept a valid string value', () => {
|
||
|
|
expect(validateSettingsValue('some value')).toBeNull();
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should accept empty string value', () => {
|
||
|
|
expect(validateSettingsValue('')).toBeNull();
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should reject undefined', () => {
|
||
|
|
expect(validateSettingsValue(undefined)).toContain('required');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should reject null', () => {
|
||
|
|
expect(validateSettingsValue(null)).toContain('required');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('should reject non-string types', () => {
|
||
|
|
expect(validateSettingsValue(123)).toContain('string');
|
||
|
|
expect(validateSettingsValue(true)).toContain('string');
|
||
|
|
});
|
||
|
|
});
|
||
|
|
});
|