ChannelBatchProgressV3Test.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. use App\Models\Channel;
  3. use Illuminate\Foundation\Testing\RefreshDatabase;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Str;
  6. uses(RefreshDatabase::class);
  7. /**
  8. * POST /api/v3/channels/batch-progress:一批句子在各 channel 的完成度(取代 v2 channel-progress)。
  9. *
  10. * 这里断言的是形状而非契约(spectator 未安装,见 v3-resource skill 第 3 步)。
  11. */
  12. function paliOriginal(): string
  13. {
  14. config(['mint.admin.root_uuid' => config('mint.admin.root_uuid') ?: (string) Str::uuid()]);
  15. $root = config('mint.admin.root_uuid');
  16. $existing = Channel::where('name', '_System_Pali_VRI_')->where('owner_uid', $root)->value('uid');
  17. return $existing ?? makeChannel($root, '_System_Pali_VRI_');
  18. }
  19. it('reports per-sentence progress for visible channels with content plus my writable ones', function () {
  20. $pali = paliOriginal();
  21. makeSentence($pali, 1, 2, 1, str_repeat('a', 10));
  22. makeSentence($pali, 1, 2, 5, str_repeat('b', 20));
  23. $me = makeStudio('me');
  24. $public = makeChannel(makeStudio('a'), 'public translation');
  25. makeSentence($public, 1, 2, 1, 'done');
  26. $privateOther = makeChannel(makeStudio('b'), 'private other');
  27. Channel::where('uid', $privateOther)->update(['status' => Channel::STATUS_PRIVATE]);
  28. makeSentence($privateOther, 1, 2, 1, 'secret');
  29. $myEmpty = makeChannel($me, 'my empty');
  30. Channel::where('uid', $myEmpty)->update(['status' => Channel::STATUS_PRIVATE]);
  31. $data = collect($this->postJson('/api/v3/channels/batch-progress', [
  32. 'sentences' => ['1-2-1-1', '1-2-5-5'],
  33. ], authHeader($me))->assertOk()->json('data'))->keyBy('id');
  34. // 原文 channel 自己也有内容且公开,所以也会出现;这里只看关心的几个
  35. expect($data->has($privateOther))->toBeFalse();
  36. expect($data[$public]['final'])->toBe([[10, true], [20, false]]);
  37. expect($data[$public]['abilities']['write'])->toBeFalse();
  38. expect($data[$public]['content_created_at'])->not->toBeNull();
  39. expect($data[$myEmpty]['final'])->toBeNull();
  40. expect($data[$myEmpty]['abilities']['write'])->toBeTrue();
  41. });
  42. it('only lists a studio\'s channels when studio is given', function () {
  43. paliOriginal();
  44. [$team, $users] = makeTeam();
  45. $teamChannel = makeChannel($team, 'team channel');
  46. makeSentence($teamChannel, 1, 2, 1, 'x');
  47. $other = makeChannel(makeStudio('a'), 'other');
  48. makeSentence($other, 1, 2, 1, 'y');
  49. $ids = array_column($this->postJson('/api/v3/channels/batch-progress', [
  50. 'sentences' => ['1-2-1-1'], 'studio' => $team,
  51. ], authHeader($users['reader']))->assertOk()->json('data'), 'id');
  52. expect($ids)->toBe([$teamChannel]);
  53. $this->postJson('/api/v3/channels/batch-progress', ['sentences' => ['1-2-1-1'], 'studio' => $team], authHeader($users['outsider']))
  54. ->assertForbidden();
  55. });
  56. it('works without login, public channels only', function () {
  57. paliOriginal();
  58. $public = makeChannel(makeStudio('a'), 'public');
  59. makeSentence($public, 1, 2, 1, 'x');
  60. expect(array_column($this->postJson('/api/v3/channels/batch-progress', ['sentences' => ['1-2-1-1']])->assertOk()->json('data'), 'id'))
  61. ->toContain($public);
  62. });
  63. it('validates the sentence list', function (array $payload, string $field) {
  64. $this->postJson('/api/v3/channels/batch-progress', $payload)
  65. ->assertUnprocessable()
  66. ->assertJsonValidationErrors($field);
  67. })->with([
  68. 'missing' => [[], 'sentences'],
  69. 'bad id' => [['sentences' => ['1-2-3']], 'sentences.0'],
  70. 'too many' => [['sentences' => array_fill(0, 2001, '1-1-1-1')], 'sentences'],
  71. ]);
  72. it('keeps a fixed number of queries regardless of channel count', function () {
  73. paliOriginal();
  74. $me = makeStudio('me');
  75. $countQueriesFor = function (int $extra) use ($me): int {
  76. for ($i = 0; $i < $extra; $i++) {
  77. makeSentence(makeChannel(makeStudio('o-'.Str::random(8))), 3, 3, 1, 'x');
  78. }
  79. DB::flushQueryLog();
  80. DB::enableQueryLog();
  81. $this->postJson('/api/v3/channels/batch-progress', ['sentences' => ['3-3-1-1']], authHeader($me))->assertOk();
  82. $count = count(DB::getQueryLog());
  83. DB::disableQueryLog();
  84. return $count;
  85. };
  86. expect($countQueriesFor(6))->toBe($countQueriesFor(1));
  87. });