#!/bin/bash
set -e

log-entrypoint "Install python packages required for testing."
uv pip install --no-cache-dir coverage \
  websocket-client

log-entrypoint "Collect external python dependencies from modules in $TEST_ADDONS_DIR."
if [ -n "$TEST_INCLUDE" ]; then
    manifestoo --select-include "$TEST_INCLUDE" --select-exclude "$TEST_EXCLUDE" list-external-dependencies python \
        > test-requirements.txt
else
    manifestoo --select-addons-dir "$TEST_ADDONS_DIR" --select-exclude "$TEST_EXCLUDE" list-external-dependencies python \
        > test-requirements.txt
fi

log-entrypoint "Install collected python dependencies."
uv pip install -r test-requirements.txt

if [ -n "$TEST_INCLUDE" ]; then
    TEST_DEPENDS_MODULES=$(manifestoo --select-include "$TEST_INCLUDE" --select-exclude "$TEST_EXCLUDE" list-depends --separator=,)
else
    TEST_DEPENDS_MODULES=$(manifestoo --select-addons-dir "$TEST_ADDONS_DIR" --select-exclude "$TEST_EXCLUDE" list-depends --separator=,)
fi

log-entrypoint "Install depends modules for testing: $TEST_DEPENDS_MODULES"
unbuffer /opt/odoo-venv/bin/odoo \
    -d "$ODOO_DATABASE" \
    -i "${TEST_DEPENDS_MODULES:-base}" \
    --load-language=en_US \
    --stop-after-init \
    --http-port=8068
