# SPDX-License-Identifier: AGPL-3.0-or-later
"""Shared pytest collection helper for meta-tests.

Runs pytest in collect-only mode and returns the collected node ids, so a
meta-test can assert that a registry's mapped tests still exist.
"""
from __future__ import annotations

import os
import subprocess
import sys

PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


def collected_node_ids() -> set[str]:
    result = subprocess.run(
        [sys.executable, "-m", "pytest", "--collect-only", "-q", "-p", "no:cacheprovider"],
        cwd=PROJECT_ROOT,
        capture_output=True,
        text=True,
        check=False,
    )
    assert result.returncode == 0, (
        f"pytest --collect-only failed:\n{result.stdout}\n{result.stderr}"
    )
    return {
        line.strip()
        for line in result.stdout.splitlines()
        if line.startswith("tests/") and "::" in line
    }
