<?php

use App\Models\GuidePage;

it('normalizes uploaded guide image urls for rendering', function () {
    $page = new GuidePage([
        'content' => <<<'HTML'
<figure data-trix-attachment='{"url":"http://localhost:8000/storage/guide-pages/example.png?signature=abc"}'>
    <img src="http://localhost:8000/storage/guide-pages/example.png?signature=abc">
</figure>
HTML,
    ]);

    expect($page->normalized_content)
        ->toContain('/storage/guide-pages/example.png?signature=abc')
        ->not->toContain('http://localhost:8000/storage/guide-pages/example.png?signature=abc');
});

it('adds a leading slash to relative guide image urls', function () {
    $page = new GuidePage([
        'content' => '<img src="storage/guide-pages/example.png">',
    ]);

    expect($page->normalized_content)
        ->toBe('<img src="/storage/guide-pages/example.png">');
});

it('keeps external image urls unchanged', function () {
    $page = new GuidePage([
        'content' => '<img src="https://example.com/images/example.png">',
    ]);

    expect($page->normalized_content)
        ->toBe('<img src="https://example.com/images/example.png">');
});

it('builds root relative upload urls for new attachments', function () {
    expect(GuidePage::uploadedAttachmentUrl('guide-pages/example.png'))
        ->toBe('/storage/guide-pages/example.png');
});
