/**
* Tests for analysis tools
*/
import {
compareTexts,
analyzeSentence,
batchCount,
} from '../src/tools/analysis.js';
describe('compareTexts', () => {
it('should find common characters', () => {
const result = compareTexts({
text1: 'hello',
text2: 'world',
case_sensitive: false,
});
expect(result.common_characters).toContain('l');
expect(result.common_characters).toContain('o');
expect(result.common_characters).toHaveLength(2);
});
it('should find characters unique to text1', () => {
const result = compareTexts({
text1: 'hello',
text2: 'world',
case_sensitive: false,
});
expect(result.unique_to_text1).toContain('h');
expect(result.unique_to_text1).toContain('e');
});
it('should find characters unique to text2', () => {
const result = compareTexts({
text1: 'hello',
text2: 'world',
case_sensitive: false,
});
expect(result.unique_to_text2).toContain('w');
expect(result.unique_to_text2).toContain('r');
expect(result.unique_to_text2).toContain('d');
});
it('should calculate similarity score', () => {
const result = compareTexts({
text1: 'abc',
text2: 'abc',
case_sensitive: false,
});
expect(result.similarity_score).toBe(100);
});
it('should calculate 0 similarity for completely different texts', () => {
const result = compareTexts({
text1: 'abc',
text2: 'xyz',
case_sensitive: false,
});
expect(result.similarity_score).toBe(0);
});
it('should provide frequency comparison', () => {
const result = compareTexts({
text1: 'aab',
text2: 'abb',
case_sensitive: false,
});
const aComp = result.frequency_comparison.find(f => f.char === 'a');
expect(aComp?.count_in_text1).toBe(2);
expect(aComp?.count_in_text2).toBe(1);
expect(aComp?.difference).toBe(1);
});
});
describe('analyzeSentence', () => {
it('should analyze each word separately', () => {
const result = analyzeSentence({
text: 'The strawberry was ripe',
letter: 'r',
case_sensitive: false,
});
expect(result.word_count).toBe(4);
expect(result.words).toHaveLength(4);
});
it('should count correctly per word', () => {
const result = analyzeSentence({
text: 'strawberry raspberry',
letter: 'r',
case_sensitive: false,
});
const strawberry = result.words.find(w => w.word === 'strawberry');
const raspberry = result.words.find(w => w.word === 'raspberry');
expect(strawberry?.letter_count).toBe(3);
expect(raspberry?.letter_count).toBe(3);
expect(result.total_count).toBe(6);
});
it('should include positions within each word', () => {
const result = analyzeSentence({
text: 'rare',
letter: 'r',
case_sensitive: false,
});
expect(result.words[0].letter_positions).toEqual([0, 2]);
});
it('should provide breakdown table', () => {
const result = analyzeSentence({
text: 'hello world',
letter: 'l',
case_sensitive: false,
});
expect(result.breakdown_table).toContain('hello');
expect(result.breakdown_table).toContain('world');
});
it('should handle words with no matches', () => {
const result = analyzeSentence({
text: 'the cat sat',
letter: 'z',
case_sensitive: false,
});
expect(result.total_count).toBe(0);
expect(result.words.every(w => w.letter_count === 0)).toBe(true);
});
});
describe('batchCount', () => {
it('should count letter in multiple words', () => {
const result = batchCount({
words: ['strawberry', 'raspberry', 'blueberry'],
letter: 'r',
case_sensitive: false,
});
expect(result.results).toHaveLength(3);
expect(result.results[0].word).toBe('strawberry');
expect(result.results[0].count).toBe(3);
expect(result.results[1].word).toBe('raspberry');
expect(result.results[1].count).toBe(3);
expect(result.results[2].word).toBe('blueberry');
expect(result.results[2].count).toBe(2); // blueberry has 2 r's
});
it('should calculate total count', () => {
const result = batchCount({
words: ['strawberry', 'raspberry', 'blueberry'],
letter: 'r',
case_sensitive: false,
});
expect(result.total_count).toBe(8); // 3 + 3 + 2
});
it('should count words with and without letter', () => {
const result = batchCount({
words: ['apple', 'banana', 'cherry'],
letter: 'a',
case_sensitive: false,
});
expect(result.words_with_letter).toBe(2); // apple and banana have 'a', cherry doesn't
expect(result.words_without_letter).toBe(1);
});
it('should handle words without the letter', () => {
const result = batchCount({
words: ['cat', 'dog', 'fish'],
letter: 'z',
case_sensitive: false,
});
expect(result.words_with_letter).toBe(0);
expect(result.words_without_letter).toBe(3);
expect(result.total_count).toBe(0);
});
it('should provide sorted results', () => {
const result = batchCount({
words: ['a', 'aaa', 'aa'],
letter: 'a',
case_sensitive: false,
});
expect(result.sorted_by_count[0].count).toBe(3);
expect(result.sorted_by_count[1].count).toBe(2);
expect(result.sorted_by_count[2].count).toBe(1);
});
it('should include positions for each word', () => {
const result = batchCount({
words: ['rare'],
letter: 'r',
case_sensitive: false,
});
expect(result.results[0].positions).toEqual([0, 2]);
});
it('should respect case sensitivity', () => {
const result = batchCount({
words: ['RaRe', 'rare'],
letter: 'r',
case_sensitive: true,
});
expect(result.results[0].count).toBe(0); // 'RaRe' has no lowercase 'r'
expect(result.results[1].count).toBe(2); // 'rare' has 2 lowercase 'r's
});
});