2
0

AgentThreadV3Test.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. use App\Models\AgentRun;
  3. use App\Models\AgentThread;
  4. use Illuminate\Foundation\Testing\RefreshDatabase;
  5. use Illuminate\Support\Str;
  6. uses(RefreshDatabase::class);
  7. /**
  8. * v3 me/agent-threads:登录用户的 AI 对话会话历史(web chat 与 mobile 共用)。
  9. *
  10. * GET /api/v3/me/agent-threads 列表(filter[archived])
  11. * GET /api/v3/me/agent-threads/{thread} 详情(带可见的问答轮次)
  12. * PATCH /api/v3/me/agent-threads/{thread} 改标题
  13. * DELETE /api/v3/me/agent-threads/{thread} 删除(软删,返回 200 + 资源)
  14. * PUT /api/v3/me/agent-threads/{thread}/archive 存档
  15. * DELETE /api/v3/me/agent-threads/{thread}/archive 取消存档
  16. *
  17. * 这里断言的是形状而非契约(spectator 尚未安装,见 v3-resource skill 第 3 步)。
  18. */
  19. /* ------------------------------------------------------------------ 列表 */
  20. it('lists my threads, most recent first', function () {
  21. $me = (string) Str::uuid();
  22. $older = AgentThread::factory()->forUser($me)->create(['last_run_at' => now()->subDay()]);
  23. $newer = AgentThread::factory()->forUser($me)->create(['last_run_at' => now()]);
  24. AgentThread::factory()->forUser((string) Str::uuid())->create();
  25. AgentThread::factory()->create();
  26. $data = $this->getJson('/api/v3/me/agent-threads', authHeader($me))
  27. ->assertOk()
  28. ->assertJsonStructure(['data' => [['thread_id', 'title', 'archived_at', 'last_run_at']], 'meta' => ['total']])
  29. ->json('data');
  30. expect(array_column($data, 'thread_id'))->toBe([$newer->thread_id, $older->thread_id])
  31. ->and($data[0])->not->toHaveKey('runs');
  32. });
  33. it('filters threads by archived', function () {
  34. $me = (string) Str::uuid();
  35. $archived = AgentThread::factory()->forUser($me)->archived()->create();
  36. $active = AgentThread::factory()->forUser($me)->create();
  37. expect($this->getJson('/api/v3/me/agent-threads?filter[archived]=true', authHeader($me))->json('data.*.thread_id'))
  38. ->toBe([$archived->thread_id])
  39. ->and($this->getJson('/api/v3/me/agent-threads?filter[archived]=false', authHeader($me))->json('data.*.thread_id'))
  40. ->toBe([$active->thread_id]);
  41. });
  42. it('does not list deleted threads', function () {
  43. $me = (string) Str::uuid();
  44. AgentThread::factory()->forUser($me)->create(['deleted_at' => now()]);
  45. $this->getJson('/api/v3/me/agent-threads', authHeader($me))->assertOk()->assertJsonCount(0, 'data');
  46. });
  47. it('requires login to list threads', function () {
  48. $this->getJson('/api/v3/me/agent-threads')->assertUnauthorized();
  49. });
  50. it('rejects an invalid archived filter', function () {
  51. $this->getJson('/api/v3/me/agent-threads?filter[archived]=maybe', authHeader((string) Str::uuid()))
  52. ->assertUnprocessable();
  53. });
  54. /* ------------------------------------------------------------------ 详情 */
  55. it('shows a thread with its visible runs in order', function () {
  56. $me = (string) Str::uuid();
  57. $thread = AgentThread::factory()->forUser($me)->create();
  58. $first = AgentRun::factory()->for($thread, 'thread')->create(['started_at' => now()->subMinutes(10), 'question' => 'Q1']);
  59. $replaced = AgentRun::factory()->for($thread, 'thread')->create(['started_at' => now()->subMinutes(5), 'question' => 'Q2']);
  60. AgentRun::factory()->for($thread, 'thread')->create([
  61. 'started_at' => now()->subMinutes(4), 'question' => 'Q2', 'answer' => 'regenerated', 'replaces_run_id' => $replaced->run_id,
  62. ]);
  63. $runs = $this->getJson("/api/v3/me/agent-threads/{$thread->thread_id}", authHeader($me))
  64. ->assertOk()
  65. ->assertJsonPath('data.thread_id', $thread->thread_id)
  66. ->json('data.runs');
  67. expect($runs)->toHaveCount(2)
  68. ->and($runs[0]['run_id'])->toBe($first->run_id)
  69. ->and($runs[1]['answer'])->toBe('regenerated')
  70. ->and($runs[0])->toHaveKeys(['question', 'answer', 'citations', 'deep', 'user_message_id', 'assistant_message_id'])
  71. ->and($runs[0])->not->toHaveKeys(['evidence', 'plan', 'client_ip', 'improve']);
  72. });
  73. it('hides other users threads as 404', function () {
  74. $other = AgentThread::factory()->forUser((string) Str::uuid())->create();
  75. $this->getJson("/api/v3/me/agent-threads/{$other->thread_id}", authHeader((string) Str::uuid()))
  76. ->assertNotFound();
  77. });
  78. it('does not expose guest threads through the user api', function () {
  79. $guest = AgentThread::factory()->create();
  80. $this->getJson("/api/v3/me/agent-threads/{$guest->thread_id}", authHeader((string) Str::uuid()))
  81. ->assertNotFound();
  82. });
  83. it('returns 404 for a non-uuid thread id', function () {
  84. $this->getJson('/api/v3/me/agent-threads/not-a-uuid', authHeader((string) Str::uuid()))->assertNotFound();
  85. });
  86. /* ------------------------------------------------------------------ 改名 */
  87. it('renames my thread', function () {
  88. $me = (string) Str::uuid();
  89. $thread = AgentThread::factory()->forUser($me)->create();
  90. $this->patchJson("/api/v3/me/agent-threads/{$thread->thread_id}", ['title' => '新标题'], authHeader($me))
  91. ->assertOk()
  92. ->assertJsonPath('data.title', '新标题');
  93. });
  94. it('ignores fields other than title on rename', function () {
  95. $me = (string) Str::uuid();
  96. $thread = AgentThread::factory()->forUser($me)->create();
  97. $this->patchJson("/api/v3/me/agent-threads/{$thread->thread_id}", ['account' => 'user:someone-else'], authHeader($me))
  98. ->assertOk();
  99. expect($thread->fresh()->account)->toBe('user:'.$me);
  100. });
  101. it('rejects an empty title', function () {
  102. $me = (string) Str::uuid();
  103. $thread = AgentThread::factory()->forUser($me)->create();
  104. $this->patchJson("/api/v3/me/agent-threads/{$thread->thread_id}", ['title' => ''], authHeader($me))
  105. ->assertUnprocessable();
  106. });
  107. it('does not register PUT on a thread', function () {
  108. $me = (string) Str::uuid();
  109. $thread = AgentThread::factory()->forUser($me)->create();
  110. $this->putJson("/api/v3/me/agent-threads/{$thread->thread_id}", ['title' => 'x'], authHeader($me))
  111. ->assertStatus(405);
  112. });
  113. /* ------------------------------------------------------------------ 存档 */
  114. it('archives and unarchives my thread', function () {
  115. $me = (string) Str::uuid();
  116. $thread = AgentThread::factory()->forUser($me)->create();
  117. $archivedAt = $this->putJson("/api/v3/me/agent-threads/{$thread->thread_id}/archive", [], authHeader($me))
  118. ->assertOk()
  119. ->json('data.archived_at');
  120. expect($archivedAt)->not->toBeNull();
  121. // 幂等:再存档一次,时间不变
  122. $this->putJson("/api/v3/me/agent-threads/{$thread->thread_id}/archive", [], authHeader($me))
  123. ->assertOk()
  124. ->assertJsonPath('data.archived_at', $archivedAt);
  125. $this->deleteJson("/api/v3/me/agent-threads/{$thread->thread_id}/archive", [], authHeader($me))
  126. ->assertOk()
  127. ->assertJsonPath('data.archived_at', null);
  128. });
  129. it('cannot archive another users thread', function () {
  130. $other = AgentThread::factory()->forUser((string) Str::uuid())->create();
  131. $this->putJson("/api/v3/me/agent-threads/{$other->thread_id}/archive", [], authHeader((string) Str::uuid()))
  132. ->assertNotFound();
  133. });
  134. it('requires login to archive', function () {
  135. $thread = AgentThread::factory()->forUser((string) Str::uuid())->create();
  136. $this->putJson("/api/v3/me/agent-threads/{$thread->thread_id}/archive")->assertUnauthorized();
  137. });
  138. /* ------------------------------------------------------------------ 删除 */
  139. it('soft deletes my thread together with its runs and steps', function () {
  140. $me = (string) Str::uuid();
  141. $thread = AgentThread::factory()->forUser($me)->create();
  142. $run = AgentRun::factory()->for($thread, 'thread')->create();
  143. $step = $run->steps()->create(['seq' => 0, 'kind' => 'tool', 'name' => 'wikipali_search']);
  144. $otherRun = AgentRun::factory()->for(AgentThread::factory()->forUser($me), 'thread')->create();
  145. $this->deleteJson("/api/v3/me/agent-threads/{$thread->thread_id}", [], authHeader($me))
  146. ->assertOk()
  147. ->assertJsonPath('data.thread_id', $thread->thread_id);
  148. // 软删、不是真删:数据都还在
  149. $this->assertSoftDeleted($thread);
  150. $this->assertSoftDeleted($run);
  151. $this->assertSoftDeleted($step);
  152. expect(AgentRun::where('agent_thread_id', $thread->id)->count())->toBe(0)
  153. ->and(AgentRun::withTrashed()->where('agent_thread_id', $thread->id)->count())->toBe(1)
  154. ->and($otherRun->fresh()->deleted_at)->toBeNull();
  155. $this->getJson("/api/v3/me/agent-threads/{$thread->thread_id}", authHeader($me))->assertNotFound();
  156. });
  157. it('cannot delete another users thread', function () {
  158. $other = AgentThread::factory()->forUser((string) Str::uuid())->create();
  159. $this->deleteJson("/api/v3/me/agent-threads/{$other->thread_id}", [], authHeader((string) Str::uuid()))
  160. ->assertNotFound();
  161. expect($other->fresh()->deleted_at)->toBeNull();
  162. });