2
0

UpgradeCommunityTermTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. use App\Models\Channel;
  3. use App\Models\DhammaTerm;
  4. use App\Services\V3\GlossaryService;
  5. use Illuminate\Foundation\Testing\RefreshDatabase;
  6. use Illuminate\Support\Str;
  7. uses(RefreshDatabase::class);
  8. /*
  9. * upgrade:community.term:社区术语表(_community_term_{lang}_)只由这个任务维护。
  10. * 历史上 tag 的 NULL / '' / 空白各建了一行,导致同一个词重复;跑一次之后应恢复成每个 word + tag 一行。
  11. */
  12. function communityTerm(string $channel, string $word, ?string $tag, 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. 'tag' => $tag,
  21. 'meaning' => "{$word}-义",
  22. 'channal' => $channel,
  23. 'language' => 'zh-Hans',
  24. 'owner' => config('mint.admin.root_uuid'),
  25. 'editor_id' => 0,
  26. 'create_time' => time() * 1000,
  27. 'modify_time' => time() * 1000,
  28. ])->save();
  29. return $term;
  30. }
  31. beforeEach(function () {
  32. $root = config('mint.admin.root_uuid');
  33. makeChannel($root, '_System_Pali_VRI_');
  34. $this->community = makeChannel($root, '_community_term_zh-hans_');
  35. $this->user = makeChannel($root, 'user channel');
  36. });
  37. function communityRows(string $channel): array
  38. {
  39. return DhammaTerm::where('channal', $channel)->orderBy('word')->orderBy('tag')->get()
  40. ->map(fn ($t) => [$t->word, $t->tag, $t->meaning])->all();
  41. }
  42. it('merges NULL, empty and blank tags into one row per word', function () {
  43. communityTerm($this->user, 'dhamma', null, ['meaning' => '法']);
  44. communityTerm($this->user, 'dhamma', '', ['meaning' => '法']);
  45. communityTerm($this->user, 'dhamma', ' ', ['meaning' => '法']);
  46. communityTerm($this->user, 'dhamma', 'ārammaṇa', ['meaning' => '法']);
  47. $this->artisan('upgrade:community.term', ['lang' => 'zh-Hans'])->assertSuccessful();
  48. expect(communityRows($this->community))->toBe([
  49. ['dhamma', 'ārammaṇa', '法'],
  50. ['dhamma', null, '法'],
  51. ]);
  52. });
  53. it('heals existing duplicates, keeping the oldest row, and is stable on rerun', function () {
  54. communityTerm($this->user, 'sīla', '', ['meaning' => '戒']);
  55. $oldest = communityTerm($this->community, 'sīla', null, ['create_time' => 1000]);
  56. $dupA = communityTerm($this->community, 'sīla', '', ['create_time' => 2000]);
  57. $dupB = communityTerm($this->community, 'sīla', ' ', ['create_time' => 3000]);
  58. $this->artisan('upgrade:community.term', ['lang' => 'zh-Hans'])->assertSuccessful();
  59. $this->artisan('upgrade:community.term', ['lang' => 'zh-Hans'])->assertSuccessful();
  60. expect(communityRows($this->community))->toBe([['sīla', null, '戒']])
  61. ->and(DhammaTerm::where('channal', $this->community)->value('guid'))->toBe($oldest->guid)
  62. ->and(DhammaTerm::onlyTrashed()->whereIn('guid', [$dupA->guid, $dupB->guid])->count())->toBe(2);
  63. });
  64. it('does not count its own rows as sources: rows without user terms are removed', function () {
  65. communityTerm($this->user, 'dhamma', null, ['meaning' => '法']);
  66. // 社区里自己的一票不该影响译名,也不该让没有来源的行继续存在
  67. communityTerm($this->community, 'dhamma', null, ['meaning' => '旧译名']);
  68. communityTerm($this->community, 'orphan', null);
  69. $this->artisan('upgrade:community.term', ['lang' => 'zh-Hans'])->assertSuccessful();
  70. expect(communityRows($this->community))->toBe([['dhamma', null, '法']]);
  71. });
  72. it('never takes the note from an AI-generated channel, but still counts its meaning', function () {
  73. $ai = makeChannel(config('mint.admin.root_uuid'), 'ai channel');
  74. Channel::where('uid', $ai)->update(['source_type' => 'ai']);
  75. communityTerm($ai, 'dhamma', null, ['meaning' => '法', 'note' => 'AI 写的注释']);
  76. communityTerm($ai, 'dhamma', null, ['meaning' => '法']);
  77. communityTerm($this->user, 'dhamma', null, ['meaning' => '达摩']);
  78. $this->artisan('upgrade:community.term', ['lang' => 'zh-Hans'])->assertSuccessful();
  79. $term = DhammaTerm::where('channal', $this->community)->sole();
  80. expect($term->meaning)->toBe('法')
  81. ->and((string) $term->note)->toBe('');
  82. });
  83. it('rotates the community glossary ETag after each run', function () {
  84. communityTerm($this->user, 'dhamma', null);
  85. $glossaries = app(GlossaryService::class);
  86. $before = $glossaries->etag('community', 'zh-Hans');
  87. $this->artisan('upgrade:community.term', ['lang' => 'zh-Hans'])->assertSuccessful();
  88. expect($glossaries->etag('community', 'zh-Hans'))->not->toBe($before);
  89. });
  90. it('only touches the given word when one is passed', function () {
  91. communityTerm($this->user, 'dhamma', null);
  92. communityTerm($this->user, 'sīla', null);
  93. communityTerm($this->community, 'sīla', '');
  94. communityTerm($this->community, 'sīla', null);
  95. $this->artisan('upgrade:community.term', ['lang' => 'zh-Hans', 'word' => 'dhamma'])->assertSuccessful();
  96. expect(communityRows($this->community))->toHaveCount(3);
  97. });
  98. it('refuses to wipe the channel when no source terms are found', function () {
  99. communityTerm($this->community, 'dhamma', null);
  100. $this->artisan('upgrade:community.term', ['lang' => 'zh-Hans'])->assertFailed();
  101. expect(communityRows($this->community))->toHaveCount(1);
  102. });