Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

31.2 KB
Raw Blame
"""Guard the durable result of the FastAPI migration review.

The raw row-by-row working ledger was intentionally removed by ADR-0010. A
future ``origin/main...HEAD`` diff cannot reconstruct that branch boundary
after squash merge, so keeping the old git-backed ledger checks would create a
vacuous or unrelated result. These checks instead preserve what remains
meaningful: exact historical revisions and counts, concise status boundaries,
the absence of republished working ledgers, the empty-shelf ratchet, and the
committed regression modules that exercise current behavior.
"""

# allow: no-sut-import — this guardian checks documentation and test-tree
# invariants. The required regression modules import and exercise production
# code themselves.

import re
from pathlib import Path

from tests.migration_evidence_nodes import active_pytest_nodes


REPO_ROOT = Path(__file__).resolve().parents[1]
ADR_0008 = (
    REPO_ROOT / "docs/decisions/0008-reviewing-long-lived-migration-branches.md"
)
ADR_0010 = (
    REPO_ROOT
    / "docs/decisions/0010-test-coverage-provenance-across-the-fastapi-migration.md"
)
ADR_0011 = (
    REPO_ROOT
    / "docs/decisions/0011-source-provenance-across-the-fastapi-migration.md"
)
MIGRATION_DOCS = (
    ADR_0008,
    ADR_0010,
    ADR_0011,
    REPO_ROOT / "docs/migrations/README.md",
    REPO_ROOT / "docs/migrations/wsgi-to-asgi-playbook.md",
)

SNAPSHOT_SHA = "7c298956669c5b7cf112d194435eb55a9b782af2"
COMPARISON_SHA = "fa466ad13de57a1cb4a79493df59fe18acce5657"
SHELVED_PREPORT_SHA = "a00eb215ece89692876ef8ea8b33c0ea4308986b"
SHELVED_REPORT_SHA = "81314eea7a91714fcc4a870b9797dbe259925942"
EXPECTED_TEST_COUNTS = {
    "Raw `test_*` definitions on the comparison tree": 36_720,
    "Raw `test_*` definitions on the migration tree": 36_153,
    "Python files under `tests/` absent from the migration tree": 166,
    "Raw definition lines in those absent files": 4_486,
    "Distinct `(path, test-name)` removals across 43 surviving files": 125,
    "Distinct `(path, test-name)` additions across 43 surviving files": 120,
    "Raw definition lines in 277 added Python files under `tests/`": 3_924,
    "Reconciliation residual (not directly measured)": 32_109,
}
EXPECTED_SHELVING_COUNTS = {
    "Pre-port test definitions (AST and raw)": 76,
    "Pre-port module-level-shelved modules": 8,
    "Immediate re-port unskipped pytest cases": 92,
    "Immediate re-port deliberate pytest skips": 21,
}

# These are the exact eight files measured for the separate 76-definition
# pre-port shelf snapshot. The news-input module below was re-ported in a
# separate change and is guarded as current evidence, but it is not part of
# either shelving count.
SHELVED_PREPORT_MODULES = (
    "tests/security/test_auth_security.py",
    "tests/security/test_api_security.py",
    "tests/security/test_csrf_protection.py",
    "tests/security/test_cookie_security.py",
    "tests/security/test_pagination_bounds.py",
    "tests/chat/test_chat_socket_events.py",
    "tests/research_scheduler/test_scheduler_edge_cases.py",
    "tests/test_followup_api.py",
)
SEPARATELY_REPORTED_MODULE = "tests/news/test_news_input_validation.py"

REMOVED_LEDGER_DIRS = (
    REPO_ROOT / "docs/decisions/0010-test-coverage-provenance",
)
DEAD_LEDGER_MARKERS = (
    "docs/decisions/0010-test-coverage-provenance/",
    "docs/decisions/0010-test-coverage-provenance/OUTSTANDING-SECURITY-GAPS.md",
    "docs/decisions/0010-test-coverage-provenance/CODEQL-VERDICTS.md",
    "docs/decisions/0010-test-coverage-provenance/shared-files.md",
    "docs/decisions/0010-test-coverage-provenance/shelved-modules.md",
    "docs/decisions/0010-test-coverage-provenance/added-files.md",
    "docs/decisions/0010-test-coverage-provenance/restored-files.md",
    "docs/decisions/0010-test-coverage-provenance/routes_a.md",
    "docs/decisions/0010-test-coverage-provenance/routes_b.md",
    "docs/decisions/0010-test-coverage-provenance/security-auth.md",
    "docs/decisions/0010-test-coverage-provenance/newsnotes.md",
)

# The durable behavior-level output of the removed working ledger. Requiring
# the exact evidence name makes deletion or an unreviewed rename loud even when
# another unrelated test remains in the same module.
REQUIRED_REGRESSION_EVIDENCE = {
    "tests/security/test_save_raw_config_blocked_keys_fastapi.py": (
        "test_each_blocked_pattern_is_rejected",
    ),
    "tests/security/test_metrics_hostile_input_fastapi.py": (
        "test_research_journals_are_not_readable_across_users",
        "test_journal_download_refused_under_offline_or_corrupt_scope",
    ),
    "tests/security/test_news_scheduler_isolation_fastapi.py": (
        "TestSchedulerStatusIsolation::test_caller_sees_own_jobs_and_not_the_other_users",
        "TestCustomEndpointSsrfGuardAtCreateRoute::test_rejection_happens_before_any_subscription_is_created",
    ),
    "tests/security/test_news_error_scrub_wiring.py": (
        "TestNoNewsHandlerLeaksACaughtException::test_every_except_block_scrubs_before_responding",
    ),
    "tests/security/test_settings_egress_and_secrets_fastapi.py": (
        "TestModelDiscoveryEgressPolicy::test_private_only_filters_cloud_out_of_the_cached_response",
        "TestBulkSecretWriteBackAndEcho::test_bulk_save_response_echo_redacts_secrets",
    ),
    "tests/security/test_research_password_gate_fastapi.py": (
        "TestStartResearchPasswordGate::test_expired_session_is_refused_and_starts_no_research",
        "TestFollowupStartPasswordGate::test_expired_session_is_refused_and_starts_no_research",
    ),
    "tests/security/test_followup_and_settings_guards_fastapi.py": (
        "TestFollowupCustomEndpointSsrfPreflight::test_metadata_or_non_http_endpoint_is_rejected_before_any_db_write",
        "TestCrossProviderCredentialLeak::test_provider_without_api_key_setting_gets_empty_string_not_settings",
        "TestQueuedSubmissionAttributionSpoofing::test_queued_dispatch_ignores_reserved_metadata_keys",
    ),
    "tests/security/test_history_and_benchmark_limits_fastapi.py": (
        "test_oversized_limit_is_clamped_to_the_hard_cap",
        "test_non_integer_path_segment_is_rejected_with_422",
    ),
    "tests/security/test_pagination_bounds.py": (
        "TestHistoryRoutesPagination::test_limit_clamped_to_max_500",
    ),
    "tests/security/test_api_v1_boundary_fastapi.py": (
        "TestSettingsSnapshotBoundary::test_unreadable_settings_refuse_the_run_instead_of_downgrading_it",
        "TestErrorScrubWiring::test_an_unhandled_exception_below_the_endpoint_does_not_leak",
    ),
    "tests/security/test_socket_ownership_edges_fastapi.py": (
        "TestOwnershipGateFailsClosed::test_subscribe_is_refused_when_the_ownership_lookup_fails",
        "TestWebSocketOriginPolicyDerivation::test_no_env_value_can_ever_produce_the_validation_disabling_empty_list",
        "TestEmitSocketEventRoomTargetingOverTheRealTransport::test_a_room_targeted_event_reaches_only_that_room",
    ),
    "tests/security/test_middleware_and_proxy_trust_fastapi.py": (
        "TestDatabaseMiddlewareCleanupCallSite::test_next_request_on_the_same_worker_sees_none_of_the_previous_user",
        "TestTrustProxyHeadersUvicornWiring::test_unset_means_forwarded_headers_are_not_trusted",
    ),
    "tests/security/test_research_service_isolation_fastapi.py": (
        "TestRunResearchProcessUsernameGate::test_missing_username_raises_before_any_work",
        "TestCancelResearchIsolation::test_colliding_research_id_cancels_only_the_callers_row",
        "TestGenerateReportPathContainment::test_hostile_query_cannot_escape_output_dir",
    ),
    "tests/chat/test_chat_socket_events.py": (
        "TestDisconnectUserAndSession::test_disconnect_user_severs_every_socket_that_user_holds",
        "TestUnsubscribeSessionRevalidation::test_revoked_session_is_refused_and_severed",
    ),
    "tests/security/test_library_rag_security_fastapi.py": (
        "TestDeleteDocumentNoteRefusal::test_deleting_a_note_is_refused_with_403_and_the_note_survives",
        "TestFormatTestEmbeddingErrorUnit::test_internal_exception_detail_is_suppressed_entirely",
        "TestIsDownloadableDomainAllowlist::test_non_allowlisted_hosts_are_refused",
    ),
    "tests/security/test_library_notes_authz_fastapi.py": (
        "TestBulkBlobDeleteNoteProtection::test_bulk_blob_delete_refuses_a_note_and_leaves_it_untouched",
        "TestOpenFolderIsHardDisabled::test_open_folder_always_403s",
        "TestAnnotationDeleteAnchorCheck::test_deleting_a_research_annotation_via_a_different_research_id_is_404",
        "TestProtectedCollectionDeleteMapsTo409AtTheRoute::test_deleting_a_protected_collection_is_409",
    ),
    "tests/security/test_scheduler_control_and_news_limits_fastapi.py": (
        "TestSchedulerControlGate::test_gate_disabled_returns_403_without_touching_scheduler",
        "TestNewsRateLimitValuesArePinned::test_rate_limit_value_is_pinned",
    ),
    "tests/security/test_unencrypted_db_fallback.py": (
        "TestEncryptedDatabaseFailsClosed::test_encrypted_db_with_no_password_raises",
    ),
    "tests/security/test_llm_endpoint_link_local_hardening.py": (
        "test_link_local_endpoint_is_refused_at_the_http_boundary",
        "test_self_hosted_endpoint_still_accepted",
        "test_link_local_does_not_classify_as_local_in_egress_policy",
    ),
    "tests/security/test_cross_user_isolation_invariants.py": (
        "TestThreadSpecificCacheUsernameKey::test_thread_specific_cache_does_not_collide_across_usernames_same_thread",
        "TestSettingsContextIdentityGuard::test_get_setting_from_snapshot_rejects_stale_cross_user_context",
    ),
    "tests/security/test_auth_credential_lifetime_fastapi.py": (
        "TestGetUserPasswordCrossSessionGuard::test_cross_user_request_context_refuses_to_resolve",
        "TestEnsureUserDatabaseTempAuthBinding::test_mismatched_token_does_not_open_the_victims_real_database",
        "TestEnforceSessionRevocationUnit::test_destroyed_session_is_cleared",
    ),
    "tests/security/test_realtime_channel_isolation.py": (
        "test_two_live_sockets_only_the_owner_receives_the_research_event",
        "test_idle_expired_session_keeps_receiving_until_the_socket_next_speaks",
    ),
    "tests/web/services/test_socket_connect_session_gate.py": (
        "test_destroyed_session_cannot_connect",
        "TestSubscribeRevalidation::test_dead_session_severs_every_socket_of_that_session",
    ),
    "tests/web/test_long_integration_flows.py": (
        "TestTwoConcurrentUsersInterleaved::test_concurrent_http_requests_do_not_cross_users",
    ),
    # The removed working tables lagged these 17 mainline-added RAG-upload
    # cases. Twelve retain their test names in the direct port; five renamed
    # mappings are documented at that module's top and pinned in the alternate
    # successor below.
    "tests/research_library/routes/test_rag_routes_upload_main_port.py": (
        "TestUploadToCollection::test_upload_savepoint_rollback_failure_does_not_crash_batch",
        "TestUploadToCollection::test_upload_mixed_batch_survives_savepoint_rollback_failure",
        "TestUploadToCollection::test_upload_inactive_savepoint_still_rolls_back",
        "TestUploadToCollection::test_upload_begin_nested_failure_isolates_failing_file",
        "TestUploadToCollection::test_upload_soft_validation_savepoint_commit_failure_does_not_double_report",
        "TestUploadToCollection::test_upload_success_path_savepoint_commit_failure_suppresses_publication",
        "TestUploadToCollection::test_upload_existing_doc_pdf_upgrade_failure_swallowed_and_logged",
        "TestUploadToCollection::test_upload_three_pairs_of_duplicates_all_dropped_cleanly",
        "TestUploadToCollection::test_upload_failed_first_occurrence_does_not_block_second",
        "TestUploadToCollection::test_upload_savepoint_isolates_failing_file_new_doc",
        "TestUploadToCollection::test_upload_savepoint_isolates_failing_file_existing_doc",
        "TestUploadToCollection::test_upload_intra_batch_pdf_upgrade_with_pre_existing_doc_twin",
    ),
    "tests/web/routers/test_collection_upload_dedup.py": (
        "test_intra_batch_duplicate_reported_under_own_filename",
        "test_same_bytes_into_another_collection_is_added_to_collection",
        "test_failing_file_does_not_poison_the_rest_of_the_batch",
        "test_intra_batch_duplicate_pdf_upgrades_the_kept_twin",
        "test_pdf_upgrade_exception_does_not_fail_the_upload",
    ),
    "tests/notes/test_notes_router_fastapi.py": (
        "TestAIEndpoints::test_index_omitted_force_reindex_defaults_to_false",
        "TestAIEndpoints::test_synthesize_omitted_create_note_defaults_to_true",
    ),
    "tests/web/services/test_subscription_owner_scoping.py": (
        "test_two_users_same_run_id_do_not_see_each_other",
        "test_each_owner_receives_their_own_run",
        "test_string_and_int_benchmark_ids_are_one_subscription",
    ),
}

REPORTED_MODULE_EVIDENCE = {
    "tests/security/test_auth_security.py": (
        "TestAccessControl::test_authenticated_access_is_allowed",
    ),
    "tests/security/test_api_security.py": (
        "TestAPISecurityOWASPTop10::test_api2_broken_authentication",
    ),
    "tests/security/test_csrf_protection.py": (
        "TestCSRFProtection::test_post_request_with_invalid_csrf_token_rejected",
    ),
    "tests/security/test_cookie_security.py": (
        "TestHttpCookieSecurity::test_http_no_secure_flag",
    ),
    "tests/security/test_pagination_bounds.py": (
        "TestHistoryRoutesPagination::test_limit_clamped_to_max_500",
    ),
    "tests/chat/test_chat_socket_events.py": (
        "TestSubscribeStatusPush::test_subscribe_sends_current_status_if_available",
    ),
    "tests/research_scheduler/test_scheduler_edge_cases.py": (
        "TestSchedulerRouteRegistration::test_scheduler_routes_are_mounted_on_the_app",
    ),
    "tests/test_followup_api.py": (
        "TestStartFollowUp::test_start_followup_requires_a_live_session_password",
    ),
    "tests/news/test_news_input_validation.py": (
        "TestJSONBodyValidation::test_non_dict_json_body_is_rejected",
    ),
}

REGRESSION_MODULES = set(REQUIRED_REGRESSION_EVIDENCE)

FALSE_CURRENT_STATUS = (
    re.compile(r"\bstill[- ]open security gaps?\b", re.I),
    re.compile(r"\bADR-0010 Tier \d+\b", re.I),
    re.compile(r"\bGAP \d+[a-z]?\b"),
    re.compile(r"\boutstanding rows?\b", re.I),
    re.compile(r"\bthe one open item\b", re.I),
)


class TestDurableDecisionRecord:
    def test_migration_adrs_are_present_and_concise(self):
        limits = {ADR_0008: 180, ADR_0010: 215, ADR_0011: 155}
        for path, maximum in limits.items():
            assert path.is_file(), f"missing migration decision record: {path}"
            lines = path.read_text(encoding="utf-8").splitlines()
            assert "**Amendment:** 2026-08-30, tracked in [#6007]" in "\n".join(
                lines
            )
            assert len(lines) <= maximum, (
                f"{path.relative_to(REPO_ROOT)} grew to {len(lines)} lines; "
                f"the durable ADR limit is {maximum}"
            )

    def test_adr_0010_pins_the_exact_historical_measurement(self):
        text = ADR_0010.read_text(encoding="utf-8")
        assert SNAPSHOT_SHA in text
        assert COMPARISON_SHA in text
        normalized = " ".join(text.lower().split())
        assert "git fetch origin refs/pull/3299/head" in normalized
        assert "git cat-file -e" in normalized
        assert "does not promise indefinite pr-ref retention" in normalized
        assert "git ls-tree -r --name-only <sha> -- tests" in normalized
        assert "functiondef" in normalized
        assert "asyncfunctiondef" in normalized
        assert "ast.walk" in normalized
        assert "per-common-path set differences" in normalized

        all_counts = {
            label.strip(): int(count.replace(",", ""))
            for label, count in re.findall(
                r"^\| ([^|]+?) \| ([\d,]+) \|$", text, re.M
            )
        }
        assert set(all_counts) == set(EXPECTED_TEST_COUNTS) | set(
            EXPECTED_SHELVING_COUNTS
        )
        assert {
            label: all_counts[label] for label in EXPECTED_TEST_COUNTS
        } == EXPECTED_TEST_COUNTS
        assert 4_486 + 125 + 32_109 == 36_720
        assert 32_109 + 120 + 3_924 == 36_153

    def test_adr_0010_keeps_shelving_evidence_separate(self):
        text = ADR_0010.read_text(encoding="utf-8")
        normalized = " ".join(text.lower().split())
        assert SHELVED_PREPORT_SHA in text
        assert SHELVED_REPORT_SHA in text
        parsed = {
            label.strip(): int(count.replace(",", ""))
            for label, count in re.findall(
                r"^\| ([^|]+?) \| ([\d,]+) \|$", text, re.M
            )
            if label.strip() in EXPECTED_SHELVING_COUNTS
        }
        assert parsed == EXPECTED_SHELVING_COUNTS
        assert "measured at different revisions" in normalized
        assert "not part of the comparison table" in normalized
        assert "a definition count is not a pytest case count" in normalized

        for relpath in SHELVED_PREPORT_MODULES:
            assert f"`{relpath}`" in text
        assert f"`{SEPARATELY_REPORTED_MODULE}`" in text
        assert len(SHELVED_PREPORT_MODULES) == 8
        assert set(SHELVED_PREPORT_MODULES) <= set(REPORTED_MODULE_EVIDENCE)
        assert SEPARATELY_REPORTED_MODULE in REPORTED_MODULE_EVIDENCE
        assert SEPARATELY_REPORTED_MODULE not in SHELVED_PREPORT_MODULES

    def test_status_and_ownership_are_not_implicit(self):
        for path in (ADR_0010, ADR_0011):
            text = path.read_text(encoding="utf-8").lower()
            for token in (
                "historical snapshot",
                "not current",
                "github issues",
                "security.md",
            ):
                assert token in text, (
                    f"{path.relative_to(REPO_ROOT)} lost status boundary {token!r}"
                )
        assert (
            "not current suite totals"
            in ADR_0010.read_text(encoding="utf-8").lower()
        )
        source_status = " ".join(
            ADR_0011.read_text(encoding="utf-8").lower().split()
        )
        assert "not current product or security status" in source_status


class TestRawWorkingLedgerStaysRemoved:
    def test_raw_ledger_directories_are_absent(self):
        # This protects the normal documentation path only. It deliberately
        # does not forbid a future, clearly labelled immutable archive in a
        # review-artifact location chosen by maintainers.
        present = [path for path in REMOVED_LEDGER_DIRS if path.exists()]
        assert not present, f"raw migration ledger returned: {present}"

        siblings = set(
            (REPO_ROOT / "docs/decisions").glob(
                "0010-test-coverage-provenance*"
            )
        )
        assert siblings == {ADR_0010}

    def test_no_dead_ledger_reference_is_republished(self):
        violations: list[str] = []
        roots = (
            REPO_ROOT / ".github",
            REPO_ROOT / "changelog.d",
            REPO_ROOT / "docs",
            REPO_ROOT / "src",
            REPO_ROOT / "tests",
        )
        for root in roots:
            for path in root.rglob("*"):
                if not path.is_file() or path == Path(__file__):
                    continue
                if path.suffix not in {
                    ".html",
                    ".js",
                    ".json",
                    ".md",
                    ".py",
                    ".rst",
                    ".toml",
                    ".txt",
                    ".yaml",
                    ".yml",
                }:
                    continue
                text = path.read_text(encoding="utf-8", errors="replace")
                for marker in DEAD_LEDGER_MARKERS:
                    if marker in text:
                        violations.append(
                            f"{path.relative_to(REPO_ROOT)}: {marker}"
                        )
        assert not violations, (
            "removed ledger references were republished:\n  - "
            + "\n  - ".join(violations)
        )

    def test_reader_facing_docs_do_not_present_working_status_tables(self):
        stale = ("| **GAP** |", "| GAP |", "| **UNCERTAIN** |", "| UNCERTAIN |")
        violations = []
        for path in MIGRATION_DOCS:
            text = path.read_text(encoding="utf-8")
            for marker in stale:
                if marker in text:
                    violations.append(
                        f"{path.relative_to(REPO_ROOT)}: {marker}"
                    )
        assert not violations


class TestRegressionEvidenceRemainsCollectible:
    def test_required_regression_evidence_is_still_named(self):
        failures = []
        for evidence_map in (
            REQUIRED_REGRESSION_EVIDENCE,
            REPORTED_MODULE_EVIDENCE,
        ):
            for relpath, required_names in sorted(evidence_map.items()):
                path = REPO_ROOT / relpath
                if not path.is_file():
                    failures.append(f"{relpath}: missing")
                    continue
                try:
                    active = active_pytest_nodes(
                        path.read_text(encoding="utf-8")
                    )
                except SyntaxError as exc:
                    failures.append(f"{relpath}: cannot parse ({exc})")
                    continue
                missing = sorted(set(required_names) - active)
                if missing:
                    failures.append(
                        f"{relpath}: missing or not statically active/"
                        f"collectible {missing}"
                    )
        assert not failures, "\n".join(failures)

    def test_regression_prose_does_not_claim_historical_rows_are_open(self):
        violations = []
        for relpath in sorted(REGRESSION_MODULES):
            text = (REPO_ROOT / relpath).read_text(encoding="utf-8")
            for pattern in FALSE_CURRENT_STATUS:
                for match in pattern.finditer(text):
                    line = text.count("\n", 0, match.start()) + 1
                    violations.append(f"{relpath}:{line}: {match.group(0)}")
        assert not violations, (
            "historical coverage prose still looks like current status:\n  - "
            + "\n  - ".join(violations)
        )

    def test_empty_shelf_ratchet_remains_active(self):
        ratchet = REPO_ROOT / "tests/test_migration_shelved_coverage_ratchet.py"
        assert ratchet.is_file()
        text = ratchet.read_text(encoding="utf-8")
        assert "SHELVED_BY_MIGRATION: set[str] = set()" in text
        for test_name in (
            "test_shelved_set_has_not_grown",
            "test_shelved_entries_still_exist_and_are_still_shelved",
        ):
            assert test_name in active_pytest_nodes(text)


class TestStaticEvidenceNodeResolver:
    def test_accepts_only_direct_active_pytest_nodes(self):
        source = """
pytestmark = []
pytestmark.append(pytest.mark.timeout(1))

def test_top_level():
    pass

def test_explicitly_enabled():
    pass
test_explicitly_enabled.__test__ = True

@pytest.mark.timeout(1)
class TestActive:
    @pytest.mark.asyncio
    async def test_direct_method(self):
        pass

    class TestNested:
        def test_nested_method(self):
            pass

class TestPassOnly:
    pass
"""
        assert active_pytest_nodes(source) == {
            "test_top_level",
            "test_explicitly_enabled",
            "TestActive::test_direct_method",
        }

    def test_rejects_skip_and_xfail_at_each_collection_scope(self):
        blocked_sources = (
            """
@pytest.mark.skip(reason="disabled")
def test_blocked():
    pass
""",
            """
@unittest.skipIf(True, "disabled")
class TestBlocked:
    def test_node(self):
        pass
""",
            """
class TestBlocked:
    @pytest.mark.xfail(reason="disabled")
    def test_node(self):
        pass
""",
            """
pytestmark = pytest.mark.skipif(True, reason="disabled")
def test_blocked():
    pass
""",
            """
pytest.skip("disabled", allow_module_level=True)
def test_blocked():
    pass
""",
            """
pytest.importorskip("optional_dependency")
def test_blocked():
    pass
""",
            """
from pytest import skip as stop_collection
stop_collection("disabled", allow_module_level=True)
def test_blocked():
    pass
""",
            """
from pytest import mark as test_mark
try:
    pytestmark = test_mark.xfail(reason="disabled")
except RuntimeError:
    pass
def test_blocked():
    pass
""",
            """
pytestmark = build_dynamic_marker()
def test_blocked():
    pass
""",
            """
pytestmark = []
pytestmark += [pytest.mark.skip(reason="disabled")]
def test_blocked():
    pass
""",
            """
pytestmark = []
pytestmark.append(pytest.mark.skip(reason="disabled"))
def test_blocked():
    pass
""",
            """
pytestmark = []
pytestmark.extend([pytest.mark.skip(reason="disabled")])
def test_blocked():
    pass
""",
            """
class TestBlocked:
    pytestmark = []
    pytestmark += [pytest.mark.skip(reason="disabled")]
    def test_node(self):
        pass
""",
            """
@pytest.fixture
def test_blocked():
    pass
""",
            """
ENABLED = False
def test_blocked():
    pass
test_blocked.__test__ = ENABLED
""",
            """
class TestBlocked:
    pytest.skip("disabled", allow_module_level=True)
    def test_node(self):
        pass
""",
            """
def test_blocked():
    pass
test_blocked = pytest.fixture(test_blocked)
""",
            """
def test_blocked():
    pass
test_blocked = object()
""",
            """
class TestBlocked:
    @property
    def test_node(self):
        return True
""",
            """
class TestBlocked:
    def test_node(self):
        pass
    test_node = object()
""",
        )
        for source in blocked_sources:
            assert active_pytest_nodes(source) == set()

    def test_rejects_explicit_collection_opt_outs(self):
        assert (
            active_pytest_nodes(
                """
__test__ = False
def test_blocked():
    pass
"""
            )
            == set()
        )
        assert (
            active_pytest_nodes(
                """
class TestBlocked:
    __test__ = False
    def test_node(self):
        pass
"""
            )
            == set()
        )
        assert (
            active_pytest_nodes(
                """
def test_blocked():
    pass
test_blocked.__test__ = False
"""
            )
            == set()
        )

    def test_rejects_duplicate_or_custom_constructed_nodes(self):
        source = """
def test_duplicate():
    pass
def test_duplicate():
    pass

class TestDuplicate:
    def test_node(self):
        pass
class TestDuplicate:
    def test_node(self):
        pass

class TestDuplicateMethod:
    def test_node(self):
        pass
    def test_node(self):
        pass

class TestCustomConstruction:
    def __init__(self):
        pass
    def test_node(self):
        pass
"""
        assert active_pytest_nodes(source) == set()

    def test_rejects_statically_empty_literal_parametrization(self):
        source = """
@pytest.mark.parametrize("value", [])
def test_empty(value):
    pass

class TestEmpty:
    @pytest.mark.parametrize("value", argvalues=())
    def test_empty(self, value):
        pass
"""
        assert active_pytest_nodes(source) == set()

    def test_rejects_empty_parametrization_at_every_collection_scope(self):
        stacked = """
@pytest.mark.parametrize("present", [1])
@pytest.mark.parametrize("missing", [])
def test_stacked(present, missing):
    pass

@pytest.mark.parametrize("missing", [])
class TestEmptyDecorator:
    def test_node(self, missing):
        pass

class TestEmptyMarker:
    pytestmark = pytest.mark.parametrize("missing", [])
    def test_node(self, missing):
        pass
"""
        assert active_pytest_nodes(stacked) == set()

        module_mark = """
pytestmark = pytest.mark.parametrize("missing", [])
def test_module_mark(missing):
    pass
"""
        assert active_pytest_nodes(module_mark) == set()

    def test_rejects_definition_time_stops_and_assigned_constructors(self):
        definition_time_stop = """
def test_blocked(value=pytest.importorskip("missing_dependency")):
    pass
"""
        assert active_pytest_nodes(definition_time_stop) == set()

        class_local_alias_stop = """
def test_top_level():
    pass

class Helper:
    from pytest import skip as stop
    def helper(value=stop("removed in the migration", allow_module_level=True)):
        pass
"""
        assert active_pytest_nodes(class_local_alias_stop) == set()

        direct_skiptest = """
import unittest
def test_top_level():
    pass
raise unittest.SkipTest("removed in the migration")
"""
        assert active_pytest_nodes(direct_skiptest) == set()

        assigned_stop_alias = """
import pytest
stop = pytest.skip
stop("removed in the migration", allow_module_level=True)
def test_top_level():
    pass
"""
        assert active_pytest_nodes(assigned_stop_alias) == set()

        same_line_alias = """
import pytest; stop = pytest.skip; stop(
    "removed in the migration", allow_module_level=True
)
def test_top_level():
    pass
"""
        assert active_pytest_nodes(same_line_alias) == set()

        assigned_constructor = """
class TestCustomConstruction:
    __init__ = lambda self: None
    def test_node(self):
        pass
"""
        assert active_pytest_nodes(assigned_constructor) == set()

    def test_unknown_decorator_shapes_fail_closed(self):
        custom = """
@custom_marker
def test_node():
    pass
"""
        chained = """
@pytest.mark.skip.with_args(reason="disabled")
def test_node():
    pass
"""
        for source in (custom, chained):
            assert active_pytest_nodes(source) == set()

    def test_rejects_late_member_mutation_and_inherited_constructors(self):
        late_method = """
class TestLate:
    def test_node(self):
        pass
TestLate.test_node = None
"""
        late_constructor = """
class TestLate:
    def test_node(self):
        pass
TestLate.__init__ = lambda self: None
"""
        inherited_constructor = """
class Base:
    def __init__(self):
        pass
class TestChild(Base):
    def test_node(self):
        pass
"""
        for source in (late_method, late_constructor, inherited_constructor):
            assert active_pytest_nodes(source) == set()

    def test_rejects_other_rebindings_and_late_pytestmarks(self):
        sources = (
            """
def test_node():
    pass
for test_node in [None]:
    pass
""",
            """
import pytest
(pytestmark := pytest.mark.skip(reason="disabled"))
def test_node():
    pass
""",
            """
import pytest
def test_node():
    pass
test_node.pytestmark = pytest.mark.skip(reason="disabled")
""",
            """
import pytest
class TestNode:
    def test_method(self):
        pass
TestNode.pytestmark = pytest.mark.skip(reason="disabled")
""",
            """
import pytest
class TestNode:
    def test_method(self):
        pass
    test_method.pytestmark = pytest.mark.skip(reason="disabled")
""",
            """
from support import pytestmark
def test_node():
    pass
""",
            """
from support import *
def test_node():
    pass
""",
        )
        for source in sources:
            assert active_pytest_nodes(source) == set()

    def test_rejects_a_named_empty_parametrization(self):
        constant_params = """
import pytest
VALUES = ()
@pytest.mark.parametrize("value", VALUES)
def test_node(value):
    pass
"""
        assert active_pytest_nodes(constant_params) == set()

        defined_too_late = """
import pytest
@pytest.mark.parametrize("value", VALUES)
def test_node(value):
    pass
VALUES = [1]
"""
        assert active_pytest_nodes(defined_too_late) == set()