trypost/tests/Feature/Image/PostImagePipelineTest.php
Paulo Castellano 4efaa0bf99 Harden automations module: full-post generation, reliable runs, editor UX
Generation
- Generate node now produces the full post (text + AI image + carousel)
  via a shared PostImagePipeline extracted from StreamPostCreation
- Generate config UI mirrors the /posts/create wizard (carousel slide
  count, include-image toggle); drop the decorative format/unsplash keys

Flow correctness
- RSS/HTTP nodes expose named has-items (default) and no-items output
  handles, labeled and colored like the Condition node
- AdvanceAutomationRun records a no_matching_edge terminal instead of
  completing silently; "0 new items" feedback in the test panel
- Manual/test runs no longer persist the production dedup watermark

Run reliability
- Pause truly halts in-flight runs (production only; manual test runs
  always run regardless of automation status)
- ProcessAutomationNode::failed() marks the run failed
- automation:recover-stuck-runs and automation:prune-dry-runs commands

Webhook / HTTP
- Branded User-Agent (config-driven) on outbound webhook + http_request
- Webhook fails on invalid JSON instead of silently sending {}
- HTTP custom headers editor; CodeMirror-based CodeEditor for JSON

Editor UX
- Header Test button only opens the panel; the panel has a Run button
  (saves first) and owns the with-real-data toggle
- Clicking a node closes the test panel and opens its config
- Node cards: max-width + truncate so long URLs don't grow the node
2026-06-10 20:45:01 -03:00

107 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
use App\Enums\Media\Source;
use App\Enums\PostPlatform\ContentType;
use App\Models\SocialAccount;
use App\Models\Workspace;
use App\Services\Image\PostImagePipeline;
use App\Services\Image\TemplateImageGenerator;
use Illuminate\Support\Facades\Storage;
beforeEach(function () {
Storage::fake();
$this->workspace = Workspace::factory()->create();
$this->account = SocialAccount::factory()->instagram()->create([
'workspace_id' => $this->workspace->id,
]);
Storage::put('ai-images/x.webp', 'fake-image-bytes');
$this->rendered = [
'path' => 'ai-images/x.webp',
'source_meta' => [
'keywords' => ['productivity'],
'style' => 'cinematic',
'language' => 'en',
],
];
});
test('forSingle returns a media item with the expected shape and persists a Media row', function () {
$this->mock(TemplateImageGenerator::class, function ($mock) {
$mock->shouldReceive('render')->once()->andReturn($this->rendered);
});
$structured = [
'content' => 'A single productivity tip',
'image_title' => 'Tip',
'image_body' => 'Do less',
'image_keywords' => ['productivity'],
];
$pipeline = app(PostImagePipeline::class);
$media = $pipeline->forSingle($this->workspace, $this->account, $structured, ContentType::InstagramFeed);
expect($media)->toHaveCount(1);
$item = $media[0];
expect($item)->toHaveKeys(['id', 'path', 'url', 'type', 'mime_type', 'source', 'source_meta']);
expect($item['path'])->toBe('ai-images/x.webp');
expect($item['type'])->toBe('image');
expect($item['mime_type'])->toBe('image/webp');
expect($item['source'])->toBe(Source::Ai->value);
expect($item['source_meta'])->toBe($this->rendered['source_meta']);
$this->assertDatabaseHas('medias', [
'id' => $item['id'],
'path' => 'ai-images/x.webp',
'collection' => 'ai-generated',
]);
});
test('forSingle returns an empty array when the generator renders nothing', function () {
$this->mock(TemplateImageGenerator::class, function ($mock) {
$mock->shouldReceive('render')->once()->andReturn(null);
});
$structured = [
'image_title' => 'Tip',
'image_body' => 'Do less',
'image_keywords' => [],
];
$pipeline = app(PostImagePipeline::class);
$media = $pipeline->forSingle($this->workspace, $this->account, $structured, ContentType::InstagramFeed);
expect($media)->toBe([]);
});
test('forCarousel returns one media item per slide', function () {
$this->mock(TemplateImageGenerator::class, function ($mock) {
$mock->shouldReceive('render')->times(3)->andReturn($this->rendered);
});
$structured = [
'caption' => 'Swipe',
'slides' => [
['title' => 'Tip 1', 'body' => 'First', 'image_keywords' => ['a']],
['title' => 'Tip 2', 'body' => 'Second', 'image_keywords' => ['b']],
['title' => 'Tip 3', 'body' => 'Third', 'image_keywords' => ['c']],
],
];
$pipeline = app(PostImagePipeline::class);
$media = $pipeline->forCarousel($this->workspace, $this->account, $structured, ContentType::InstagramFeed);
expect($media)->toHaveCount(3);
expect($media[0])->toHaveKeys(['id', 'path', 'url', 'type', 'mime_type', 'source', 'source_meta']);
$this->assertDatabaseCount('medias', 3);
});