from __future__ import annotations import importlib.util import json import wave from pathlib import Path import numpy as np PROJECT_ROOT = Path(__file__).resolve().parents[1] SCRIPT = ( PROJECT_ROOT / "scripts" / "data" / "build_continuous_audio_command_sources.py" ) def _load_module(): spec = importlib.util.spec_from_file_location( "build_continuous_audio_command_sources_test", SCRIPT, ) assert spec is not None and spec.loader is not None module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module def _write_wav(path: Path, samples: np.ndarray) -> None: path.parent.mkdir(parents=True, exist_ok=True) pcm = np.rint( np.clip(np.asarray(samples), -1.0, 1.0) * 32767.0 ).astype(" None: module = _load_module() source_key = "seed_audio/000001/female_01_en" speech_root = tmp_path / "speech" mixed_root = tmp_path / "mixed" time = np.arange(16_000, dtype=np.float32) / 16_000.0 speech = 0.1 * np.sin(2.0 * np.pi * 440.0 * time) _write_wav(speech_root / f"{source_key}.wav", speech) background_pcm = ( (np.arange(160_000, dtype=np.int32) % 2000) - 1000 ).astype("= 2 def test_background_slot_is_deterministic_and_never_wraps() -> None: module = _load_module() sessions = [ {"key": "short", "window_count": 10}, {"key": "long", "window_count": 100}, ] first = module._select_background_slot( source_key="seed_audio/000001/voice", listen_windows=20, max_motion_steps=30, sessions=sessions, initial_idle_max_steps=5, safety_steps=10, seed=42, ) second = module._select_background_slot( source_key="seed_audio/000001/voice", listen_windows=20, max_motion_steps=30, sessions=sessions, initial_idle_max_steps=5, safety_steps=10, seed=42, ) assert first[0]["key"] == "long" assert (first[0]["key"], first[1]) == (second[0]["key"], second[1]) assert first[1] >= 5 assert first[1] + 20 + 30 + 10 <= first[0]["window_count"] def test_valid_source_list_can_be_reused_after_legacy_cache_cleanup( tmp_path: Path, ) -> None: module = _load_module() assignment_path = tmp_path / "source_background_assignments.jsonl" rows = [ { "source_key": "seed_audio/000001/female_01_en", "listen_window_count": 7, }, { "source_key": "seed_audio/000002/male_01_zh", "listen_window_count": 11, }, ] assignment_path.write_text( "".join(json.dumps(row) + "\n" for row in rows), encoding="utf-8", ) result = module._load_valid_sources( tmp_path / "deleted_legacy_cache", assignment_path, ) assert result == { "seed_audio/000001/female_01_en": 7, "seed_audio/000002/male_01_zh": 11, }