fixed tests

This commit is contained in:
2025-11-19 20:46:08 +01:00
parent 7b07e0cfae
commit 17c7a2e295
8 changed files with 291 additions and 16068 deletions

View File

@@ -276,6 +276,51 @@ async def remove_from_queue(
)
@router.delete("/", status_code=status.HTTP_204_NO_CONTENT)
async def remove_multiple_from_queue(
request: QueueOperationRequest,
_: dict = Depends(require_auth),
download_service: DownloadService = Depends(get_download_service),
):
"""Remove multiple items from the download queue.
Removes multiple download items from the queue based on provided IDs.
Items that are currently downloading will be cancelled.
Requires authentication.
Args:
request: Request containing list of item IDs to remove
Raises:
HTTPException: 401 if not authenticated, 404 if no items found,
500 on service error
"""
try:
removed_ids = await download_service.remove_from_queue(
request.item_ids
)
if not removed_ids:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="No matching items found in queue",
)
except DownloadServiceError as e:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=str(e),
)
except HTTPException:
raise
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to remove items from queue: {str(e)}",
)
@router.post("/start", status_code=status.HTTP_200_OK)
async def start_queue(
_: dict = Depends(require_auth),
@@ -423,8 +468,7 @@ async def reorder_queue(
500 on service error
"""
try:
# For now, this is a no-op that returns success
# A full implementation would reorder the pending queue
await download_service.reorder_queue(request.item_ids)
return {
"status": "success",
"message": f"Queue reordered with {len(request.item_ids)} items",

View File

@@ -54,9 +54,20 @@ class AnimeService:
args: DownloadStatusEventArgs from SeriesApp
"""
try:
# Check if there's a running event loop
try:
loop = asyncio.get_running_loop()
except RuntimeError:
# No running loop - log and skip
logger.debug(
"No running event loop for download status event",
status=args.status
)
return
# Map SeriesApp download events to progress service
if args.status == "started":
asyncio.create_task(
loop.create_task(
self._progress_service.start_progress(
progress_id=f"download_{args.serie_folder}_{args.season}_{args.episode}", # noqa: E501
progress_type=ProgressType.DOWNLOAD,
@@ -65,7 +76,7 @@ class AnimeService:
)
)
elif args.status == "progress":
asyncio.create_task(
loop.create_task(
self._progress_service.update_progress(
progress_id=f"download_{args.serie_folder}_{args.season}_{args.episode}", # noqa: E501
current=int(args.progress),
@@ -74,14 +85,14 @@ class AnimeService:
)
)
elif args.status == "completed":
asyncio.create_task(
loop.create_task(
self._progress_service.complete_progress(
progress_id=f"download_{args.serie_folder}_{args.season}_{args.episode}", # noqa: E501
message="Download completed",
)
)
elif args.status == "failed":
asyncio.create_task(
loop.create_task(
self._progress_service.fail_progress(
progress_id=f"download_{args.serie_folder}_{args.season}_{args.episode}", # noqa: E501
error_message=args.message or str(args.error),
@@ -101,10 +112,21 @@ class AnimeService:
"""
try:
scan_id = "library_scan"
# Check if there's a running event loop
try:
loop = asyncio.get_running_loop()
except RuntimeError:
# No running loop - log and skip
logger.debug(
"No running event loop for scan status event",
status=args.status
)
return
# Map SeriesApp scan events to progress service
if args.status == "started":
asyncio.create_task(
loop.create_task(
self._progress_service.start_progress(
progress_id=scan_id,
progress_type=ProgressType.SCAN,
@@ -113,7 +135,7 @@ class AnimeService:
)
)
elif args.status == "progress":
asyncio.create_task(
loop.create_task(
self._progress_service.update_progress(
progress_id=scan_id,
current=args.current,
@@ -122,21 +144,21 @@ class AnimeService:
)
)
elif args.status == "completed":
asyncio.create_task(
loop.create_task(
self._progress_service.complete_progress(
progress_id=scan_id,
message=args.message or "Scan completed",
)
)
elif args.status == "failed":
asyncio.create_task(
loop.create_task(
self._progress_service.fail_progress(
progress_id=scan_id,
error_message=args.message or str(args.error),
)
)
elif args.status == "cancelled":
asyncio.create_task(
loop.create_task(
self._progress_service.fail_progress(
progress_id=scan_id,
error_message=args.message or "Scan cancelled",

View File

@@ -360,6 +360,59 @@ class DownloadService:
f"Failed to remove items: {str(e)}"
) from e
async def reorder_queue(self, item_ids: List[str]) -> None:
"""Reorder pending queue items.
Args:
item_ids: List of item IDs in desired order.
Items not in this list remain at end of queue.
Raises:
DownloadServiceError: If reordering fails
"""
try:
# Build new queue based on specified order
new_queue = deque()
remaining_items = list(self._pending_queue)
# Add items in specified order
for item_id in item_ids:
if item_id in self._pending_items_by_id:
item = self._pending_items_by_id[item_id]
new_queue.append(item)
remaining_items.remove(item)
# Add remaining items that weren't in the reorder list
for item in remaining_items:
new_queue.append(item)
# Replace queue
self._pending_queue = new_queue
# Save updated queue
self._save_queue()
# Notify via progress service
queue_status = await self.get_queue_status()
await self._progress_service.update_progress(
progress_id="download_queue",
message=f"Queue reordered with {len(item_ids)} items",
metadata={
"action": "queue_reordered",
"reordered_count": len(item_ids),
"queue_status": queue_status.model_dump(mode="json"),
},
force_broadcast=True,
)
logger.info("Queue reordered", reordered_count=len(item_ids))
except Exception as e:
logger.error("Failed to reorder queue", error=str(e))
raise DownloadServiceError(
f"Failed to reorder queue: {str(e)}"
) from e
async def start_queue_processing(self) -> Optional[str]:
"""Start automatic queue processing of all pending downloads.