2026-04-16 00:06:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
use App\Models\Account;
|
|
|
|
|
use App\Models\AiUsageLog;
|
|
|
|
|
use App\Models\Workspace;
|
|
|
|
|
|
2026-05-03 22:36:26 +00:00
|
|
|
test('monthly credits returns sum of credits for account this month', function () {
|
2026-04-16 00:06:06 +00:00
|
|
|
$account = Account::factory()->create();
|
|
|
|
|
$workspace = Workspace::factory()->create(['account_id' => $account->id]);
|
|
|
|
|
|
2026-05-03 22:36:26 +00:00
|
|
|
AiUsageLog::factory()->text(credits: 5)->count(3)->create([
|
2026-04-16 00:06:06 +00:00
|
|
|
'account_id' => $account->id,
|
|
|
|
|
'workspace_id' => $workspace->id,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-05-03 22:36:26 +00:00
|
|
|
AiUsageLog::factory()->text(credits: 10)->count(2)->create([
|
2026-04-16 00:06:06 +00:00
|
|
|
'account_id' => $account->id,
|
|
|
|
|
'workspace_id' => $workspace->id,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-05-03 22:36:26 +00:00
|
|
|
expect(AiUsageLog::monthlyCredits($account->id))->toBe(35);
|
2026-04-16 00:06:06 +00:00
|
|
|
});
|
|
|
|
|
|
2026-05-03 22:36:26 +00:00
|
|
|
test('monthly credits excludes logs from other months', function () {
|
2026-04-16 00:06:06 +00:00
|
|
|
$account = Account::factory()->create();
|
|
|
|
|
$workspace = Workspace::factory()->create(['account_id' => $account->id]);
|
|
|
|
|
|
2026-05-03 22:36:26 +00:00
|
|
|
AiUsageLog::factory()->text(credits: 10)->create([
|
2026-04-16 00:06:06 +00:00
|
|
|
'account_id' => $account->id,
|
|
|
|
|
'workspace_id' => $workspace->id,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-05-03 22:36:26 +00:00
|
|
|
AiUsageLog::factory()->text(credits: 50)->create([
|
2026-04-16 00:06:06 +00:00
|
|
|
'account_id' => $account->id,
|
|
|
|
|
'workspace_id' => $workspace->id,
|
|
|
|
|
'created_at' => now()->subMonth(),
|
|
|
|
|
]);
|
|
|
|
|
|
2026-05-03 22:36:26 +00:00
|
|
|
expect(AiUsageLog::monthlyCredits($account->id))->toBe(10);
|
2026-04-16 00:06:06 +00:00
|
|
|
});
|
|
|
|
|
|
2026-05-03 22:36:26 +00:00
|
|
|
test('monthly credits excludes logs from other accounts', function () {
|
2026-04-16 00:06:06 +00:00
|
|
|
$account = Account::factory()->create();
|
|
|
|
|
$otherAccount = Account::factory()->create();
|
|
|
|
|
$workspace = Workspace::factory()->create(['account_id' => $account->id]);
|
|
|
|
|
$otherWorkspace = Workspace::factory()->create(['account_id' => $otherAccount->id]);
|
|
|
|
|
|
2026-05-03 22:36:26 +00:00
|
|
|
AiUsageLog::factory()->text(credits: 10)->create([
|
2026-04-16 00:06:06 +00:00
|
|
|
'account_id' => $account->id,
|
|
|
|
|
'workspace_id' => $workspace->id,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-05-03 22:36:26 +00:00
|
|
|
AiUsageLog::factory()->text(credits: 100)->create([
|
2026-04-16 00:06:06 +00:00
|
|
|
'account_id' => $otherAccount->id,
|
|
|
|
|
'workspace_id' => $otherWorkspace->id,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-05-03 22:36:26 +00:00
|
|
|
expect(AiUsageLog::monthlyCredits($account->id))->toBe(10);
|
2026-04-16 00:06:06 +00:00
|
|
|
});
|