2
0

TipitakaChapterV3Test.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. use App\Models\Channel;
  3. use App\Models\PaliText;
  4. use App\Models\ProgressChapter;
  5. use App\Models\Tag;
  6. use App\Models\TagMap;
  7. use App\Services\V3\TipitakaChapterService;
  8. use Illuminate\Foundation\Testing\RefreshDatabase;
  9. use Illuminate\Support\Str;
  10. uses(RefreshDatabase::class);
  11. /*
  12. * GET /v3/tipitaka-chapters:按名字找章节(容错)、列出一本书的标题。
  13. *
  14. * 这里断言的是形状而非契约(spectator 未安装,见 v3-resource skill 第 3 步)。
  15. */
  16. function tcV3Title(int $book, int $para, int $level, string $toc, int $chapterLen = 10, ?array $path = null): PaliText
  17. {
  18. $row = new PaliText;
  19. $row->forceFill([
  20. 'book' => $book,
  21. 'paragraph' => $para,
  22. 'level' => $level,
  23. 'class' => '',
  24. 'toc' => $toc,
  25. 'text' => $toc,
  26. 'html' => '',
  27. 'pcd_book_id' => 0,
  28. 'chapter_len' => $chapterLen,
  29. 'chapter_strlen' => $chapterLen * 100,
  30. 'parent' => $level > 1 ? 3 : -1,
  31. 'prev_chapter' => 0,
  32. 'next_chapter' => -1,
  33. 'path' => json_encode($path ?? [['book' => $book, 'paragraph' => 3, 'title' => 'book', 'level' => '1']]),
  34. 'uid' => (string) Str::uuid(),
  35. ])->save();
  36. return $row;
  37. }
  38. function tcV3Tag(PaliText $book, string $name): void
  39. {
  40. $tag = Tag::where('name', $name)->first();
  41. if (! $tag) {
  42. $tag = new Tag;
  43. $tag->forceFill(['id' => (string) Str::uuid(), 'name' => $name, 'color' => 0, 'owner_id' => (string) Str::uuid()])->save();
  44. }
  45. (new TagMap)->forceFill([
  46. 'id' => (string) Str::uuid(), 'table_name' => 'pali_texts', 'anchor_id' => $book->uid, 'tag_id' => $tag->id,
  47. ])->save();
  48. }
  49. function tcV3Translation(int $book, int $para, string $title, int $status = 30, string $lang = 'zh'): void
  50. {
  51. $channel = makeChannel((string) Str::uuid());
  52. Channel::where('uid', $channel)->update(['status' => $status]);
  53. (new ProgressChapter)->forceFill([
  54. 'id' => random_int(1, PHP_INT_MAX),
  55. 'uid' => (string) Str::uuid(),
  56. 'book' => $book,
  57. 'para' => $para,
  58. 'lang' => $lang,
  59. 'channel_id' => $channel,
  60. 'progress' => 0.5,
  61. 'title' => $title."\n",
  62. 'all_trans' => 0,
  63. 'public' => 0,
  64. ])->save();
  65. }
  66. /**
  67. * 根本典 book 94(长部大品)与它的义注 book 104,各一部经;另有一部 anna 书里同名的短章。
  68. */
  69. function tcV3Fixture(): void
  70. {
  71. tcV3Tag(tcV3Title(94, 3, 1, '(DN) Mahāvaggapāḷi', 1000), 'mūla');
  72. tcV3Title(94, 287, 2, '3. Mahāparinibbānasuttaṃ', 376);
  73. tcV3Title(94, 585, 4, 'Tathāgatapacchimavācā', 4);
  74. tcV3Title(94, 663, 2, '4. Mahāsudassanasuttaṃ', 74);
  75. tcV3Tag(tcV3Title(104, 3, 1, '(DN) Mahāvaggaṭṭhakathā', 1000), 'aṭṭhakathā');
  76. tcV3Title(104, 420, 2, '3. Mahāparinibbānasuttavaṇṇanā', 300);
  77. tcV3Title(162, 3809, 4, '3. Ānandattheragāthā', 112);
  78. tcV3Title(162, 1539, 4, '3. Girimānandattheragāthā', 12);
  79. tcV3Translation(94, 287, '大般涅槃经');
  80. tcV3Translation(94, 663, '私密译名', 10);
  81. }
  82. it('lists a book in canonical order with coordinates and size', function () {
  83. tcV3Fixture();
  84. $response = $this->getJson('/api/v3/tipitaka-chapters?book=94')->assertOk();
  85. expect(array_column($response->json('data'), 'para'))->toBe([3, 287, 585, 663]);
  86. $sutta = $response->json('data.1');
  87. expect($sutta)->toMatchArray([
  88. 'book' => 94, 'para' => 287, 'level' => 2, 'title' => '3. Mahāparinibbānasuttaṃ',
  89. 'layer' => 'mula', 'paragraphs' => 376, 'chars' => 37600, 'parent' => 3, 'prev' => null, 'next' => null,
  90. ])->and($sutta['translations'])->toBe([['lang' => 'zh', 'title' => '大般涅槃经']])
  91. ->and($sutta['path'][0])->toBe(['book' => 94, 'para' => 3, 'title' => 'book', 'level' => 1])
  92. ->and($sutta)->not->toHaveKey('score');
  93. expect($response->json('meta.total'))->toBe(4);
  94. });
  95. it('narrows a book listing to one chapter and by level', function () {
  96. tcV3Fixture();
  97. $this->getJson('/api/v3/tipitaka-chapters?book=94&para=287')
  98. ->assertOk()
  99. ->assertJsonPath('data.*.para', [287, 585]);
  100. $this->getJson('/api/v3/tipitaka-chapters?book=94&max_level=2')
  101. ->assertOk()
  102. ->assertJsonPath('data.*.para', [3, 287, 663]);
  103. });
  104. it('finds a title regardless of case and diacritics, canonical text first', function () {
  105. tcV3Fixture();
  106. $data = $this->getJson('/api/v3/tipitaka-chapters?q=MAHAPARINIBBANA')->assertOk()->json('data');
  107. expect(array_map(fn ($r) => [$r['book'], $r['para'], $r['score']], $data))
  108. ->toBe([[94, 287, 0.95], [104, 420, 0.95]]);
  109. });
  110. it('tolerates misspellings and ranks the closer title first', function () {
  111. tcV3Fixture();
  112. $typo = $this->getJson('/api/v3/tipitaka-chapters?q=mahaparinibanasuta')->assertOk()->json('data');
  113. expect($typo[0]['para'])->toBe(287)
  114. ->and($typo[0]['score'])->toBeLessThan(0.86)->toBeGreaterThan(0.6);
  115. $ananda = $this->getJson('/api/v3/tipitaka-chapters?q=anandatheragatha')->assertOk()->json('data');
  116. expect($ananda[0]['para'])->toBe(3809);
  117. });
  118. it('finds a chapter by its public translated title only', function () {
  119. tcV3Fixture();
  120. $this->getJson('/api/v3/tipitaka-chapters?q='.urlencode('大般涅槃'))
  121. ->assertOk()
  122. ->assertJsonPath('data.*.para', [287]);
  123. $this->getJson('/api/v3/tipitaka-chapters?q='.urlencode('私密译名'))
  124. ->assertOk()
  125. ->assertJsonCount(0, 'data');
  126. $this->getJson('/api/v3/tipitaka-chapters?book=94&para=663')
  127. ->assertOk()
  128. ->assertJsonPath('data.0.translations', []);
  129. });
  130. it('filters by layer', function () {
  131. tcV3Fixture();
  132. $this->getJson('/api/v3/tipitaka-chapters?q=mahaparinibbana&layer=atthakatha')
  133. ->assertOk()
  134. ->assertJsonPath('data.*.book', [104]);
  135. });
  136. it('rejects a query without q or book, para without book, and unknown layers', function (string $query, string $field) {
  137. $this->getJson('/api/v3/tipitaka-chapters'.$query)
  138. ->assertUnprocessable()
  139. ->assertJsonValidationErrors($field);
  140. })->with([
  141. 'no condition' => ['', 'q'],
  142. 'para without book' => ['?q=x&para=287', 'book'],
  143. 'unknown layer' => ['?book=94&layer=sutta', 'layer'],
  144. 'per_page too large' => ['?book=94&per_page=5000', 'per_page'],
  145. ]);
  146. it('scores tiers so that misspellings never outrank correct spellings', function () {
  147. expect(TipitakaChapterService::score('mahaparinibbana', 'mahaparinibbana')[0])->toBe(1.0)
  148. ->and(TipitakaChapterService::score('mahaparinibbana', 'mahaparinibbanasuttam')[0])->toBe(0.95)
  149. ->and(TipitakaChapterService::score('parinibbana', 'mahaparinibbanasuttam')[0])->toBe(0.9)
  150. ->and(TipitakaChapterService::score('mahaparinibanasuta', 'mahaparinibbanasuttam')[0])->toBeLessThanOrEqual(0.85)
  151. ->and(TipitakaChapterService::score('vessantara', 'mahaparinibbanasuttam')[0])->toBe(0.0)
  152. ->and(TipitakaChapterService::normalize('(DN) 3. Mahāparinibbānasuttaṃ'))->toBe('mahaparinibbanasuttam');
  153. });