trypost/tests/Feature/Actions/PostComment/NotifyMentionsTest.php
Paulo Castellano 3c3b170b21 feat: @mentions in comments, AI Action layer + MCP tools, settings tabs
Mentions in post comments
- @mention autocomplete (workspace members, current user excluded) with
  marker syntax @[uuid] persisted, display names rendered via CommentBody
  chips; live edit replaces markers with names and converts back on save.
- NotifyMentions action with workspace-scoped membership check, dedupes
  same user, only newly-added mentions on update.
- Email + in-app via SendNotification job, respecting per-user
  notification_preferences.mentioned_in_comment.
- Heartbeat-based presence (Cache, 60s TTL, 30s ping) so online recipients
  get only the in-app notification — no email noise.
- Real-time bell on workspace.{id}.user.{id} private channel
  (NotificationCreated event), scoped channel name avoids client-side
  filtering and lays out a convention for future workspace channels.
- Mailable localized via lang/{en,es,pt-BR}/mail.php; Maizzle source
  template for the email is committed and built into resources/views/mail.

AI generation refactor (Action layer + MCP)
- Extracted Actions/Ai/Generate{Image,Video} with QuotaExhaustedException
  so agent tools and MCP tools share a single domain entry point.
- Mcp/Tools/Ai/Generate{Image,Video}Tool registered in TryPostServer; both
  return MediaResource payloads.
- Orientation::imageApiSize maps non-OpenAI ratios to 1:1/2:3/3:2.
- config/ai.php is now the single source of truth driven by env, removing
  the trypost.ai shim. Default text/image providers flipped to OpenAI.

Settings/UX
- /settings/workspace split into shadcn Tabs (Workspace / Brand / Users)
  with three components.
- /assets and the in-editor MediaPicker open the ImagePreviewDialog
  lightbox on image click while preserving action button behaviour.
- Comments tab landed via ?tab=comments&comment=<id> from notification
  click (scroll-to + temporary highlight).
- Mention autocomplete popover flips above when near the viewport bottom.
- Real social platform PNGs replace Tabler brand glyphs in schedule
  pills and post list, with hover tooltip carrying display_name + handle.

Bug fixes
- AcceptInvite: controller now passes workspace + role payload that the
  Vue page expects; login/register CTAs preselect the invite email.
- WorkspaceInvite mailable: stopped referencing nonexistent
  $invite->workspace and $invite->role; column added to the migration,
  Invite model casts role to WorkspaceRole, CreateInvite persists it.
- PostCommentCreated: added broadcastAs so .PostCommentCreated actually
  matches the Echo listener; payload now includes mentioned_users so
  receivers render the chip correctly without a refetch.
- Preview components for X/Pinterest/Threads/Bluesky/LinkedIn/Mastodon/
  TikTok/YouTube switched from item.type === 'image' to
  !isVideoMedia(item) so media without a persisted type still renders.
- UpdatePostRequest now accepts media.*.{type,mime_type,size,...} so the
  posts.media JSON keeps the metadata that the previews need.
- Removed throttle:6,1 from social connect routes (was 429ing legitimate
  OAuth retries).
- Used MediaType enum cases instead of literal 'image'/'video' strings
  when creating media rows.

Tests
- MentionParser unit tests, NotifyMentions feature tests including
  online/offline channel selection and preference gating, MCP AI tool
  happy paths, MentionedInComment mailable rendering, AcceptInvite +
  search-members + index mentioned_users path. 1229 passing.
2026-05-01 20:59:03 -03:00

210 lines
6.5 KiB
PHP

<?php
declare(strict_types=1);
use App\Actions\PostComment\NotifyMentions;
use App\Enums\Notification\Channel;
use App\Enums\Notification\Type;
use App\Enums\UserWorkspace\Role;
use App\Jobs\SendNotification;
use App\Mail\MentionedInComment;
use App\Models\Notification;
use App\Models\Post;
use App\Models\PostComment;
use App\Models\User;
use App\Models\Workspace;
use App\Support\WorkspacePresence;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Queue;
beforeEach(function () {
Mail::fake();
Queue::fake();
$this->author = User::factory()->create();
$this->mentioned = User::factory()->create();
$this->stranger = User::factory()->create();
$this->workspace = Workspace::factory()->create(['user_id' => $this->author->id]);
$this->workspace->members()->attach($this->author->id, ['role' => Role::Member->value]);
$this->workspace->members()->attach($this->mentioned->id, ['role' => Role::Member->value]);
$this->author->update(['current_workspace_id' => $this->workspace->id]);
$this->post = Post::factory()->create([
'workspace_id' => $this->workspace->id,
'user_id' => $this->author->id,
]);
});
test('notifies a workspace member mentioned in the comment body', function () {
$comment = PostComment::factory()->create([
'post_id' => $this->post->id,
'user_id' => $this->author->id,
'body' => "Hey @[{$this->mentioned->id}] could you take a look?",
]);
NotifyMentions::execute($comment);
Queue::assertPushed(SendNotification::class, fn ($job) => $job->user->id === $this->mentioned->id
&& $job->type === Type::MentionedInComment
);
});
test('does not notify the comment author when self-mentioning', function () {
$comment = PostComment::factory()->create([
'post_id' => $this->post->id,
'user_id' => $this->author->id,
'body' => "Note for myself @[{$this->author->id}]",
]);
NotifyMentions::execute($comment);
Queue::assertNotPushed(SendNotification::class);
});
test('does not notify users that are not workspace members', function () {
$comment = PostComment::factory()->create([
'post_id' => $this->post->id,
'user_id' => $this->author->id,
'body' => "FYI @[{$this->stranger->id}]",
]);
NotifyMentions::execute($comment);
Queue::assertNotPushed(SendNotification::class);
});
test('on update only notifies newly added mentions', function () {
$comment = PostComment::factory()->create([
'post_id' => $this->post->id,
'user_id' => $this->author->id,
'body' => "Hi @[{$this->mentioned->id}]",
]);
$secondMember = User::factory()->create();
$this->workspace->members()->attach($secondMember->id, ['role' => Role::Member->value]);
$previousBody = $comment->body;
$comment->update(['body' => "Hi @[{$this->mentioned->id}] and @[{$secondMember->id}]"]);
NotifyMentions::execute($comment, $previousBody);
Queue::assertPushed(SendNotification::class, 1);
Queue::assertPushed(
SendNotification::class,
fn ($job) => $job->user->id === $secondMember->id
);
});
test('dedupes repeated mentions of the same user', function () {
$comment = PostComment::factory()->create([
'post_id' => $this->post->id,
'user_id' => $this->author->id,
'body' => "@[{$this->mentioned->id}] @[{$this->mentioned->id}] @[{$this->mentioned->id}]",
]);
NotifyMentions::execute($comment);
Queue::assertPushed(SendNotification::class, 1);
});
test('with no mention markers no jobs are queued', function () {
$comment = PostComment::factory()->create([
'post_id' => $this->post->id,
'user_id' => $this->author->id,
'body' => 'Plain comment with no mention',
]);
NotifyMentions::execute($comment);
Queue::assertNotPushed(SendNotification::class);
});
test('online recipient (workspace presence) gets InApp only — no mailable', function () {
WorkspacePresence::markOnline($this->workspace->id, $this->mentioned->id);
$comment = PostComment::factory()->create([
'post_id' => $this->post->id,
'user_id' => $this->author->id,
'body' => "Hey @[{$this->mentioned->id}]",
]);
NotifyMentions::execute($comment);
Queue::assertPushed(SendNotification::class, function ($job) {
expect($job->channel)->toBe(Channel::InApp);
expect($job->mailable)->toBeNull();
return true;
});
});
test('offline recipient gets Both (in-app + email)', function () {
$comment = PostComment::factory()->create([
'post_id' => $this->post->id,
'user_id' => $this->author->id,
'body' => "Hey @[{$this->mentioned->id}]",
]);
NotifyMentions::execute($comment);
Queue::assertPushed(SendNotification::class, function ($job) {
expect($job->channel)->toBe(Channel::Both);
expect($job->mailable)->toBeInstanceOf(MentionedInComment::class);
return true;
});
});
test('respects mentioned_in_comment preference: when disabled, no email is queued', function () {
$this->mentioned->notificationPreference()->create([
'post_published' => true,
'post_failed' => true,
'account_disconnected' => true,
'mentioned_in_comment' => false,
]);
$comment = PostComment::factory()->create([
'post_id' => $this->post->id,
'user_id' => $this->author->id,
'body' => "Hey @[{$this->mentioned->id}]",
]);
NotifyMentions::execute($comment);
Queue::assertPushed(SendNotification::class, function ($job) {
$job->handle();
return true;
});
// In-app notification still saved (Channel::Both, but email path is gated by user preference)
expect(Notification::where('user_id', $this->mentioned->id)
->where('type', Type::MentionedInComment)
->count())->toBe(1);
Mail::assertNothingQueued();
});
test('processed job persists a Notification row + sends the mailable', function () {
$comment = PostComment::factory()->create([
'post_id' => $this->post->id,
'user_id' => $this->author->id,
'body' => "Hey @[{$this->mentioned->id}]",
]);
NotifyMentions::execute($comment);
Queue::assertPushed(SendNotification::class, function ($job) {
$job->handle();
return true;
});
expect(Notification::where('user_id', $this->mentioned->id)
->where('type', Type::MentionedInComment)
->count())->toBe(1);
Mail::assertQueued(MentionedInComment::class);
});