GlossaryV3Test.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. use App\Models\DhammaTerm;
  3. use App\Services\V3\GlossaryService;
  4. use Illuminate\Foundation\Testing\RefreshDatabase;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Str;
  7. uses(RefreshDatabase::class);
  8. /*
  9. * GET /v3/glossaries/{grammar|community}:系统 channel 里的术语,整表、按词排序。
  10. * 这里断言的是形状而非契约(spectator 未安装,见 v3-resource skill 第 3 步)。
  11. */
  12. function glossaryTerm(string $channel, string $word, array $attrs = []): DhammaTerm
  13. {
  14. $term = new DhammaTerm;
  15. $term->forceFill($attrs + [
  16. 'id' => random_int(1, PHP_INT_MAX),
  17. 'guid' => (string) Str::uuid(),
  18. 'word' => $word,
  19. 'word_en' => $word,
  20. 'meaning' => "{$word}-义",
  21. 'channal' => $channel,
  22. 'language' => 'zh-Hans',
  23. 'owner' => config('mint.admin.root_uuid'),
  24. 'editor_id' => 0,
  25. 'create_time' => time() * 1000,
  26. 'modify_time' => time() * 1000,
  27. ])->save();
  28. return $term;
  29. }
  30. it('lists the community glossary of a language, sorted by word', function () {
  31. $root = config('mint.admin.root_uuid');
  32. $zh = makeChannel($root, '_community_term_zh-hans_');
  33. $grammar = makeChannel($root, '_System_Grammar_Term_zh-hans_');
  34. glossaryTerm($zh, 'sīla', ['tag' => '', 'other_meaning' => '戒律']);
  35. glossaryTerm($zh, 'dhamma');
  36. glossaryTerm($grammar, 'vibhatti');
  37. glossaryTerm($zh, 'deleted')->delete();
  38. $this->getJson('/api/v3/glossaries/community?lang=zh-Hans')
  39. ->assertOk()
  40. ->assertJsonPath('meta', ['kind' => 'community', 'lang' => 'zh-Hans', 'total' => 2])
  41. ->assertJsonPath('data.*.word', ['dhamma', 'sīla'])
  42. ->assertJsonPath('data.1.tag', null)
  43. ->assertJsonPath('data.1.other_meaning', '戒律');
  44. $this->getJson('/api/v3/glossaries/grammar?lang=zh-Hans')->assertJsonPath('data.*.word', ['vibhatti']);
  45. });
  46. it('falls back to English, and defaults to the negotiated interface language', function () {
  47. $root = config('mint.admin.root_uuid');
  48. $en = makeChannel($root, '_community_term_en_');
  49. $zh = makeChannel($root, '_community_term_zh-hans_');
  50. glossaryTerm($en, 'en-word');
  51. glossaryTerm($zh, 'zh-word');
  52. $this->getJson('/api/v3/glossaries/community?lang=my')
  53. ->assertJsonPath('meta.lang', 'en')
  54. ->assertJsonPath('data.*.word', ['en-word']);
  55. $this->getJson('/api/v3/glossaries/community', ['Accept-Language' => 'zh-CN'])
  56. ->assertJsonPath('meta.lang', 'zh-Hans')
  57. ->assertJsonPath('data.*.word', ['zh-word']);
  58. });
  59. it('returns an empty list when neither the language nor English has the glossary', function () {
  60. $this->getJson('/api/v3/glossaries/grammar?lang=zh-Hans')
  61. ->assertOk()
  62. ->assertJsonPath('data', [])
  63. ->assertJsonPath('meta.total', 0);
  64. });
  65. it('rejects an unknown kind or a malformed language', function () {
  66. $this->getJson('/api/v3/glossaries/other')->assertNotFound();
  67. $this->getJson('/api/v3/glossaries/community?lang='.urlencode('../x'))
  68. ->assertUnprocessable()->assertJsonValidationErrors('lang');
  69. });
  70. it('gives the term guid only in the grammar glossary, and never the auto-increment id', function () {
  71. $root = config('mint.admin.root_uuid');
  72. $grammar = glossaryTerm(makeChannel($root, '_System_Grammar_Term_zh-hans_'), 'vibhatti');
  73. $community = glossaryTerm(makeChannel($root, '_community_term_zh-hans_'), 'dhamma');
  74. $row = $this->getJson('/api/v3/glossaries/grammar?lang=zh-Hans')->json('data.0');
  75. expect(array_keys($row))->toBe(['id', 'word', 'tag', 'meaning', 'other_meaning'])
  76. ->and($row['id'])->toBe($grammar->guid)
  77. ->and(json_encode($row))->not->toContain((string) $grammar->id);
  78. $row = $this->getJson('/api/v3/glossaries/community?lang=zh-Hans')->json('data.0');
  79. expect(array_keys($row))->toBe(['word', 'tag', 'meaning', 'other_meaning'])
  80. ->and(json_encode($row))->not->toContain($community->guid)
  81. ->and(json_encode($row))->not->toContain((string) $community->id);
  82. });
  83. it('answers 304 for an unchanged community glossary without loading it, and 200 after the task rotates the ETag', function () {
  84. $zh = makeChannel(config('mint.admin.root_uuid'), '_community_term_zh-hans_');
  85. glossaryTerm($zh, 'dhamma');
  86. $first = $this->getJson('/api/v3/glossaries/community?lang=zh-Hans')->assertOk();
  87. $etag = $first->headers->get('ETag');
  88. expect($etag)->not->toBeEmpty()
  89. ->and($first->headers->get('Cache-Control'))->toContain('no-cache')->toContain('public')
  90. ->and($first->headers->get('Vary'))->toContain('Accept-Language');
  91. DB::enableQueryLog();
  92. $this->getJson('/api/v3/glossaries/community?lang=zh-Hans', ['If-None-Match' => $etag])
  93. ->assertStatus(304)
  94. ->assertHeader('ETag', $etag)
  95. ->assertContent('');
  96. // nginx gzip 会把 ETag 改成弱校验 W/"…",浏览器带回来的也是它
  97. $this->getJson('/api/v3/glossaries/community?lang=zh-Hans', ['If-None-Match' => 'W/'.$etag])->assertStatus(304);
  98. $loaded = collect(DB::getQueryLog())->contains(fn ($q) => str_contains($q['query'], 'dhamma_terms'));
  99. expect($loaded)->toBeFalse();
  100. app(GlossaryService::class)->rotateEtag($zh);
  101. $fresh = $this->getJson('/api/v3/glossaries/community?lang=zh-Hans', ['If-None-Match' => $etag])->assertOk();
  102. expect($fresh->headers->get('ETag'))->not->toBe($etag)
  103. ->and($fresh->json('data.*.word'))->toBe(['dhamma']);
  104. });
  105. it('gives no ETag for the grammar glossary or a missing table', function () {
  106. $grammar = makeChannel(config('mint.admin.root_uuid'), '_System_Grammar_Term_zh-hans_');
  107. glossaryTerm($grammar, 'vibhatti');
  108. expect($this->getJson('/api/v3/glossaries/grammar?lang=zh-Hans')->assertOk()->headers->has('ETag'))->toBeFalse()
  109. ->and($this->getJson('/api/v3/glossaries/community?lang=zh-Hans')->assertOk()->headers->has('ETag'))->toBeFalse();
  110. });