learning_ai_notes/backend/src/lib/reading-time.test.ts

45 lines
1.4 KiB
TypeScript

/**
* Reading time estimation tests.
*/
import { describe, it, expect } from 'vitest';
import { estimateReadingTime } from './reading-time.js';
describe('estimateReadingTime', () => {
it('returns 1 minute minimum for empty content', () => {
const result = estimateReadingTime('');
expect(result.minutes).toBe(1);
expect(result.words).toBe(0);
});
it('returns 1 minute for short text', () => {
const result = estimateReadingTime('Hello world');
expect(result.minutes).toBe(1);
expect(result.words).toBe(2);
});
it('strips HTML tags before counting', () => {
const result = estimateReadingTime('<p>Hello <strong>world</strong></p>');
expect(result.words).toBe(2);
});
it('calculates correct minutes for long text (~238 wpm)', () => {
const words = Array.from({ length: 476 }, (_, i) => `word${i}`).join(' ');
const result = estimateReadingTime(words);
expect(result.words).toBe(476);
expect(result.minutes).toBe(2);
});
it('rounds up reading time', () => {
const words = Array.from({ length: 239 }, (_, i) => `word${i}`).join(' ');
const result = estimateReadingTime(words);
expect(result.words).toBe(239);
expect(result.minutes).toBe(2); // 239/238 = 1.004 → ceil = 2
});
it('handles whitespace-heavy content correctly', () => {
const result = estimateReadingTime(' one two three ');
expect(result.words).toBe(3);
});
});