2
0

PaliWordSuggestionV3Test.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. use App\Models\WordIndex;
  3. use Illuminate\Foundation\Testing\RefreshDatabase;
  4. uses(RefreshDatabase::class);
  5. /*
  6. * GET /v3/pali-word-suggestions:搜索框的巴利词补全(取代 v2 pali-word-index?view=key)。
  7. * 这里断言的是形状而非契约(spectator 未安装,见 v3-resource skill 第 3 步)。
  8. */
  9. function suggestWord(string $word, string $wordEn, int $count, int $bold = 0): void
  10. {
  11. WordIndex::forceCreate([
  12. 'id' => random_int(1, PHP_INT_MAX >> 33),
  13. 'word' => $word, 'word_en' => $wordEn, 'count' => $count, 'normal' => $count - $bold,
  14. 'bold' => $bold, 'is_base' => 0, 'len' => mb_strlen($word),
  15. ]);
  16. }
  17. it('completes a prefix, exact match first then by count, matching words without diacritics', function () {
  18. suggestWord('dhammā', 'dhamma', 19436, 413);
  19. suggestWord('dhammaṃ', 'dhammam', 19963, 739);
  20. suggestWord('dhammassa', 'dhammassa', 7423, 64);
  21. suggestWord('adhamma', 'adhamma', 500); // 不是前缀
  22. $this->getJson('/api/v3/pali-word-suggestions?q=Dhamma')
  23. ->assertOk()
  24. ->assertJsonPath('meta.total', 3)
  25. ->assertJsonPath('data', [
  26. ['word' => 'dhammā', 'count' => 19436, 'bold' => 413], // 去变音后与输入相同
  27. ['word' => 'dhammaṃ', 'count' => 19963, 'bold' => 739],
  28. ['word' => 'dhammassa', 'count' => 7423, 'bold' => 64],
  29. ]);
  30. $this->getJson('/api/v3/pali-word-suggestions?q=dhamma&limit=1')->assertJsonCount(1, 'data');
  31. });
  32. it('treats LIKE wildcards in the input literally', function () {
  33. suggestWord('dhammo', 'dhammo', 10);
  34. $this->getJson('/api/v3/pali-word-suggestions?q=dh%25')->assertOk()->assertJsonCount(0, 'data');
  35. $this->getJson('/api/v3/pali-word-suggestions?q=dh_mmo')->assertOk()->assertJsonCount(0, 'data');
  36. });
  37. it('rejects a missing prefix and a limit out of range', function () {
  38. $this->getJson('/api/v3/pali-word-suggestions')->assertStatus(422)->assertJsonValidationErrors('q');
  39. $this->getJson('/api/v3/pali-word-suggestions?q=a&limit=51')->assertStatus(422)->assertJsonValidationErrors('limit');
  40. });