#!/bin/sh
set -e

cd "${AUTOPKGTEST_BUILDTREE:-$PWD}"
mkdir -p build

export HOME=$PWD/build
export MNE_DONTWRITE_HOME=true
export MNE_SKIP_SAMPLE_DATASET_TESTS=true
export MNE_FORCE_SERIAL=true
export MNE_SKIP_NETWORK_TESTS=1
# to avoid cannot set toolkit to qt4 because it has already been set to wx
export ETS_TOOLKIT=qt4

PYVER=$(python3 -c 'import sys; v=sys.version_info; print(f"{v.major}.{v.minor}")')
export PYTHONPATH="$PWD:$PWD/.pybuild/cpython3_${PYVER}_mne/build"

# --- Architecture-specific test adjustments ---
arch="$(dpkg-architecture -qDEB_HOST_ARCH 2>/dev/null || uname -m)"
KEXTRA=""
DESELECT=""

case "$arch" in
  ppc64el|ppc64|riscv64)
    # test_spectrum_complex[welch-False]: a Welch PSD bin can underflow to exactly
    # 0.0 on ppc64el (denormal/flush-to-zero), producing a "Zero value in spectrum"
    # UserWarning which MNE/pytest (filterwarnings="error:") turns into a FAIL.
    # Only this one node is deselected; the other welch/multitaper variants run.
    DESELECT="--deselect=mne/time_frequency/tests/test_spectrum.py::test_spectrum_complex[welch-False]"
    ;;
  armhf|i386)
    # 32-bit arches (C long == int32):
    # - test_peak_finder expects int64 indices, but they come back as int32
    # - test_split_saving_and_loading_back writes fewer part files than predicted
    #   (the last expected file is always missing)
    # - test_decimate_surface_vtk[2..4]: Debian armhf VTK has vtkIdType==int32,
    #   but MNE casts triangles to np.int64 -> ValueError. [0]/[1] don't use VTK,
    #   so they stay enabled via the --deselect list (brackets are invalid in -k).
    KEXTRA=" and not test_peak_finder and not test_split_saving_and_loading_back"
    DESELECT="--deselect=mne/tests/test_surface.py::test_decimate_surface_vtk[2] \
--deselect=mne/tests/test_surface.py::test_decimate_surface_vtk[3] \
--deselect=mne/tests/test_surface.py::test_decimate_surface_vtk[4]"
    ;;
esac

xvfb-run \
    --auto-servernum \
    --server-num=20 \
    -s "-screen 0 1024x768x24 -ac +extension GLX +render -noreset" \
    python3 -m pytest mne -k "not test_parallel_func_n_jobs_none and not test_qt_block_sigint${KEXTRA}" $DESELECT
