NissayaCardV3Test.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. use App\Models\DhammaTerm;
  3. use App\Models\NissayaEnding;
  4. use App\Models\Relation;
  5. use Illuminate\Foundation\Testing\RefreshDatabase;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Str;
  8. uses(RefreshDatabase::class);
  9. /*
  10. * GET /v3/nissaya-cards/{ending}:缅文词尾卡片(取代 v2 nissaya-card)。
  11. * 这里断言的是形状而非契约(spectator 未安装,见 v3-resource skill 第 3 步)。
  12. */
  13. function grammarTerm(string $channel, string $word, string $meaning, ?string $note = null): void
  14. {
  15. (new DhammaTerm)->forceFill([
  16. 'id' => random_int(1, PHP_INT_MAX), 'guid' => (string) Str::uuid(), 'word' => $word, 'word_en' => $word,
  17. 'meaning' => $meaning, 'note' => $note, 'channal' => $channel, 'language' => 'zh-Hans',
  18. 'owner' => config('mint.admin.root_uuid'), 'editor_id' => 0, 'content_type' => 'markdown-e2',
  19. 'create_time' => time() * 1000, 'modify_time' => time() * 1000,
  20. ])->save();
  21. }
  22. function relation(string $name, ?array $from, ?array $to, ?string $category = null): void
  23. {
  24. (new Relation)->forceFill([
  25. 'id' => (string) Str::uuid(), 'name' => $name, 'from' => $from ? json_encode($from) : null,
  26. 'to' => $to ? json_encode($to) : null, 'category' => $category, 'editor_id' => (string) Str::uuid(),
  27. ])->save();
  28. }
  29. function nissayaEnding(string $ending, string $relation, string $lang = 'my', ?array $from = null): void
  30. {
  31. (new NissayaEnding)->forceFill([
  32. 'id' => (string) Str::uuid(), 'ending' => $ending, 'relation' => $relation, 'lang' => $lang,
  33. 'from' => $from ? json_encode($from) : null, 'strlen' => mb_strlen($ending), 'count' => 0,
  34. 'editor_id' => (string) Str::uuid(),
  35. ])->save();
  36. }
  37. beforeEach(function () {
  38. $this->zh = makeChannel(config('mint.admin.root_uuid'), '_System_Grammar_Term_zh-hans_');
  39. });
  40. it('builds the card of an ending with its term and relations', function () {
  41. grammarTerm($this->zh, 'သည်', '主格语尾', '主格的**标记**。');
  42. grammarTerm($this->zh, 'SV', '主谓关系');
  43. grammarTerm($this->zh, 'nom', '主格');
  44. grammarTerm($this->zh, 'SV active', '主动分词', '说明**正文**。');
  45. relation('SV', ['case' => ['nom']], ['case' => ['nom', 'prp'], 'spell' => 'x'], 'SV active');
  46. relation('NOREL', null, null);
  47. nissayaEnding('သည်', 'SV');
  48. nissayaEnding('သည်', 'NOREL');
  49. nissayaEnding('是', 'SV', 'zh-Hans'); // 翻译建议:from 为空
  50. nissayaEnding('乃', 'SV', 'zh-Hans', ['case' => ['nom']]); // from 与关系相同
  51. nissayaEnding('非', 'SV', 'zh-Hans', ['case' => ['gen']]); // from 不同:不算
  52. $data = $this->getJson('/api/v3/nissaya-cards/'.rawurlencode('သည်').'?lang=zh-Hans')->assertOk()->json('data');
  53. expect($data['ending'])->toBe('သည်')->and($data['lang'])->toBe('zh-Hans');
  54. expect($data['term'])->toMatchArray(['word' => 'သည်', 'meaning' => '主格语尾', 'tag' => null])
  55. ->and($data['term']['body'])->toBeArray()->not->toBeEmpty();
  56. expect(array_column($data['relations'], 'relation'))->toBe(['NOREL', 'SV']);
  57. expect($data['relations'][0])->toMatchArray(['from' => null, 'to' => null, 'category' => null, 'relation_label' => null]);
  58. expect($data['relations'][1])->toMatchArray([
  59. 'relation_label' => '主谓关系',
  60. 'from' => ['cases' => [['case' => 'nom', 'label' => '主格']], 'spell' => null],
  61. 'to' => ['cases' => [['case' => 'nom', 'label' => '主格'], ['case' => 'prp', 'label' => null]], 'spell' => 'x'],
  62. 'category' => ['name' => 'SV active', 'meaning' => '主动分词', 'note' => '说明正文。'],
  63. 'local_endings' => ['是', '乃'],
  64. ]);
  65. });
  66. it('falls back to the English grammar terms, and returns an empty card for an unknown ending', function () {
  67. $en = makeChannel(config('mint.admin.root_uuid'), '_System_Grammar_Term_en_');
  68. grammarTerm($en, 'သည်', 'nominative ending');
  69. $this->getJson('/api/v3/nissaya-cards/'.rawurlencode('သည်').'?lang=th')
  70. ->assertOk()->assertJsonPath('data.lang', 'en')->assertJsonPath('data.term.meaning', 'nominative ending');
  71. $this->getJson('/api/v3/nissaya-cards/nothing?lang=zh-Hans')
  72. ->assertOk()->assertJsonPath('data.term', null)->assertJsonPath('data.relations', []);
  73. $this->getJson('/api/v3/nissaya-cards/x?lang='.urlencode('../x'))->assertUnprocessable()->assertJsonValidationErrors('lang');
  74. });
  75. it('builds a card with a constant number of queries', function () {
  76. $count = function (int $n) {
  77. $ending = "e{$n}";
  78. foreach (range(1, $n) as $i) {
  79. grammarTerm($this->zh, "case{$n}-{$i}", "格{$i}");
  80. relation("R{$n}-{$i}", ['case' => ["case{$n}-{$i}"]], ['case' => ["case{$n}-{$i}"]], "cat{$n}-{$i}");
  81. nissayaEnding($ending, "R{$n}-{$i}");
  82. }
  83. DB::flushQueryLog();
  84. DB::enableQueryLog();
  85. $this->getJson("/api/v3/nissaya-cards/{$ending}?lang=zh-Hans")->assertOk();
  86. $queries = count(DB::getQueryLog());
  87. DB::disableQueryLog();
  88. return $queries;
  89. };
  90. expect($count(6))->toBe($count(2));
  91. });