#!/usr/bin/env bash
# Copyright 2026 - Ronald Portier (Open Eye Development)
# SPDX-License-Identifier: AGPL-3.0-or-later  (https://www.gnu.org/licenses/agpl.html)
#
# Run tests for one or more Odoo modules.
# Installs the module(s) with --test-enable, then exits.
# Does NOT stop or start the main service — runs in a separate process.
#
# Usage: bin/test <module ...>
#   bin/test sale              Test the 'sale' module
#   bin/test sale_management    Test both modules
#
# Run as the instance user.

set -euo pipefail

BUILD_DIR="$(cd "$(dirname "$0")/.." && pwd)"
INSTANCE="$(basename "$(dirname "$BUILD_DIR")")"
PYTHON="${BUILD_DIR}/.venv/bin/python"
ODOO_BIN="${BUILD_DIR}/parts/odoo/odoo-bin"
CONFIG="${BUILD_DIR}/etc/odoo.conf"
DB="${INSTANCE}"

info()  { echo "[INFO]  $*"; }
error() { echo "[ERROR] $*" >&2; exit 1; }

modules="${*:-}"
[[ -n "$modules" ]] || { echo "Usage: bin/test <module ...>" >&2; exit 1; }

info "Testing module(s): ${modules}"
"${PYTHON}" "${ODOO_BIN}" \
    --config "${CONFIG}" \
    -d "$DB" \
    -i "$modules" \
    --test-enable \
    --log-level=test \
    --stop-after-init
info "Test run complete."
