File size: 4,276 Bytes
1501522
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php

namespace Tests\Feature;

use App\Services\FreepikAssetRemixService;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class FreepikAssetRemixServiceTest extends TestCase
{
    public function test_remix_image_returns_generated_urls_from_freepik_reference_workflow(): void
    {
        $diskRoot = sys_get_temp_dir().'/dev-local-public-remix-'.uniqid();
        if (! is_dir($diskRoot)) {
            mkdir($diskRoot, 0777, true);
        }

        config([
            'app.url' => 'https://dev.local',
            'services.freepik.api_key' => 'test-freepik-key',
            'services.freepik.base_url' => 'https://api.freepik.com/v1/ai/text-to-image',
            'filesystems.disks.public.root' => $diskRoot,
            'filesystems.disks.public.url' => '/storage',
        ]);

        Storage::disk('public')->put('social_assets/images/source.jpg', 'source-image-binary');

        Http::fake([
            'https://api.freepik.com/v1/ai/text-to-image/seedream-v4-5-edit' => Http::response([
                'data' => [
                    'task_id' => 'img-task-1',
                    'status' => 'CREATED',
                ],
            ], 200),
            'https://api.freepik.com/v1/ai/text-to-image/seedream-v4-5-edit/img-task-1' => Http::response([
                'data' => [
                    'status' => 'COMPLETED',
                    'generated' => ['https://cdn.example.com/remix-image.jpg'],
                ],
            ], 200),
        ]);

        $service = app(FreepikAssetRemixService::class);
        $result = $service->remixImage([
            'type' => 'image',
            'url' => '/storage/social_assets/images/source.jpg',
        ], 'Ubah jadi visual promosi premium', 'restyle');

        $this->assertSame('freepik-seedream-v45-edit', $result['provider']);
        $this->assertSame(['https://cdn.example.com/remix-image.jpg'], $result['urls']);
    }

    public function test_remix_video_persists_generated_mp4_inside_public_storage(): void
    {
        $diskRoot = sys_get_temp_dir().'/dev-local-public-remix-video-'.uniqid();
        if (! is_dir($diskRoot)) {
            mkdir($diskRoot, 0777, true);
        }

        config([
            'app.url' => 'https://dev.local',
            'services.freepik.api_key' => 'test-freepik-key',
            'services.freepik.base_url' => 'https://api.freepik.com/v1/ai/text-to-image',
            'filesystems.disks.public.root' => $diskRoot,
            'filesystems.disks.public.url' => '/storage',
        ]);

        Storage::disk('public')->put('social_assets/images/ref.jpg', 'reference-image-binary');

        Http::fake([
            'https://api.freepik.com/v1/ai/image-to-video/runway-4-5' => Http::response([
                'data' => [
                    'task_id' => 'vid-task-1',
                    'status' => 'CREATED',
                ],
            ], 200),
            'https://api.freepik.com/v1/ai/image-to-video/runway-4-5/vid-task-1' => Http::response([
                'data' => [
                    'status' => 'COMPLETED',
                    'generated' => ['https://cdn.example.com/remix-video.mp4'],
                ],
            ], 200),
            'https://cdn.example.com/remix-video.mp4' => Http::response('fake-video-binary', 200, [
                'Content-Type' => 'video/mp4',
            ]),
        ]);

        $service = app(FreepikAssetRemixService::class);
        $result = $service->remixVideo([
            'type' => 'image',
            'url' => '/storage/social_assets/images/ref.jpg',
        ], 'Animasikan jadi story vertikal', 'mockup');

        Http::assertSent(function ($request) {
            if ($request->url() !== 'https://api.freepik.com/v1/ai/image-to-video/runway-4-5') {
                return false;
            }

            $image = $request['image'] ?? null;

            return is_string($image) && str_starts_with($image, 'data:image/');
        });

        $relativePath = ltrim(str_replace('/storage/', '', $result['url']), '/');

        $this->assertSame('freepik-runway-i2v', $result['provider']);
        $this->assertTrue(Storage::disk('public')->exists($relativePath));
        $this->assertSame(5, $result['duration_seconds']);
    }
}