import { describe, expect, it } from 'vitest' import { contentBlocks, readingMinutes } from '~/lib/post-content' import type { Block } from '~/data/schema' function block(id: string, type: string, data: Record): Block { return { id, postId: 'post', sort: 0, type, data } as Block } describe('contentBlocks', () => { it('lässt Blöcke unangetastet, wenn nichts doppelt ist', () => { const blocks = [block('a', 'text', { text: 'Ein eigener Absatz.' })] expect(contentBlocks(blocks, 'Anderer Anreißer', null)).toHaveLength(1) }) it('überspringt den ersten Textblock, wenn er den Anreißer wiederholt', () => { const blocks = [ block('a', 'text', { text: ' Tickets reagieren jetzt selbst. ' }), block('b', 'text', { text: 'Und noch etwas.' }), ] const result = contentBlocks(blocks, 'Tickets reagieren jetzt selbst.', null) expect(result.map(entry => entry.id)).toEqual(['b']) }) it('überspringt einen Bildblock, der das Aufmacherbild wiederholt', () => { const blocks = [ block('a', 'image', { mediaId: 'cover-1' }), block('b', 'image', { mediaId: 'andere-2' }), ] const result = contentBlocks(blocks, null, 'cover-1') expect(result.map(entry => entry.id)).toEqual(['b']) }) it('entfernt beide Wiederholungen zusammen', () => { const blocks = [ block('a', 'image', { mediaId: 'cover-1' }), block('b', 'text', { text: 'Der Anreißer.' }), block('c', 'text', { text: 'Der eigentliche Inhalt.' }), ] const result = contentBlocks(blocks, 'Der Anreißer.', 'cover-1') expect(result.map(entry => entry.id)).toEqual(['c']) }) it('behält den Bildblock, wenn es kein Aufmacherbild gibt', () => { const blocks = [block('a', 'image', { mediaId: 'irgendwas' })] expect(contentBlocks(blocks, null, null)).toHaveLength(1) }) }) describe('readingMinutes', () => { it('meldet mindestens eine Minute', () => { expect(readingMinutes([], null)).toBe(1) }) it('rechnet Anreißer und Blöcke zusammen', () => { const words = Array.from({ length: 400 }, () => 'Wort').join(' ') expect(readingMinutes([block('a', 'text', { text: words })], null)).toBe(2) }) })