import os
import uuid
import pytest
from fastapi.testclient import TestClient
from unittest.mock import patch
# Add the project root to the Python path
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
from app.main import app
from app.models import Job, JobStatus
# --- Test Setup ---
client = TestClient(app)
# Re-use the assets and output dir from the other test file
ASSETS_DIR = os.path.join(os.path.dirname(__file__), "assets")
TEST_OUTPUT_DIR = os.path.join(os.path.dirname(__file__), "test_outputs")
VALID_FILE = os.path.join(ASSETS_DIR, "cube.obj")
# --- Test Cases ---
@patch('app.main.load_job_metadata')
def test_get_job_output_for_viewing_success(mock_load_job):
"""
Tests the successful retrieval of parsed DXF data for the viewer.
"""
# --- Setup ---
job_id = uuid.uuid4()
# In a real scenario, the file would be created by the worker.
# For this test, we create it manually from a known good source.
from app.features.dxf_layered_curves.processing import process as create_layered_curves_dxf
output_dxf_path = os.path.join(TEST_OUTPUT_DIR, f"{job_id}_test.dxf")
# Run the generator to create the file
import asyncio
job = Job(
id=job_id,
feature_id="dxf_layered_curves",
filename="cube.obj",
status=JobStatus.QUEUED,
input_path=VALID_FILE,
output_path=output_dxf_path,
params={"num_layers": 5, "num_points_per_layer": 10},
)
async def run_generator():
async for _ in create_layered_curves_dxf(job):
pass
asyncio.run(run_generator())
# Mock the job that the endpoint will load
mock_job = Job(
id=job_id,
feature_id="dxf_layered_curves",
filename="cube.obj",
status=JobStatus.COMPLETE,
input_path="dummy",
output_path=output_dxf_path # Point to the real DXF file
)
mock_load_job.return_value = mock_job
# --- Execution ---
response = client.get(f"/api/jobs/{job_id}/view")
# --- Assertions ---
assert response.status_code == 200
data = response.json()
assert "polylines" in data
assert isinstance(data["polylines"], list)
assert len(data["polylines"]) > 0 # Should have found the layers
assert isinstance(data["polylines"][0][0], list) # Check for list of points
assert len(data["polylines"][0][0]) == 3 # Check for [x, y, z] coordinates
@patch('app.main.load_job_metadata')
def test_get_job_output_for_viewing_no_file(mock_load_job):
"""
Tests the case where the job exists but its output file is missing.
"""
job_id = uuid.uuid4()
mock_job = Job(
id=job_id,
feature_id="dxf_layered_curves",
filename="test.obj",
status=JobStatus.COMPLETE,
input_path="dummy",
output_path="/path/to/non_existent_file.dxf" # This file doesn't exist
)
mock_load_job.return_value = mock_job
response = client.get(f"/api/jobs/{job_id}/view")
assert response.status_code == 404
assert "Output file not found" in response.json()["detail"]