File size: 2,289 Bytes
a63cedf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98b0b74
 
 
 
 
 
 
 
 
 
a63cedf
98b0b74
a63cedf
 
 
98b0b74
a63cedf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import os
import pytest
from PIL import Image

@pytest.fixture(scope="function")
def dummy_image():
    """Creates a temporary dummy image for testing."""
    filename = "test_trigger.jpg"
    img = Image.new('RGB', (100, 100), color='blue')
    img.save(filename)
    yield filename
    if os.path.exists(filename):
        try:
            os.remove(filename)
        except:
            pass

def test_inference_starts_on_upload(page, dummy_image, test_server):
    """
    Verifies that after selecting a file, the /analyze endpoint is called.
    """
    page.goto(test_server)
    
    # Handle Medical Disclaimer
    print("Checking for Medical Disclaimer...")
    try:
        disclaimer_btn = page.locator("sl-button", has_text="I Understand & Agree").first
        disclaimer_btn.wait_for(state="visible", timeout=5000)
        disclaimer_btn.click()
        print("Disclaimer accepted.")
    except:
        print("Disclaimer not visible or already accepted, continuing...")
    
    # Clear session to ensure we are fresh
    clear_btn = page.locator("sl-button", has_text="Clear History").first
    if clear_btn.is_visible():
        page.once("dialog", lambda dialog: dialog.accept())
        clear_btn.click()
        page.locator("text=History Empty").wait_for(state="visible", timeout=5000)

    # We expect a POST to /api/photos/*/analyze
    # The app.js calls it after a 300ms timeout
    print("Uploading file and waiting for analyze request...")
    with page.expect_response("**/api/photos/*/analyze", timeout=60000) as response_info:
        page.set_input_files("input[type='file']", dummy_image)
        
    response = response_info.value
    print(f"Intercepted analyze request: {response.url}")
    assert response.request.method == "POST", "Expected a POST request for analysis"
    assert response.ok, f"Analyze request failed with status {response.status}"
    
    # Also verify UI shows it's analyzing
    photo_item = page.locator("[data-analyzed='false']").first
    # It might be very fast, but usually it stays 'Pending' or shows a spinner
    # If it's already done, it should have [data-analyzed='true']
    
    page.wait_for_selector("[data-analyzed='true']", timeout=300000)
    print("Inference completed successfully according to DOM state.")