normalize($body))->toBe($normalized); })->with([ 'one blank line' => ["a\n\nb", "a\n\nb"], 'two blank lines' => ["a\n\n\nb", "a\n\nb"], 'many blank lines' => ["a\n\n\n\n\n\nb", "a\n\nb"], 'blank lines with spaces and tabs' => ["a \n\t\n \n\nb", "a\n\nb"], 'windows line breaks' => ["a\r\nb\r\n\r\n\r\n\r\nc", "a\nb\n\nc"], 'standalone carriage returns' => ["a\rb\r\r\rc", "a\nb\n\nc"], 'mixed line endings' => ["a\r\nb\rc\n\n\rd", "a\nb\nc\n\nd"], 'blank lines in a code block' => ["a\n```py\ndef a():\n pass\n\n\ndef b():\n pass\n```\nb", "a\n```py\ndef a():\n pass\n\n\ndef b():\n pass\n```\nb"], 'blank lines around a code block' => ["a\n\n\n\n```\ncode\n```\n\n\n\nb", "a\n\n```\ncode\n```\n\nb"], ]); it('has no excerpt for a body short enough to show whole', function (string $body): void { expect(resolve(CommitBodies::class)->excerpt($body))->toBeNull(); })->with([ 'short' => 'It was DNS.', '280 characters' => str_repeat('a', 280), '280 multibyte characters' => str_repeat('🚀', 280), '8 lines' => implode("\n", range(1, 8)), 'short final-line tail' => str_repeat('a', 280).' more', 'short final-line tail after a word boundary' => "A year ago today we got Opus 4.5.\n\nOver the holidays, builders finally finished the projects they'd been putting off. Then OpenClaw in January.\n\nMy guess for this Christmas: closed-loop agents that work out of the box for non-technical builders.\n\nWhat are you building this holiday season?", 'short multibyte final-line tail' => str_repeat('é ', 141), ]); it('cuts a long body at the last whitespace that fits', function (string $body, string $excerpt): void { expect(resolve(CommitBodies::class)->excerpt($body))->toBe($excerpt); })->with([ 'in the middle of a word' => [str_repeat('word ', 56).'overflowing words that keep going today now', mb_rtrim(str_repeat('word ', 56)).'…'], 'right before a whitespace' => [str_repeat('a', 280).str_repeat(' more', 9), str_repeat('a', 280).'…'], 'after the 8th line' => [implode("\n", range(1, 12)), implode("\n", range(1, 8)).'…'], 'multibyte characters' => [str_repeat('é ', 160), mb_rtrim(str_repeat('é ', 140)).'…'], ]); it('cuts a word too long to fit only when nothing comes before it', function (string $body, string $excerpt): void { expect(resolve(CommitBodies::class)->excerpt($body))->toBe($excerpt); })->with([ 'first word' => ['https://example.com/'.str_repeat('a', 300), 'https://example.com/'.str_repeat('a', 260).'…'], 'after code' => ['`make`'.str_repeat('a', 300), '`make`…'], ]); it('ends the excerpt before code that does not fit', function (string $body, string $excerpt): void { expect(resolve(CommitBodies::class)->excerpt($body))->toBe($excerpt); })->with([ 'code block' => ["Look at this:\n```php\n".str_repeat("echo 1;\n", 40).'```', 'Look at this:…'], 'code block past the 8th line' => ["One\nTwo\n```\n".implode("\n", range(1, 10))."\n```", "One\nTwo…"], 'inline code' => [str_repeat('a', 270).' `composer install`', str_repeat('a', 270).'…'], ]); it('keeps the first lines of a code block that the body starts with', function (string $body, string $excerpt): void { expect(resolve(CommitBodies::class)->excerpt($body))->toBe($excerpt); })->with([ 'many short lines' => ["```php\n".implode("\n", array_map(fn (int $line): string => "echo {$line};", range(1, 12)))."\n```\nNeat.", "```php\n".implode("\n", array_map(fn (int $line): string => "echo {$line};", range(1, 6)))."\n```\n…"], 'long lines' => ["```\n".str_repeat('a', 200)."\n".str_repeat('b', 200)."\n```", "```\n".str_repeat('a', 200)."\n```\n…"], 'one line too long' => ["```\n".str_repeat('a', 400)."\n```", "```\n".str_repeat('a', 272)."\n```\n…"], 'longer fence' => ["````\n".str_repeat("line\n", 20).'````', "````\n".mb_rtrim(str_repeat("line\n", 6))."\n````\n…"], 'windows line breaks' => ["```\r\n".str_repeat("line\r\n", 60).'```', "```\n".mb_rtrim(str_repeat("line\n", 6))."\n```\n…"], 'whole block before a word too long to fit' => ["```\nshort\n```\nhttps://example.com/".str_repeat('a', 300), "```\nshort\n```\n…"], 'after blank prose' => [" \n```\n".str_repeat("line\n", 20).'```', " \n```\n".mb_rtrim(str_repeat("line\n", 5))."\n```\n…"], ]); it('cuts inline code short when the body starts with it', function (): void { expect(resolve(CommitBodies::class)->excerpt('`'.str_repeat('a', 300).'` done'))->toBe('`'.str_repeat('a', 278).'`…'); }); it('measures the folded excerpt from the words without their image references', function (): void { $body = 'Look !(1)[a caption long enough to push an otherwise short body past the excerpt limit '.str_repeat('word ', 100).'] go.'; $segments = resolve(CommitBodies::class)->segments($body); expect($segments['body_segments'])->toContain([ 'type' => 'image', 'index' => 1, 'alt' => 'a caption long enough to push an otherwise short body past the excerpt limit '.mb_trim(str_repeat('word ', 100)), ]); expect($segments['preview_segments'])->toBeNull(); }); it('never lets an image caption cut the excerpt it would have had', function (): void { $prose = str_repeat('word ', 55).str_repeat('tail', 20); $body = $prose.' !(1)[ '.str_repeat('caption ', 100).']'; $preview = resolve(CommitBodies::class)->segments($body)['preview_segments']; $text = implode('', array_map( static fn (array $segment): string => is_string($segment['text'] ?? null) ? $segment['text'] : '', $preview ?? [], )); expect($preview)->not->toBeNull(); expect($text)->toBe(resolve(CommitBodies::class)->excerpt($prose)); expect($text)->not->toContain('caption'); expect($text)->toEndWith('…'); }); it('places images in the body but not in a folded preview', function (): void { $segments = resolve(CommitBodies::class)->segments('Look !(1)[the build] go. '.str_repeat('more ', 100)); expect($segments['body_segments'])->toContain([ 'type' => 'image', 'index' => 1, 'alt' => 'the build', ]); expect($segments['preview_segments'])->not->toContain([ 'type' => 'image', 'index' => 1, 'alt' => 'the build', ]); }); it('keeps the first image in place in a folded feed', function (): void { $segments = feed('Seeing if it works: !(1)[img 1] Oh yeah? !(2)[] And finally !(3)[how TUIOS does something]'); expect($segments)->toBe([ ['type' => 'text', 'text' => 'Seeing if it works: '], ['type' => 'image', 'index' => 1, 'alt' => 'img 1'], ['type' => 'text', 'text' => ' Oh yeah? And finally '], ]); }); it('leaves later images out of a long folded feed', function (): void { $segments = feed('Seeing if it works: !(1)[img 1] Oh yeah? '.str_repeat('more ', 100).'!(2)[last]'); $last = end($segments); expect($segments[1])->toBe(['type' => 'image', 'index' => 1, 'alt' => 'img 1']); expect($segments)->not->toContain(['type' => 'image', 'index' => 2, 'alt' => 'last']); expect($last['type'])->toBe('text'); expect($last['text'])->toEndWith('…'); }); it('shows no image in a folded feed once it falls past the preview', function (): void { $segments = feed(str_repeat('more ', 100).'!(1)[last]'); $last = end($segments); expect($segments)->not->toContain(['type' => 'image', 'index' => 1, 'alt' => 'last']); expect($last['type'])->toBe('text'); expect($last['text'])->toEndWith('…'); }); it('leaves a short body with one image whole in a feed', function (): void { expect(feed('Look !(1)[the build] go.'))->toBeNull(); }); it('never guesses where images sit when their metadata was not loaded', function (): void { expect(feed('One !(1)[a] two !(2)[b]', null))->toBeNull(); }); it('keeps short wide images in the feed until their height budget is full', function (): void { $body = 'Just noticing some things:!(2)[Extra Pasteis stuff]'."\n".'!(1)[new since your last visit]'."\n".'!(3)[when a commit is already translated]'; $images = [ ['width' => 465, 'height' => 105], ['width' => 566, 'height' => 146], ['width' => 850, 'height' => 231], ]; expect(feed($body, $images))->toBe([ ['type' => 'text', 'text' => 'Just noticing some things:'], ['type' => 'image', 'index' => 2, 'alt' => 'Extra Pasteis stuff'], ['type' => 'text', 'text' => "\n"], ['type' => 'image', 'index' => 1, 'alt' => 'new since your last visit'], ]); }); it('keeps all three images inline when each is small enough', function (): void { $body = 'One !(1)[a] two !(2)[b] three !(3)[c]'; $images = array_fill(0, 3, ['width' => 400, 'height' => 60]); $segments = feed($body, $images); expect(array_values(array_filter($segments ?? [], static fn (array $segment): bool => $segment['type'] === 'image')))->toBe([ ['type' => 'image', 'index' => 1, 'alt' => 'a'], ['type' => 'image', 'index' => 2, 'alt' => 'b'], ['type' => 'image', 'index' => 3, 'alt' => 'c'], ]); }); it('clips a tall first image to the feed budget', function (): void { $body = 'One !(1)[tall] two !(2)[short]'; $images = [ ['width' => 300, 'height' => 600], ['width' => 600, 'height' => 60], ]; $segments = feed($body, $images); expect(inlineImages($segments))->toBe([ ['type' => 'image', 'index' => 1, 'alt' => 'tall', 'visibleHeight' => 280], ]); expect(mb_trim(proseAfterLastImage($segments)))->toBe(''); }); it('clips the next image when two lines of the budget remain', function (): void { $body = 'First !(1)[one] then !(2)[two] after this'; $images = [ ['width' => 530, 'height' => 200], ['width' => 530, 'height' => 100], ]; $segments = feed($body, $images); expect(inlineImages($segments))->toBe([ ['type' => 'image', 'index' => 1, 'alt' => 'one'], ['type' => 'image', 'index' => 2, 'alt' => 'two', 'visibleHeight' => 80], ]); expect(mb_trim(proseAfterLastImage($segments)))->toBe(''); }); it('omits an image with less than two lines of budget and stops the excerpt there', function (): void { $body = 'First !(1)[one] then !(2)[two] after this'; $images = [ ['width' => 530, 'height' => 240], ['width' => 530, 'height' => 100], ]; $segments = feed($body, $images); expect(inlineImages($segments))->toBe([ ['type' => 'image', 'index' => 1, 'alt' => 'one'], ]); expect(end($segments)['text'])->toEndWith('…'); expect(mb_trim(proseAfterLastImage($segments)))->toBe('…'); }); it('stops at an image whose dimensions are unknown', function (): void { $body = 'One !(1)[known] two !(2)[unknown] three !(3)[later]'; $images = [ ['width' => 530, 'height' => 100], ['width' => null, 'height' => null], ['width' => 530, 'height' => 100], ]; $segments = feed($body, $images); expect(inlineImages($segments))->toBe([ ['type' => 'image', 'index' => 1, 'alt' => 'known'], ]); expect(end($segments)['text'])->toEndWith('…'); expect(mb_trim(proseAfterLastImage($segments)))->toBe('…'); }); it('does not mark hidden image alt descriptions as missing prose', function (): void { $body = 'Look !(1)[one] !(2)[] !(3)[three]'; $images = [ ['width' => 530, 'height' => 130], ['width' => 530, 'height' => 130], ['width' => 530, 'height' => 100], ]; $segments = feed($body, $images); expect(inlineImages($segments))->toHaveCount(2); expect(mb_trim(proseAfterLastImage($segments)))->toBe(''); }); /** * @param list|object>|null $images * @return list>|null */ function feed(string $body, ?array $images = []): ?array { return resolve(FeedBodies::class)->segments($body, $images); } /** * @param list>|null $segments * @return list> */ function inlineImages(?array $segments): array { return array_values(array_filter( $segments ?? [], static fn (array $segment): bool => $segment['type'] === 'image', )); } /** * @param list>|null $segments */ function proseAfterLastImage(?array $segments): string { $lastImage = null; foreach ($segments ?? [] as $position => $segment) { if ($segment['type'] === 'image') { $lastImage = $position; } } if ($lastImage === null) { return ''; } return implode('', array_map( static fn (array $segment): string => is_string($segment['text'] ?? null) ? $segment['text'] : '', array_slice($segments, $lastImage + 1), )); }