77 lines
2.2 KiB
Python
77 lines
2.2 KiB
Python
import json
|
|
import pytest
|
|
from voice_gen.registry import VoiceRegistry, VoiceEntry
|
|
from voice_gen.errors import VoiceNotFoundError
|
|
|
|
|
|
def test_load_missing_file_returns_empty_registry(tmp_voices_json):
|
|
reg = VoiceRegistry.load(tmp_voices_json)
|
|
assert reg.path == tmp_voices_json
|
|
assert reg.list_names() == []
|
|
|
|
|
|
def test_load_populated_file(populated_voices_json):
|
|
reg = VoiceRegistry.load(populated_voices_json)
|
|
assert reg.list_names() == ["wantsong"]
|
|
|
|
|
|
def test_get_returns_entry(populated_voices_json):
|
|
reg = VoiceRegistry.load(populated_voices_json)
|
|
entry = reg.get("wantsong")
|
|
assert entry.file_id == 396061597295017
|
|
assert entry.filename == "Wantsong人物录音.mp3"
|
|
assert entry.created_at == "2026-06-08T12:00:00Z"
|
|
|
|
|
|
def test_get_unknown_raises(populated_voices_json):
|
|
reg = VoiceRegistry.load(populated_voices_json)
|
|
with pytest.raises(VoiceNotFoundError) as exc:
|
|
reg.get("nope")
|
|
assert "nope" in str(exc.value)
|
|
|
|
|
|
def test_add_appends_and_persists(tmp_voices_json):
|
|
reg = VoiceRegistry.load(tmp_voices_json)
|
|
entry = VoiceEntry(
|
|
file_id=123,
|
|
filename="sample.mp3",
|
|
created_at="2026-06-09T00:00:00Z",
|
|
)
|
|
reg.add("alice", entry)
|
|
assert reg.has("alice")
|
|
assert reg.get("alice") is entry
|
|
|
|
reg.save()
|
|
reloaded = VoiceRegistry.load(tmp_voices_json)
|
|
assert reloaded.has("alice")
|
|
assert reloaded.get("alice").file_id == 123
|
|
|
|
|
|
def test_add_duplicate_raises(tmp_voices_json):
|
|
reg = VoiceRegistry.load(tmp_voices_json)
|
|
entry = VoiceEntry(
|
|
file_id=1, filename="a.mp3", created_at="2026-06-09T00:00:00Z"
|
|
)
|
|
reg.add("dup", entry)
|
|
with pytest.raises(ValueError, match="already exists"):
|
|
reg.add("dup", entry)
|
|
|
|
|
|
def test_remove_existing(tmp_voices_json):
|
|
reg = VoiceRegistry.load(tmp_voices_json)
|
|
reg.add("bob", VoiceEntry(file_id=2, filename="b.mp3", created_at="2026-06-09T00:00:00Z"))
|
|
reg.save()
|
|
|
|
reg2 = VoiceRegistry.load(tmp_voices_json)
|
|
reg2.remove("bob")
|
|
reg2.save()
|
|
|
|
reg3 = VoiceRegistry.load(tmp_voices_json)
|
|
assert not reg3.has("bob")
|
|
|
|
|
|
def test_remove_missing_raises(tmp_voices_json):
|
|
reg = VoiceRegistry.load(tmp_voices_json)
|
|
with pytest.raises(VoiceNotFoundError):
|
|
reg.remove("ghost")
|