179 lines
5.5 KiB
TypeScript
179 lines
5.5 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import {
|
|
generateEventId,
|
|
now,
|
|
uuid,
|
|
isFunction,
|
|
isString,
|
|
isObject,
|
|
safeGet,
|
|
truncate,
|
|
matchPatterns,
|
|
} from '../src/utils/helper';
|
|
|
|
describe('helper utils', () => {
|
|
describe('generateEventId', () => {
|
|
it('should generate 32 character hex string', () => {
|
|
const id = generateEventId();
|
|
expect(id).toMatch(/^[a-f0-9]{32}$/i);
|
|
});
|
|
|
|
it('should generate unique IDs', () => {
|
|
const id1 = generateEventId();
|
|
const id2 = generateEventId();
|
|
expect(id1).not.toBe(id2);
|
|
});
|
|
});
|
|
|
|
describe('now', () => {
|
|
it('should return current timestamp in milliseconds', () => {
|
|
const before = Date.now();
|
|
const timestamp = now();
|
|
const after = Date.now();
|
|
expect(timestamp).toBeGreaterThanOrEqual(before);
|
|
expect(timestamp).toBeLessThanOrEqual(after);
|
|
});
|
|
});
|
|
|
|
describe('uuid', () => {
|
|
it('should generate 32 character hex string (UUID without hyphens)', () => {
|
|
const id = uuid();
|
|
expect(id).toMatch(/^[a-f0-9]{32}$/i);
|
|
});
|
|
|
|
it('should generate unique UUIDs', () => {
|
|
const id1 = uuid();
|
|
const id2 = uuid();
|
|
expect(id1).not.toBe(id2);
|
|
});
|
|
});
|
|
|
|
describe('isFunction', () => {
|
|
it('should return true for functions', () => {
|
|
expect(isFunction(() => {})).toBe(true);
|
|
expect(isFunction(async () => {})).toBe(true);
|
|
expect(isFunction(function() {})).toBe(true);
|
|
});
|
|
|
|
it('should return false for non-functions', () => {
|
|
expect(isFunction('string')).toBe(false);
|
|
expect(isFunction(123)).toBe(false);
|
|
expect(isFunction(null)).toBe(false);
|
|
expect(isFunction(undefined)).toBe(false);
|
|
expect(isFunction({})).toBe(false);
|
|
expect(isFunction([])).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('isString', () => {
|
|
it('should return true for strings', () => {
|
|
expect(isString('')).toBe(true);
|
|
expect(isString('hello')).toBe(true);
|
|
expect(isString(String(123))).toBe(true);
|
|
});
|
|
|
|
it('should return false for non-strings', () => {
|
|
expect(isString(123)).toBe(false);
|
|
expect(isString(null)).toBe(false);
|
|
expect(isString(undefined)).toBe(false);
|
|
expect(isString({})).toBe(false);
|
|
expect(isString([])).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('isObject', () => {
|
|
it('should return true for objects', () => {
|
|
expect(isObject({})).toBe(true);
|
|
expect(isObject({ a: 1 })).toBe(true);
|
|
expect(isObject(new Object())).toBe(true);
|
|
});
|
|
|
|
it('should return false for non-objects', () => {
|
|
expect(isObject(null)).toBe(false);
|
|
expect(isObject([])).toBe(false);
|
|
expect(isObject('string')).toBe(false);
|
|
expect(isObject(123)).toBe(false);
|
|
expect(isObject(undefined)).toBe(false);
|
|
expect(isObject(() => {})).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('safeGet', () => {
|
|
it('should return result of function when successful', () => {
|
|
const result = safeGet(() => 42, 0);
|
|
expect(result).toBe(42);
|
|
});
|
|
|
|
it('should return default value when function throws', () => {
|
|
const result = safeGet(() => { throw new Error('test'); }, 'default');
|
|
expect(result).toBe('default');
|
|
});
|
|
|
|
it('should return default value when function returns undefined', () => {
|
|
const result = safeGet(() => undefined, 'default');
|
|
expect(result).toBe('default');
|
|
});
|
|
|
|
it('should return null when function returns null (null is a valid value)', () => {
|
|
const result = safeGet(() => null, 'default');
|
|
expect(result).toBeNull();
|
|
});
|
|
|
|
it('should allow falsy values that are not undefined', () => {
|
|
expect(safeGet(() => 0, 100)).toBe(0);
|
|
expect(safeGet(() => '', 'default')).toBe('');
|
|
expect(safeGet(() => false, true)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('truncate', () => {
|
|
it('should return original string if shorter than max length', () => {
|
|
expect(truncate('hello', 10)).toBe('hello');
|
|
expect(truncate('hello', 5)).toBe('hello');
|
|
});
|
|
|
|
it('should truncate string longer than max length', () => {
|
|
expect(truncate('hello world', 5)).toBe('hello...');
|
|
});
|
|
|
|
it('should handle empty string', () => {
|
|
expect(truncate('', 10)).toBe('');
|
|
});
|
|
|
|
it('should handle exact length', () => {
|
|
expect(truncate('hello', 5)).toBe('hello');
|
|
});
|
|
});
|
|
|
|
describe('matchPatterns', () => {
|
|
it('should return false for empty patterns', () => {
|
|
expect(matchPatterns('test', [])).toBe(false);
|
|
expect(matchPatterns('test', undefined as any)).toBe(false);
|
|
});
|
|
|
|
it('should match string patterns', () => {
|
|
expect(matchPatterns('error: something went wrong', ['error'])).toBe(true);
|
|
expect(matchPatterns('something went wrong', ['error'])).toBe(false);
|
|
});
|
|
|
|
it('should match multiple string patterns (OR)', () => {
|
|
expect(matchPatterns('warning: low memory', ['error', 'warning'])).toBe(true);
|
|
expect(matchPatterns('info: normal', ['error', 'warning'])).toBe(false);
|
|
});
|
|
|
|
it('should match RegExp patterns', () => {
|
|
expect(matchPatterns('Error: validation failed', [/Error/])).toBe(true);
|
|
expect(matchPatterns('error: validation failed', [/validation/])).toBe(true);
|
|
});
|
|
|
|
it('should match combined string and RegExp patterns', () => {
|
|
expect(matchPatterns('WARNING: high CPU', ['error', /WARNING/])).toBe(true);
|
|
});
|
|
|
|
it('should handle special regex characters in string patterns', () => {
|
|
expect(matchPatterns('test.file.txt', ['test.file'])).toBe(true);
|
|
expect(matchPatterns('test123', ['test(123)'])).toBe(false);
|
|
});
|
|
});
|
|
});
|