import { TextTrimmer } from './TextTrimmer.js';
describe('TextTrimmer', () => {
describe('Should_TrimText_When_ExceedsMaxChars', () => {
it('trims text longer than max chars', () => {
const text = 'This is a very long text that should be trimmed';
const maxChars = 20;
const result = TextTrimmer.trim(text, maxChars);
expect(result).toBe('This is a very l...');
expect(result.length).toBe(maxChars);
});
it('returns original text when within limit', () => {
const text = 'Short text';
const maxChars = 20;
const result = TextTrimmer.trim(text, maxChars);
expect(result).toBe('Short text');
});
it('returns ellipsis when maxChars is very small', () => {
const text = 'Long text';
const maxChars = 2;
const result = TextTrimmer.trim(text, maxChars);
expect(result).toBe('..');
expect(result.length).toBe(maxChars);
});
});
describe('Should_TrimAtWordBoundary_When_ExceedsMaxChars', () => {
it('trims at word boundary when possible', () => {
const text = 'This is a very long text that should be trimmed';
const maxChars = 20;
const result = TextTrimmer.trimAtWordBoundary(text, maxChars);
expect(result).toBe('This is a very...');
expect(result.length).toBeLessThanOrEqual(maxChars);
});
it('falls back to character trim when no word boundary', () => {
const text = 'Thisisaverylongtextwithoutspaces';
const maxChars = 20;
const result = TextTrimmer.trimAtWordBoundary(text, maxChars);
expect(result).toBe('Thisisaverylong...');
expect(result.length).toBe(maxChars);
});
});
describe('Should_ThrowError_When_InvalidMaxChars', () => {
it('throws error for zero maxChars', () => {
expect(() => TextTrimmer.trim('text', 0)).toThrow('maxChars must be greater than 0');
});
it('throws error for negative maxChars', () => {
expect(() => TextTrimmer.trim('text', -1)).toThrow('maxChars must be greater than 0');
});
});
});