AgentCreditV3Test.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <?php
  2. use App\Models\AgentThread;
  3. use App\Models\AiModel;
  4. use App\Models\CreditLedgerEntry;
  5. use App\Models\ReadingDaily;
  6. use App\Models\UserInfo;
  7. use App\Models\UserOperationDaily;
  8. use App\Services\AuthService;
  9. use App\Services\V3\AgentAccount;
  10. use Illuminate\Foundation\Testing\RefreshDatabase;
  11. use Illuminate\Support\Str;
  12. uses(RefreshDatabase::class);
  13. /**
  14. * v3 AI 对话额度与积分(策略在 config mint.credits.policy):
  15. *
  16. * POST /api/v3/agent-charges 提问扣费(服务间,X-Agent-Service-Key)
  17. * GET /api/v3/agent-quota 额度概况(可选登录,游客带 device_id)
  18. * POST /api/v3/me/reading-time 读经时长上报(登录)
  19. * GET /api/v3/credit-policy 当前积分策略(公开)
  20. * artisan credits:settle-activity / credits:adjust
  21. *
  22. * v1:游客每日 100、登录 500;普通 10、深度 30;读经 1/分钟每日上限 100;编辑 5/分钟不设上限;
  23. * 游客同 IP 每日 500。
  24. *
  25. * 这里断言的是形状而非契约(spectator 尚未安装,见 v3-resource skill 第 3 步)。
  26. */
  27. beforeEach(function () {
  28. config(['mint.app.agent_service_key' => 'test-agent-key']);
  29. });
  30. function chargeHeaders(?string $userUid = null): array
  31. {
  32. $headers = ['X-Agent-Service-Key' => 'test-agent-key'];
  33. return $userUid ? $headers + authHeader($userUid) : $headers;
  34. }
  35. function charge(array $body, ?string $userUid = null, array $headers = [])
  36. {
  37. // 游客必须带客户端 IP(runtime 写入);没指定就给一个测试用的
  38. return test()->postJson('/api/v3/agent-charges', $body + ['run_id' => (string) Str::uuid(), 'client_ip' => '192.0.2.1'], chargeHeaders($userUid) + $headers);
  39. }
  40. function spendFree(AgentAccount $account, int $amount, ?string $ip = null): void
  41. {
  42. CreditLedgerEntry::create([
  43. ...$account->columns(), 'delta' => -$amount, 'kind' => 'spend', 'bucket' => 'free',
  44. 'source' => 'agent_run', 'ref' => (string) Str::uuid(), 'day' => now('UTC')->toDateString(),
  45. 'policy_version' => 1, 'client_ip' => $ip,
  46. ]);
  47. }
  48. /* ------------------------------------------------------------------ 扣费 */
  49. it('refuses a guest without a client ip but not a signed-in user', function () {
  50. $this->postJson('/api/v3/agent-charges', ['run_id' => (string) Str::uuid(), 'device_id' => (string) Str::uuid()], chargeHeaders())
  51. ->assertUnprocessable()
  52. ->assertJsonValidationErrors('client_ip');
  53. expect(CreditLedgerEntry::count())->toBe(0);
  54. $this->postJson('/api/v3/agent-charges', ['run_id' => (string) Str::uuid()], chargeHeaders(makeStudio('noip-'.Str::random(5))))
  55. ->assertCreated();
  56. });
  57. it('refuses to charge for a thread that belongs to another account', function () {
  58. $thread = AgentThread::factory()->forUser((string) Str::uuid())->create();
  59. charge(['device_id' => (string) Str::uuid(), 'thread_id' => $thread->thread_id])->assertForbidden();
  60. expect(CreditLedgerEntry::count())->toBe(0);
  61. });
  62. it('charges for my own thread, deleted or not, and for a new one', function () {
  63. $device = (string) Str::uuid();
  64. $thread = AgentThread::factory()->forGuest($device)->create();
  65. $thread->delete();
  66. charge(['device_id' => $device, 'thread_id' => $thread->thread_id])->assertCreated();
  67. charge(['device_id' => $device, 'thread_id' => (string) Str::uuid()])->assertCreated();
  68. });
  69. it('requires the service key to charge', function () {
  70. $this->postJson('/api/v3/agent-charges', ['run_id' => (string) Str::uuid(), 'device_id' => (string) Str::uuid()])
  71. ->assertUnauthorized();
  72. });
  73. it('gives a guest ten normal questions a day', function () {
  74. $device = (string) Str::uuid();
  75. foreach (range(1, 10) as $i) {
  76. charge(['device_id' => $device])->assertCreated()->assertJsonPath('data.free_remaining', 100 - 10 * $i);
  77. }
  78. charge(['device_id' => $device])
  79. ->assertStatus(402)
  80. ->assertJsonPath('type', fn ($type) => str_ends_with($type, 'quota-exceeded'))
  81. ->assertJsonPath('reason', 'insufficient')
  82. ->assertJsonPath('cost', 10)
  83. ->assertJsonPath('free_remaining', 0);
  84. });
  85. it('charges a deep question thirty credits', function () {
  86. $user = makeStudio('deep-'.Str::random(5));
  87. charge(['deep' => true], $user)
  88. ->assertCreated()
  89. ->assertJsonPath('data.cost', 30)
  90. ->assertJsonPath('data.charged_free', 30)
  91. ->assertJsonPath('data.free_remaining', 470);
  92. });
  93. it('is idempotent on run_id', function () {
  94. $device = (string) Str::uuid();
  95. $run = (string) Str::uuid();
  96. charge(['device_id' => $device, 'run_id' => $run])->assertCreated();
  97. charge(['device_id' => $device, 'run_id' => $run])->assertOk()->assertJsonPath('data.cost', 10);
  98. expect(CreditLedgerEntry::where('ref', $run)->count())->toBe(1);
  99. });
  100. it('spends daily free first, then earned credits', function () {
  101. $user = makeStudio('mix-'.Str::random(5));
  102. spendFree(AgentAccount::user($user), 490);
  103. $this->artisan('credits:adjust', ['user' => $user, 'delta' => 50, '--reason' => 'test'])->assertSuccessful();
  104. charge(['deep' => true], $user)
  105. ->assertCreated()
  106. ->assertJsonPath('data.charged_free', 10)
  107. ->assertJsonPath('data.charged_earned', 20)
  108. ->assertJsonPath('data.free_remaining', 0)
  109. ->assertJsonPath('data.earned_balance', 30);
  110. });
  111. it('caps all guests behind one ip', function () {
  112. spendFree(AgentAccount::guest((string) Str::uuid()), 490, '198.51.100.9');
  113. charge(['device_id' => (string) Str::uuid(), 'client_ip' => '198.51.100.9'])->assertCreated();
  114. charge(['device_id' => (string) Str::uuid(), 'client_ip' => '198.51.100.9'])
  115. ->assertStatus(402)
  116. ->assertJsonPath('reason', 'ip_cap');
  117. charge(['device_id' => (string) Str::uuid(), 'client_ip' => '198.51.100.10'])->assertCreated();
  118. });
  119. it('does not apply the ip cap to signed-in users', function () {
  120. spendFree(AgentAccount::guest((string) Str::uuid()), 500, '198.51.100.9');
  121. charge(['client_ip' => '198.51.100.9'], makeStudio('ipuser-'.Str::random(5)))->assertCreated();
  122. });
  123. it('treats a model token as a guest when charging', function () {
  124. $human = makeStudio('owner-'.Str::random(5));
  125. $model = AiModel::factory()->ownedBy($human)->create();
  126. $this->postJson('/api/v3/agent-charges', ['run_id' => (string) Str::uuid()], [
  127. 'X-Agent-Service-Key' => 'test-agent-key',
  128. 'Authorization' => 'Bearer '.AuthService::getUserToken($model->uid),
  129. ])->assertUnprocessable()->assertJsonValidationErrors('device_id');
  130. });
  131. /* ------------------------------------------------------------------ 退款 */
  132. it('refunds the charge when the run failed without an answer', function () {
  133. $device = (string) Str::uuid();
  134. $run = (string) Str::uuid();
  135. charge(['device_id' => $device, 'run_id' => $run])->assertCreated();
  136. $this->postJson('/api/v3/agent-traces', [
  137. 'run_id' => $run, 'thread_id' => (string) Str::uuid(), 'device_id' => $device,
  138. 'question' => 'q', 'status' => 'error', 'error' => 'boom',
  139. ], chargeHeaders())->assertCreated();
  140. $this->getJson("/api/v3/agent-quota?device_id={$device}")->assertOk()->assertJsonPath('data.free_remaining', 100);
  141. // 补发同一条研究过程不会退两次
  142. $this->postJson('/api/v3/agent-traces', [
  143. 'run_id' => $run, 'thread_id' => (string) Str::uuid(), 'device_id' => $device,
  144. 'question' => 'q', 'status' => 'error',
  145. ], chargeHeaders())->assertOk();
  146. expect(CreditLedgerEntry::where('ref', $run)->where('kind', 'refund')->count())->toBe(1);
  147. });
  148. it('keeps the charge when the user cancelled', function () {
  149. $device = (string) Str::uuid();
  150. $run = (string) Str::uuid();
  151. charge(['device_id' => $device, 'run_id' => $run])->assertCreated();
  152. $this->postJson('/api/v3/agent-traces', [
  153. 'run_id' => $run, 'thread_id' => (string) Str::uuid(), 'device_id' => $device,
  154. 'question' => 'q', 'status' => 'cancelled',
  155. ], chargeHeaders())->assertCreated();
  156. $this->getJson("/api/v3/agent-quota?device_id={$device}")->assertJsonPath('data.free_remaining', 90);
  157. });
  158. /* ------------------------------------------------------------------ 额度概况 */
  159. it('shows guest quota without earn rules', function () {
  160. $this->getJson('/api/v3/agent-quota?device_id='.Str::uuid())
  161. ->assertOk()
  162. ->assertJsonPath('data.account_type', 'guest')
  163. ->assertJsonPath('data.daily_free', 100)
  164. ->assertJsonPath('data.cost.deep', 30)
  165. ->assertJsonPath('data.earn', [])
  166. ->assertJsonStructure(['data' => ['resets_at', 'policy_version', 'earned_balance', 'earned_today']]);
  167. });
  168. it('never mistakes a guest whose device id equals a user uid for that user', function () {
  169. // 设备 id 由客户端填,可以故意填成别人的 uid:账号必须按 account_type + account_id 两列认
  170. $user = makeStudio('twin-'.Str::random(5));
  171. CreditLedgerEntry::create([
  172. ...AgentAccount::user($user)->columns(), 'delta' => 200, 'kind' => 'adjust', 'bucket' => 'earned',
  173. 'source' => 'admin', 'ref' => 'gift', 'day' => now('UTC')->toDateString(), 'policy_version' => 1,
  174. ]);
  175. $userThread = AgentThread::factory()->forUser($user)->create();
  176. // 看不到他的积分
  177. $this->getJson("/api/v3/agent-quota?device_id={$user}")
  178. ->assertOk()
  179. ->assertJsonPath('data.account_type', 'guest')
  180. ->assertJsonPath('data.earned_balance', 0);
  181. // 不能往他的会话里提问
  182. charge(['device_id' => $user, 'thread_id' => $userThread->thread_id])->assertForbidden();
  183. // 花掉游客自己的赠送额度后就被拒,碰不到他的积分
  184. spendFree(AgentAccount::guest($user), 100);
  185. charge(['device_id' => $user])->assertStatus(402);
  186. $this->getJson('/api/v3/agent-quota', authHeader($user))
  187. ->assertJsonPath('data.account_type', 'user')
  188. ->assertJsonPath('data.earned_balance', 200)
  189. ->assertJsonPath('data.free_used', 0);
  190. });
  191. it('requires a device id for guest quota', function () {
  192. $this->getJson('/api/v3/agent-quota')->assertUnprocessable()->assertJsonValidationErrors('device_id');
  193. });
  194. it('shows user quota with earn rules', function () {
  195. $this->getJson('/api/v3/agent-quota', authHeader(makeStudio('q-'.Str::random(5))))
  196. ->assertOk()
  197. ->assertJsonPath('data.account_type', 'user')
  198. ->assertJsonPath('data.daily_free', 500)
  199. ->assertJsonPath('data.earn.reading.daily_cap', 100)
  200. ->assertJsonPath('data.earn.editing.credits_per_minute', 5);
  201. });
  202. /* ------------------------------------------------------------------ 读经时长 */
  203. it('turns reading time into credits', function () {
  204. $user = makeStudio('reader-'.Str::random(5));
  205. $this->postJson('/api/v3/me/reading-time', ['seconds' => 300], authHeader($user))
  206. ->assertOk()
  207. ->assertJsonPath('data.seconds', 300);
  208. $this->getJson('/api/v3/agent-quota', authHeader($user))
  209. ->assertJsonPath('data.earned_today.reading', 5)
  210. ->assertJsonPath('data.earned_balance', 5);
  211. });
  212. it('clamps reading reports to wall-clock time', function () {
  213. $user = makeStudio('clamp-'.Str::random(5));
  214. $this->postJson('/api/v3/me/reading-time', ['seconds' => 600], authHeader($user))->assertOk();
  215. $this->travel(60)->seconds();
  216. $this->postJson('/api/v3/me/reading-time', ['seconds' => 600], authHeader($user))
  217. ->assertOk()
  218. ->assertJsonPath('data.seconds', 690);
  219. });
  220. it('caps reading credits at one hundred a day', function () {
  221. $user = makeStudio('cap-'.Str::random(5));
  222. ReadingDaily::create(['user_uid' => $user, 'day' => now('UTC')->toDateString(), 'seconds' => 20000, 'seconds_reported' => 20000]);
  223. $this->getJson('/api/v3/agent-quota', authHeader($user))->assertJsonPath('data.earned_today.reading', 100);
  224. });
  225. it('rejects reading reports too far in the past', function () {
  226. $this->postJson('/api/v3/me/reading-time', [
  227. 'seconds' => 60, 'day' => now('UTC')->subDays(3)->toDateString(),
  228. ], authHeader(makeStudio('old-'.Str::random(5))))->assertUnprocessable()->assertJsonValidationErrors('day');
  229. });
  230. it('requires login to report reading time', function () {
  231. $this->postJson('/api/v3/me/reading-time', ['seconds' => 60])->assertUnauthorized();
  232. });
  233. it('rejects an oversized reading report', function () {
  234. $this->postJson('/api/v3/me/reading-time', ['seconds' => 5000], authHeader(makeStudio('big-'.Str::random(5))))
  235. ->assertUnprocessable();
  236. });
  237. /* ------------------------------------------------------------------ 编辑时长 */
  238. it('turns editing time into credits without a cap', function () {
  239. $user = makeStudio('editor-'.Str::random(5));
  240. $sn = UserInfo::where('userid', $user)->value('id');
  241. // UTC+8 的客户端:本地今天零点 = UTC 今天零点 − 8 小时
  242. $localMidnight = now('UTC')->startOfDay()->getTimestampMs() - 8 * 3600 * 1000;
  243. UserOperationDaily::forceCreate([
  244. 'id' => app('snowflake')->id(), 'user_id' => $sn, 'date_int' => $localMidnight,
  245. 'duration' => 600 * 60 * 1000, 'hit' => 10,
  246. ]);
  247. $this->getJson('/api/v3/agent-quota', authHeader($user))->assertJsonPath('data.earned_today.editing', 3000);
  248. });
  249. it('settles yesterday for everyone with the daily command', function () {
  250. $user = makeStudio('nightly-'.Str::random(5));
  251. ReadingDaily::create(['user_uid' => $user, 'day' => now('UTC')->subDay()->toDateString(), 'seconds' => 1200, 'seconds_reported' => 1200]);
  252. $this->artisan('credits:settle-activity')->assertSuccessful();
  253. expect(CreditLedgerEntry::ownedBy(AgentAccount::user($user))->where('source', 'reading')->value('delta'))->toBe(20);
  254. // 重复执行不重复入账
  255. $this->artisan('credits:settle-activity')->assertSuccessful();
  256. expect(CreditLedgerEntry::ownedBy(AgentAccount::user($user))->where('source', 'reading')->count())->toBe(1);
  257. });
  258. it('backfills every missed day since credits started, however many', function () {
  259. config(['mint.credits.earn_since' => now('UTC')->subDays(60)->toDateString()]);
  260. $user = makeStudio('missed-'.Str::random(5));
  261. $sn = UserInfo::where('userid', $user)->value('id');
  262. foreach ([10, 40] as $ago) {
  263. ReadingDaily::create(['user_uid' => $user, 'day' => now('UTC')->subDays($ago)->toDateString(), 'seconds' => 300, 'seconds_reported' => 300]);
  264. }
  265. // UTC+8 客户端 25 天前的本地日:编辑 3 分钟
  266. UserOperationDaily::forceCreate([
  267. 'id' => app('snowflake')->id(), 'user_id' => $sn,
  268. 'date_int' => now('UTC')->subDays(25)->startOfDay()->getTimestampMs() - 8 * 3600 * 1000,
  269. 'duration' => 3 * 60 * 1000, 'hit' => 3,
  270. ]);
  271. // 上线日之前的不补
  272. ReadingDaily::create(['user_uid' => $user, 'day' => now('UTC')->subDays(70)->toDateString(), 'seconds' => 600, 'seconds_reported' => 600]);
  273. // 不足 1 分钟的不给积分,也不算漏
  274. ReadingDaily::create(['user_uid' => $user, 'day' => now('UTC')->subDays(5)->toDateString(), 'seconds' => 30, 'seconds_reported' => 30]);
  275. $this->artisan('credits:settle-activity')->assertSuccessful();
  276. $earned = CreditLedgerEntry::ownedBy(AgentAccount::user($user))->orderBy('day')->get(['source', 'day', 'delta'])
  277. ->map(fn ($e) => [$e->source, $e->day->toDateString(), $e->delta])->all();
  278. expect($earned)->toBe([
  279. ['reading', now('UTC')->subDays(40)->toDateString(), 5],
  280. ['editing', now('UTC')->subDays(25)->toDateString(), 15],
  281. ['reading', now('UTC')->subDays(10)->toDateString(), 5],
  282. ]);
  283. // 再跑一次:都已结清,什么都不动
  284. $this->artisan('credits:settle-activity')->expectsOutputToContain('补结算了 0 位用户')->assertSuccessful();
  285. });
  286. it('re-settles a day that was settled before it ended and leaves closed days alone', function () {
  287. config(['mint.credits.earn_since' => now('UTC')->subDays(30)->toDateString()]);
  288. $user = makeStudio('early-'.Str::random(5));
  289. $early = now('UTC')->subDays(3)->startOfDay();
  290. $closed = now('UTC')->subDays(4)->startOfDay();
  291. foreach ([$early, $closed] as $day) {
  292. ReadingDaily::create(['user_uid' => $user, 'day' => $day->toDateString(), 'seconds' => 1200, 'seconds_reported' => 1200]);
  293. }
  294. // 那天中午就结算过(之后又读了),以及那天结束后才结算过(数额以当时为准,不再动)
  295. $entry = fn ($day, int $delta, $at) => CreditLedgerEntry::create([
  296. ...AgentAccount::user($user)->columns(), 'delta' => $delta, 'kind' => 'earn', 'bucket' => 'earned', 'source' => 'reading',
  297. 'ref' => $day->toDateString(), 'day' => $day->toDateString(), 'policy_version' => 1,
  298. ])->forceFill(['updated_at' => $at])->saveQuietly();
  299. $entry($early, 2, $early->copy()->addHours(12));
  300. $entry($closed, 7, $closed->copy()->addDay()->addHour());
  301. $this->artisan('credits:settle-activity')->assertSuccessful();
  302. $deltas = CreditLedgerEntry::ownedBy(AgentAccount::user($user))->pluck('delta', 'ref');
  303. expect($deltas[$early->toDateString()])->toBe(20)
  304. ->and($deltas[$closed->toDateString()])->toBe(7);
  305. });
  306. it('does not keep re-settling an early-settled day whose amount was already right', function () {
  307. config(['mint.credits.earn_since' => now('UTC')->subDays(30)->toDateString()]);
  308. $user = makeStudio('same-'.Str::random(5));
  309. $day = now('UTC')->subDays(3)->startOfDay();
  310. ReadingDaily::create(['user_uid' => $user, 'day' => $day->toDateString(), 'seconds' => 120, 'seconds_reported' => 120]);
  311. // 当天中途结算过,数额恰好已经对了(2 分)
  312. CreditLedgerEntry::create([
  313. ...AgentAccount::user($user)->columns(), 'delta' => 2, 'kind' => 'earn', 'bucket' => 'earned', 'source' => 'reading',
  314. 'ref' => $day->toDateString(), 'day' => $day->toDateString(), 'policy_version' => 1,
  315. ])->forceFill(['updated_at' => $day->copy()->addHours(12)])->saveQuietly();
  316. $this->artisan('credits:settle-activity')->expectsOutputToContain('补结算了 1 位用户')->assertSuccessful();
  317. $this->artisan('credits:settle-activity')->expectsOutputToContain('补结算了 0 位用户')->assertSuccessful();
  318. });
  319. it('re-settles editing on a western client day settled before that local day ended', function () {
  320. config(['mint.credits.earn_since' => now('UTC')->subDays(30)->toDateString()]);
  321. $user = makeStudio('west-'.Str::random(5));
  322. $sn = UserInfo::where('userid', $user)->value('id');
  323. $day = now('UTC')->subDays(3)->startOfDay();
  324. // UTC−8 客户端:本地那天零点 = UTC 当天 08:00,一直到 UTC 次日 08:00
  325. UserOperationDaily::forceCreate([
  326. 'id' => app('snowflake')->id(), 'user_id' => $sn, 'date_int' => $day->getTimestampMs() + 8 * 3600 * 1000,
  327. 'duration' => 10 * 60 * 1000, 'hit' => 10,
  328. ]);
  329. // 次日 01:00 UTC(本地那天还没结束)只结算到 2 分钟
  330. CreditLedgerEntry::create([
  331. ...AgentAccount::user($user)->columns(), 'delta' => 10, 'kind' => 'earn', 'bucket' => 'earned', 'source' => 'editing',
  332. 'ref' => $day->toDateString(), 'day' => $day->toDateString(), 'policy_version' => 1,
  333. ])->forceFill(['updated_at' => $day->copy()->addDay()->addHour()])->saveQuietly();
  334. $this->artisan('credits:settle-activity')->assertSuccessful();
  335. expect(CreditLedgerEntry::ownedBy(AgentAccount::user($user))->where('source', 'editing')->value('delta'))->toBe(50);
  336. });
  337. /* ------------------------------------------------------------------ 策略与调整 */
  338. it('shows the current credit policy to anyone', function () {
  339. $this->getJson('/api/v3/credit-policy')
  340. ->assertOk()
  341. ->assertJsonPath('data.version', config('mint.credits.policy.version'))
  342. ->assertJsonPath('data.day_timezone', 'UTC')
  343. ->assertJsonPath('data.daily_free.guest', 100)
  344. ->assertJsonPath('data.cost.deep', 30)
  345. ->assertJsonPath('data.earn.editing.daily_cap', null);
  346. });
  347. it('charges by the policy in config and records its version', function () {
  348. config(['mint.credits.policy.version' => 20261201, 'mint.credits.policy.cost.normal' => 5]);
  349. charge(['device_id' => (string) Str::uuid()])->assertCreated()
  350. ->assertJsonPath('data.cost', 5)
  351. ->assertJsonPath('data.policy_version', 20261201);
  352. expect(CreditLedgerEntry::value('policy_version'))->toBe(20261201);
  353. });
  354. it('applies a manual adjustment only once per ref', function () {
  355. $user = makeStudio('adj-'.Str::random(5));
  356. $this->artisan('credits:adjust', ['user' => $user, 'delta' => 100, '--ref' => 'gift-1'])->assertSuccessful();
  357. $this->artisan('credits:adjust', ['user' => $user, 'delta' => 100, '--ref' => 'gift-1'])->assertSuccessful();
  358. $this->getJson('/api/v3/agent-quota', authHeader($user))->assertJsonPath('data.earned_balance', 100);
  359. });
  360. it('tells guests to sign in and users to read when out of credits', function () {
  361. $device = (string) Str::uuid();
  362. spendFree(AgentAccount::guest($device), 100);
  363. $user = makeStudio('out-'.Str::random(5));
  364. spendFree(AgentAccount::user($user), 500);
  365. $guestDetail = charge(['device_id' => $device], null, ['Accept-Language' => 'zh-Hans'])->assertStatus(402)->json('detail');
  366. $userDetail = charge([], $user, ['Accept-Language' => 'zh-Hans'])->assertStatus(402)->json('detail');
  367. expect($guestDetail)->toContain('登录')
  368. ->and($userDetail)->toContain('读经')
  369. ->and($guestDetail)->not->toBe('messages.agent_quota_insufficient_guest');
  370. });