trypost/tests/Feature/Services/Social/TelegramPublisherTest.php
Paulo Castellano 6496588bbc
Defuse links in X posts to avoid the link-post fee (#308)
X bills a post containing a URL at a much higher rate than a plain post, and
its algorithm demotes link posts. The X version of a post now rewrites every
URL non-clickable (https://example.com/post becomes example(.)com/post):
scheme and www. dropped, every dot of the host replaced with (.).

Leaving a single dot intact would still leave a resolvable domain for X to
detect, so all of them are broken. A scheme or www. proves a token is a URL on
its own; a bare host only counts when its last label is a delegated TLD, which
is the one thing telling acme.com apart from Node.js. That check runs against
App\Support\LinkTlds, generated from the whole IANA root zone in every form a
TLD can appear in a post -- ASCII, punycode and the Unicode it decodes to --
because whatever X links is what X bills, so a hand-picked subset would leave
us paying for its gaps. If the regex engine bails out on pathological input the
original content is returned instead of crashing the publisher.

The transform lives in the Platform::X arm of ContentSanitizer, so it reaches
publishing and the app/API/MCP previews from one place and cannot touch any
other network. Off by default; opt in with X_DEFUSE_LINKS.

The editor counts characters and renders its preview client-side and cannot ask
the server on every keystroke, so the rewrite is mirrored in TypeScript. PHP
stays the source of truth: a parity test fails if the two TLD sets drift, and a
browser test drives the real editor so the mirror is covered rather than
assumed. Without it the composer promised text the network never receives.

Character limits now measure the text a reader will see: sanitized, then with
markup resolved away. Measuring the raw draft blocked saving posts that publish
fine and let through posts the network rejects, and counted the editor's HTML
toward the limit. Measuring the sanitized form alone would have counted
Telegram's escaped entities, rejecting messages Telegram accepts.

Empty content is handled once inside the sanitizer instead of by a guard
repeated at every call site.
2026-08-29 15:31:05 -03:00

233 lines
7.9 KiB
PHP

<?php
declare(strict_types=1);
use App\Enums\PostPlatform\ContentType;
use App\Enums\SocialAccount\Platform;
use App\Exceptions\Social\TelegramPublishException;
use App\Models\Post;
use App\Models\PostPlatform;
use App\Models\SocialAccount;
use App\Models\User;
use App\Models\Workspace;
use App\Services\Social\Telegram\TelegramPublisher;
use Illuminate\Support\Facades\Http;
beforeEach(function () {
config(['trypost.platforms.telegram.bot_token' => 'TESTTOKEN']);
$this->user = User::factory()->create();
$this->workspace = Workspace::factory()->create(['user_id' => $this->user->id]);
$this->socialAccount = SocialAccount::factory()->telegram()->create([
'workspace_id' => $this->workspace->id,
]);
$this->post = Post::factory()->create([
'workspace_id' => $this->workspace->id,
'user_id' => $this->user->id,
'content' => 'Hello world',
]);
$this->postPlatform = PostPlatform::factory()->create([
'post_id' => $this->post->id,
'social_account_id' => $this->socialAccount->id,
'platform' => Platform::Telegram,
'content_type' => ContentType::TelegramPost,
]);
$this->publisher = new TelegramPublisher;
});
function telegramOk(array $result): array
{
return ['ok' => true, 'result' => $result];
}
test('telegram publisher sends a text-only message', function () {
Http::fake([
'*/botTESTTOKEN/sendMessage' => Http::response(telegramOk(['message_id' => 42]), 200),
]);
$result = $this->publisher->publish($this->postPlatform);
expect($result['id'])->toBe('42');
expect($result['url'])->toBe('https://t.me/mychannel/42');
Http::assertSent(function ($request) {
return str_contains($request->url(), '/sendMessage')
&& $request['chat_id'] === '-1001234567890'
&& $request['text'] === 'Hello world'
&& $request['parse_mode'] === 'HTML';
});
});
test('telegram publisher sends a single image with caption', function () {
$this->post->update([
'content' => 'A photo',
'media' => [[
'id' => 'm1',
'path' => 'media/2026-01/pic.jpg',
'url' => 'https://cdn.test/pic.jpg',
'mime_type' => 'image/jpeg',
'original_filename' => 'pic.jpg',
]],
]);
Http::fake([
'*/botTESTTOKEN/sendPhoto' => Http::response(telegramOk(['message_id' => 7]), 200),
]);
$this->publisher->publish($this->postPlatform);
Http::assertSent(function ($request) {
return str_contains($request->url(), '/sendPhoto')
&& str_contains($request['photo'], 'pic.jpg')
&& $request['caption'] === 'A photo'
&& $request['parse_mode'] === 'HTML';
});
});
test('telegram publisher sends a single video', function () {
$this->post->update([
'content' => 'A clip',
'media' => [[
'id' => 'm1',
'path' => 'media/clip.mp4',
'url' => 'https://cdn.test/clip.mp4',
'mime_type' => 'video/mp4',
'original_filename' => 'clip.mp4',
]],
]);
Http::fake([
'*/botTESTTOKEN/sendVideo' => Http::response(telegramOk(['message_id' => 8]), 200),
]);
$this->publisher->publish($this->postPlatform);
Http::assertSent(function ($request) {
return str_contains($request->url(), '/sendVideo')
&& str_contains($request['video'], 'clip.mp4')
&& $request['caption'] === 'A clip';
});
});
test('telegram publisher sends a non-image, non-video file as a document', function () {
$this->post->update([
'content' => 'A file',
'media' => [[
'id' => 'm1',
'path' => 'media/report.pdf',
'url' => 'https://cdn.test/report.pdf',
'mime_type' => 'application/pdf',
'original_filename' => 'report.pdf',
]],
]);
Http::fake([
'*/botTESTTOKEN/sendDocument' => Http::response(telegramOk(['message_id' => 9]), 200),
]);
$this->publisher->publish($this->postPlatform);
Http::assertSent(function ($request) {
return str_contains($request->url(), '/sendDocument')
&& str_contains($request['document'], 'report.pdf');
});
});
test('telegram publisher sends multiple media as an album', function () {
$this->post->update([
'content' => 'Album',
'media' => [
['id' => 'm1', 'path' => 'media/a.jpg', 'url' => 'https://cdn.test/a.jpg', 'mime_type' => 'image/jpeg', 'original_filename' => 'a.jpg'],
['id' => 'm2', 'path' => 'media/b.jpg', 'url' => 'https://cdn.test/b.jpg', 'mime_type' => 'image/jpeg', 'original_filename' => 'b.jpg'],
],
]);
Http::fake([
'*/botTESTTOKEN/sendMediaGroup' => Http::response(telegramOk([['message_id' => 11], ['message_id' => 12]]), 200),
]);
$result = $this->publisher->publish($this->postPlatform);
expect($result['id'])->toBe('11');
Http::assertSent(function ($request) {
if (! str_contains($request->url(), '/sendMediaGroup')) {
return false;
}
$media = json_decode($request['media'], true);
return count($media) === 2
&& $media[0]['type'] === 'photo'
&& $media[0]['caption'] === 'Album'
&& ! isset($media[1]['caption']);
});
});
test('telegram publisher sends long text as its own message after media', function () {
$longText = str_repeat('x', 1500); // over the 1024 caption limit
$this->post->update([
'content' => $longText,
'media' => [[
'id' => 'm1', 'path' => 'media/p.jpg', 'url' => 'https://cdn.test/p.jpg', 'mime_type' => 'image/jpeg', 'original_filename' => 'p.jpg',
]],
]);
Http::fake([
'*/botTESTTOKEN/sendPhoto' => Http::response(telegramOk(['message_id' => 5]), 200),
'*/botTESTTOKEN/sendMessage' => Http::response(telegramOk(['message_id' => 6]), 200),
]);
$this->publisher->publish($this->postPlatform);
// Photo carries no caption (too long); the text follows as a separate message.
Http::assertSent(fn ($request) => str_contains($request->url(), '/sendPhoto') && $request['caption'] === '');
Http::assertSent(fn ($request) => str_contains($request->url(), '/sendMessage') && $request['text'] === $longText);
});
test('telegram publisher rejects content over the 4096 limit', function () {
$this->post->update(['content' => str_repeat('x', 4097)]);
Http::fake();
expect(fn () => $this->publisher->publish($this->postPlatform))->toThrow(Exception::class);
Http::assertNothingSent();
});
test('telegram publisher throws on a non-ok response', function () {
Http::fake([
'*/botTESTTOKEN/sendMessage' => Http::response(['ok' => false, 'error_code' => 403, 'description' => 'Forbidden: bot is not a member of the channel chat'], 403),
]);
expect(fn () => $this->publisher->publish($this->postPlatform))->toThrow(TelegramPublishException::class);
});
test('telegram publisher builds a private-channel url when there is no username', function () {
$this->socialAccount->update(['username' => null, 'meta' => ['chat_id' => '-1009876543210', 'type' => 'channel']]);
Http::fake([
'*/botTESTTOKEN/sendMessage' => Http::response(telegramOk(['message_id' => 99]), 200),
]);
$result = $this->publisher->publish($this->postPlatform);
expect($result['url'])->toBe('https://t.me/c/9876543210/99');
});
test('telegram publisher keeps links intact', function () {
config()->set('trypost.platforms.x.defuse_links', true);
$this->post->update(['content' => 'New post: https://acme.com/blog']);
Http::fake(['*/botTESTTOKEN/sendMessage' => Http::response(telegramOk(['message_id' => 42]), 200)]);
$this->publisher->publish($this->postPlatform);
Http::assertSent(fn ($request) => str_contains($request->url(), '/sendMessage')
&& $request['text'] === 'New post: https://acme.com/blog');
});