Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.
PHP ai-agentsblueskyfacebookinstagramlaravellinkedinmastodonmcpmcp-serveropen-sourcerest-apiself-hostedsocial-mediasocial-media-managementsocial-media-schedulerthreadstiktokvuejsxyoutube
fix: refresh tokens before verifying social connections and fix dark mode
Paulo Castellanoa976e112639d
diff --git a/app/Services/Social/ConnectionVerifier.php b/app/Services/Social/ConnectionVerifier.phpindex cf7ede133..27299aaac 100644--- a/app/Services/Social/ConnectionVerifier.php+++ b/app/Services/Social/ConnectionVerifier.php@@ -6,6 +6,7 @@ use App\Enums\SocialAccount\Platform;use App\Exceptions\TokenExpiredException;use App\Models\SocialAccount;use Illuminate\Support\Facades\Http;+use Illuminate\Support\Facades\Log;class ConnectionVerifier{@@ -16,6 +17,11 @@ class ConnectionVerifier*/public function verify(SocialAccount $account): bool{+ // Refresh token if expired or expiring soon before verifying+ if ($account->is_token_expired || $account->is_token_expiring_soon) {+ $this->refreshTokenIfNeeded($account);+ }+return match ($account->platform) {Platform::LinkedIn => $this->verifyLinkedIn($account),Platform::LinkedInPage => $this->verifyLinkedInPage($account),@@ -31,6 +37,251 @@ class ConnectionVerifier};}+ /**+ * Refresh token based on platform type.+ *+ * @throws TokenExpiredException if refresh fails+ */+ private function refreshTokenIfNeeded(SocialAccount $account): void+ {+ match ($account->platform) {+ Platform::LinkedIn, Platform::LinkedInPage => $this->refreshLinkedInToken($account),+ Platform::X => $this->refreshXToken($account),+ Platform::Bluesky => $this->refreshBlueskyToken($account),+ Platform::YouTube => $this->refreshYouTubeToken($account),+ Platform::TikTok => $this->refreshTikTokToken($account),+ Platform::Pinterest => $this->refreshPinterestToken($account),+ Platform::Threads => $this->refreshThreadsToken($account),+ // Facebook, Instagram use long-lived tokens without refresh mechanism+ // Mastodon tokens don't expire+ default => null,+ };+ }++ private function refreshLinkedInToken(SocialAccount $account): void+ {+ if (! $account->refresh_token) {+ throw new TokenExpiredException('No refresh token available for LinkedIn account');+ }++ $response = Http::asForm()->post('https://www.linkedin.com/oauth/v2/accessToken', [+ 'grant_type' => 'refresh_token',+ 'refresh_token' => $account->refresh_token,+ 'client_id' => config('services.linkedin.client_id'),+ 'client_secret' => config('services.linkedin.client_secret'),+ ]);++ if ($response->failed()) {+ Log::error('ConnectionVerifier: LinkedIn token refresh failed', ['body' => $response->body()]);+ throw new TokenExpiredException('Failed to refresh LinkedIn token');+ }++ $data = $response->json();++ $account->update([+ 'access_token' => $data['access_token'],+ 'refresh_token' => $data['refresh_token'] ?? $account->refresh_token,+ 'token_expires_at' => isset($data['expires_in']) ? now()->addSeconds($data['expires_in']) : null,+ ]);++ $account->refresh();+ }++ private function refreshXToken(SocialAccount $account): void+ {+ if (! $account->refresh_token) {+ throw new TokenExpiredException('No refresh token available for X account');+ }++ $response = Http::asForm()+ ->withBasicAuth(config('services.x.client_id'), config('services.x.client_secret'))+ ->post('https://api.x.com/2/oauth2/token', [+ 'grant_type' => 'refresh_token',+ 'refresh_token' => $account->refresh_token,+ ]);++ if ($response->failed()) {+ Log::error('ConnectionVerifier: X token refresh failed', ['body' => $response->body()]);+ throw new TokenExpiredException('Failed to refresh X token');+ }++ $data = $response->json();++ $account->update([+ 'access_token' => $data['access_token'],+ 'refresh_token' => $data['refresh_token'] ?? $account->refresh_token,+ 'token_expires_at' => now()->addSeconds($data['expires_in'] ?? 7200),+ ]);++ $account->refresh();+ }++ private function refreshBlueskyToken(SocialAccount $account): void+ {+ $service = $account->meta['service'] ?? 'https://bsky.social';++ // Try refresh token first+ $response = Http::withToken($account->refresh_token)+ ->post("{$service}/xrpc/com.atproto.server.refreshSession");++ if ($response->successful()) {+ $data = $response->json();+ $account->update([+ 'access_token' => $data['accessJwt'],+ 'refresh_token' => $data['refreshJwt'],+ 'token_expires_at' => now()->addHours(2),+ ]);++ $account->refresh();++ return;+ }++ // If refresh fails, re-authenticate with stored credentials+ if (isset($account->meta['password'])) {+ try {+ $password = decrypt($account->meta['password']);+ $identifier = $account->meta['identifier'];++ $response = Http::post("{$service}/xrpc/com.atproto.server.createSession", [+ 'identifier' => $identifier,+ 'password' => $password,+ ]);++ if ($response->successful()) {+ $data = $response->json();+ $account->update([+ 'access_token' => $data['accessJwt'],+ 'refresh_token' => $data['refreshJwt'],+ 'token_expires_at' => now()->addHours(2),+ ]);++ $account->refresh();++ return;+ }+ } catch (\Exception $e) {+ Log::error('ConnectionVerifier: Bluesky re-authentication failed', [+ 'error' => $e->getMessage(),+ ]);+ }+ }++ throw new TokenExpiredException('Bluesky session expired');+ }++ private function refreshYouTubeToken(SocialAccount $account): void+ {+ if (! $account->refresh_token) {+ throw new TokenExpiredException('No refresh token available for YouTube account');+ }++ $response = Http::asForm()->post('https://oauth2.googleapis.com/token', [+ 'grant_type' => 'refresh_token',+ 'refresh_token' => $account->refresh_token,+ 'client_id' => config('services.youtube.client_id'),+ 'client_secret' => config('services.youtube.client_secret'),+ ]);++ if ($response->failed()) {+ Log::error('ConnectionVerifier: YouTube token refresh failed', ['body' => $response->body()]);+ throw new TokenExpiredException('Failed to refresh YouTube token');+ }++ $data = $response->json();++ $account->update([+ 'access_token' => $data['access_token'],+ 'token_expires_at' => isset($data['expires_in']) ? now()->addSeconds($data['expires_in']) : null,+ ]);++ $account->refresh();+ }++ private function refreshTikTokToken(SocialAccount $account): void+ {+ if (! $account->refresh_token) {+ throw new TokenExpiredException('No refresh token available for TikTok account');+ }++ $response = Http::asForm()->post('https://open.tiktokapis.com/v2/oauth/token/', [+ 'grant_type' => 'refresh_token',+ 'refresh_token' => $account->refresh_token,+ 'client_key' => config('services.tiktok.client_id'),+ 'client_secret' => config('services.tiktok.client_secret'),+ ]);++ if ($response->failed()) {+ Log::error('ConnectionVerifier: TikTok token refresh failed', ['body' => $response->body()]);+ throw new TokenExpiredException('Failed to refresh TikTok token');+ }++ $data = $response->json();++ $account->update([+ 'access_token' => $data['access_token'],+ 'refresh_token' => $data['refresh_token'] ?? $account->refresh_token,+ 'token_expires_at' => isset($data['expires_in']) ? now()->addSeconds($data['expires_in']) : null,+ ]);++ $account->refresh();+ }++ private function refreshPinterestToken(SocialAccount $account): void+ {+ if (! $account->refresh_token) {+ throw new TokenExpiredException('No refresh token available for Pinterest account');+ }++ $credentials = base64_encode(config('services.pinterest.client_id').':'.config('services.pinterest.client_secret'));++ $response = Http::withHeaders([+ 'Authorization' => "Basic {$credentials}",+ 'Content-Type' => 'application/x-www-form-urlencoded',+ ])->asForm()->post('https://api.pinterest.com/v5/oauth/token', [+ 'grant_type' => 'refresh_token',+ 'refresh_token' => $account->refresh_token,+ ]);++ if ($response->failed()) {+ Log::error('ConnectionVerifier: Pinterest token refresh failed', ['body' => $response->body()]);+ throw new TokenExpiredException('Failed to refresh Pinterest token');+ }++ $data = $response->json();++ $account->update([+ 'access_token' => $data['access_token'],+ 'refresh_token' => $data['refresh_token'] ?? $account->refresh_token,+ 'token_expires_at' => isset($data['expires_in']) ? now()->addSeconds($data['expires_in']) : null,+ ]);++ $account->refresh();+ }++ private function refreshThreadsToken(SocialAccount $account): void+ {+ // Threads uses long-lived tokens that can be refreshed+ $response = Http::get('https://graph.threads.net/refresh_access_token', [+ 'grant_type' => 'th_refresh_token',+ 'access_token' => $account->access_token,+ ]);++ if ($response->failed()) {+ Log::error('ConnectionVerifier: Threads token refresh failed', ['body' => $response->body()]);+ throw new TokenExpiredException('Failed to refresh Threads token');+ }++ $data = $response->json();++ $account->update([+ 'access_token' => $data['access_token'],+ 'token_expires_at' => isset($data['expires_in']) ? now()->addSeconds($data['expires_in']) : null,+ ]);++ $account->refresh();+ }+private function verifyLinkedIn(SocialAccount $account): bool{$response = Http::withToken($account->access_token)diff --git a/resources/js/pages/posts/Calendar.vue b/resources/js/pages/posts/Calendar.vueindex 205702984..5e27e29a4 100644--- a/resources/js/pages/posts/Calendar.vue+++ b/resources/js/pages/posts/Calendar.vue@@ -165,14 +165,14 @@ const isCurrentMonth = (day: dayjs.Dayjs): boolean => {const getStatusColor = (status: string): string => {const colors: Record<string, string> = {- draft: 'bg-gray-100 border-gray-300 text-gray-700',- scheduled: 'bg-blue-50 border-blue-300 text-blue-700',- publishing: 'bg-yellow-50 border-yellow-300 text-yellow-700',- published: 'bg-green-50 border-green-300 text-green-700',- partially_published: 'bg-orange-50 border-orange-300 text-orange-700',- failed: 'bg-red-50 border-red-300 text-red-700',+ draft: 'bg-neutral-100 border-neutral-300 text-neutral-700 dark:bg-neutral-800 dark:border-neutral-600 dark:text-neutral-300',+ scheduled: 'bg-blue-50 border-blue-300 text-blue-700 dark:bg-blue-950 dark:border-blue-800 dark:text-blue-300',+ publishing: 'bg-yellow-50 border-yellow-300 text-yellow-700 dark:bg-yellow-950 dark:border-yellow-800 dark:text-yellow-300',+ published: 'bg-green-50 border-green-300 text-green-700 dark:bg-green-950 dark:border-green-800 dark:text-green-300',+ partially_published: 'bg-orange-50 border-orange-300 text-orange-700 dark:bg-orange-950 dark:border-orange-800 dark:text-orange-300',+ failed: 'bg-red-50 border-red-300 text-red-700 dark:bg-red-950 dark:border-red-800 dark:text-red-300',};- return colors[status] || 'bg-gray-100 border-gray-300 text-gray-700';+ return colors[status] || 'bg-neutral-100 border-neutral-300 text-neutral-700 dark:bg-neutral-800 dark:border-neutral-600 dark:text-neutral-300';};const getPlatformLogo = (platform: string): string => {@@ -279,9 +279,9 @@ const formatTime = (scheduledAt: string): string => {<div class="flex -space-x-1 mb-1.5"><img v-for="pp in post.post_platforms.slice(0, 4)" :key="pp.id":src="getPlatformLogo(pp.platform)" :alt="pp.platform"- class="h-5 w-5 rounded-full ring-2 ring-white" />+ class="h-5 w-5 rounded-full ring-2 ring-background" /><span v-if="post.post_platforms.length > 4"- class="flex items-center justify-center h-5 w-5 rounded-full bg-muted text-[10px] font-medium ring-2 ring-white">+ class="flex items-center justify-center h-5 w-5 rounded-full bg-muted text-[10px] font-medium ring-2 ring-background">+{{ post.post_platforms.length - 4 }}</span></div>@@ -342,9 +342,9 @@ const formatTime = (scheduledAt: string): string => {<div class="flex -space-x-1 shrink-0"><img v-for="pp in post.post_platforms.slice(0, post.post_platforms.length > 4 ? 3 : 4)":key="pp.id" :src="getPlatformLogo(pp.platform)" :alt="pp.platform"- class="h-4 w-4 rounded-full ring-1 ring-white" />+ class="h-4 w-4 rounded-full ring-1 ring-background" /><span v-if="post.post_platforms.length > 4"- class="flex items-center justify-center h-4 w-4 rounded-full bg-muted text-[9px] font-medium ring-1 ring-white">+ class="flex items-center justify-center h-4 w-4 rounded-full bg-muted text-[9px] font-medium ring-1 ring-background">+{{ post.post_platforms.length - 3 }}</span></div>diff --git a/resources/js/pages/posts/Edit.vue b/resources/js/pages/posts/Edit.vueindex 683fb77c9..48d98da87 100644--- a/resources/js/pages/posts/Edit.vue+++ b/resources/js/pages/posts/Edit.vue@@ -273,7 +273,7 @@ const getPlatformIcon = (platform: string): Component => {const getStatusConfig = (status: string) => {const configs: Record<string, { color: string; icon: any }> = {- 'draft': { color: 'bg-gray-100 text-gray-800', icon: IconClock },+ 'draft': { color: 'bg-neutral-100 text-neutral-800', icon: IconClock },'scheduled': { color: 'bg-blue-100 text-blue-800', icon: IconClock },'publishing': { color: 'bg-yellow-100 text-yellow-800', icon: IconLoader2 },'published': { color: 'bg-green-100 text-green-800', icon: IconCircleCheck },@@ -658,18 +658,17 @@ const deletePost = () => {</div><div v-if="selectedPlatforms.length > 0"- class="bg-muted text-muted-foreground inline-flex h-9 w-fit items-center justify-center rounded-lg p-[3px]">+ class="bg-muted text-muted-foreground inline-flex h-9 w-fit items-center justify-center rounded-lg p-1"><TooltipProvider><Tooltip v-for="pp in selectedPlatforms" :key="pp.id"><TooltipTrigger asChild><button type="button" @click="activeTabId = pp.id" :class="[- 'relative inline-flex h-[calc(100%-1px)] items-center justify-center gap-1.5 rounded-md border border-transparent px-2 py-1 text-sm font-medium whitespace-nowrap transition-[color,box-shadow] focus-visible:ring-[3px] focus-visible:outline-1 focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:outline-ring',+ 'relative inline-flex items-center justify-center gap-1.5 rounded-md px-2 py-1.5 text-sm font-medium whitespace-nowrap transition-all',activeTabId === pp.id- ? 'bg-background text-foreground shadow-sm dark:border-input dark:bg-input/30'- : 'text-foreground dark:text-muted-foreground'+ ? 'bg-background text-foreground shadow-sm'+ : 'hover:bg-background/50 hover:text-foreground']">- <component :is="getPlatformIcon(pp.platform)"- class="h-5 w-5 text-neutral-500 rounded-full" />+ <component :is="getPlatformIcon(pp.platform)" class="h-5 w-5" /><!-- Status indicator (read-only) or Validation indicator (edit mode) --><span v-if="isReadOnly"class="absolute -top-0.5 -right-0.5 h-2 w-2 rounded-full border border-background"@@ -677,7 +676,7 @@ const deletePost = () => {'bg-green-500': pp.status === 'published','bg-red-500': pp.status === 'failed','bg-yellow-500 animate-pulse': pp.status === 'publishing',- 'bg-gray-400': !['published', 'failed', 'publishing'].includes(pp.status)+ 'bg-neutral-400': !['published', 'failed', 'publishing'].includes(pp.status)}" /><span v-else-if="contentValidation[pp.id]"class="absolute -top-0.5 -right-0.5 h-2 w-2 rounded-full border border-background"@@ -770,7 +769,7 @@ const deletePost = () => {<div class="flex items-center justify-between p-4 rounded-lg border"><div class="flex items-center gap-3"><component :is="getPlatformIcon(activePlatform.platform)"- class="h-8 w-8 text-neutral-600" />+ class="h-8 w-8 text-muted-foreground" /><div><p class="font-medium">{{ activePlatform.social_account.display_name }}</p><p class="text-sm text-muted-foreground">{{@@ -885,7 +884,7 @@ const deletePost = () => {<button v-for="pp in post.post_platforms" :key="pp.id" type="button"@click="togglePlatform(pp.id)"class="flex items-center gap-2 px-3 py-2 rounded-lg border hover:bg-muted transition-colors">- <component :is="getPlatformIcon(pp.platform)" class="h-5 w-5 text-neutral-600" />+ <component :is="getPlatformIcon(pp.platform)" class="h-5 w-5 text-muted-foreground" /><span class="text-sm">{{ pp.social_account.display_name }}</span></button></div>diff --git a/tests/Feature/Services/Social/ConnectionVerifierTest.php b/tests/Feature/Services/Social/ConnectionVerifierTest.phpnew file mode 100644index 000000000..fef0b2787--- /dev/null+++ b/tests/Feature/Services/Social/ConnectionVerifierTest.php@@ -0,0 +1,277 @@+<?php++use App\Exceptions\TokenExpiredException;+use App\Models\SocialAccount;+use App\Services\Social\ConnectionVerifier;+use Illuminate\Support\Facades\Http;++test('verifies account without refresh when token is not expired', function () {+ Http::fake([+ 'api.linkedin.com/*' => Http::response(['sub' => '123'], 200),+ ]);++ $account = SocialAccount::factory()->linkedin()->create([+ 'token_expires_at' => now()->addDays(30),+ ]);++ $verifier = new ConnectionVerifier;+ $result = $verifier->verify($account);++ expect($result)->toBeTrue();++ Http::assertSentCount(1);+ Http::assertSent(fn ($request) => str_contains($request->url(), 'api.linkedin.com/rest/userinfo'));+});++test('refreshes linkedin token before verifying when expired', function () {+ Http::fake([+ 'www.linkedin.com/oauth/v2/accessToken' => Http::response([+ 'access_token' => 'new_token',+ 'refresh_token' => 'new_refresh_token',+ 'expires_in' => 5184000,+ ], 200),+ 'api.linkedin.com/*' => Http::response(['sub' => '123'], 200),+ ]);++ $account = SocialAccount::factory()->linkedin()->create([+ 'token_expires_at' => now()->subHour(),+ 'refresh_token' => 'old_refresh_token',+ ]);++ $verifier = new ConnectionVerifier;+ $result = $verifier->verify($account);++ expect($result)->toBeTrue();+ expect($account->fresh()->token_expires_at)->toBeGreaterThan(now());++ Http::assertSent(fn ($request) => str_contains($request->url(), 'linkedin.com/oauth/v2/accessToken'));+});++test('refreshes x token before verifying when expired', function () {+ Http::fake([+ 'api.x.com/2/oauth2/token' => Http::response([+ 'access_token' => 'new_token',+ 'refresh_token' => 'new_refresh_token',+ 'expires_in' => 7200,+ ], 200),+ 'api.x.com/2/users/me' => Http::response(['data' => ['id' => '123']], 200),+ ]);++ $account = SocialAccount::factory()->x()->create([+ 'token_expires_at' => now()->subHour(),+ 'refresh_token' => 'old_refresh_token',+ ]);++ $verifier = new ConnectionVerifier;+ $result = $verifier->verify($account);++ expect($result)->toBeTrue();+ expect($account->fresh()->token_expires_at)->toBeGreaterThan(now());++ Http::assertSent(fn ($request) => str_contains($request->url(), 'api.x.com/2/oauth2/token'));+});++test('refreshes bluesky token before verifying when expired', function () {+ Http::fake([+ 'bsky.social/xrpc/com.atproto.server.refreshSession' => Http::response([+ 'accessJwt' => 'new_access_token',+ 'refreshJwt' => 'new_refresh_token',+ ], 200),+ 'bsky.social/xrpc/app.bsky.actor.getProfile*' => Http::response(['did' => 'did:plc:123'], 200),+ ]);++ $account = SocialAccount::factory()->bluesky()->create([+ 'token_expires_at' => now()->subHour(),+ 'refresh_token' => 'old_refresh_token',+ ]);++ $verifier = new ConnectionVerifier;+ $result = $verifier->verify($account);++ expect($result)->toBeTrue();+ expect($account->fresh()->token_expires_at)->toBeGreaterThan(now());++ Http::assertSent(fn ($request) => str_contains($request->url(), 'refreshSession'));+});++test('refreshes youtube token before verifying when expired', function () {+ Http::fake([+ 'oauth2.googleapis.com/token' => Http::response([+ 'access_token' => 'new_token',+ 'expires_in' => 3600,+ ], 200),+ 'www.googleapis.com/youtube/*' => Http::response(['items' => []], 200),+ ]);++ $account = SocialAccount::factory()->youtube()->create([+ 'token_expires_at' => now()->subHour(),+ 'refresh_token' => 'old_refresh_token',+ ]);++ $verifier = new ConnectionVerifier;+ $result = $verifier->verify($account);++ expect($result)->toBeTrue();++ Http::assertSent(fn ($request) => str_contains($request->url(), 'oauth2.googleapis.com/token'));+});++test('refreshes tiktok token before verifying when expired', function () {+ Http::fake([+ 'open.tiktokapis.com/v2/oauth/token/' => Http::response([+ 'access_token' => 'new_token',+ 'refresh_token' => 'new_refresh_token',+ 'expires_in' => 86400,+ ], 200),+ 'open.tiktokapis.com/v2/user/info/*' => Http::response(['data' => ['user' => []]], 200),+ ]);++ $account = SocialAccount::factory()->tiktok()->create([+ 'token_expires_at' => now()->subHour(),+ 'refresh_token' => 'old_refresh_token',+ ]);++ $verifier = new ConnectionVerifier;+ $result = $verifier->verify($account);++ expect($result)->toBeTrue();++ Http::assertSent(fn ($request) => str_contains($request->url(), 'tiktokapis.com/v2/oauth/token'));+});++test('refreshes pinterest token before verifying when expired', function () {+ Http::fake([+ 'api.pinterest.com/v5/oauth/token' => Http::response([+ 'access_token' => 'new_token',+ 'refresh_token' => 'new_refresh_token',+ 'expires_in' => 86400,+ ], 200),+ 'api.pinterest.com/v5/user_account' => Http::response(['username' => 'test'], 200),+ ]);++ $account = SocialAccount::factory()->pinterest()->create([+ 'token_expires_at' => now()->subHour(),+ 'refresh_token' => 'old_refresh_token',+ ]);++ $verifier = new ConnectionVerifier;+ $result = $verifier->verify($account);++ expect($result)->toBeTrue();++ Http::assertSent(fn ($request) => str_contains($request->url(), 'pinterest.com/v5/oauth/token'));+});++test('refreshes threads token before verifying when expired', function () {+ Http::fake([+ 'graph.threads.net/refresh_access_token*' => Http::response([+ 'access_token' => 'new_token',+ 'expires_in' => 5184000,+ ], 200),+ 'graph.threads.net/v1.0/me*' => Http::response(['id' => '123', 'username' => 'test'], 200),+ ]);++ $account = SocialAccount::factory()->threads()->create([+ 'token_expires_at' => now()->subHour(),+ ]);++ $verifier = new ConnectionVerifier;+ $result = $verifier->verify($account);++ expect($result)->toBeTrue();++ Http::assertSent(fn ($request) => str_contains($request->url(), 'refresh_access_token'));+});++test('throws exception when linkedin refresh fails', function () {+ Http::fake([+ 'www.linkedin.com/oauth/v2/accessToken' => Http::response(['error' => 'invalid_grant'], 400),+ ]);++ $account = SocialAccount::factory()->linkedin()->create([+ 'token_expires_at' => now()->subHour(),+ 'refresh_token' => 'old_refresh_token',+ ]);++ $verifier = new ConnectionVerifier;++ expect(fn () => $verifier->verify($account))+ ->toThrow(TokenExpiredException::class, 'Failed to refresh LinkedIn token');+});++test('throws exception when x refresh fails', function () {+ Http::fake([+ 'api.x.com/2/oauth2/token' => Http::response(['error' => 'invalid_grant'], 400),+ ]);++ $account = SocialAccount::factory()->x()->create([+ 'token_expires_at' => now()->subHour(),+ 'refresh_token' => 'old_refresh_token',+ ]);++ $verifier = new ConnectionVerifier;++ expect(fn () => $verifier->verify($account))+ ->toThrow(TokenExpiredException::class, 'Failed to refresh X token');+});++test('does not refresh facebook token as it uses long-lived tokens', function () {+ Http::fake([+ 'graph.facebook.com/*' => Http::response(['id' => '123', 'name' => 'Test'], 200),+ ]);++ $account = SocialAccount::factory()->facebook()->create([+ 'token_expires_at' => now()->subHour(),+ ]);++ $verifier = new ConnectionVerifier;+ $result = $verifier->verify($account);++ expect($result)->toBeTrue();++ // Should only call the verify endpoint, no refresh+ Http::assertSentCount(1);+ Http::assertSent(fn ($request) => str_contains($request->url(), 'graph.facebook.com'));+});++test('does not refresh instagram token as it uses long-lived tokens', function () {+ Http::fake([+ 'graph.instagram.com/*' => Http::response(['id' => '123', 'username' => 'test'], 200),+ ]);++ $account = SocialAccount::factory()->instagram()->create([+ 'token_expires_at' => now()->subHour(),+ ]);++ $verifier = new ConnectionVerifier;+ $result = $verifier->verify($account);++ expect($result)->toBeTrue();++ Http::assertSentCount(1);+ Http::assertSent(fn ($request) => str_contains($request->url(), 'graph.instagram.com'));+});++test('refreshes token when expiring soon', function () {+ Http::fake([+ 'www.linkedin.com/oauth/v2/accessToken' => Http::response([+ 'access_token' => 'new_token',+ 'refresh_token' => 'new_refresh_token',+ 'expires_in' => 5184000,+ ], 200),+ 'api.linkedin.com/*' => Http::response(['sub' => '123'], 200),+ ]);++ // Token expires in 30 minutes (less than 1 hour threshold)+ $account = SocialAccount::factory()->linkedin()->create([+ 'token_expires_at' => now()->addMinutes(30),+ 'refresh_token' => 'old_refresh_token',+ ]);++ $verifier = new ConnectionVerifier;+ $result = $verifier->verify($account);++ expect($result)->toBeTrue();++ Http::assertSent(fn ($request) => str_contains($request->url(), 'linkedin.com/oauth/v2/accessToken'));+});