trypost/tests/Feature/AiUsageLogTest.php
Paulo Castellano cbf8fb283c feat: per-workspace pricing, onboarding, and billing overhaul
Pricing
- Bill per workspace ($12/mo or $120/yr each); Stripe quantity tracks the
  workspace count and syncs on workspace create/delete.
- 2,500 AI credits per workspace, pooled at the account level; monthly reset
  on the billing anniversary, annual granted upfront (no rollover).
- One social account per network per workspace; remove all count-based limits
  (workspace/social/member) and the legacy plan tiers (single Workspace plan).

Onboarding (cloud only: SELF_HOSTED=false + PostHog)
- Replace the /subscribe plan picker with /onboarding persona selection
  (Creator/Freelancer/Startup/Agency/Small business/Other), saved on the user
  (users.persona) and mirrored to PostHog, then Stripe Checkout on the monthly
  price. 8-day trial so Stripe displays 7.

Billing screen
- Remove the Change Plan dialog (dead with a single plan); add an annual-upgrade
  banner for monthly subscribers (swapToYearly).
- Current-plan card shows the workspace count instead of the plan name.

System AI
- Brand analyzer / workspace autofill is always allowed and never debits credits
  (system feature, not the user's usage).

Self-hosted (SELF_HOSTED=true) bypasses all billing, credit, limit, network,
and onboarding logic.
2026-06-21 20:40:03 -03:00

85 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\Account;
use App\Models\AiUsageLog;
use App\Models\Workspace;
use Illuminate\Support\Carbon;
test('credits used between sums credits within the window', function () {
$account = Account::factory()->create();
$workspace = Workspace::factory()->create(['account_id' => $account->id]);
AiUsageLog::factory()->text(credits: 5)->count(3)->create([
'account_id' => $account->id,
'workspace_id' => $workspace->id,
'created_at' => Carbon::parse('2026-06-10'),
]);
AiUsageLog::factory()->text(credits: 10)->count(2)->create([
'account_id' => $account->id,
'workspace_id' => $workspace->id,
'created_at' => Carbon::parse('2026-06-12'),
]);
$used = AiUsageLog::creditsUsedBetween(
$account->id,
Carbon::parse('2026-06-01'),
Carbon::parse('2026-07-01'),
);
expect($used)->toBe(35);
});
test('credits used between excludes logs outside the window', function () {
$account = Account::factory()->create();
$workspace = Workspace::factory()->create(['account_id' => $account->id]);
AiUsageLog::factory()->text(credits: 10)->create([
'account_id' => $account->id,
'workspace_id' => $workspace->id,
'created_at' => Carbon::parse('2026-06-10'),
]);
AiUsageLog::factory()->text(credits: 50)->create([
'account_id' => $account->id,
'workspace_id' => $workspace->id,
'created_at' => Carbon::parse('2026-05-10'),
]);
$used = AiUsageLog::creditsUsedBetween(
$account->id,
Carbon::parse('2026-06-01'),
Carbon::parse('2026-07-01'),
);
expect($used)->toBe(10);
});
test('credits used between excludes logs from other accounts', function () {
$account = Account::factory()->create();
$otherAccount = Account::factory()->create();
$workspace = Workspace::factory()->create(['account_id' => $account->id]);
$otherWorkspace = Workspace::factory()->create(['account_id' => $otherAccount->id]);
AiUsageLog::factory()->text(credits: 10)->create([
'account_id' => $account->id,
'workspace_id' => $workspace->id,
'created_at' => Carbon::parse('2026-06-10'),
]);
AiUsageLog::factory()->text(credits: 100)->create([
'account_id' => $otherAccount->id,
'workspace_id' => $otherWorkspace->id,
'created_at' => Carbon::parse('2026-06-10'),
]);
$used = AiUsageLog::creditsUsedBetween(
$account->id,
Carbon::parse('2026-06-01'),
Carbon::parse('2026-07-01'),
);
expect($used)->toBe(10);
});