trypost/tests/Feature/WorkspaceControllerTest.php
Paulo Castellano eb52b6b699
Fix OpenRouter via laravel/ai default provider (revises #216) (#220)
* Fix OpenRouter support by using laravel/ai default provider config.

PR #216 patched Lab::OpenRouter into every agent match, but laravel/ai
already resolves config('ai.default') — including openrouter — when agents
omit provider(). Those matches also forced unknown providers to Gemini and
BrandAnalyzerRunner checked a non-existent services.openrouter key.

Remove the duplicated provider() overrides, gate availability on
ai.providers.*.key, and document OPENROUTER_API_KEY.

Co-authored-by: Paulo Castellano <hello@paulocastellano.com>

* chore(deps): bump laravel/ai to v0.10.3 for native OpenRouter support

v0.5.1's OpenRouter driver only covered text/embeddings via the legacy
Prism gateway. v0.10.3 ships a native OpenRouter gateway with image,
audio (TTS/STT), and web search support, and drops prism-php/prism as
a dependency. ai-sdk-development skill docs refreshed via boost:update
to match the installed version.

* fix: honor AI_IMAGE_PROVIDER instead of hardcoding OpenAI's gpt-image-2

AiImageClient always passed model: 'gpt-image-2' to Image::of()->generate(),
so AI_IMAGE_PROVIDER silently did nothing for any provider other than
OpenAI — gemini/xai/openrouter would fail against an OpenAI-only model id
and quietly fall back to a stock photo. Usage recording was hardcoded to
provider 'openai' too, so credits were billed against the wrong model
whenever a different provider actually ran.

Drop the hardcoded model so generation falls through to the SDK's own
config('ai.default_for_images') + per-provider default model, and read
the actual provider/model back off the response's meta for usage
recording and source_meta instead of assuming OpenAI.

* refactor: extract AiImageClient into single-purpose steps, fix uncaught exception

generate() built the prompt, called the SDK, and unpacked the response all
in one block, with bytes extraction happening after the try/catch — so a
response with an empty images collection threw an uncaught RuntimeException
from ImageResponse::firstImage() instead of returning null as documented.

Split into cleanKeywords(), buildPrompt(), resolveBrandContext(), and
toResult(), and moved response unpacking inside the try block so any
malformed response is treated as a failure like everything else. Added a
regression test with an empty-images fake response.

* fix: drop hardcoded default_text_model, resolve model per provider

default_text_model was the only per-modality model override in ai.php —
image, audio, transcription, embeddings, and reranking all just pick a
provider and let it use its own default model. Text had a config-pinned
model on top, forced into every agent's model() and into every usage
log's model field regardless of which provider actually ran. Switching
AI_TEXT_PROVIDER (e.g. to openrouter) kept sending OpenAI's model id to
whichever provider ended up handling the request.

Removed model() from all six agents so laravel/ai resolves the model
from the active provider's own default (OpenAI's default is already
'gpt-5.4', so no behavior change there). Usage-recording call sites now
read the actual provider/model back off the response's meta instead of
assuming config('ai.default')/default_text_model. StreamPostContent
needed the then() callback since broadcast()'s StreamableAgentResponse
doesn't expose meta directly.

* refactor: drop AiConfiguration wrapper, use data_get() for array reads

AiConfiguration was a one-line static helper used by only two call
sites, with no laravel/ai equivalent to lean on (confirmed AiManager
and the Provider base class expose no isConfigured()/hasKey() check —
the package's model is try-then-catch, not pre-flight checks). Inlined
the filled(config(...)) check directly into HandleInertiaRequests and
BrandAnalyzerRunner instead of keeping a class around one line of logic.

Also swapped direct array-key reads for data_get() per project
convention across every file touched by the recent AI provider/model
fixes (agents' budget arrays, RunGenerateNode/StreamPostCreation's
humanizer merge, RegeneratePostMediaImage's baseContext/copy/rendered
access). Write/assignment sites (`$x['key'] = ...`) are left as-is —
data_get() only reads.

* feat: enhance AI configuration with new providers and options

Added strict types declaration and updated Azure OpenAI API version. Introduced new 'bedrock' provider configuration with AWS credentials and role assumptions. Enhanced existing providers with additional options, including image deployment for Azure and OpenAI, and updated URLs for Gemini and Ollama. Added support for an 'openai-compatible' driver to broaden integration capabilities.

* fix: remove trailing newline in AI configuration file

* feat: add support for OpenRouter and ElevenLabs API keys in configuration

Updated the production Docker Compose file and example environment file to include commented-out entries for OPENROUTER_API_KEY and ELEVENLABS_API_KEY. This enhances the configuration options for AI providers, allowing for easier integration of additional services.

* feat: allow per-provider model overrides for every AI modality

Adds a `models` array to each provider block, wired to env vars, so
self-hosted operators can pin a specific text/image/audio/transcription/
embeddings/reranking model instead of relying on the package's built-in
default for that provider. Only added for the modalities each provider
actually implements (verified against laravel/ai's Provider classes).

Azure is left untouched — it resolves models via deployment names
(AZURE_OPENAI_DEPLOYMENT etc.), not raw model strings, which was already
wired before this change.

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
2026-08-10 21:38:50 -03:00

918 lines
34 KiB
PHP

<?php
declare(strict_types=1);
use App\Ai\Agents\BrandAnalyzer;
use App\Enums\UserWorkspace\Role;
use App\Enums\Workspace\ContentLanguage;
use App\Models\Account;
use App\Models\AiUsageLog;
use App\Models\User;
use App\Models\Workspace;
use App\Services\Brand\LogoAttacher;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Http;
beforeEach(function () {
$this->user = User::factory()->create([]);
$this->workspace = Workspace::factory()->create(['user_id' => $this->user->id]);
$this->workspace->members()->attach($this->user->id, ['role' => Role::Member->value]);
$this->user->update(['current_workspace_id' => $this->workspace->id]);
});
// Index tests
test('workspaces index requires authentication', function () {
$response = $this->get(route('app.workspaces.index'));
$response->assertRedirect(route('login'));
});
test('workspaces index shows all workspaces for user', function () {
$workspaces = Workspace::factory()->count(2)->create(['user_id' => $this->user->id]);
foreach ($workspaces as $workspace) {
$workspace->members()->attach($this->user->id, ['role' => Role::Member->value]);
}
$response = $this->actingAs($this->user)->get(route('app.workspaces.index'));
$response->assertOk();
$response->assertInertia(fn ($page) => $page
->component('workspaces/Index', false)
->has('workspaces', 3)
->has('currentWorkspaceId')
);
});
// Create tests
test('create workspace requires authentication', function () {
$response = $this->get(route('app.workspaces.create'));
$response->assertRedirect(route('login'));
});
test('create workspace shows form for user with no workspaces', function () {
// Delete existing workspace so user has none
$this->user->update(['current_workspace_id' => null]);
$this->workspace->delete();
$response = $this->actingAs($this->user)->get(route('app.workspaces.create'));
$response->assertOk();
$response->assertInertia(fn ($page) => $page
->component('workspaces/Create', false)
->has('availableContentLanguages', count(ContentLanguage::cases()))
->where('availableContentLanguages.0', ['value' => 'en', 'label' => 'English', 'englishName' => 'English'])
->where('availableContentLanguages.1', ['value' => 'uk', 'label' => 'Українська', 'englishName' => 'Ukrainian'])
);
});
test('create workspace shows form when user already has workspace in self-hosted mode', function () {
config(['trypost.self_hosted' => true]);
$response = $this->actingAs($this->user)->get(route('app.workspaces.create'));
$response->assertOk();
$response->assertInertia(fn ($page) => $page
->component('workspaces/Create', false)
);
});
// Store tests
test('store workspace requires authentication', function () {
$response = $this->post(route('app.workspaces.store'), ['name' => 'Test Workspace']);
$response->assertRedirect(route('login'));
});
test('store workspace creates first workspace', function () {
// Delete existing workspace so user has none
$this->user->update(['current_workspace_id' => null]);
$this->workspace->delete();
$response = $this->actingAs($this->user)->post(route('app.workspaces.store'), [
'name' => 'New Workspace',
]);
$response->assertRedirect(route('app.accounts'));
$this->assertDatabaseHas('workspaces', [
'name' => 'New Workspace',
'user_id' => $this->user->id,
]);
});
test('store workspace creates second workspace in self-hosted mode', function () {
config(['trypost.self_hosted' => true]);
$response = $this->actingAs($this->user)->post(route('app.workspaces.store'), [
'name' => 'Second Workspace',
]);
$response->assertRedirect(route('app.accounts'));
$this->assertDatabaseHas('workspaces', [
'name' => 'Second Workspace',
'user_id' => $this->user->id,
]);
});
test('store workspace validates name is required', function () {
$response = $this->actingAs($this->user)->post(route('app.workspaces.store'), [
'name' => '',
]);
$response->assertSessionHasErrors('name');
});
test('store workspace sets new workspace as current', function () {
// Delete existing workspace so user has none
$this->user->update(['current_workspace_id' => null]);
$this->workspace->delete();
$this->actingAs($this->user)->post(route('app.workspaces.store'), [
'name' => 'New Workspace',
]);
$this->user->refresh();
$newWorkspace = Workspace::where('name', 'New Workspace')->first();
expect($this->user->current_workspace_id)->toBe($newWorkspace->id);
});
// Switch tests
test('switch workspace requires authentication', function () {
$response = $this->post(route('app.workspaces.switch', $this->workspace));
$response->assertRedirect(route('login'));
});
test('switch workspace changes current workspace', function () {
$otherWorkspace = Workspace::factory()->create(['user_id' => $this->user->id]);
$otherWorkspace->members()->attach($this->user->id, ['role' => Role::Member->value]);
$response = $this->actingAs($this->user)->post(route('app.workspaces.switch', $otherWorkspace));
$response->assertRedirect(route('app.calendar'));
$this->user->refresh();
expect($this->user->current_workspace_id)->toBe($otherWorkspace->id);
});
test('switch workspace returns 403 for workspace user does not belong to', function () {
$otherUser = User::factory()->create([]);
$otherWorkspace = Workspace::factory()->create(['user_id' => $otherUser->id]);
$response = $this->actingAs($this->user)->post(route('app.workspaces.switch', $otherWorkspace));
$response->assertForbidden();
});
test('switch workspace returns 403 for personal workspace after joining another account', function () {
$sharedOwner = User::factory()->create();
$invitee = User::factory()->create();
$personalAccountId = $invitee->account_id;
$personalWorkspace = Workspace::factory()->create([
'account_id' => $personalAccountId,
'user_id' => $invitee->id,
]);
$personalWorkspace->members()->attach($invitee->id, ['role' => Role::Admin->value]);
$sharedWorkspace = Workspace::factory()->create([
'account_id' => $sharedOwner->account_id,
'user_id' => $sharedOwner->id,
]);
$sharedWorkspace->members()->attach($invitee->id, ['role' => Role::Member->value]);
$invitee->update([
'account_id' => $sharedOwner->account_id,
'current_workspace_id' => $sharedWorkspace->id,
]);
$response = $this->actingAs($invitee)->post(route('app.workspaces.switch', $personalWorkspace));
$response->assertForbidden();
expect($invitee->fresh()->current_workspace_id)->toBe($sharedWorkspace->id);
});
test('workspace index only lists workspaces on the current account', function () {
$sharedOwner = User::factory()->create();
$invitee = User::factory()->create();
$personalAccountId = $invitee->account_id;
$personalWorkspace = Workspace::factory()->create([
'account_id' => $personalAccountId,
'user_id' => $invitee->id,
]);
$personalWorkspace->members()->attach($invitee->id, ['role' => Role::Admin->value]);
$sharedWorkspace = Workspace::factory()->create([
'account_id' => $sharedOwner->account_id,
'user_id' => $sharedOwner->id,
]);
$sharedWorkspace->members()->attach($invitee->id, ['role' => Role::Member->value]);
$invitee->update([
'account_id' => $sharedOwner->account_id,
'current_workspace_id' => $sharedWorkspace->id,
]);
$response = $this->actingAs($invitee)->get(route('app.workspaces.index'));
$response->assertOk();
$response->assertInertia(fn ($page) => $page
->has('workspaces', 1)
->where('workspaces.0.id', $sharedWorkspace->id));
});
// Settings tests
test('workspace settings requires authentication', function () {
$response = $this->get(route('app.workspace.settings'));
$response->assertRedirect(route('login'));
});
test('workspace settings shows the workspace settings page', function () {
$response = $this->actingAs($this->user)->get(route('app.workspace.settings'));
$response->assertOk();
$response->assertInertia(fn ($page) => $page
->component('settings/workspace/Workspace', false)
->has('workspace')
->where('isOnlyWorkspace', false)
->where('otherMemberCount', 0)
);
});
test('workspace settings marks only workspace in saas mode', function () {
config(['trypost.self_hosted' => false]);
$this->user->account->subscriptions()->create([
'type' => Account::SUBSCRIPTION_NAME,
'stripe_id' => 'sub_test_'.fake()->uuid(),
'stripe_status' => 'active',
'stripe_price' => 'price_123',
]);
$response = $this->actingAs($this->user)->get(route('app.workspace.settings'));
$response->assertOk();
$response->assertInertia(fn ($page) => $page
->where('isOnlyWorkspace', true)
);
});
test('workspace settings does not mark only workspace when account has more than one', function () {
config(['trypost.self_hosted' => false]);
$this->user->account->subscriptions()->create([
'type' => Account::SUBSCRIPTION_NAME,
'stripe_id' => 'sub_test_'.fake()->uuid(),
'stripe_status' => 'active',
'stripe_price' => 'price_123',
]);
Workspace::factory()->create([
'account_id' => $this->user->account_id,
'user_id' => $this->user->id,
]);
$response = $this->actingAs($this->user)->get(route('app.workspace.settings'));
$response->assertOk();
$response->assertInertia(fn ($page) => $page
->where('isOnlyWorkspace', false)
);
});
test('workspace settings otherMemberCount only includes members without another account workspace', function () {
$stranded = User::factory()->create([
'account_id' => $this->user->account_id,
]);
$alsoOnOther = User::factory()->create([
'account_id' => $this->user->account_id,
]);
$otherWorkspace = Workspace::factory()->create([
'account_id' => $this->user->account_id,
'user_id' => $this->user->id,
]);
$this->workspace->members()->attach($stranded->id, ['role' => Role::Member->value]);
$this->workspace->members()->attach($alsoOnOther->id, ['role' => Role::Member->value]);
$otherWorkspace->members()->attach($alsoOnOther->id, ['role' => Role::Member->value]);
$response = $this->actingAs($this->user)->get(route('app.workspace.settings'));
$response->assertOk();
$response->assertInertia(fn ($page) => $page
->where('otherMemberCount', 1)
);
});
test('brand settings shows the brand settings page', function () {
$response = $this->actingAs($this->user)->get(route('app.workspace.brand'));
$response->assertOk();
$response->assertInertia(fn ($page) => $page
->component('settings/workspace/Brand', false)
->has('workspace')
->has('availableContentLanguages', count(ContentLanguage::cases()))
->where('availableContentLanguages.1', ['value' => 'uk', 'label' => 'Українська', 'englishName' => 'Ukrainian'])
);
});
test('workspace settings redirects to create if no workspace', function () {
$this->user->update(['current_workspace_id' => null]);
$response = $this->actingAs($this->user)->get(route('app.workspace.settings'));
$response->assertRedirect(route('app.workspaces.create'));
});
// Update settings tests
test('update workspace settings requires authentication', function () {
$response = $this->put(route('app.workspace.settings.update'), [
'name' => 'Updated Name',
]);
$response->assertRedirect(route('login'));
});
test('update workspace settings updates workspace and redirects back', function () {
$response = $this->actingAs($this->user)
->from(route('app.workspace.brand'))
->put(route('app.workspace.settings.update'), [
'name' => 'Updated Name',
'brand_font' => 'Inter',
'image_style' => 'cinematic',
]);
$response->assertRedirect(route('app.workspace.brand'));
$this->workspace->refresh();
expect($this->workspace->name)->toBe('Updated Name');
});
test('update workspace settings persists brand voice traits', function () {
$this->actingAs($this->user)
->from(route('app.workspace.brand'))
->put(route('app.workspace.settings.update'), [
'name' => $this->workspace->name,
'brand_font' => 'Inter',
'image_style' => 'cinematic',
'brand_voice_traits' => ['third_person', 'no_hype', 'data_driven'],
])->assertRedirect(route('app.workspace.brand'));
expect($this->workspace->refresh()->brand_voice_traits)->toBe(['third_person', 'no_hype', 'data_driven']);
});
test('update workspace settings rejects an unknown brand voice trait', function () {
$this->actingAs($this->user)
->from(route('app.workspace.brand'))
->put(route('app.workspace.settings.update'), [
'name' => $this->workspace->name,
'brand_font' => 'Inter',
'image_style' => 'cinematic',
'brand_voice_traits' => ['third_person', 'not_a_real_trait'],
])->assertSessionHasErrors('brand_voice_traits.1');
});
test('update workspace settings persists the image_style choice', function () {
$this->actingAs($this->user)
->from(route('app.workspace.brand'))
->put(route('app.workspace.settings.update'), [
'name' => $this->workspace->name,
'brand_font' => 'Inter',
'image_style' => 'minimalist',
])->assertRedirect(route('app.workspace.brand'));
expect($this->workspace->refresh()->image_style->value)->toBe('minimalist');
});
test('update workspace settings updates the name when only the name is submitted', function () {
$this->actingAs($this->user)
->from(route('app.workspace.settings'))
->put(route('app.workspace.settings.update'), [
'name' => 'Name Only Update',
])
->assertRedirect(route('app.workspace.settings'))
->assertSessionHasNoErrors();
expect($this->workspace->refresh()->name)->toBe('Name Only Update');
});
test('update workspace settings still requires brand_font and image_style when submitted empty', function () {
$this->actingAs($this->user)
->put(route('app.workspace.settings.update'), [
'name' => $this->workspace->name,
'brand_font' => '',
'image_style' => '',
])->assertSessionHasErrors(['brand_font', 'image_style']);
});
test('update workspace settings rejects unknown image_style values', function () {
$this->actingAs($this->user)
->put(route('app.workspace.settings.update'), [
'name' => $this->workspace->name,
'brand_font' => 'Inter',
'image_style' => 'pixel-art',
])->assertSessionHasErrors(['image_style']);
});
test('update workspace settings persists a newly supported content language', function () {
$this->actingAs($this->user)
->from(route('app.workspace.brand'))
->put(route('app.workspace.settings.update'), [
'name' => $this->workspace->name,
'content_language' => 'fr',
])->assertRedirect(route('app.workspace.brand'))
->assertSessionHasNoErrors();
expect($this->workspace->refresh()->content_language)->toBe('fr');
});
test('update workspace settings persists Ukrainian as a content language', function () {
$this->actingAs($this->user)
->from(route('app.workspace.brand'))
->put(route('app.workspace.settings.update'), [
'name' => $this->workspace->name,
'content_language' => 'uk',
])->assertRedirect(route('app.workspace.brand'))
->assertSessionHasNoErrors();
expect($this->workspace->refresh()->content_language)->toBe('uk');
});
test('update workspace settings rejects an unsupported content language', function () {
$this->actingAs($this->user)
->put(route('app.workspace.settings.update'), [
'name' => $this->workspace->name,
'content_language' => 'sv',
])->assertSessionHasErrors(['content_language']);
});
test('update workspace settings validates required fields', function () {
$response = $this->actingAs($this->user)->put(route('app.workspace.settings.update'), [
'name' => '',
]);
$response->assertSessionHasErrors(['name']);
});
// Logo upload tests
test('upload workspace logo requires authentication', function () {
$response = $this->post(route('app.workspace.upload-logo'), [
'photo' => UploadedFile::fake()->image('logo.jpg'),
]);
$response->assertRedirect(route('login'));
});
test('upload workspace logo succeeds with valid image', function () {
$response = $this->actingAs($this->user)->post(route('app.workspace.upload-logo'), [
'photo' => UploadedFile::fake()->image('logo.jpg', 200, 200),
]);
$response->assertRedirect();
$this->workspace->refresh();
expect($this->workspace->has_logo)->toBeTrue();
expect($this->workspace->logo_url)->not->toBeNull();
});
test('upload workspace logo validates file is an image', function () {
$response = $this->actingAs($this->user)->post(route('app.workspace.upload-logo'), [
'photo' => UploadedFile::fake()->create('document.pdf', 100),
]);
$response->assertSessionHasErrors('photo');
});
test('upload workspace logo validates max size', function () {
$response = $this->actingAs($this->user)->post(route('app.workspace.upload-logo'), [
'photo' => UploadedFile::fake()->image('logo.jpg')->size(3000),
]);
$response->assertSessionHasErrors('photo');
});
test('upload workspace logo requires authorization', function () {
$otherUser = User::factory()->create([]);
$otherWorkspace = Workspace::factory()->create(['user_id' => $otherUser->id]);
$otherWorkspace->members()->attach($otherUser->id, ['role' => Role::Member->value]);
$otherUser->update(['current_workspace_id' => $otherWorkspace->id]);
$otherUser->account->subscriptions()->create([
'type' => Account::SUBSCRIPTION_NAME,
'stripe_id' => 'sub_test_'.fake()->uuid(),
'stripe_status' => 'active',
'stripe_price' => 'price_123',
]);
// otherUser is account owner of their own account/workspace, so policy 'update' passes for their own workspace.
// Switch their current workspace to the original $this->workspace (which they don't own) to trigger forbidden.
$otherUser->update(['current_workspace_id' => $this->workspace->id]);
$response = $this->actingAs($otherUser)->post(route('app.workspace.upload-logo'), [
'photo' => UploadedFile::fake()->image('logo.jpg'),
]);
$response->assertForbidden();
});
test('delete workspace logo requires authentication', function () {
$response = $this->delete(route('app.workspace.delete-logo'));
$response->assertRedirect(route('login'));
});
test('delete workspace logo succeeds', function () {
// Upload first
$this->actingAs($this->user)->post(route('app.workspace.upload-logo'), [
'photo' => UploadedFile::fake()->image('logo.jpg', 200, 200),
]);
$this->workspace->refresh();
expect($this->workspace->has_logo)->toBeTrue();
// Delete
$response = $this->actingAs($this->user)->delete(route('app.workspace.delete-logo'));
$response->assertRedirect();
$this->workspace->refresh();
expect($this->workspace->has_logo)->toBeFalse();
});
test('delete workspace logo requires authorization', function () {
$otherUser = User::factory()->create([]);
$otherWorkspace = Workspace::factory()->create(['user_id' => $otherUser->id]);
$otherWorkspace->members()->attach($otherUser->id, ['role' => Role::Member->value]);
$otherUser->update(['current_workspace_id' => $otherWorkspace->id]);
$otherUser->account->subscriptions()->create([
'type' => Account::SUBSCRIPTION_NAME,
'stripe_id' => 'sub_test_'.fake()->uuid(),
'stripe_status' => 'active',
'stripe_price' => 'price_123',
]);
$otherUser->update(['current_workspace_id' => $this->workspace->id]);
$response = $this->actingAs($otherUser)->delete(route('app.workspace.delete-logo'));
$response->assertForbidden();
});
// Destroy tests
test('destroy workspace requires authentication', function () {
$response = $this->delete(route('app.workspaces.destroy', $this->workspace));
$response->assertRedirect(route('login'));
});
test('destroy workspace deletes the workspace', function () {
Workspace::factory()->create([
'account_id' => $this->user->account_id,
'user_id' => $this->user->id,
]);
$workspaceId = $this->workspace->id;
$response = $this->actingAs($this->user)->delete(route('app.workspaces.destroy', $this->workspace));
$response->assertRedirect(route('app.calendar'));
$response->assertSessionHas('flash.success', __('workspaces.flash.deleted'));
expect(Workspace::find($workspaceId))->toBeNull();
});
test('destroy workspace falls back to another account workspace when deleting current', function () {
$other = Workspace::factory()->create([
'account_id' => $this->user->account_id,
'user_id' => $this->user->id,
]);
$this->actingAs($this->user)->delete(route('app.workspaces.destroy', $this->workspace));
$this->user->refresh();
expect($this->user->current_workspace_id)->toBe($other->id);
expect($this->user->belongsToWorkspace($other))->toBeTrue();
});
test('destroy workspace reassigns current to another joined workspace', function () {
$other = Workspace::factory()->create(['user_id' => $this->user->id]);
$other->members()->attach($this->user->id, ['role' => Role::Member->value]);
$this->actingAs($this->user)->delete(route('app.workspaces.destroy', $this->workspace));
$this->user->refresh();
expect($this->user->current_workspace_id)->toBe($other->id);
});
test('destroy workspace is blocked when it is the only workspace', function () {
config(['trypost.self_hosted' => false]);
$this->user->account->subscriptions()->create([
'type' => Account::SUBSCRIPTION_NAME,
'stripe_id' => 'sub_test_'.fake()->uuid(),
'stripe_status' => 'active',
'stripe_price' => 'price_123',
]);
$workspaceId = $this->workspace->id;
$response = $this->actingAs($this->user)->delete(route('app.workspaces.destroy', $this->workspace));
$response->assertSessionHas('flash.error', __('workspaces.cannot_delete_last'));
expect(Workspace::find($workspaceId))->not->toBeNull();
});
test('destroy workspace allows deleting the only workspace in self-hosted mode', function () {
config(['trypost.self_hosted' => true]);
$workspaceId = $this->workspace->id;
$response = $this->actingAs($this->user)->delete(route('app.workspaces.destroy', $this->workspace));
$response->assertRedirect(route('app.workspaces.create'));
$response->assertSessionHas('flash.success', __('workspaces.flash.deleted'));
expect(Workspace::find($workspaceId))->toBeNull();
expect($this->user->fresh()->current_workspace_id)->toBeNull();
});
test('destroy workspace returns 403 for non-owner', function () {
$otherUser = User::factory()->create([]);
$otherWorkspace = Workspace::factory()->create(['user_id' => $otherUser->id]);
$otherWorkspace->members()->attach($otherUser->id, ['role' => Role::Member->value]);
$otherUser->update(['current_workspace_id' => $otherWorkspace->id]);
$otherUser->account->subscriptions()->create([
'type' => Account::SUBSCRIPTION_NAME,
'stripe_id' => 'sub_test_'.fake()->uuid(),
'stripe_status' => 'active',
'stripe_price' => 'price_123',
]);
$response = $this->actingAs($otherUser)->delete(route('app.workspaces.destroy', $this->workspace));
$response->assertForbidden();
});
test('destroy workspace returns 403 for workspace admin', function () {
Workspace::factory()->create([
'account_id' => $this->user->account_id,
'user_id' => $this->user->id,
]);
$admin = User::factory()->create([
'account_id' => $this->user->account_id,
'current_workspace_id' => $this->workspace->id,
]);
$this->workspace->members()->attach($admin->id, ['role' => Role::Admin->value]);
$response = $this->actingAs($admin)->delete(route('app.workspaces.destroy', $this->workspace));
$response->assertForbidden();
expect(Workspace::find($this->workspace->id))->not->toBeNull();
});
test('destroy workspace returns 403 for workspace member', function () {
Workspace::factory()->create([
'account_id' => $this->user->account_id,
'user_id' => $this->user->id,
]);
$member = User::factory()->create([
'account_id' => $this->user->account_id,
'current_workspace_id' => $this->workspace->id,
]);
$this->workspace->members()->attach($member->id, ['role' => Role::Member->value]);
$response = $this->actingAs($member)->delete(route('app.workspaces.destroy', $this->workspace));
$response->assertForbidden();
expect(Workspace::find($this->workspace->id))->not->toBeNull();
});
// Autofill brand tests
test('autofillBrand returns metadata without persisting anything', function () {
$account = Account::factory()->create();
$user = User::factory()->create(['account_id' => $account->id]);
$account->update(['owner_id' => $user->id]);
Http::fake([
'example.com/*' => Http::response(
'<html><head><title>Acme</title><meta name="description" content="We sell rockets." /></head><body></body></html>',
200,
['Content-Type' => 'text/html'],
),
'example.com' => Http::response(
'<html><head><title>Acme</title><meta name="description" content="We sell rockets." /></head><body></body></html>',
200,
['Content-Type' => 'text/html'],
),
]);
$initialWorkspaceCount = Workspace::count();
$response = $this->actingAs($user)
->postJson(route('app.workspaces.autofill'), ['url' => 'https://example.com']);
$response->assertOk();
$response->assertJsonStructure(['name', 'brand_description', 'content_language', 'logo_url']);
expect(Workspace::count())->toBe($initialWorkspaceCount);
});
test('autofillBrand is always allowed even without a subscription', function () {
config(['trypost.self_hosted' => false]);
$account = Account::factory()->create(['trial_ends_at' => null]);
$user = User::factory()->create(['account_id' => $account->id]);
$account->update(['owner_id' => $user->id]);
expect($account->subscribed(Account::SUBSCRIPTION_NAME))->toBeFalse();
Http::fake([
'example.com/*' => Http::response(
'<html><head><title>Acme</title><meta name="description" content="We sell rockets." /></head><body></body></html>',
200,
['Content-Type' => 'text/html'],
),
'example.com' => Http::response(
'<html><head><title>Acme</title><meta name="description" content="We sell rockets." /></head><body></body></html>',
200,
['Content-Type' => 'text/html'],
),
]);
$this->actingAs($user)
->postJson(route('app.workspaces.autofill'), ['url' => 'https://example.com'])
->assertOk();
});
test('autofillBrand never records AI usage even when the LLM runs', function () {
config(['trypost.self_hosted' => false]);
config()->set('ai.providers.gemini.key', 'fake-key');
config()->set('ai.default', 'gemini');
Http::fake([
'example.com' => Http::response(
'<html lang="en"><head><title>Acme</title><meta name="description" content="Terse." /></head><body><main><p>Acme ships rockets fast.</p></main></body></html>',
200,
['Content-Type' => 'text/html'],
),
]);
BrandAnalyzer::fake([
['description' => 'Acme ships rockets fast.', 'language' => 'en'],
]);
$this->actingAs($this->user)
->postJson(route('app.workspaces.autofill'), ['url' => 'https://example.com'])
->assertOk();
expect(AiUsageLog::count())->toBe(0);
});
test('autofillBrand validates url is required', function () {
$account = Account::factory()->create();
$user = User::factory()->create(['account_id' => $account->id]);
$this->actingAs($user)
->postJson(route('app.workspaces.autofill'), [])
->assertUnprocessable()
->assertJsonValidationErrors('url');
});
// Brand-aware store tests
test('store persists brand fields and redirects to /accounts', function () {
$account = Account::factory()->create();
$user = User::factory()->create(['account_id' => $account->id]);
$account->update(['owner_id' => $user->id]);
$account->subscriptions()->create([
'type' => Account::SUBSCRIPTION_NAME,
'stripe_id' => 'sub_test_'.fake()->uuid(),
'stripe_status' => 'active',
'stripe_price' => 'price_123',
]);
$response = $this->actingAs($user)->post(route('app.workspaces.store'), [
'name' => 'Acme Inc',
'brand_website' => 'https://acme.example',
'brand_description' => 'We sell rockets.',
'brand_voice_traits' => ['third_person', 'no_hype'],
'content_language' => 'en',
]);
$response->assertRedirect(route('app.accounts'));
$workspace = Workspace::where('name', 'Acme Inc')->sole();
expect($workspace->name)->toBe('Acme Inc');
expect($workspace->brand_website)->toBe('https://acme.example');
expect($workspace->brand_description)->toBe('We sell rockets.');
});
test('store persists a newly supported non-default content language', function () {
$account = Account::factory()->create();
$user = User::factory()->create(['account_id' => $account->id]);
$account->update(['owner_id' => $user->id]);
$this->actingAs($user)->post(route('app.workspaces.store'), [
'name' => 'Beispiel GmbH',
'content_language' => 'de',
])->assertSessionHasNoErrors();
// 'de' is not the DB default ('en'), so this proves the field is written.
expect(Workspace::where('name', 'Beispiel GmbH')->sole()->content_language)->toBe('de');
});
test('store rejects an unsupported content language', function () {
$account = Account::factory()->create();
$user = User::factory()->create(['account_id' => $account->id]);
$account->update(['owner_id' => $user->id]);
$this->actingAs($user)->post(route('app.workspaces.store'), [
'name' => 'Bad Lang',
'content_language' => 'sv',
])->assertSessionHasErrors(['content_language']);
});
test('store redirects additional workspace to /accounts', function () {
$account = Account::factory()->create();
$user = User::factory()->create(['account_id' => $account->id]);
$account->update(['owner_id' => $user->id]);
$account->subscriptions()->create([
'type' => Account::SUBSCRIPTION_NAME,
'stripe_id' => 'sub_test_'.fake()->uuid(),
'stripe_status' => 'active',
'stripe_price' => 'price_123',
]);
// First workspace already exists
$existing = Workspace::factory()->create(['account_id' => $account->id, 'user_id' => $user->id]);
$existing->members()->attach($user->id, ['role' => Role::Member->value]);
$response = $this->actingAs($user)->post(route('app.workspaces.store'), [
'name' => 'Second Workspace',
]);
$response->assertRedirect(route('app.accounts'));
expect(Workspace::where('account_id', $account->id)->count())->toBe(2);
});
test('store blocks a second workspace without an active subscription', function () {
config(['trypost.self_hosted' => false]);
$account = Account::factory()->create();
$user = User::factory()->create(['account_id' => $account->id]);
$account->update(['owner_id' => $user->id]);
// The account already owns one workspace and has no subscription, so a
// direct POST must not bootstrap a second (billable) workspace.
$existing = Workspace::factory()->create(['account_id' => $account->id, 'user_id' => $user->id]);
$existing->members()->attach($user->id, ['role' => Role::Member->value]);
$response = $this->actingAs($user)->post(route('app.workspaces.store'), [
'name' => 'Second Workspace',
]);
$response->assertRedirect(route('app.billing.index'));
expect(Workspace::where('account_id', $account->id)->count())->toBe(1);
});
test('store attaches logo when logo_url is provided', function () {
$account = Account::factory()->create();
$user = User::factory()->create(['account_id' => $account->id]);
$account->update(['owner_id' => $user->id]);
$account->subscriptions()->create([
'type' => Account::SUBSCRIPTION_NAME,
'stripe_id' => 'sub_test_'.fake()->uuid(),
'stripe_status' => 'active',
'stripe_price' => 'price_123',
]);
$logoAttacher = $this->mock(LogoAttacher::class);
$logoAttacher->shouldReceive('attach')->once();
$this->actingAs($user)->post(route('app.workspaces.store'), [
'name' => 'Acme',
'logo_url' => 'https://example.com/logo.png',
])->assertRedirect();
});
test('update settings attaches the autofilled logo when logo_url is provided', function () {
$logoAttacher = $this->mock(LogoAttacher::class);
$logoAttacher->shouldReceive('attach')->once();
$this->actingAs($this->user)
->from(route('app.workspace.brand'))
->put(route('app.workspace.settings.update'), [
'name' => $this->workspace->name,
'logo_url' => 'https://example.com/logo.png',
])->assertRedirect(route('app.workspace.brand'))
->assertSessionHasNoErrors();
});
test('update settings does not touch the logo when no logo_url is provided', function () {
$logoAttacher = $this->mock(LogoAttacher::class);
$logoAttacher->shouldReceive('attach')->never();
$this->actingAs($this->user)
->put(route('app.workspace.settings.update'), [
'name' => $this->workspace->name,
])->assertRedirect();
});