<?php

use App\Services\EmailImageInliner;
use Illuminate\Support\Facades\File;

it('inlines public storage images to base64 data uris', function () {
    $dir = storage_path('app/public/email-images');
    if (! is_dir($dir)) {
        mkdir($dir, 0777, true);
    }
    $path = $dir.'/test-inline.png';
    // a tiny 1x1 PNG
    $png = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO8m4y8AAAAASUVORK5CYII=');
    File::put($path, $png);

    $html = '<p><img src="/storage/email-images/test-inline.png" alt="x"></p>';
    $service = new EmailImageInliner;
    $out = $service->inline($html);

    expect($out)->toContain('src="data:image/png;base64,');
});

it('inlines absolute URLs that point to /storage paths', function () {
    $dir = storage_path('app/public/email-images');
    if (! is_dir($dir)) {
        mkdir($dir, 0777, true);
    }
    $path = $dir.'/test-abs.png';
    // a tiny 1x1 PNG
    $png = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO8m4y8AAAAASUVORK5CYII=');
    File::put($path, $png);

    $html = '<p><img src="http://localhost/storage/email-images/test-abs.png" alt="x"></p>';
    $service = new EmailImageInliner;
    $out = $service->inline($html);

    expect($out)->toContain('src="data:image/png;base64,');
});
