Files
Athena-Deck/deploy/applio-web/backend/tests/test_jobs.py
T
Mikei386 06cacafbad Add native Applio RVC and preserve the custom studio UI
Pin Applio 3.6.5 and verified basis assets, migrate private voice provenance, and coordinate conversion/live/training with Deck GPU leases. Prepare an inactive voice profile and a restricted studio container. Move CUDA and Torch caches out of the small temporary filesystem to prevent OmniVoice reference failures. Package app updates from the explicit allowlist in one Docker layer.
2026-10-05 10:02:07 +02:00

296 lines
9.1 KiB
Python

"""Tests for job states, queue, and lifecycle."""
def test_extract_defaults_do_not_reference_missing_global_mute_assets():
from app.api.jobs import ExtractRequest
request = ExtractRequest(model_name="Anna")
assert request.include_mutes == 0
def test_training_sample_rate_rejects_unsupported_44100():
import pytest
from pydantic import ValidationError
from app.api.jobs import TrainRequest
with pytest.raises(ValidationError):
TrainRequest(model_name="Anna", sample_rate=44100)
def test_model_name_rejects_path_traversal():
import pytest
from pydantic import ValidationError
from app.api.jobs import TrainRequest
with pytest.raises(ValidationError):
TrainRequest(model_name="../Anna")
def test_retry_terminal_job_creates_new_job(client):
from app.jobs.models import Job, JobStatus, JobType
store = client.app.state.job_store
original = Job(type=JobType.TRAIN, model_name="Anna", parameters={"total_epoch": 20})
original.status = JobStatus.FAILED
original.error = "test failure"
store.save(original)
response = client.post(f"/api/jobs/{original.id}/retry")
assert response.status_code == 200
new_job_id = response.json()["new_job_id"]
assert new_job_id != original.id
retried = store.get(new_job_id)
assert retried is not None
assert retried.type == original.type
assert retried.model_name == original.model_name
def test_retry_tts_job_uses_mp3_for_edge_tts_intermediate(client):
from app.jobs.models import Job, JobStatus, JobType
store = client.app.state.job_store
original = Job(type=JobType.TTS_CONVERT, model_name="Anna", parameters={})
original.status = JobStatus.FAILED
original.error = "test failure"
store.save(original)
response = client.post(f"/api/jobs/{original.id}/retry")
assert response.status_code == 200
retried = store.get(response.json()["new_job_id"])
assert retried is not None
assert retried.parameters["output_tts_path"].endswith("/tts.mp3")
def test_job_summary_is_available(client):
response = client.get("/api/jobs/summary")
assert response.status_code == 200
assert {"running", "waiting", "recent", "counts"} <= response.json().keys()
def test_get_job_prefers_live_in_memory_state(client):
from app.jobs.enums import JobType
from app.jobs.models import Job
queue = client.app.state.job_queue
live = Job(type=JobType.TRAIN, model_name="Anna")
live.mark_running()
live.add_log("Epoche 20 erreicht")
client.app.state.job_store.save(live.model_copy(update={"log": []}))
with queue._lock:
queue._running[live.id] = live
try:
response = client.get(f"/api/jobs/{live.id}")
assert response.status_code == 200
assert any("Epoche 20" in line for line in response.json()["log"])
finally:
with queue._lock:
queue._running.pop(live.id, None)
from app.jobs.enums import JobPhase, JobStatus, JobType
from app.jobs.models import Job, JobResponse
from app.jobs.store import JobStore
def test_job_initial_state():
job = Job(type=JobType.CONVERT)
assert job.status == JobStatus.PREPARED
assert job.phase == JobPhase.ACCEPTED
assert job.progress == 0.0
assert not job.cancelled
assert job.id # has an ID
def test_job_lifecycle_success():
job = Job(type=JobType.CONVERT)
job.mark_running()
assert job.status == JobStatus.RUNNING
assert job.started_at is not None
job.set_phase(JobPhase.CONVERTING_VOICE, "Stimme wird umgewandelt")
assert job.phase == JobPhase.CONVERTING_VOICE
assert job.progress_message == "Stimme wird umgewandelt"
job.mark_success()
assert job.status == JobStatus.SUCCESS
assert job.phase == JobPhase.COMPLETED
assert job.progress == 100.0
assert job.finished_at is not None
assert job.duration_seconds is not None
assert job.is_terminal()
def test_job_lifecycle_failure():
job = Job(type=JobType.TRAIN)
job.mark_running()
job.mark_failed("GPU-Speicher nicht ausreichend")
assert job.status == JobStatus.FAILED
assert job.phase == JobPhase.FAILED
assert job.error == "GPU-Speicher nicht ausreichend"
assert job.is_terminal()
def test_job_lifecycle_cancel():
job = Job(type=JobType.CONVERT)
job.mark_running()
job.mark_cancelled()
assert job.status == JobStatus.CANCELLED
assert job.phase == JobPhase.CANCELLED
assert job.cancelled
assert job.is_terminal()
def test_job_log():
job = Job(type=JobType.CONVERT)
job.add_log("Erste Nachricht")
job.add_log("Zweite Nachricht")
assert len(job.log) == 2
assert "Erste Nachricht" in job.log[0]
assert "Zweite Nachricht" in job.log[1]
def test_job_response_from_job():
job = Job(type=JobType.CONVERT, model_name="TestModel")
job.parameters = {"pitch": 5}
job.input_files = ["/tmp/in.wav"]
job.output_files = ["/tmp/out.wav"]
resp = JobResponse.from_job(job)
assert resp.id == job.id
assert resp.type == JobType.CONVERT
assert resp.model_name == "TestModel"
assert resp.parameters == {"pitch": 5}
assert resp.input_files == ["/tmp/in.wav"]
assert resp.output_files == ["/tmp/out.wav"]
def test_job_store_save_and_get(tmp_dirs):
store = JobStore(db_path=tmp_dirs["jobs"] / "test.db")
job = Job(type=JobType.CONVERT, model_name="StoreTest")
job.parameters = {"pitch": 3}
store.save(job)
loaded = store.get(job.id)
assert loaded is not None
assert loaded.id == job.id
assert loaded.type == JobType.CONVERT
assert loaded.model_name == "StoreTest"
assert loaded.parameters == {"pitch": 3}
def test_job_store_list(tmp_dirs):
store = JobStore(db_path=tmp_dirs["jobs"] / "test.db")
for i in range(5):
job = Job(type=JobType.CONVERT)
job.status = JobStatus.SUCCESS if i % 2 == 0 else JobStatus.FAILED
store.save(job)
all_jobs = store.list()
assert len(all_jobs) == 5
success_jobs = store.list(status=JobStatus.SUCCESS)
assert len(success_jobs) == 3
failed_jobs = store.list(status=JobStatus.FAILED)
assert len(failed_jobs) == 2
def test_job_store_count(tmp_dirs):
store = JobStore(db_path=tmp_dirs["jobs"] / "test.db")
for i in range(3):
store.save(Job(type=JobType.CONVERT))
assert store.count() == 3
def test_job_store_deletes_only_terminal_jobs(tmp_dirs):
store = JobStore(db_path=tmp_dirs["jobs"] / "test.db")
finished = Job(type=JobType.CONVERT)
finished.status = JobStatus.FAILED
active = Job(type=JobType.CONVERT)
active.status = JobStatus.RUNNING
store.save(finished)
store.save(active)
assert store.delete_terminal(finished.id)
assert not store.delete_terminal(active.id)
assert store.get(finished.id) is None
assert store.get(active.id) is not None
def test_clear_finished_jobs_keeps_active_and_removes_results(client, tmp_dirs):
store = client.app.state.job_store
finished = Job(type=JobType.CONVERT)
finished.status = JobStatus.SUCCESS
active = Job(type=JobType.CONVERT)
active.status = JobStatus.RUNNING
store.save(finished)
store.save(active)
result_dir = tmp_dirs["results"] / finished.id
result_dir.mkdir()
(result_dir / "converted.wav").write_bytes(b"audio")
response = client.delete("/api/jobs")
assert response.status_code == 200
assert response.json()["deleted"] == 1
assert store.get(finished.id) is None
assert store.get(active.id) is not None
assert not result_dir.exists()
def test_delete_job_rejects_active_job(client):
store = client.app.state.job_store
active = Job(type=JobType.CONVERT)
active.status = JobStatus.RUNNING
store.save(active)
response = client.delete(f"/api/jobs/{active.id}")
assert response.status_code == 409
assert store.get(active.id) is not None
def test_job_store_cleanup_old(tmp_dirs):
from datetime import datetime, timedelta
store = JobStore(db_path=tmp_dirs["jobs"] / "test.db")
# Create an old finished job
job = Job(type=JobType.CONVERT)
job.status = JobStatus.SUCCESS
job.finished_at = datetime.now() - timedelta(days=60)
store.save(job)
# Create a recent job
job2 = Job(type=JobType.CONVERT)
job2.status = JobStatus.SUCCESS
job2.finished_at = datetime.now()
store.save(job2)
cleaned = store.cleanup_old(retention_days=30)
assert cleaned == 1
assert store.count() == 1
def test_job_store_get_nonexistent(tmp_dirs):
store = JobStore(db_path=tmp_dirs["jobs"] / "test.db")
assert store.get("nonexistent-id") is None
def test_job_persistence_across_restart(tmp_dirs):
"""Jobs survive a store 'restart' (simulated by creating a new store)."""
db_path = tmp_dirs["jobs"] / "persist.db"
store1 = JobStore(db_path=db_path)
job = Job(type=JobType.TRAIN, model_name="PersistTest")
job.mark_running()
job.set_phase(JobPhase.CONVERTING_VOICE, "Training läuft")
store1.save(job)
# Simulate restart
store2 = JobStore(db_path=db_path)
loaded = store2.get(job.id)
assert loaded is not None
assert loaded.model_name == "PersistTest"
assert loaded.status == JobStatus.RUNNING
assert loaded.phase == JobPhase.CONVERTING_VOICE