import { describe, it, expect } from 'bun:test';
import {
sanitizeText,
truncateText,
extractDomain,
searchInText,
highlightMatches,
} from '@/utils/text.js';
describe('text utilities', () => {
describe('sanitizeText', () => {
it('should normalize whitespace', () => {
expect(sanitizeText(' hello world ')).toBe('hello world');
expect(sanitizeText('line1\n\n\nline2')).toBe('line1\nline2');
});
it('should trim leading and trailing whitespace', () => {
expect(sanitizeText(' text ')).toBe('text');
});
});
describe('truncateText', () => {
it('should truncate long text', () => {
const longText = 'this is a very long text that needs to be truncated';
expect(truncateText(longText, 20)).toBe('this is a very lo...');
});
it('should not truncate short text', () => {
expect(truncateText('short', 20)).toBe('short');
});
it('should handle exact length', () => {
expect(truncateText('exactly20characters!', 20)).toBe('exactly20characters!');
});
});
describe('extractDomain', () => {
it('should extract domain from url', () => {
expect(extractDomain('https://docs.example.com/path')).toBe('docs.example.com');
expect(extractDomain('http://example.com:8080/api')).toBe('example.com');
});
it('should return empty string for invalid urls', () => {
expect(extractDomain('not-a-url')).toBe('');
expect(extractDomain('')).toBe('');
});
});
describe('searchInText', () => {
it('should find matches case-insensitively', () => {
expect(searchInText('Hello World', 'hello')).toBe(true);
expect(searchInText('Hello World', 'WORLD')).toBe(true);
});
it('should return false for no matches', () => {
expect(searchInText('Hello World', 'goodbye')).toBe(false);
});
});
describe('highlightMatches', () => {
it('should highlight matches with markdown bold', () => {
expect(highlightMatches('Hello world', 'world')).toBe('Hello **world**');
expect(highlightMatches('Hello World', 'WORLD')).toBe('Hello **World**');
});
it('should handle multiple matches', () => {
expect(highlightMatches('test test test', 'test')).toBe('**test** **test** **test**');
});
it('should return original text if no matches', () => {
expect(highlightMatches('Hello world', 'goodbye')).toBe('Hello world');
});
});
});