Skip to content

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.

Make AspectRatio the single source for crop math; fill test gaps

Paulo Castellanoefe49c34de06
diff --git a/app/Enums/PostPlatform/AspectRatio.php b/app/Enums/PostPlatform/AspectRatio.php
index 52365b4c5..89a34e768 100644
--- a/app/Enums/PostPlatform/AspectRatio.php
+++ b/app/Enums/PostPlatform/AspectRatio.php
@@ -10,4 +10,18 @@ enum AspectRatio: string
case Portrait = '4:5';
case Landscape = '16:9';
case Original = 'original';
+
+ /**
+ * Width-to-height ratio used to center-crop an image. `Original` means "no
+ * crop", so it resolves to a square (1.0) only as a safe fallback — callers
+ * should bypass cropping entirely for `Original`.
+ */
+ public function toFloat(): float
+ {
+ return match ($this) {
+ self::Portrait => 4 / 5,
+ self::Landscape => 16 / 9,
+ self::Square, self::Original => 1.0,
+ };
+ }
}
diff --git a/app/Http/Controllers/Api/PostController.php b/app/Http/Controllers/Api/PostController.php
index fefa3d0e8..30be5e480 100644
--- a/app/Http/Controllers/Api/PostController.php
+++ b/app/Http/Controllers/Api/PostController.php
@@ -75,7 +75,10 @@ class PostController extends Controller
);
}
- return new PostResource(data_get($result, 'post'));
+ $updated = data_get($result, 'post');
+ $updated->load(['postPlatforms.socialAccount']);
+
+ return new PostResource($updated);
}
public function destroy(Request $request, Post $post): JsonResponse
diff --git a/app/Services/Social/Concerns/CropsImageForAspectRatio.php b/app/Services/Social/Concerns/CropsImageForAspectRatio.php
index f807e62a2..ae4cefdb2 100644
--- a/app/Services/Social/Concerns/CropsImageForAspectRatio.php
+++ b/app/Services/Social/Concerns/CropsImageForAspectRatio.php
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Services\Social\Concerns;
+use App\Enums\PostPlatform\AspectRatio;
use App\Exceptions\Social\SocialPublishException;
use App\Services\Media\MediaOptimizer;
use Illuminate\Support\Facades\Http;
@@ -51,11 +52,7 @@ trait CropsImageForAspectRatio
protected function aspectRatioToFloat(string $ratio): float
{
- return match ($ratio) {
- '4:5' => 4 / 5,
- '16:9' => 16 / 9,
- default => 1.0,
- };
+ return AspectRatio::tryFrom($ratio)?->toFloat() ?? 1.0;
}
/**
diff --git a/tests/Feature/Api/PostApiTest.php b/tests/Feature/Api/PostApiTest.php
index eee56afda..5163af934 100644
--- a/tests/Feature/Api/PostApiTest.php
+++ b/tests/Feature/Api/PostApiTest.php
@@ -624,3 +624,42 @@ it('rejects updating a post with an invalid aspect_ratio', function () {
])
->assertJsonValidationErrors(['platforms.0.meta.aspect_ratio']);
});
+
+it('accepts a valid aspect_ratio on update and persists it', function () {
+ $post = Post::factory()->create([
+ 'workspace_id' => $this->workspace->id,
+ 'user_id' => $this->user->id,
+ 'status' => PostStatus::Draft,
+ ]);
+ $postPlatform = PostPlatform::factory()->linkedin()->create([
+ 'post_id' => $post->id,
+ 'social_account_id' => $this->socialAccount->id,
+ 'enabled' => true,
+ ]);
+
+ $this->withHeaders(['Authorization' => 'Bearer '.$this->plainToken])
+ ->putJson(route('api.posts.update', $post), [
+ 'status' => 'draft',
+ 'platforms' => [
+ ['id' => $postPlatform->id, 'content_type' => 'linkedin_post', 'meta' => ['aspect_ratio' => '16:9']],
+ ],
+ ])
+ ->assertOk()
+ ->assertJsonPath('platforms.0.meta.aspect_ratio', '16:9');
+
+ expect($postPlatform->fresh()->meta['aspect_ratio'])->toBe('16:9');
+});
+
+it('accepts the original aspect_ratio (no crop) on create', function () {
+ $this->withHeaders(['Authorization' => 'Bearer '.$this->plainToken])
+ ->postJson(route('api.posts.store'), [
+ 'platforms' => [
+ ['social_account_id' => $this->socialAccount->id, 'content_type' => 'linkedin_post', 'meta' => ['aspect_ratio' => 'original']],
+ ],
+ ])
+ ->assertCreated();
+
+ $platform = Post::where('workspace_id', $this->workspace->id)->first()
+ ->postPlatforms()->where('social_account_id', $this->socialAccount->id)->first();
+ expect($platform->meta['aspect_ratio'])->toBe('original');
+});
diff --git a/tests/Feature/Mcp/PostToolTest.php b/tests/Feature/Mcp/PostToolTest.php
index c234460df..dfd631b49 100644
--- a/tests/Feature/Mcp/PostToolTest.php
+++ b/tests/Feature/Mcp/PostToolTest.php
@@ -323,3 +323,36 @@ test('update post rejects an invalid aspect_ratio', function () {
$response->assertHasErrors();
});
+
+test('create post returns the platform meta in the response (read-back)', function () {
+ TryPostServer::actingAs($this->user)
+ ->tool(CreatePostTool::class, [
+ 'platforms' => [
+ ['social_account_id' => $this->socialAccount->id, 'content_type' => 'linkedin_post', 'meta' => ['aspect_ratio' => '4:5']],
+ ],
+ ])
+ ->assertOk()
+ ->assertStructuredContent(fn (AssertableJson $json) => $json->where('platforms.0.meta.aspect_ratio', '4:5')->etc());
+});
+
+test('update post accepts a valid aspect_ratio and persists it', function () {
+ $post = Post::factory()->create([
+ 'workspace_id' => $this->workspace->id,
+ 'user_id' => $this->user->id,
+ ]);
+ $platform = PostPlatform::factory()->create([
+ 'post_id' => $post->id,
+ 'social_account_id' => $this->socialAccount->id,
+ ]);
+
+ TryPostServer::actingAs($this->user)
+ ->tool(UpdatePostTool::class, [
+ 'post_id' => $post->id,
+ 'platforms' => [
+ ['id' => $platform->id, 'meta' => ['aspect_ratio' => '16:9']],
+ ],
+ ])
+ ->assertOk();
+
+ expect($platform->fresh()->meta['aspect_ratio'])->toBe('16:9');
+});
diff --git a/tests/Unit/Enums/AspectRatioTest.php b/tests/Unit/Enums/AspectRatioTest.php
new file mode 100644
index 000000000..8adebf0ae
--- /dev/null
+++ b/tests/Unit/Enums/AspectRatioTest.php
@@ -0,0 +1,19 @@
+<?php
+
+declare(strict_types=1);
+
+use App\Enums\PostPlatform\AspectRatio;
+
+test('aspect ratio has the expected cases and values', function () {
+ expect(array_column(AspectRatio::cases(), 'value'))
+ ->toEqualCanonicalizing(['1:1', '4:5', '16:9', 'original']);
+});
+
+test('aspect ratio maps to the correct crop float', function (string $value, float $expected) {
+ expect(AspectRatio::from($value)->toFloat())->toBe($expected);
+})->with([
+ '1:1' => ['1:1', 1.0],
+ '4:5' => ['4:5', 4 / 5],
+ '16:9' => ['16:9', 16 / 9],
+ 'original' => ['original', 1.0],
+]);