"""Submit-form validation tests (P0-1 normalisation, email / model-id checks, mismatch warning). ``app`` is imported once with the bucket-backed loaders stubbed out, so no network access, no bucket I/O and no queue worker is involved. ``app.py`` only launches the server under ``__main__``; ``demo.load`` handlers never run without a browser session. Run: ``ffasrvenv/bin/python -m pytest tests/test_submit_validation.py -q`` """ from __future__ import annotations import os import sys from pathlib import Path import pytest ROOT = Path(__file__).resolve().parents[1] if str(ROOT) not in sys.path: sys.path.insert(0, str(ROOT)) # Never run evaluations or touch production storage from tests. os.environ.pop("FFASR_REMOTE_JOBS", None) os.environ.pop("FFASR_ALLOW_PROD_WRITES", None) os.environ.setdefault("FFASR_MODERATION", "0") @pytest.fixture(scope="session") def app_module(): """Import ``app`` with storage-backed loaders stubbed (no bucket reads at import time).""" import init import diarization_results as dr import examples_renderer init.load_raw_results = lambda *a, **k: [] init.list_leaderboard_versions = lambda: [] init.raw_rows_for_version = lambda v: [] dr.load_raw_results = lambda *a, **k: [] examples_renderer.ensure_assets = lambda: None import app # noqa: WPS433 - deliberate late import return app @pytest.fixture def no_network(app_module, monkeypatch): """Stub the Hub check + queue so nothing leaves the process; return call recorders.""" calls: dict = {"hub": [], "enqueue": []} def fake_hub(model_id): calls["hub"].append(model_id) if model_id == "does-not/exist": return False, "was not found on the hub!" return True, None def fake_enqueue(model_id, family_id, submission_notes="", **kwargs): calls["enqueue"].append({"model_id": model_id, "family_id": family_id, "submission_notes": submission_notes, **kwargs}) return "job1234", 1, None, False monkeypatch.setattr(app_module, "is_model_on_hub", fake_hub) monkeypatch.setattr(app_module, "load_raw_results", lambda *a, **k: []) monkeypatch.setattr(app_module.job_queue, "enqueue", fake_enqueue) monkeypatch.setattr(app_module.dr, "row_for_model", lambda mid: None) return calls EMAIL = "someone@example.com" def test_untouched_example_script_is_normalised_to_empty(app_module, no_network): from constants import DEFAULT_CUSTOM_EVAL_EXAMPLE assert app_module.normalize_custom_script(DEFAULT_CUSTOM_EVAL_EXAMPLE) == "" assert app_module.normalize_custom_script(" " + DEFAULT_CUSTOM_EVAL_EXAMPLE + "\n\n") == "" v = app_module.validate_submission( "openai/whisper-tiny", "", EMAIL, DEFAULT_CUSTOM_EVAL_EXAMPLE, "", "asr" ) assert v["error"] is None assert v["custom_script"] == "" # With the example dropped the submission is a plain Hub model, so the Hub check runs. assert no_network["hub"] == ["openai/whisper-tiny"] # No mismatch warning should be raised for a script that was discarded. assert v["warnings"] == [] def test_diarization_is_refused_while_the_track_is_off(app_module, no_network, monkeypatch): monkeypatch.delenv("FFASR_ENABLE_DIARIZATION", raising=False) v = app_module.validate_submission("pyannote/speaker-diarization-3.1", "", EMAIL, "", "", "diarization") assert v["error"] and "not open yet" in v["error"] status, job_id = app_module._submit_model_impl( "pyannote/speaker-diarization-3.1", "", EMAIL, "", "", "", "", False, "diarization" ) assert job_id == "" and "not open yet" in status assert no_network["enqueue"] == [] def test_untouched_diarization_example_is_normalised_to_empty(app_module, no_network, monkeypatch): from constants import DEFAULT_DIARIZATION_EVAL_EXAMPLE monkeypatch.setenv("FFASR_ENABLE_DIARIZATION", "1") v = app_module.validate_submission( "pyannote/speaker-diarization-3.1", "", EMAIL, DEFAULT_DIARIZATION_EVAL_EXAMPLE, "", "diarization" ) assert v["error"] is None assert v["custom_script"] == "" def test_submit_model_enqueues_empty_script_for_untouched_form(app_module, no_network): from constants import DEFAULT_CUSTOM_EVAL_EXAMPLE html = app_module.submit_model( "openai/whisper-tiny", "", EMAIL, "", "", DEFAULT_CUSTOM_EVAL_EXAMPLE, "", False, "asr" ) assert "job1234" in html assert len(no_network["enqueue"]) == 1 job = no_network["enqueue"][0] assert job["model_id"] == "openai/whisper-tiny" assert job["custom_script"] == "" assert no_network["hub"] == ["openai/whisper-tiny"] def test_untouched_form_with_missing_model_is_rejected_by_hub_check(app_module, no_network): from constants import DEFAULT_CUSTOM_EVAL_EXAMPLE html = app_module.submit_model( "does-not/exist", "", EMAIL, "", "", DEFAULT_CUSTOM_EVAL_EXAMPLE, "", False, "asr" ) assert "does-not/exist" in html assert "not found on the hub" in html assert no_network["hub"] == ["does-not/exist"] assert no_network["enqueue"] == [] @pytest.mark.parametrize("email", ["", " ", "not-an-email", "two@@at.com", "user@nodot"]) def test_bad_email_rejected(app_module, no_network, email): v = app_module.validate_submission("openai/whisper-tiny", "", email, "", "", "asr") assert v["error"] is not None assert "email" in v["error"].lower() # Cheap checks run before any network call. assert no_network["hub"] == [] def test_model_id_without_slash_rejected(app_module, no_network): v = app_module.validate_submission("whisper-tiny", "", EMAIL, "", "", "asr") assert v["error"] is not None assert "author/model_name" in v["error"] assert no_network["hub"] == [] html = app_module.submit_model("whisper-tiny", "", EMAIL, "", "", "", "", False, "asr") assert "author/model_name" in html assert no_network["enqueue"] == [] def test_empty_model_id_rejected(app_module, no_network): v = app_module.validate_submission("", "", EMAIL, "", "", "asr") assert v["error"] is not None assert "model ID" in v["error"] def test_script_model_mismatch_warning(app_module, no_network): script = ( "from transformers import AutoModel\n" "model = AutoModel.from_pretrained(\n 'CohereLabs/cohere-transcribe-03-2026'\n)\n" "def evaluate(file):\n return ''\n" ) warn = app_module.script_model_mismatch_warning(script, "openai/whisper-tiny") assert warn and "CohereLabs/cohere-transcribe-03-2026" in warn and "openai/whisper-tiny" in warn # Matching id (case-insensitive) → no warning; no from_pretrained → no warning. assert app_module.script_model_mismatch_warning(script, "coherelabs/Cohere-Transcribe-03-2026") is None assert app_module.script_model_mismatch_warning("def evaluate(f):\n return ''", "a/b") is None v = app_module.validate_submission("openai/whisper-tiny", "my notes", EMAIL, script, "", "asr") assert v["error"] is None assert v["custom_script"] == script assert v["warnings"] and "CohereLabs/cohere-transcribe-03-2026" in v["warnings"][0] # Shown to the submitter ... assert "Warning" in v["script_hint"] and "cohere-transcribe" in v["script_hint"] # ... and recorded in the notes so moderators see it, without losing the submitter's notes. assert v["submission_notes"].startswith("my notes") assert "[auto] Warning" in v["submission_notes"] # Custom scripts skip the Hub check (API / non-Hub models are allowed). assert no_network["hub"] == [] html = app_module.submit_model("openai/whisper-tiny", "my notes", EMAIL, "", "", script, "", False, "asr") assert "Warning" in html and "job1234" in html assert "[auto] Warning" in no_network["enqueue"][0]["submission_notes"] assert no_network["enqueue"][0]["custom_script"] == script def test_enqueue_without_client_key_kwarg_is_tolerated(app_module, monkeypatch, no_network): """Older ``job_queue.enqueue`` builds (no ``client_key``) must still work.""" seen = {} def legacy_enqueue(model_id, family_id, submission_notes="", *, contact_email="", extra_requirements="", setup_script="", custom_script="", recipe_id="", is_gated=False, task="asr"): seen["model_id"] = model_id return "legacy01", 2, None, False monkeypatch.setattr(app_module.job_queue, "enqueue", legacy_enqueue) html = app_module.submit_model("openai/whisper-tiny", "", EMAIL, "", "", "", "", False, "asr") assert "legacy01" in html assert seen["model_id"] == "openai/whisper-tiny" def test_public_api_signature_is_backward_compatible(app_module): import inspect params = list(inspect.signature(app_module.submit_model).parameters) assert params[:9] == [ "model_id", "submission_notes", "contact_email", "extra_requirements", "setup_script", "custom_script", "recipe_id", "is_gated", "task", ] # Gradio-injected request stays optional and last. assert params[-1] == "request" assert inspect.signature(app_module.submit_model).parameters["request"].default is None