A calm place to write long-form, and publish it to the open social web.
skypress.blog/
1import { describe, expect, it } from 'vitest';
2import { deriveExcerpt } from './excerpt';
3
4describe( 'deriveExcerpt', () => {
5 it( 'returns empty string for empty or whitespace-only input', () => {
6 expect( deriveExcerpt( '' ) ).toBe( '' );
7 expect( deriveExcerpt( ' \n\t ' ) ).toBe( '' );
8 } );
9
10 it( 'returns short text unchanged, with no ellipsis', () => {
11 expect( deriveExcerpt( 'A short lede.' ) ).toBe( 'A short lede.' );
12 } );
13
14 it( 'collapses internal whitespace and newlines to single spaces', () => {
15 expect( deriveExcerpt( 'one\n\ntwo three' ) ).toBe( 'one two three' );
16 } );
17
18 it( 'truncates long text on a word boundary with a trailing ellipsis', () => {
19 const long = 'word '.repeat( 100 ).trim(); // 499 chars, all word boundaries
20 const result = deriveExcerpt( long );
21 expect( result.endsWith( '…' ) ).toBe( true );
22 // Body (sans ellipsis) stays within the limit and never splits a word.
23 const body = result.slice( 0, -1 );
24 expect( body.length ).toBeLessThanOrEqual( 200 );
25 expect( body.endsWith( 'word' ) ).toBe( true );
26 expect( body.endsWith( ' ' ) ).toBe( false );
27 } );
28
29 it( 'honours a custom maxChars and cuts at the last space within it', () => {
30 expect( deriveExcerpt( 'alpha beta gamma', 10 ) ).toBe( 'alpha…' );
31 } );
32
33 it( 'truncates an unsplittable token at maxChars with an ellipsis', () => {
34 const result = deriveExcerpt( 'x'.repeat( 300 ) );
35 expect( result ).toBe( `${ 'x'.repeat( 200 ) }…` );
36 } );
37} );