From 9f0c264c39a42fb86658c5c7fc9c223684bd2b08 Mon Sep 17 00:00:00 2001 From: Paulo Castellano Date: Sat, 13 Jun 2026 18:21:16 -0300 Subject: [PATCH] Cover mention endpoint fallback and partial resolution Add tests for the two untested branches of the DID resolution: falling back to the public AppView when the account's PDS can't resolve a handle, and a post mixing a resolvable mention (kept as a facet) with an unresolvable one (a 200 without a DID, left as plain text). The prior tests only hit the happy path and the all-endpoints-fail path via a wildcard that masked the fallback. --- .../Services/Social/BlueskyPublisherTest.php | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/tests/Feature/Services/Social/BlueskyPublisherTest.php b/tests/Feature/Services/Social/BlueskyPublisherTest.php index 0c38df33..d7a296e5 100644 --- a/tests/Feature/Services/Social/BlueskyPublisherTest.php +++ b/tests/Feature/Services/Social/BlueskyPublisherTest.php @@ -169,6 +169,80 @@ }); }); +test('bluesky publisher falls back to the public AppView when the PDS cannot resolve', function () { + $this->post->update(['content' => 'Shout out to @friend.bsky.social']); + + $service = config('trypost.platforms.bluesky.default_service'); + $appView = config('trypost.platforms.bluesky.public_appview'); + + Http::fake([ + // The account's PDS fails to resolve; resolution must fall back to the + // public AppView rather than giving up after the first endpoint. + $service.'/xrpc/com.atproto.identity.resolveHandle*' => Http::response(['error' => 'InvalidRequest'], 400), + $appView.'/xrpc/com.atproto.identity.resolveHandle*' => Http::response(['did' => 'did:plc:friend456'], 200), + $service.'/xrpc/com.atproto.repo.createRecord' => Http::response([ + 'uri' => 'at://did:plc:testuser123/app.bsky.feed.post/3abc123xyz', + 'cid' => 'bafyreiabc123', + ], 200), + ]); + + $this->publisher->publish($this->postPlatform); + + Http::assertSent(function ($request) { + $record = $request->data()['record'] ?? null; + + if (! $record) { + return false; + } + + foreach ($record['facets'] ?? [] as $facet) { + $feature = $facet['features'][0]; + if ($feature['$type'] === 'app.bsky.richtext.facet#mention') { + return $feature['did'] === 'did:plc:friend456'; + } + } + + return false; + }); +}); + +test('bluesky publisher resolves some mentions and skips the unresolvable ones', function () { + $this->post->update(['content' => 'cc @good.bsky.social and @bad.bsky.social']); + + Http::fake(function ($request) { + if (str_contains($request->url(), 'resolveHandle')) { + // `good` resolves; `bad` answers 200 without a DID — treated as + // unresolvable, exercising the str_starts_with('did:') guard. + return str_contains($request->url(), 'good.bsky.social') + ? Http::response(['did' => 'did:plc:good999'], 200) + : Http::response([], 200); + } + + return Http::response([ + 'uri' => 'at://did:plc:testuser123/app.bsky.feed.post/3abc123xyz', + 'cid' => 'bafyreiabc123', + ], 200); + }); + + $this->publisher->publish($this->postPlatform); + + Http::assertSent(function ($request) { + $record = $request->data()['record'] ?? null; + + if (! $record) { + return false; + } + + $mentions = collect($record['facets'] ?? []) + ->filter(fn ($facet) => $facet['features'][0]['$type'] === 'app.bsky.richtext.facet#mention'); + + // Only the resolvable handle becomes a facet; the other stays plain text. + return $mentions->count() === 1 + && $mentions->first()['features'][0]['did'] === 'did:plc:good999' + && str_contains($record['text'], '@bad.bsky.social'); + }); +}); + test('bluesky publisher uploads images', function () { // Create a media item through the PostPlatform's media() relation $this->post->update([