2026-05-03 20:26:55 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
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 23:40:03 +00:00
|
|
|
use App\Enums\Plan\Slug;
|
2026-05-03 20:26:55 +00:00
|
|
|
use App\Models\Account;
|
|
|
|
|
use App\Models\Invite;
|
|
|
|
|
use App\Models\Plan;
|
2026-05-07 13:41:37 +00:00
|
|
|
use App\Models\Post;
|
2026-05-03 20:26:55 +00:00
|
|
|
use App\Models\SocialAccount;
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use App\Models\Workspace;
|
2026-05-07 13:41:37 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2026-05-03 20:26:55 +00:00
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
|
$this->account = Account::factory()->create();
|
|
|
|
|
$this->owner = User::factory()->create(['account_id' => $this->account->id]);
|
|
|
|
|
$this->account->update(['owner_id' => $this->owner->id]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('usage returns correct counts across the account', function () {
|
|
|
|
|
Workspace::factory()->count(2)->create([
|
|
|
|
|
'account_id' => $this->account->id,
|
|
|
|
|
'user_id' => $this->owner->id,
|
|
|
|
|
]);
|
|
|
|
|
$workspace = $this->account->workspaces()->first();
|
|
|
|
|
SocialAccount::factory()->count(3)->create(['workspace_id' => $workspace->id]);
|
|
|
|
|
|
|
|
|
|
User::factory()->count(2)->create(['account_id' => $this->account->id]);
|
|
|
|
|
Invite::factory()->count(2)->create([
|
|
|
|
|
'account_id' => $this->account->id,
|
|
|
|
|
'invited_by' => $this->owner->id,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$usage = $this->account->usage();
|
|
|
|
|
|
|
|
|
|
expect($usage)->toBe([
|
|
|
|
|
'workspaceCount' => 2,
|
|
|
|
|
'socialAccountCount' => 3,
|
|
|
|
|
'memberCount' => 3,
|
|
|
|
|
'pendingInviteCount' => 2,
|
feat: end-to-end PostHog tracking with reactive group metrics
Wire PostHog identify + dual-group context across the stack so events
land on the right person and account/workspace groups, with counts kept
fresh by Inertia navigations rather than per-domain triggers.
- New `app/Jobs/SyncUserToPostHog.php` (queue `posthog`): centralised
high-level sync — identifies the user and group-identifies their
account + current workspace using `$account->usage()` so the metrics
reuse the same source of truth Inertia ships in shared props.
- `app/Actions/User/CreateUser.php`: dispatches `SyncUserToPostHog` on
signup instead of calling `PostHogService` inline. Keeps the action
fast and routes everything through the queue.
- `app/Listeners/StripeEventListener.php`: webhook now captures
`subscription.created`/`updated`/`cancelled` against the account
owner profile (with `account` group auto-attached) and re-dispatches
`SyncUserToPostHog` so plan/has_active_subscription/is_on_trial
refresh after Stripe state changes.
- `app/Services/PostHogService.php`: `capture()` accepts an optional
`Account` that auto-attaches `$groups.account`, `account_id`, and
`plan` properties. Each public method short-circuits when
`POSTHOG_API_KEY` is unset so self-hosted installs are unaffected.
- `app/Models/Traits/HasUsage.php`: adds `postCount` to the usage
shape (combined `withCount(['socialAccounts','posts'])` query) so
posts count is part of the same payload Inertia already ships.
- `config/horizon.php`: adds `posthog` to `supervisor-1` queues so the
queued PostHog jobs actually drain in production.
- `resources/js/app.ts`: extracts `syncPostHogContext(page)` and calls
it on boot AND on every Inertia navigation, reading the fresh
`usage` props. This:
- Refreshes account group counts (workspaces, social accounts,
posts, members, credits) without per-domain triggers.
- Resolves the workspace-switch case where `setup()` does not
re-run but `navigate` fires with the new `auth.currentWorkspace`.
- Captures the initial `$pageview` so the first page of a session
is no longer dropped.
- `resources/js/components/UserMenuContent.vue`: `posthog.reset()` on
logout so a follow-up login on the same browser doesn't keep events
attributed to the previous user.
- `resources/js/composables/useFeatureAccess.ts`: TS `Usage`
interface gains `postCount`.
- `tests/Feature/Models/HasUsageTraitTest.php`: updated for the new
usage shape. Full suite: 1407 passed.
Hierarchy aligned with the domain model: person = User,
group `account` = billing/plan parent, group `workspace` =
collaboration child (carries `account_id` for drill-down).
2026-05-07 12:28:02 +00:00
|
|
|
'postCount' => 0,
|
2026-05-03 22:36:26 +00:00
|
|
|
'creditsUsed' => 0,
|
2026-05-03 20:26:55 +00:00
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
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 23:40:03 +00:00
|
|
|
test('featureLimits returns the monthly credits allotment', function () {
|
|
|
|
|
$plan = Plan::where('slug', Slug::Workspace)->first();
|
2026-05-03 20:26:55 +00:00
|
|
|
$this->account->update(['plan_id' => $plan->id]);
|
|
|
|
|
|
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 23:40:03 +00:00
|
|
|
Workspace::factory()->count(2)->create([
|
|
|
|
|
'account_id' => $this->account->id,
|
|
|
|
|
'user_id' => $this->owner->id,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-05-03 20:26:55 +00:00
|
|
|
$limits = $this->account->featureLimits();
|
|
|
|
|
|
|
|
|
|
expect($limits)->toBe([
|
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 23:40:03 +00:00
|
|
|
'monthlyCreditsLimit' => 5000,
|
2026-05-03 20:26:55 +00:00
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('pendingInviteCount excludes accepted invites', function () {
|
|
|
|
|
Invite::factory()->create([
|
|
|
|
|
'account_id' => $this->account->id,
|
|
|
|
|
'invited_by' => $this->owner->id,
|
|
|
|
|
]);
|
|
|
|
|
Invite::factory()->create([
|
|
|
|
|
'account_id' => $this->account->id,
|
|
|
|
|
'invited_by' => $this->owner->id,
|
|
|
|
|
'accepted_at' => now(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
expect($this->account->usage()['pendingInviteCount'])->toBe(1);
|
|
|
|
|
});
|
2026-05-07 13:41:37 +00:00
|
|
|
|
|
|
|
|
test('postCount is cached and survives new posts within the TTL', function () {
|
|
|
|
|
$workspace = Workspace::factory()->create([
|
|
|
|
|
'account_id' => $this->account->id,
|
|
|
|
|
'user_id' => $this->owner->id,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
Post::factory()->count(2)->create([
|
|
|
|
|
'workspace_id' => $workspace->id,
|
|
|
|
|
'user_id' => $this->owner->id,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// First call primes the cache.
|
|
|
|
|
expect($this->account->usage()['postCount'])->toBe(2);
|
|
|
|
|
|
|
|
|
|
// A new post is created mid-window. The cached value should win.
|
|
|
|
|
Post::factory()->create([
|
|
|
|
|
'workspace_id' => $workspace->id,
|
|
|
|
|
'user_id' => $this->owner->id,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
expect($this->account->usage()['postCount'])->toBe(2);
|
|
|
|
|
|
|
|
|
|
// Forgetting the cache key returns the fresh count.
|
2026-05-19 22:00:57 +00:00
|
|
|
Cache::forget(Account::postsCountCacheKey((string) $this->account->id));
|
2026-05-07 13:41:37 +00:00
|
|
|
expect($this->account->usage()['postCount'])->toBe(3);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('postCount returns zero without querying when account has no workspaces', function () {
|
|
|
|
|
expect($this->account->usage()['postCount'])->toBe(0);
|
|
|
|
|
|
|
|
|
|
// No cache entry should be written for the empty case.
|
2026-05-19 22:00:57 +00:00
|
|
|
expect(Cache::has(Account::postsCountCacheKey((string) $this->account->id)))->toBeFalse();
|
2026-05-07 13:41:37 +00:00
|
|
|
});
|
2026-05-07 17:31:07 +00:00
|
|
|
|
|
|
|
|
test('postCount survives a string-typed cache value (Redis serializer quirk)', function () {
|
|
|
|
|
// Laravel's RedisStore stores numeric values raw (not serialised) so they
|
|
|
|
|
// can be INCRemented atomically. The side effect: an int written via
|
|
|
|
|
// Cache::put comes back as a string on read. The test driver is `array`
|
|
|
|
|
// which preserves type, so we seed the cache with a literal string here
|
|
|
|
|
// to mimic what production sees and assert the return type stays int.
|
|
|
|
|
Workspace::factory()->create([
|
|
|
|
|
'account_id' => $this->account->id,
|
|
|
|
|
'user_id' => $this->owner->id,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-05-19 22:00:57 +00:00
|
|
|
Cache::put(Account::postsCountCacheKey((string) $this->account->id), '42', 300);
|
2026-05-07 17:31:07 +00:00
|
|
|
|
|
|
|
|
$usage = $this->account->usage();
|
|
|
|
|
|
|
|
|
|
expect($usage['postCount'])->toBe(42);
|
|
|
|
|
expect($usage['postCount'])->toBeInt();
|
|
|
|
|
});
|