AgentRouteEnumTest.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. use App\Http\Controllers\V3\AdminAgentRunController;
  3. use App\Http\Resources\V3\AdminAgentFeedbackResource;
  4. use App\Http\Resources\V3\AdminAgentRunResource;
  5. use App\Models\AgentRun;
  6. use App\Models\AgentRunStep;
  7. use App\Models\UserInfo;
  8. use Illuminate\Foundation\Testing\RefreshDatabase;
  9. use Illuminate\Support\Facades\Log;
  10. use Illuminate\Support\Str;
  11. uses(RefreshDatabase::class);
  12. /**
  13. * 研究路径枚举(AgentRun::ROUTES)的唯一来源守卫,以及入库对未知路由 / 步骤类型的宽容。
  14. *
  15. * OpenAPI 的枚举来自 Resource `@return` 与控制器 `@queryParam … Enum:` 里写的字面量(PHPDoc 引用不了常量),
  16. * 这里断言它们与常量一致;backend 的离线测试再拿 OpenAPI 规格核对 routing.py。
  17. */
  18. it('declares the same route enum in every doc the OpenAPI spec is generated from', function () {
  19. foreach ([AdminAgentRunResource::class, AdminAgentFeedbackResource::class] as $resource) {
  20. $doc = (new ReflectionMethod($resource, 'toArray'))->getDocComment();
  21. expect(preg_match("/\\broute: ((?:'[a-z_]+'\\|)+)null/", $doc, $m))->toBe(1, "{$resource} 的 route 没写成字面量联合")
  22. ->and(explode('|', str_replace("'", '', rtrim($m[1], '|'))))->toBe(AgentRun::ROUTES);
  23. }
  24. $doc = (new ReflectionMethod(AdminAgentRunController::class, 'index'))->getDocComment();
  25. expect(preg_match('/@queryParam filter\[route\].*Enum: ([a-z_,]+)/', $doc, $m))->toBe(1)
  26. ->and(explode(',', $m[1]))->toBe(AgentRun::ROUTES);
  27. });
  28. it('keeps a trace with a route or step kind it does not know yet, and warns', function () {
  29. config(['mint.app.agent_service_key' => 'k']);
  30. Log::spy();
  31. $run = (string) Str::uuid();
  32. $this->postJson('/api/v3/agent-traces', [
  33. 'run_id' => $run, 'thread_id' => (string) Str::uuid(), 'device_id' => (string) Str::uuid(),
  34. 'question' => '新路由', 'status' => 'ok', 'route' => 'compare', 'answer' => '……',
  35. 'steps' => [['seq' => 0, 'kind' => 'rerank', 'name' => 'x'], ['seq' => 1, 'kind' => 'tool', 'name' => 'wikipali_search']],
  36. ], ['X-Agent-Service-Key' => 'k'])->assertCreated();
  37. expect(AgentRun::where('run_id', $run)->value('route'))->toBe('compare')
  38. ->and(AgentRunStep::where('agent_run_id', AgentRun::where('run_id', $run)->value('id'))->orderBy('seq')->pluck('kind')->all())->toBe(['rerank', 'tool']);
  39. Log::shouldHaveReceived('warning')->with('agent trace: unknown route', Mockery::on(fn ($c) => $c['route'] === 'compare'))->once();
  40. Log::shouldHaveReceived('warning')->with('agent trace: unknown step kinds', Mockery::on(fn ($c) => $c['kinds'] === ['rerank']))->once();
  41. });
  42. it('rejects an unknown route in the admin filter', function () {
  43. $uid = makeStudio('re-'.Str::random(5));
  44. UserInfo::where('userid', $uid)->update(['role' => json_encode(['administrator'])]);
  45. $this->getJson('/api/v3/admin/agent-runs?filter[route]=compare', authHeader($uid))->assertUnprocessable();
  46. $this->getJson('/api/v3/admin/agent-runs?filter[route]=term', authHeader($uid))->assertOk();
  47. });