#!/bin/bash

# Run the upstream test suite against the installed python3-logfire and
# python3-logfire-api packages.

set -e -u

# Keep in sync with LOGFIRE_INTEGRATION_TESTS and LOGFIRE_TEST_ARGS in
# debian/rules.
LOGFIRE_INTEGRATION_TESTS="aiohttp_client aiohttp_server anthropic anthropic_bedrock asgi aws_lambda claude_sdk django dspy fastapi flask httpx openai pydantic_ai_mcp pytest_plugin requests system_metrics wsgi"

SRC="${AUTOPKGTEST_TMP}/src"
mkdir -p "${SRC}" "${AUTOPKGTEST_TMP}/home"

# The tests read these relative to the source root. logfire-api/ is not an
# importable path, so copying it does not shadow the installed logfire_api.
cp -a tests docs scripts README.md pyproject.toml logfire-api "${SRC}"

# The tests also read files from the logfire package dir (docstring examples
# in test_docs.py, .agents/skills in test_agent_setup_prompt.py). Mirror the
# installed package with "cp -as" (real directories, symlinked files) instead
# of copying the sources, so the tests still import the installed code. The
# agent skill files are installed as documentation, so mirror them back to
# where the tests expect them. Symlinked directories would not work: since
# Python 3.13, the "**" globs the tests use do not descend into them.
cp -as /usr/lib/python3/dist-packages/logfire "${SRC}/logfire"
mkdir -p "${SRC}/logfire/.agents"
cp -as /usr/share/doc/python3-logfire/agent-skills "${SRC}/logfire/.agents/skills"

PYTEST_ARGS=(tests)
for TEST_FILE in tests/otel_integrations/test_*.py; do
    NAME=${TEST_FILE#tests/otel_integrations/test_}
    NAME=${NAME%.py}
    case " ${LOGFIRE_INTEGRATION_TESTS} " in
        *" ${NAME} "*) ;;
        *) PYTEST_ARGS+=("--ignore=${TEST_FILE}") ;;
    esac
done
PYTEST_ARGS+=(--ignore=tests/test_variable_composition.py)

export HOME="${AUTOPKGTEST_TMP}/home"
export DJANGO_SETTINGS_MODULE=tests.otel_integrations.django_test_project.django_test_site.settings

for py3vers in $(py3versions -s); do
    echo
    echo "***************************"
    echo "*** Testing with ${py3vers}"
    echo "***************************"
    echo
    cd "${SRC}" && \
        "${py3vers}" -m pytest -p no:cacheprovider "${PYTEST_ARGS[@]}" || exit 1
done

echo

exit 0
