# retoor <retoor@molodetz.nl>

import io
import re

from PIL import Image
from playwright.sync_api import expect

from tests.conftest import BASE_URL


def _png():
    buf = io.BytesIO()
    Image.new("RGB", (8, 8), (0, 128, 255)).save(buf, "PNG")
    return buf.getvalue()


def test_create_modal_collects_attachment_uid(alice):
    page, _ = alice
    page.goto(f"{BASE_URL}/issues", wait_until="domcontentloaded")
    page.click("button[data-modal='create-issue-modal']")
    page.locator("#create-issue-modal").wait_for(state="visible")
    file_input = page.locator(
        "#create-issue-modal dp-upload .dp-upload-input"
    ).first
    file_input.set_input_files(
        {"name": "shot.png", "mimeType": "image/png", "buffer": _png()}
    )
    page.locator(
        "#create-issue-modal dp-upload .dp-upload-count"
    ).first.wait_for(state="visible", timeout=15000)
    expect(
        page.locator(
            "#create-issue-modal dp-upload input[name='attachment_uids']"
        )
    ).to_have_value(re.compile(r".+"))
