Files
portal/app/tests/Unit/Bot/HelpArticleParserTest.php
T

48 lines
1.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
use App\Support\Help\HelpArticleParser;
use Tests\TestCase;
uses(TestCase::class);
it('парсит frontmatter и режет тело на чанки по абзацам', function () {
$md = <<<'MD'
---
title: Что такое проект
tour: create-project
topics: создать проект, заявка на лиды
---
Первый абзац про проект.
Второй абзац про создание.
MD;
$article = (new HelpArticleParser)->parse('help/x.md', $md);
expect($article->title)->toBe('Что такое проект')
->and($article->tour)->toBe('create-project')
->and($article->topics)->toBe('создать проект, заявка на лиды')
->and($article->chunks)->toHaveCount(1)
->and($article->chunks[0])->toContain('Первый абзац');
});
it('без frontmatter кидает понятную ошибку', function () {
expect(fn () => (new HelpArticleParser)->parse('help/bad.md', 'просто текст'))
->toThrow(InvalidArgumentException::class);
});
it('длинное тело режет на несколько чанков ~1200 символов по границам абзацев', function () {
$body = implode("\n\n", array_fill(0, 10, str_repeat('а', 300)));
$md = "---\ntitle: Т\ntopics: т\n---\n\n".$body;
$article = (new HelpArticleParser)->parse('help/long.md', $md);
expect(count($article->chunks))->toBeGreaterThan(1);
foreach ($article->chunks as $chunk) {
expect(mb_strlen($chunk))->toBeLessThanOrEqual(1500);
}
});