import json import time import types import numpy as np import pytest from studio.daw import api, engine, jobs, stems SR = 44100 def _proj_with_asset(): res = json.loads(api.daw_generate("sfx", "procedural", "impact", "", "", 2, 120, "", 0, None)) return {"tracks": [], "clips": [], "assets": [{"id": "a1", "name": "x", "path": res["path"]}]} class _FakeApi: calls: list = [] private = True def __init__(self, token=None): _FakeApi.calls.append(("init", token)) def create_repo(self, repo, **kw): _FakeApi.calls.append(("create_repo", repo, kw)) def upload_file(self, **kw): _FakeApi.calls.append(("upload_file", kw)) def repo_info(self, repo, **kw): _FakeApi.calls.append(("repo_info", repo)) return types.SimpleNamespace(private=_FakeApi.private) def list_repo_files(self, repo, **kw): return [] def create_commit(self, repo, operations, **kw): _FakeApi.calls.append(("create_commit", repo, [o.path_in_repo for o in operations], kw)) return types.SimpleNamespace(oid="abc123") @pytest.fixture def fake_hf(monkeypatch): import huggingface_hub _FakeApi.calls = [] _FakeApi.private = True api._UPLOADED.clear() monkeypatch.setattr(huggingface_hub, "HfApi", _FakeApi) monkeypatch.setenv("DAW_HF_SAVE", "1") return _FakeApi.calls def test_save_with_mocked_oauth_goes_to_private_dataset(fake_hf): tok = types.SimpleNamespace(token="hf_mocked_not_real") prof = types.SimpleNamespace(username="rob-boss") res = json.loads(api.daw_save(json.dumps(_proj_with_asset()), "Mein Song", tok, prof)) assert res["hf"] == "https://huggingface.co/datasets/rob-boss/2b-studio-projects" create = [c for c in fake_hf if c[0] == "create_repo"][0] assert create[1] == "rob-boss/2b-studio-projects" and create[2]["private"] is True and create[2]["repo_type"] == "dataset" up = [c for c in fake_hf if c[0] == "upload_file"][0][1] assert up["repo_id"] == "rob-boss/2b-studio-projects" and up["path_in_repo"].startswith("projects/") and up["path_in_repo"].endswith(".2bproj.zip") assert ("init", "hf_mocked_not_real") in fake_hf @pytest.mark.parametrize("bad", ["../evil", "a/b", "", "x" * 120, "-lead"]) def test_save_rejects_invalid_username(fake_hf, bad): tok = types.SimpleNamespace(token="t") res = json.loads(api.daw_save(json.dumps(_proj_with_asset()), "T", tok, types.SimpleNamespace(username=bad))) assert res["hf"] is None and "hf_error" in res assert not [c for c in fake_hf if c[0] in ("create_repo", "upload_file")] def test_save_without_login_is_download_only(fake_hf): res = json.loads(api.daw_save(json.dumps(_proj_with_asset()), "T", None, None)) assert res["hf"] is None and res["path"].endswith(".2bproj.zip") and not fake_hf def test_jobs_progress_and_error(): def ok(report): report(0.5, "halb") return {"x": 1} jid = jobs.start("t", ok) for _ in range(100): j = jobs.get(jid) if j["state"] == "done": break time.sleep(0.02) assert j["state"] == "done" and j["result"] == {"x": 1} and j["progress"] == 1.0 def bad(report): raise RuntimeError("kaputt") jid = jobs.start("t", bad) for _ in range(100): j = jobs.get(jid) if j["state"] == "error": break time.sleep(0.02) assert j["state"] == "error" and "kaputt" in j["error"] assert jobs.get("nope") is None def test_stems_length_limit(monkeypatch): res = json.loads(api.daw_generate("sfx", "procedural", "riser", "", "", 6, 120, "", 0, None)) monkeypatch.setattr(stems, "MAX_SECONDS", 2.0) monkeypatch.setattr(stems, "available", lambda: True) with pytest.raises(Exception, match="max"): api.daw_stems(res["path"], 0, 0) @pytest.mark.skipif(not stems.available(), reason="demucs nicht installiert") def test_stems_job_end_to_end(): t = np.arange(int(SR * 4)) / SR x = (0.2 * np.sin(2 * np.pi * 110 * t) + 0.1 * np.sin(2 * np.pi * 880 * t)).astype(np.float32) path = api.DAW_DIR / "stemtest.wav" engine.save(path, np.vstack([x, x]), SR) start = json.loads(api.daw_stems(str(path), 0, 0)) for _ in range(600): j = json.loads(api.daw_job(start["job"])) if j["state"] in ("done", "error"): break time.sleep(0.2) assert j["state"] == "done", j assert {s["stem"] for s in j["result"]} == {"vocals", "drums", "bass", "other"} assert all(s["url"].startswith("/gradio_api/file=") for s in j["result"])