AdminStatsV3Test.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. use App\Models\AgentRun;
  3. use App\Models\AgentThread;
  4. use App\Models\UserInfo;
  5. use Illuminate\Foundation\Testing\RefreshDatabase;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Str;
  8. uses(RefreshDatabase::class);
  9. /**
  10. * v3 管理后台统计(仅 root / administrator):
  11. *
  12. * GET /api/v3/admin/reading-stats 阅读统计
  13. * GET /api/v3/admin/agent-stats AI 对话统计
  14. * GET /api/v3/admin/agent-runs 提问列表
  15. * GET /api/v3/admin/agent-runs/{run} 研究过程
  16. *
  17. * 这里断言的是形状而非契约(spectator 尚未安装,见 v3-resource skill 第 3 步)。
  18. */
  19. function makeAdmin(string $role = 'administrator'): string
  20. {
  21. $uid = makeStudio('admin-'.Str::random(6));
  22. UserInfo::where('userid', $uid)->update(['role' => json_encode([$role, 'member'])]);
  23. return $uid;
  24. }
  25. function addRecent(string $user, string $article, string $mode, string $at): void
  26. {
  27. DB::table('recents')->insert([
  28. 'id' => (string) Str::uuid(), 'type' => 'chapter', 'article_id' => $article,
  29. 'param' => json_encode(['mode' => $mode]), 'user_uid' => $user, 'created_at' => $at, 'updated_at' => $at,
  30. ]);
  31. }
  32. /* ------------------------------------------------------------------ 权限 */
  33. it('requires login for admin stats', function (string $path) {
  34. $this->getJson("/api/v3/admin/{$path}")->assertUnauthorized();
  35. })->with(['reading-stats', 'agent-stats', 'agent-runs']);
  36. it('forbids non-admin users', function (string $path) {
  37. $this->getJson("/api/v3/admin/{$path}", authHeader(makeStudio('plain-'.Str::random(5))))->assertForbidden();
  38. })->with(['reading-stats', 'agent-stats', 'agent-runs']);
  39. it('lets the root account in', function () {
  40. $this->getJson('/api/v3/admin/agent-stats', authHeader(config('mint.admin.root_uuid')))->assertOk();
  41. });
  42. it('lets administrators in', function () {
  43. $this->getJson('/api/v3/admin/reading-stats', authHeader(makeAdmin()))->assertOk();
  44. });
  45. /* ------------------------------------------------------------------ 区间 */
  46. it('defaults to the last 30 days', function () {
  47. $this->getJson('/api/v3/admin/agent-stats', authHeader(makeAdmin()))
  48. ->assertOk()
  49. ->assertJsonPath('data.range.to', now('UTC')->toDateString())
  50. ->assertJsonPath('data.range.from', now('UTC')->subDays(29)->toDateString())
  51. ->assertJsonCount(30, 'data.daily');
  52. });
  53. it('rejects a range longer than 366 days', function () {
  54. $this->getJson('/api/v3/admin/reading-stats?from=2024-01-01&to=2026-01-01', authHeader(makeAdmin()))
  55. ->assertUnprocessable()
  56. ->assertJsonValidationErrors('from');
  57. });
  58. it('rejects a range that ends before it starts', function () {
  59. $this->getJson('/api/v3/admin/reading-stats?from=2026-10-10&to=2026-10-01', authHeader(makeAdmin()))
  60. ->assertUnprocessable();
  61. });
  62. /* ------------------------------------------------------------------ 阅读统计 */
  63. it('counts readers by client and compares with the previous period', function () {
  64. $a = makeStudio('r1-'.Str::random(4));
  65. $b = makeStudio('r2-'.Str::random(4));
  66. addRecent($a, '93-6', 'reading', '2026-10-20 08:00:00'); // App
  67. addRecent($a, '93-9', 'reading', '2026-10-21 08:00:00');
  68. addRecent($b, '93-6', 'read', '2026-10-21 09:00:00'); // Web
  69. addRecent($b, '169-3', 'read', '2026-10-10 09:00:00'); // 上一区间(10-08~10-14)
  70. DB::table('reading_dailies')->insert([
  71. 'user_uid' => $a, 'day' => '2026-10-20', 'seconds' => 1200, 'seconds_reported' => 1500,
  72. 'created_at' => now(), 'updated_at' => now(),
  73. ]);
  74. $data = $this->getJson('/api/v3/admin/reading-stats?from=2026-10-15&to=2026-10-21', authHeader(makeAdmin()))
  75. ->assertOk()->json('data');
  76. expect($data['summary']['active_readers'])->toBe(2)
  77. ->and($data['summary']['chapters_opened'])->toBe(3)
  78. ->and($data['summary']['reading_seconds'])->toBe(1200)
  79. ->and($data['previous']['active_readers'])->toBe(1)
  80. ->and($data['daily'])->toHaveCount(7)
  81. ->and(collect($data['daily'])->firstWhere('date', '2026-10-21'))->toMatchArray(['readers_web' => 1, 'readers_mobile' => 1])
  82. ->and(collect($data['daily'])->firstWhere('date', '2026-10-20')['reading_minutes'])->toBe(20)
  83. ->and($data['top_chapters'][0])->toMatchArray(['article_id' => '93-6', 'readers' => 2])
  84. ->and(collect($data['duration_buckets'])->firstWhere('bucket', '15-30')['user_days'])->toBe(1);
  85. });
  86. it('filters reading stats by client', function () {
  87. $a = makeStudio('c1-'.Str::random(4));
  88. addRecent($a, '93-6', 'reading', '2026-10-20 08:00:00');
  89. addRecent(makeStudio('c2-'.Str::random(4)), '93-6', 'read', '2026-10-20 08:00:00');
  90. $web = $this->getJson('/api/v3/admin/reading-stats?from=2026-10-20&to=2026-10-20&client=web', authHeader(makeAdmin()))
  91. ->assertOk()->json('data');
  92. expect($web['summary']['active_readers'])->toBe(1)
  93. ->and($web['summary']['reading_seconds'])->toBeNull()
  94. ->and($web['reading_credits'])->toBeNull();
  95. });
  96. it('rejects an unknown client', function () {
  97. $this->getJson('/api/v3/admin/reading-stats?client=tv', authHeader(makeAdmin()))->assertUnprocessable();
  98. });
  99. /* ------------------------------------------------------------------ AI 对话统计 */
  100. it('summarises agent runs', function () {
  101. $user = (string) Str::uuid();
  102. $userThread = AgentThread::factory()->forUser($user)->create();
  103. $guestThread = AgentThread::factory()->create();
  104. $at = '2026-10-20 10:00:00';
  105. AgentRun::factory()->for($userThread, 'thread')->create(['route' => 'term', 'deep' => true, 'latency_ms' => 1000, 'started_at' => $at, 'tokens_in' => 100]);
  106. AgentRun::factory()->for($userThread, 'thread')->create(['route' => 'passage', 'latency_ms' => 3000, 'started_at' => $at]);
  107. AgentRun::factory()->for($guestThread, 'thread')->create(['route' => null, 'status' => 'denied', 'latency_ms' => 50, 'started_at' => $at, 'improve' => false]);
  108. AgentRun::factory()->for($guestThread, 'thread')->create(['route' => 'question', 'status' => 'ok', 'latency_ms' => 99999, 'started_at' => '2026-09-01 00:00:00']);
  109. $data = $this->getJson('/api/v3/admin/agent-stats?from=2026-10-20&to=2026-10-20', authHeader(makeAdmin()))
  110. ->assertOk()->json('data');
  111. expect($data['summary'])->toMatchArray([
  112. 'runs' => 3, 'deep' => 1, 'passage' => 1, 'denied' => 1, 'improve' => 2,
  113. 'askers' => 2, 'askers_user' => 1, 'askers_guest' => 1, 'latency_p50_ms' => 2000, 'tokens_in' => 100,
  114. ])->and($data['daily'][0])->toMatchArray(['term' => 1, 'passage' => 1, 'none' => 1, 'denied' => 1]);
  115. $users = $this->getJson('/api/v3/admin/agent-stats?from=2026-10-20&to=2026-10-20&account_type=user', authHeader(makeAdmin()))
  116. ->json('data.summary.runs');
  117. expect($users)->toBe(2);
  118. });
  119. it('lists tool calls and credit flows', function () {
  120. $thread = AgentThread::factory()->create();
  121. $run = AgentRun::factory()->for($thread, 'thread')->create(['started_at' => '2026-10-20 10:00:00']);
  122. $run->steps()->createMany([
  123. ['seq' => 0, 'kind' => 'tool', 'name' => 'wikipali_search', 'ms' => 100],
  124. ['seq' => 1, 'kind' => 'tool', 'name' => 'wikipali_search', 'ms' => 300, 'error' => 'boom'],
  125. ['seq' => 2, 'kind' => 'event', 'name' => 'quota', 'args' => ['result' => 'unchecked']],
  126. ]);
  127. DB::table('credit_ledger')->insert([
  128. 'account_type' => $thread->account_type, 'account_id' => $thread->account_id, 'delta' => -10, 'kind' => 'spend', 'bucket' => 'free', 'source' => 'agent_run',
  129. 'ref' => $run->run_id, 'day' => '2026-10-20', 'policy_version' => 1, 'created_at' => now(), 'updated_at' => now(),
  130. ]);
  131. $data = $this->getJson('/api/v3/admin/agent-stats?from=2026-10-20&to=2026-10-20', authHeader(makeAdmin()))->json('data');
  132. expect($data['tools'][0])->toMatchArray(['name' => 'wikipali_search', 'calls' => 2, 'errors' => 1, 'avg_ms' => 200])
  133. ->and($data['summary']['unchecked'])->toBe(1)
  134. ->and($data['credits']['spent_free'])->toBe(10);
  135. });
  136. /* ------------------------------------------------------------------ 提问列表与研究过程 */
  137. it('lists runs newest first with filters', function () {
  138. $thread = AgentThread::factory()->create();
  139. AgentRun::factory()->for($thread, 'thread')->create(['question' => '什么是四圣谛?', 'status' => 'ok']);
  140. AgentRun::factory()->for($thread, 'thread')->create(['question' => 'What is anatta?', 'status' => 'denied']);
  141. $admin = authHeader(makeAdmin());
  142. $this->getJson('/api/v3/admin/agent-runs', $admin)
  143. ->assertOk()
  144. ->assertJsonPath('data.0.question', 'What is anatta?')
  145. ->assertJsonPath('data.0.account_type', 'guest')
  146. ->assertJsonMissingPath('data.0.steps')
  147. ->assertJsonPath('meta.total', 2);
  148. $this->getJson('/api/v3/admin/agent-runs?filter[status]=ok', $admin)->assertJsonCount(1, 'data');
  149. $this->getJson('/api/v3/admin/agent-runs?filter[q]=四圣谛', $admin)->assertJsonCount(1, 'data');
  150. });
  151. it('shows the full research process of a run', function () {
  152. $user = makeStudio('asker-'.Str::random(4));
  153. $thread = AgentThread::factory()->forUser($user)->create();
  154. $run = AgentRun::factory()->for($thread, 'thread')->create(['evidence' => '证据全文']);
  155. $run->steps()->create(['seq' => 0, 'kind' => 'route', 'args' => ['route' => 'term']]);
  156. $this->getJson("/api/v3/admin/agent-runs/{$run->run_id}", authHeader(makeAdmin()))
  157. ->assertOk()
  158. ->assertJsonPath('data.evidence', '证据全文')
  159. ->assertJsonPath('data.steps.0.kind', 'route')
  160. ->assertJsonPath('data.user.id', $user);
  161. });
  162. it('returns 404 for an unknown run', function () {
  163. $this->getJson('/api/v3/admin/agent-runs/'.Str::uuid(), authHeader(makeAdmin()))->assertNotFound();
  164. });
  165. it('rejects an unknown status filter', function () {
  166. $this->getJson('/api/v3/admin/agent-runs?filter[status]=weird', authHeader(makeAdmin()))->assertUnprocessable();
  167. });