Repository navigation
Expand file tree
/
Copy pathLlmsGeneratorTest.php
More file actions
571 lines (469 loc) · 18.5 KB
/
Copy pathLlmsGeneratorTest.php
File metadata and controls
571 lines (469 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
<?php
declare(strict_types=1);
namespace LightTest\Unit\App\Service;
use Light\App\Service\LlmsGenerator;
use Light\Blog\Entity\Category;
use Light\Blog\Entity\Post;
use Light\Blog\Repository\PostRepository;
use LightTest\Unit\UnitTest;
use PHPUnit\Framework\MockObject\Exception;
use RuntimeException;
use function array_diff;
use function bin2hex;
use function file_get_contents;
use function file_put_contents;
use function is_dir;
use function mb_strpos;
use function mkdir;
use function random_bytes;
use function rmdir;
use function scandir;
use function sprintf;
use function sys_get_temp_dir;
use function unlink;
use const DIRECTORY_SEPARATOR;
class LlmsGeneratorTest extends UnitTest
{
private string $workDir;
private string $sourceDir;
private string $pagesDir;
private string $outputFile;
protected function setUp(): void
{
parent::setUp();
$this->workDir = sprintf(
'%s%slight-llms-index-%s',
sys_get_temp_dir(),
DIRECTORY_SEPARATOR,
bin2hex(random_bytes(8))
);
$this->sourceDir = $this->workDir . DIRECTORY_SEPARATOR . 'md-articles';
// Mirrors production: page markdown files live at the public root, alongside md-articles/.
$this->pagesDir = $this->workDir;
$this->outputFile = $this->workDir . DIRECTORY_SEPARATOR . 'llms.txt';
mkdir($this->sourceDir, 0775, true);
}
protected function tearDown(): void
{
$this->removeDirectory($this->workDir);
parent::tearDown();
}
public function testGetOutputFileReturnsTheConfiguredPath(): void
{
$this->assertSame($this->outputFile, $this->createGenerator()->getOutputFile());
}
/**
* @throws Exception
*/
public function testWriteReturnsTheNumberOfCategorySectionsWritten(): void
{
$posts = [
$this->createPost('A post', 'a-post', 'dotkernel', 'Dotkernel'),
$this->createPost('Another post', 'another-post', 'best-practice', 'Best Practice'),
];
$this->assertSame(2, $this->createGenerator($posts)->write());
}
/**
* @throws Exception
*/
public function testWriteReturnsZeroAndWritesOnlyTheHeaderWhenThereAreNoPublishedPosts(): void
{
$this->assertSame(0, $this->createGenerator()->write());
$output = $this->writtenOutput();
$this->assertStringStartsWith('# Dotkernel', $output);
$this->assertStringContainsString('## Docs', $output);
$this->assertStringNotContainsString(' post)', $output);
$this->assertStringNotContainsString(' posts)', $output);
}
/**
* @throws Exception
*/
public function testWriteUsesTheConfiguredTitleAsTheHeading(): void
{
$this->createGenerator(title: 'Custom Blog Title')->write();
$this->assertStringStartsWith('# Custom Blog Title', $this->writtenOutput());
}
/**
* @throws Exception
*/
public function testWriteOrdersCategorySectionsAccordingToTheCuratedOrder(): void
{
// Inserted in the "wrong" order on purpose - php-development is curated after dotkernel.
$posts = [
$this->createPost('PHP post', 'php-post', 'php-development', 'PHP Development'),
$this->createPost('Dotkernel post', 'dotkernel-post', 'dotkernel', 'Dotkernel'),
];
$this->createGenerator($posts)->write();
$output = $this->writtenOutput();
$dotkernelAt = mb_strpos($output, '## Dotkernel');
$phpDevAt = mb_strpos($output, '## PHP Development');
$this->assertNotFalse($dotkernelAt);
$this->assertNotFalse($phpDevAt);
$this->assertLessThan($phpDevAt, $dotkernelAt);
}
/**
* @throws Exception
*/
public function testWriteAppendsCategoriesNotInTheCuratedOrderAfterTheCuratedOnesSortedByPostCountDescending(): void
{
$posts = [
$this->createPost('Curated post', 'curated-post', 'dotkernel', 'Dotkernel'),
$this->createPost('Small extra post', 'small-extra-post', 'extra-small', 'Extra Small'),
$this->createPost('Big extra post one', 'big-extra-post-one', 'extra-big', 'Extra Big'),
$this->createPost('Big extra post two', 'big-extra-post-two', 'extra-big', 'Extra Big'),
];
$this->createGenerator($posts)->write();
$output = $this->writtenOutput();
$dotkernelAt = mb_strpos($output, '## Dotkernel');
$bigAt = mb_strpos($output, '## Extra Big');
$smallAt = mb_strpos($output, '## Extra Small');
$this->assertNotFalse($dotkernelAt);
$this->assertNotFalse($bigAt);
$this->assertNotFalse($smallAt);
$this->assertLessThan($bigAt, $dotkernelAt);
$this->assertLessThan($smallAt, $bigAt);
}
/**
* @throws Exception
*/
public function testWriteMovesAnOptionalCategoryPostToATrailingOptionalSectionWithoutAHeading(): void
{
$posts = [
$this->createPost('Curated post', 'curated-post', 'dotkernel', 'Dotkernel'),
$this->createPost('JS post', 'js-post', 'javascript', 'Javascript'),
];
$this->createGenerator($posts)->write();
$output = $this->writtenOutput();
$dotkernelAt = mb_strpos($output, '## Dotkernel');
$optionalAt = mb_strpos($output, '## Optional');
$jsPostAt = mb_strpos($output, '[JS post]');
$this->assertNotFalse($dotkernelAt);
$this->assertNotFalse($optionalAt);
$this->assertNotFalse($jsPostAt);
$this->assertLessThan($optionalAt, $dotkernelAt);
$this->assertLessThan($jsPostAt, $optionalAt);
$this->assertStringNotContainsString('## Javascript', $output);
}
/**
* @throws Exception
*/
public function testWriteOmitsTheOptionalSectionWhenNoOptionalCategoryHasPublishedPosts(): void
{
$posts = [$this->createPost('Curated post', 'curated-post', 'dotkernel', 'Dotkernel')];
$this->createGenerator($posts)->write();
$this->assertStringNotContainsString('## Optional', $this->writtenOutput());
}
/**
* @throws Exception
*/
public function testWriteOmitsTheCategoryTitleAndBlurbForAnOptionalCategory(): void
{
$posts = [$this->createPost('JS post', 'js-post', 'javascript', 'Javascript')];
$this->createGenerator($posts)->write();
$output = $this->writtenOutput();
$this->assertStringContainsString(
'- [JS post](https://example.test/javascript/js-post.md) *(Javascript)*: An excerpt.',
$output
);
$this->assertStringNotContainsString('## Javascript', $output);
$this->assertStringNotContainsString('frontend/JS topics', $output);
}
/**
* @throws Exception
*/
public function testWriteReturnsTheNumberOfCategorySectionsWrittenIncludingTheOptionalSection(): void
{
$posts = [
$this->createPost('Curated post', 'curated-post', 'dotkernel', 'Dotkernel'),
$this->createPost('JS post', 'js-post', 'javascript', 'Javascript'),
];
$this->assertSame(2, $this->createGenerator($posts)->write());
}
/**
* @throws Exception
*/
public function testWriteSortsPostsWithinACategoryAlphabeticallyByTitleCaseInsensitively(): void
{
$posts = [
$this->createPost('zebra post', 'zebra-post', 'dotkernel', 'Dotkernel'),
$this->createPost('Alpha post', 'alpha-post', 'dotkernel', 'Dotkernel'),
$this->createPost('banana post', 'banana-post', 'dotkernel', 'Dotkernel'),
];
$this->createGenerator($posts)->write();
$output = $this->writtenOutput();
$alphaAt = mb_strpos($output, '[Alpha post]');
$bananaAt = mb_strpos($output, '[banana post]');
$zebraAt = mb_strpos($output, '[zebra post]');
$this->assertNotFalse($alphaAt);
$this->assertNotFalse($bananaAt);
$this->assertNotFalse($zebraAt);
$this->assertLessThan($bananaAt, $alphaAt);
$this->assertLessThan($zebraAt, $bananaAt);
}
/**
* @throws Exception
*/
public function testWriteIncludesTheBlurbLineForACuratedCategory(): void
{
$posts = [$this->createPost('A post', 'a-post', 'dotkernel', 'Dotkernel')];
$this->createGenerator($posts)->write();
$this->assertStringContainsString(
"## Dotkernel\n\n*the core framework",
$this->writtenOutput()
);
}
/**
* @throws Exception
*/
public function testWriteOmitsTheBlurbLineForAnUncuratedCategory(): void
{
$posts = [$this->createPost('A post', 'a-post', 'unlisted', 'Unlisted')];
$this->createGenerator($posts)->write();
$this->assertStringContainsString(
"## Unlisted\n\n- [A post]",
$this->writtenOutput()
);
}
/**
* Category headings no longer carry a post count, regardless of how many posts they hold.
*
* @throws Exception
*/
public function testWriteCategoryHeadingHasNoPostCount(): void
{
$posts = [$this->createPost('A post', 'a-post', 'dotkernel', 'Dotkernel')];
$this->createGenerator($posts)->write();
$this->assertStringContainsString("## Dotkernel\n", $this->writtenOutput());
$this->assertStringNotContainsString('## Dotkernel (', $this->writtenOutput());
}
/**
* @throws Exception
*/
public function testWriteBuildsEachPostLinkFromTheBaseUrlCategorySlugAndPostSlugAsMarkdown(): void
{
$posts = [$this->createPost('A post', 'a-post', 'dotkernel', 'Dotkernel')];
$this->createGenerator($posts, baseUrl: 'https://example.test')->write();
$this->assertStringContainsString(
'[A post](https://example.test/dotkernel/a-post.md):',
$this->writtenOutput()
);
}
/**
* @throws Exception
*/
public function testWriteBuildsTheDocsSectionLinksFromTheBaseUrl(): void
{
$this->createGenerator(baseUrl: 'https://example.test')->write();
$output = $this->writtenOutput();
$this->assertStringContainsString(
'[OSS Package Lifecycle](https://example.test/dotkernel-packages-oss-lifecycle.md):',
$output
);
}
/**
* @throws Exception
*/
public function testWritePrefersTheTitleAndDescriptionFromTheArticleFrontMatterOverThePostEntity(): void
{
$this->writeArticle('dotkernel', 'a-post', 'Clean title', 'Clean description.');
$posts = [$this->createPost('Raw title', 'a-post', 'dotkernel', 'Dotkernel', 'Raw excerpt.')];
$this->createGenerator($posts)->write();
$output = $this->writtenOutput();
$this->assertStringContainsString('[Clean title](', $output);
$this->assertStringContainsString(': Clean description.', $output);
$this->assertStringNotContainsString('Raw title', $output);
$this->assertStringNotContainsString('Raw excerpt.', $output);
}
/**
* @throws Exception
*/
public function testWriteFallsBackToThePostEntityWhenTheArticleFileIsMissing(): void
{
$posts = [$this->createPost('Raw title', 'a-post', 'dotkernel', 'Dotkernel', 'Raw excerpt.')];
$this->createGenerator($posts)->write();
$output = $this->writtenOutput();
$this->assertStringContainsString('[Raw title](', $output);
$this->assertStringContainsString(': Raw excerpt.', $output);
}
/**
* @throws Exception
*/
public function testWriteOmitsThePagesSectionWhenNoPagesDirectoryIsConfigured(): void
{
$this->writePage('api', 'Dotkernel API | Tagline');
$this->createGenerator(pagesDir: null)->write();
$this->assertStringNotContainsString('## Pages', $this->writtenOutput());
}
/**
* The hardcoded Contact entry has no source file, so it still renders even when the
* directory otherwise has nothing to glob - and nothing else does, in that case.
*
* @throws Exception
*/
public function testWritePagesSectionContainsOnlyContactWhenThePagesDirectoryIsEmpty(): void
{
$this->createGenerator()->write();
$this->assertStringContainsString(
"## Pages\n\n- [Contact](https://example.test/contact/): Get in touch, report a security issue, "
. "or find where to ask a question and contribute to the project.\n\n",
$this->writtenOutput()
);
}
/**
* @throws Exception
*/
public function testWriteIncludesEachPageSortedAlphabeticallyByTitle(): void
{
$this->writePage('api', 'Zebra page | Some tagline');
$this->writePage('admin', 'Alpha page | Some tagline');
$this->createGenerator(baseUrl: 'https://example.test')->write();
$output = $this->writtenOutput();
$alphaAt = mb_strpos($output, '[Alpha page]');
$zebraAt = mb_strpos($output, '[Zebra page]');
$this->assertStringContainsString('## Pages', $output);
$this->assertNotFalse($alphaAt);
$this->assertNotFalse($zebraAt);
$this->assertLessThan($zebraAt, $alphaAt);
}
/**
* @throws Exception
*/
public function testWriteTrimsThePageTitleAtTheTaglineSeparator(): void
{
$this->writePage('api', 'Dotkernel API | Open-source REST API skeleton for PHP', 'The description.');
$this->createGenerator(baseUrl: 'https://example.test')->write();
$this->assertStringContainsString(
'- [Dotkernel API](https://example.test/api.md): The description.',
$this->writtenOutput()
);
$this->assertStringNotContainsString('Open-source REST API skeleton for PHP]', $this->writtenOutput());
}
/**
* @throws Exception
*/
public function testWriteSkipsAPageMissingATitleOrDescription(): void
{
$this->writePage('api', 'Dotkernel API | Tagline', null);
$this->writePage('admin', 'Dotkernel Admin | Tagline', 'Has a description.');
$this->createGenerator()->write();
$output = $this->writtenOutput();
$this->assertStringNotContainsString('[Dotkernel API]', $output);
$this->assertStringContainsString('[Dotkernel Admin]', $output);
}
/**
* @throws Exception
*/
public function testWritePlacesThePagesSectionBetweenDocsAndTheFirstCategorySection(): void
{
$this->writePage('api', 'Dotkernel API | Tagline');
$posts = [$this->createPost('A post', 'a-post', 'dotkernel', 'Dotkernel')];
$this->createGenerator($posts)->write();
$output = $this->writtenOutput();
$docsAt = mb_strpos($output, '## Docs');
$pagesAt = mb_strpos($output, '## Pages');
$dotkernelAt = mb_strpos($output, '## Dotkernel');
$this->assertNotFalse($docsAt);
$this->assertNotFalse($pagesAt);
$this->assertNotFalse($dotkernelAt);
$this->assertLessThan($pagesAt, $docsAt);
$this->assertLessThan($dotkernelAt, $pagesAt);
}
/**
* @throws Exception
*/
public function testWriteOverwritesAnExistingOutputFile(): void
{
file_put_contents($this->outputFile, 'stale content');
$this->createGenerator()->write();
$this->assertStringNotContainsString('stale content', $this->writtenOutput());
}
/**
* @throws Exception
*/
public function testWriteThrowsWhenTheOutputFileCannotBeWritten(): void
{
$generator = $this->createGenerator(
outputFile: $this->workDir . DIRECTORY_SEPARATOR . 'missing' . DIRECTORY_SEPARATOR . 'llms.txt'
);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Unable to write llms.txt.');
@$generator->write();
}
/**
* @param list<Post> $posts
* @throws Exception
*/
private function createGenerator(
array $posts = [],
?string $outputFile = null,
string $baseUrl = 'https://example.test',
?string $pagesDir = '',
string $title = 'Dotkernel',
): LlmsGenerator {
$postRepository = $this->createStub(PostRepository::class);
$postRepository->method('getPublishedPosts')->willReturn($posts);
return new LlmsGenerator(
$postRepository,
$this->sourceDir,
$outputFile ?? $this->outputFile,
$baseUrl,
$pagesDir === '' ? $this->pagesDir : $pagesDir,
$title,
);
}
/**
* @throws Exception
*/
private function createPost(
string $title,
string $slug,
string $categorySlug,
string $categoryName,
string $excerpt = 'An excerpt.',
): Post {
$category = $this->createStub(Category::class);
$category->method('getSlug')->willReturn($categorySlug);
$category->method('getName')->willReturn($categoryName);
$post = $this->createStub(Post::class);
$post->method('getTitle')->willReturn($title);
$post->method('getSlug')->willReturn($slug);
$post->method('getCategory')->willReturn($category);
$post->method('getExcerpt')->willReturn($excerpt);
return $post;
}
private function writeArticle(string $categorySlug, string $slug, string $title, string $description): void
{
$directory = $this->sourceDir . DIRECTORY_SEPARATOR . $categorySlug;
if (! is_dir($directory)) {
mkdir($directory, 0775, true);
}
file_put_contents(
$directory . DIRECTORY_SEPARATOR . $slug . '.md',
sprintf("---\ntitle: \"%s\"\ndescription: \"%s\"\n---\n\nBody.\n", $title, $description)
);
}
private function writePage(string $slug, string $title, ?string $description = 'A description.'): void
{
$frontMatter = $description === null
? sprintf("---\ntitle: \"%s\"\n---\n\nBody.\n", $title)
: sprintf("---\ntitle: \"%s\"\ndescription: \"%s\"\n---\n\nBody.\n", $title, $description);
file_put_contents($this->pagesDir . DIRECTORY_SEPARATOR . $slug . '.md', $frontMatter);
}
private function writtenOutput(): string
{
$contents = file_get_contents($this->outputFile);
$this->assertIsString($contents);
return $contents;
}
private function removeDirectory(string $directory): void
{
if (! is_dir($directory)) {
return;
}
foreach (array_diff((array) scandir($directory), ['.', '..']) as $entry) {
$path = $directory . DIRECTORY_SEPARATOR . $entry;
is_dir($path) ? $this->removeDirectory($path) : unlink($path);
}
rmdir($directory);
}
}