#!/bin/sh
# Run the upstream unit tests, one process per module: the Qt application
# state does not survive many modules in one interpreter.
# Usage: run-unit-tests <python interpreter>   (from the openshot_qt directory)
# Used by the build and by the unit-tests autopkgtest.
set -u
PYTHON=${1:-python3}

# These start the whole application and then hang on exit without a sound
# device, depend on mocking that does not work with PyQt6, or on keyboard
# focus in an offscreen window (timing dependent). The last two need the
# binary "Legacy Pickle Test Pack.zip", not part of the CVE-2026-134704 backport.
# PortalBusTests start private dbus-daemons with 5 s timeouts and only run when
# the optional python3-dbus-next is installed: keep the result independent of it.
DESELECT="
tests/test_auto_keyframes.py::AutoKeyframeWorkflowTests::test_split_creation_slicing_manual_insertion_and_undo
tests/test_auto_optimize_startup.py::AutoOptimizeStartupTests::test_menu_banner_and_project_switch_in_real_window
tests/test_direct_text.py::DirectTextTests::test_full_app_text_workflow
tests/test_direct_text_properties.py::DirectTextPropertiesTests::test_real_properties_dock_and_undo
tests/test_direct_text_properties.py::DirectTextPropertiesTests::test_feedback_fixes_in_real_app
tests/test_dock_focus.py::DockFocusTests::test_focused_tab_title_matches_active_dock_underline
tests/test_notification_startup.py::NotificationStartupTests::test_help_shortcuts_and_three_themes
tests/test_project_data.py::ProjectDataTests::test_manual_legacy_pack_loads_with_window_and_reopens
tests/test_clipboard_timeline.py::TimelineClipboardTests::test_imported_legacy_clip_paste_keeps_existing_track
tests/test_portal_file_dialog.py::PortalBusTests
"
args=""
for d in $DESELECT; do args="$args --deselect $d"; done

failed=""
for t in tests/test_*.py; do
	echo "=== $t"
	# shellcheck disable=SC2086
	"$PYTHON" -m pytest -q -p no:cacheprovider --rootdir=. $args "$t"
	rc=$?
	# 5: every test in the module was deselected
	[ $rc -eq 0 ] || [ $rc -eq 5 ] || failed="$failed $t"
done
# A script, not a test module: checks the compiled translations.
echo "=== language/test_translations.py"
"$PYTHON" language/test_translations.py || failed="$failed language/test_translations.py"

if [ -n "$failed" ]; then
	echo "FAILED:$failed"
	exit 1
fi
