2
0

UpgradeCommunityTermTest.php 4.8 KB

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