/**
* Tests for spelling tools
*/
import {
spellWord,
charAt,
nthCharacter,
wordLength,
reverseText,
} from '../src/tools/spelling.js';
describe('spellWord', () => {
it('should break word into characters', () => {
const result = spellWord({ text: 'cat', include_indices: false });
expect(result.characters).toEqual(['c', 'a', 't']);
expect(result.length).toBe(3);
});
it('should include indices when requested', () => {
const result = spellWord({ text: 'cat', include_indices: true });
expect(result.indexed_characters).toEqual([
{ index: 0, char: 'c' },
{ index: 1, char: 'a' },
{ index: 2, char: 't' },
]);
});
it('should handle unicode characters correctly', () => {
const result = spellWord({ text: '😀ab', include_indices: false });
expect(result.characters).toEqual(['😀', 'a', 'b']);
expect(result.length).toBe(3);
});
it('should format spelled_out string without indices', () => {
const result = spellWord({ text: 'hi', include_indices: false });
expect(result.spelled_out).toBe("'h', 'i'");
});
it('should format spelled_out string with indices', () => {
const result = spellWord({ text: 'hi', include_indices: true });
expect(result.spelled_out).toBe("0:'h', 1:'i'");
});
});
describe('charAt', () => {
describe('positive indices', () => {
it('should return character at index 0', () => {
const result = charAt({ text: 'hello', index: 0 });
expect(result.character).toBe('h');
expect(result.valid).toBe(true);
});
it('should return character at middle index', () => {
const result = charAt({ text: 'hello', index: 2 });
expect(result.character).toBe('l');
expect(result.valid).toBe(true);
});
it('should return character at last index', () => {
const result = charAt({ text: 'hello', index: 4 });
expect(result.character).toBe('o');
expect(result.valid).toBe(true);
});
it('should return error for out of bounds index', () => {
const result = charAt({ text: 'hello', index: 10 });
expect(result.character).toBeNull();
expect(result.valid).toBe(false);
expect(result.error).toBeDefined();
});
});
describe('negative indices', () => {
it('should return last character with -1', () => {
const result = charAt({ text: 'hello', index: -1 });
expect(result.character).toBe('o');
expect(result.valid).toBe(true);
});
it('should return first character with -length', () => {
const result = charAt({ text: 'hello', index: -5 });
expect(result.character).toBe('h');
expect(result.valid).toBe(true);
});
it('should return error for too negative index', () => {
const result = charAt({ text: 'hello', index: -10 });
expect(result.character).toBeNull();
expect(result.valid).toBe(false);
});
});
});
describe('nthCharacter', () => {
describe('from start', () => {
it('should return 1st character', () => {
const result = nthCharacter({ text: 'hello', position: 1, from_end: false });
expect(result.character).toBe('h');
expect(result.valid).toBe(true);
expect(result.description).toContain('1st');
});
it('should return 2nd character', () => {
const result = nthCharacter({ text: 'hello', position: 2, from_end: false });
expect(result.character).toBe('e');
expect(result.valid).toBe(true);
expect(result.description).toContain('2nd');
});
it('should return 3rd character', () => {
const result = nthCharacter({ text: 'hello', position: 3, from_end: false });
expect(result.character).toBe('l');
expect(result.valid).toBe(true);
expect(result.description).toContain('3rd');
});
it('should return 5th character', () => {
const result = nthCharacter({ text: 'hello', position: 5, from_end: false });
expect(result.character).toBe('o');
expect(result.valid).toBe(true);
expect(result.description).toContain('5th');
});
});
describe('from end', () => {
it('should return 1st character from end (last)', () => {
const result = nthCharacter({ text: 'hello', position: 1, from_end: true });
expect(result.character).toBe('o');
expect(result.valid).toBe(true);
});
it('should return 2nd character from end', () => {
const result = nthCharacter({ text: 'hello', position: 2, from_end: true });
expect(result.character).toBe('l');
expect(result.valid).toBe(true);
});
});
describe('error cases', () => {
it('should error on position 0', () => {
const result = nthCharacter({ text: 'hello', position: 0, from_end: false });
expect(result.valid).toBe(false);
expect(result.error).toContain('at least 1');
});
it('should error on position greater than length', () => {
const result = nthCharacter({ text: 'hello', position: 10, from_end: false });
expect(result.valid).toBe(false);
expect(result.error).toContain('out of bounds');
});
});
});
describe('wordLength', () => {
it('should return correct length for simple word', () => {
const result = wordLength({ text: 'hello', count_spaces: true });
expect(result.length).toBe(5);
expect(result.word_count).toBe(1);
});
it('should count spaces when count_spaces is true', () => {
const result = wordLength({ text: 'hello world', count_spaces: true });
expect(result.length).toBe(11);
expect(result.space_count).toBe(1);
expect(result.word_count).toBe(2);
});
it('should exclude spaces when count_spaces is false', () => {
const result = wordLength({ text: 'hello world', count_spaces: false });
expect(result.length).toBe(10);
});
it('should handle empty string', () => {
const result = wordLength({ text: '', count_spaces: true });
expect(result.length).toBe(0);
expect(result.word_count).toBe(0);
});
it('should handle multiple spaces', () => {
const result = wordLength({ text: 'a b c', count_spaces: true });
expect(result.space_count).toBe(4);
expect(result.word_count).toBe(3);
});
});
describe('reverseText', () => {
describe('character reversal', () => {
it('should reverse all characters', () => {
const result = reverseText({ text: 'hello', reverse_words_only: false });
expect(result.reversed).toBe('olleh');
});
it('should reverse sentence with spaces', () => {
const result = reverseText({ text: 'hello world', reverse_words_only: false });
expect(result.reversed).toBe('dlrow olleh');
});
});
describe('word-only reversal', () => {
it('should reverse word order only', () => {
const result = reverseText({ text: 'hello world', reverse_words_only: true });
expect(result.reversed).toBe('world hello');
});
it('should preserve individual word characters', () => {
const result = reverseText({ text: 'abc def ghi', reverse_words_only: true });
expect(result.reversed).toBe('ghi def abc');
});
});
describe('palindrome detection', () => {
it('should detect "racecar" as palindrome', () => {
const result = reverseText({ text: 'racecar', reverse_words_only: false });
expect(result.is_palindrome).toBe(true);
});
it('should detect "A man a plan a canal Panama" as palindrome', () => {
const result = reverseText({ text: 'A man a plan a canal Panama', reverse_words_only: false });
expect(result.is_palindrome).toBe(true);
});
it('should not detect "hello" as palindrome', () => {
const result = reverseText({ text: 'hello', reverse_words_only: false });
expect(result.is_palindrome).toBe(false);
});
it('should detect "madam" as palindrome', () => {
const result = reverseText({ text: 'madam', reverse_words_only: false });
expect(result.is_palindrome).toBe(true);
});
});
});