TipitakaWordParagraphV3Test.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. use App\Models\BookTitle;
  3. use App\Models\PageNumber;
  4. use App\Models\PaliText;
  5. use App\Models\Tag;
  6. use App\Models\TagMap;
  7. use App\Models\WbwTemplate;
  8. use Illuminate\Foundation\Testing\RefreshDatabase;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Str;
  11. uses(RefreshDatabase::class);
  12. /*
  13. * GET /v3/tipitaka-word-paragraphs (+ /aggregate):按词形找段落、按书统计(取代 v2 search-pali-wbw / -books)。
  14. * 这里断言的是形状而非契约(spectator 未安装,见 v3-resource skill 第 3 步)。
  15. */
  16. function wbwToken(int $book, int $para, string $real, array $attrs = []): void
  17. {
  18. WbwTemplate::forceCreate($attrs + [
  19. 'book' => $book, 'paragraph' => $para, 'wid' => 1, 'word' => $real, 'real' => $real,
  20. 'type' => '', 'gramma' => '', 'part' => '', 'style' => '', 'pcd_book_id' => $book, 'weight' => 1,
  21. ]);
  22. }
  23. function wordPara(int $book, int $para, string $html, array $attrs = []): PaliText
  24. {
  25. return PaliText::forceCreate($attrs + [
  26. 'book' => $book, 'paragraph' => $para, 'level' => 100, 'class' => '', 'toc' => '', 'text' => '',
  27. 'html' => $html, 'uid' => (string) Str::uuid(),
  28. 'path' => json_encode([
  29. ['book' => $book, 'paragraph' => 1, 'title' => "Book {$book}", 'level' => '1'],
  30. ['book' => $book, 'paragraph' => 2, 'title' => 'Chapter', 'level' => 2],
  31. ]),
  32. ]);
  33. }
  34. it('lists matching paragraphs by weight with path, safe highlighted text and page refs', function () {
  35. wordPara(1, 10, '<p class="bodytext">Vitakketīti <span class="bld">vitakko</span>. <script>x</script> &amp; vitakkaṃ</p>');
  36. wbwToken(1, 10, 'vitakko', ['style' => 'bld', 'weight' => 10]);
  37. wbwToken(1, 10, 'vitakkaṃ', ['weight' => 1]);
  38. wordPara(1, 20, '<p>vitakko</p>');
  39. wbwToken(1, 20, 'vitakko', ['weight' => 1]);
  40. PageNumber::forceCreate(['type' => 'P', 'volume' => 2, 'page' => 300, 'book' => 1, 'paragraph' => 10, 'wid' => 5, 'pcd_book_id' => 1]);
  41. PageNumber::forceCreate(['type' => 'P', 'volume' => 2, 'page' => 299, 'book' => 1, 'paragraph' => 10, 'wid' => 1, 'pcd_book_id' => 1]);
  42. $res = $this->getJson('/api/v3/tipitaka-word-paragraphs?words=vitakko,vitakkaṃ')->assertOk();
  43. $res->assertJsonPath('meta.total', 2)
  44. ->assertJsonPath('data.*.id', ['1-10', '1-20'])
  45. ->assertJsonPath('data.0.score', 11)
  46. ->assertJsonPath('data.0.hits', 2)
  47. ->assertJsonPath('data.0.title', 'Chapter')
  48. ->assertJsonPath('data.0.path.0', ['book' => 1, 'para' => 1, 'title' => 'Book 1', 'level' => 1])
  49. ->assertJsonPath('data.0.refs.0', ['type' => 'PTS', 'volume' => 2, 'page' => 299, 'title' => 'Nam'])
  50. ->assertJsonPath('data.0.refs.1.type', 'WP');
  51. // 只有 <b> 与 <mark>,其余已转义;「Vitakketīti」不是命中词形,不标
  52. expect($res->json('data.0.text'))
  53. ->toBe('Vitakketīti <b><mark>vitakko</mark></b>. x &amp; <mark>vitakkaṃ</mark>');
  54. });
  55. it('filters bold-only, bold-excluded and by book_id', function () {
  56. wordPara(1, 1, '<p>a</p>');
  57. wordPara(2, 1, '<p>b</p>');
  58. wbwToken(1, 1, 'vitakko', ['style' => 'bld']);
  59. wbwToken(2, 1, 'vitakko');
  60. $this->getJson('/api/v3/tipitaka-word-paragraphs?words=vitakko&bold=only')->assertJsonPath('data.*.id', ['1-1']);
  61. $this->getJson('/api/v3/tipitaka-word-paragraphs?words=vitakko&bold=exclude')->assertJsonPath('data.*.id', ['2-1']);
  62. $this->getJson('/api/v3/tipitaka-word-paragraphs?words=vitakko&book_id=2')->assertJsonPath('data.*.id', ['2-1']);
  63. });
  64. it('never matches the blank tokens when words has empty items', function () {
  65. wordPara(1, 1, '<p>a</p>');
  66. wbwToken(1, 1, '');
  67. $this->getJson('/api/v3/tipitaka-word-paragraphs?words=,,')->assertStatus(422)->assertJsonValidationErrors('words');
  68. });
  69. it('rejects invalid parameters', function (array $query, string $field) {
  70. $this->getJson('/api/v3/tipitaka-word-paragraphs?'.http_build_query($query))
  71. ->assertStatus(422)
  72. ->assertJsonValidationErrors($field);
  73. })->with([
  74. 'missing words' => [[], 'words'],
  75. 'bad bold' => [['words' => 'x', 'bold' => 'on'], 'bold'],
  76. 'bad book_id' => [['words' => 'x', 'book_id' => '1,a'], 'book_id'],
  77. 'per_page too large' => [['words' => 'x', 'per_page' => 101], 'per_page'],
  78. 'too many words' => [['words' => implode(',', range(1, 201))], 'words'],
  79. ]);
  80. it('does not query per row', function () {
  81. $queries = function (int $rows): int {
  82. WbwTemplate::query()->delete();
  83. for ($i = 1; $i <= $rows; $i++) {
  84. wordPara(1, 100 + $i, '<p>vitakko</p>');
  85. wbwToken(1, 100 + $i, 'vitakko');
  86. PageNumber::forceCreate(['type' => 'M', 'volume' => 1, 'page' => $i, 'book' => 1, 'paragraph' => 100 + $i, 'wid' => 1, 'pcd_book_id' => 1]);
  87. }
  88. DB::flushQueryLog();
  89. DB::enableQueryLog();
  90. $this->getJson('/api/v3/tipitaka-word-paragraphs?words=vitakko')->assertOk()->assertJsonCount($rows, 'data');
  91. return count(DB::getQueryLog());
  92. };
  93. expect($queries(2))->toBe($queries(10));
  94. });
  95. it('aggregates paragraphs and occurrences per book with layer from the book tags', function () {
  96. $head = wordPara(5, 3, '', ['level' => 1, 'toc' => 'Dhammasaṅgaṇīpāḷi']);
  97. BookTitle::forceCreate(['sn' => 79, 'book' => 5, 'paragraph' => 3, 'title' => 'dhs']);
  98. $owner = (string) Str::uuid();
  99. foreach (['abhidhamma', 'mūla'] as $name) {
  100. $tag = Tag::forceCreate(['name' => $name, 'owner_id' => $owner]);
  101. TagMap::forceCreate(['table_name' => 'pali_texts', 'anchor_id' => $head->uid, 'tag_id' => $tag->id]);
  102. }
  103. wbwToken(5, 10, 'vitakko', ['pcd_book_id' => 79]);
  104. wbwToken(5, 10, 'vitakkaṃ', ['pcd_book_id' => 79]);
  105. wbwToken(5, 11, 'vitakko', ['pcd_book_id' => 79]);
  106. wbwToken(9, 1, 'vitakko', ['pcd_book_id' => 99]); // 没有 book_titles 的书
  107. $this->getJson('/api/v3/tipitaka-word-paragraphs/aggregate?words=vitakko,vitakkaṃ')
  108. ->assertOk()
  109. ->assertJsonPath('meta', ['total' => 2, 'paragraphs' => 3, 'occurrences' => 4])
  110. ->assertJsonPath('data.0.book_id', 79)
  111. ->assertJsonPath('data.0.title', 'Dhammasaṅgaṇīpāḷi')
  112. ->assertJsonPath('data.0.layer', 'mula')
  113. ->assertJsonPath('data.0.paragraphs', 2)
  114. ->assertJsonPath('data.0.occurrences', 3)
  115. ->assertJsonPath('data.1.book_id', 99)
  116. ->assertJsonPath('data.1.layer', null);
  117. });
  118. it('requires words on the aggregate endpoint', function () {
  119. $this->getJson('/api/v3/tipitaka-word-paragraphs/aggregate')->assertStatus(422)->assertJsonValidationErrors('words');
  120. });