#!/usr/bin/env bash
# List test files without corresponding plugins.
# Usage: orphan-tests

set -euo pipefail
source "$(dirname "${BASH_SOURCE[0]}")/lib/common.sh"

cd "$PROJECT_ROOT"

# Cross-cutting test suites that intentionally have no matching plugin.
ALLOWED_ORPHANS=(
    "test_helper.rb"
    "test_render_mode_coverage.rb"
    "test_link_cache_structure.rb"
    "test_architecture.rb"
    "test_markdown_output_includes.rb"
    "test_json_ld_rendered_site.rb"
    "test_dark_mode_contrast.rb"
    "test_skill_docs_paths.rb"
    "test_skill_docs_yard_objects.rb"
    "test_warning_filter.rb"
)

is_allowed_orphan() {
    local name="$1"
    for allowed in "${ALLOWED_ORPHANS[@]}"; do
        [[ "$name" == "$allowed" ]] && return 0
    done
    return 1
}

orphan_count=0

while IFS= read -r test_file; do
    basename="$(basename "$test_file")"
    is_allowed_orphan "$basename" && continue

    if ! find_plugin_for_test "$test_file" > /dev/null; then
        echo "$test_file"
        orphan_count=$((orphan_count + 1))
    fi
done < <(find "$TESTS_DIR" -name "*.rb" | LC_ALL=C sort)

echo ""
echo "---"
if [[ $orphan_count -eq 0 ]]; then
    echo "No orphan tests found."
else
    echo "Total: $orphan_count orphan tests"
fi
