SearchSuggestionV3Test.php 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. use App\Services\OpenSearchService;
  3. use GuzzleHttp\Exception\ConnectException;
  4. use GuzzleHttp\Psr7\Request as PsrRequest;
  5. /*
  6. * /v3/search-suggestions(搜索框前缀补全)。
  7. *
  8. * 这里断言的是形状而非契约(spectator 未安装,见 v3-resource skill 第 3 步)。OpenSearch 一律 mock。
  9. */
  10. /** OpenSearchService::suggest 的一条原始结果 */
  11. function rawSuggestion(string $text, string $field, float $score, string $docId = 'doc'): array
  12. {
  13. return [
  14. 'text' => $text, 'source' => $field, 'score' => $score, 'doc_id' => $docId,
  15. 'doc_source' => ['resource_type' => 'tipitaka', 'language' => 'pi', 'granularity' => 'paragraph', 'category' => ['sutta']],
  16. ];
  17. }
  18. /** 让 suggest 返回给定结果,收到的参数记进 $captured */
  19. function fakeSuggestions(array $rows, ?array &$captured = null): void
  20. {
  21. test()->mock(OpenSearchService::class)
  22. ->shouldReceive('suggest')
  23. ->andReturnUsing(function (...$args) use ($rows, &$captured) {
  24. $captured = $args;
  25. return $rows;
  26. });
  27. }
  28. it('merges fields, drops duplicate words and caps the total', function () {
  29. $captured = null;
  30. fakeSuggestions([
  31. rawSuggestion('dukkhasutta', 'title_pali', 3.0, 'a'),
  32. rawSuggestion('Dukkhasutta', 'content_pali', 2.5, 'b'), // 同一个词,分数低的丢掉
  33. rawSuggestion('dukkha', 'content_pali', 2.0, 'c'),
  34. rawSuggestion('dukkhatā', 'content_pali', 1.0, 'd'),
  35. ], $captured);
  36. $res = $this->getJson('/api/v3/search-suggestions?q=dukkha&limit=2')->assertSuccessful();
  37. expect($res->json('data'))->toHaveCount(2)
  38. ->and($res->json('data.0'))->toBe([
  39. 'text' => 'dukkhasutta', 'source' => 'title_pali', 'score' => 3, 'doc_id' => 'a',
  40. 'resource_type' => 'tipitaka', 'language' => 'pi', 'granularity' => 'paragraph', 'category' => ['sutta'],
  41. ])
  42. ->and($res->json('data.1.text'))->toBe('dukkha')
  43. ->and($res->json('meta.total'))->toBe(2);
  44. // 默认查全部四个字段,每个字段取满 limit 再统一截断
  45. expect($captured)->toBe(['dukkha', ['title_pali', 'title_zh', 'content_pali', 'content_zh'], null, 2]);
  46. });
  47. it('passes only the requested fields', function () {
  48. $captured = null;
  49. fakeSuggestions([], $captured);
  50. $this->getJson('/api/v3/search-suggestions?q=nibb&fields=title_pali, content_pali')
  51. ->assertSuccessful()
  52. ->assertJsonPath('data', [])
  53. ->assertJsonPath('meta.total', 0);
  54. expect($captured[1])->toBe(['title_pali', 'content_pali'])
  55. ->and($captured[3])->toBe(10);
  56. });
  57. it('rejects invalid parameters', function (array $query, string $field) {
  58. $this->getJson('/api/v3/search-suggestions?'.http_build_query($query))
  59. ->assertUnprocessable()
  60. ->assertJsonValidationErrors($field)
  61. ->assertHeader('Content-Type', 'application/problem+json');
  62. })->with([
  63. 'missing q' => [[], 'q'],
  64. 'empty q' => [['q' => ''], 'q'],
  65. 'unknown field' => [['q' => 'd', 'fields' => 'title'], 'fields'],
  66. 'only commas' => [['q' => 'd', 'fields' => ',,'], 'fields'],
  67. 'limit too large' => [['q' => 'd', 'limit' => 51], 'limit'],
  68. ]);
  69. it('answers 503 when the search cluster is unreachable', function () {
  70. test()->mock(OpenSearchService::class)
  71. ->shouldReceive('suggest')
  72. ->andThrow(new ConnectException('Connection refused', new PsrRequest('POST', 'http://opensearch')));
  73. $res = $this->getJson('/api/v3/search-suggestions?q=dukkha')
  74. ->assertServiceUnavailable()
  75. ->assertJsonPath('type', 'urn:problem:search-unavailable');
  76. expect($res->getContent())->not->toContain('Connection refused');
  77. });
  78. it('no longer serves the old search-suggest path', function () {
  79. $this->getJson('/api/v3/search-suggest?q=dukkha')->assertNotFound();
  80. });