import ast
from collections.abc import Generator

import pytest

from aiservice.background import _background_tasks


@pytest.fixture(autouse=True)
def _drain_background_tasks() -> Generator[None, None, None]:
    yield
    for task in list(_background_tasks):
        task.cancel()
    _background_tasks.clear()


def normalize_code(code: str | None) -> str | None:
    """Normalize code by parsing and unparsing to ignore quote style differences."""
    if code is None:
        return None
    return ast.unparse(ast.parse(code))
