Add CallerIdentity dataclass and batch JSON parser
This commit is contained in:
@@ -0,0 +1,38 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Optional
|
||||||
|
import json
|
||||||
|
|
||||||
|
REQUIRED_FIELDS = {
|
||||||
|
"name", "age", "voice_suggestion", "location", "identity",
|
||||||
|
"situation", "reason_calling", "opening_line", "secret_want",
|
||||||
|
"specific_details", "emotional_register"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class CallerIdentity:
|
||||||
|
name: str
|
||||||
|
age: int
|
||||||
|
voice_suggestion: str
|
||||||
|
location: str
|
||||||
|
identity: str
|
||||||
|
situation: str
|
||||||
|
reason_calling: str
|
||||||
|
opening_line: str
|
||||||
|
secret_want: str
|
||||||
|
specific_details: list[str]
|
||||||
|
emotional_register: str
|
||||||
|
# set after voice validation
|
||||||
|
voice_resolved: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
|
def parse_batch_response(raw: str) -> list[CallerIdentity]:
|
||||||
|
data = json.loads(raw)
|
||||||
|
callers = data.get("callers", [])
|
||||||
|
result = []
|
||||||
|
for c in callers:
|
||||||
|
missing = REQUIRED_FIELDS - set(c.keys())
|
||||||
|
if missing:
|
||||||
|
raise ValueError(f"CallerIdentity missing fields: {missing}")
|
||||||
|
result.append(CallerIdentity(**{k: c[k] for k in REQUIRED_FIELDS}))
|
||||||
|
return result
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
import pytest
|
||||||
|
from backend.services.caller_gen import CallerIdentity, parse_batch_response
|
||||||
|
|
||||||
|
SAMPLE_JSON = """
|
||||||
|
{
|
||||||
|
"callers": [
|
||||||
|
{
|
||||||
|
"name": "Danny Ortega",
|
||||||
|
"age": 47,
|
||||||
|
"voice_suggestion": "Marcus",
|
||||||
|
"location": "Silver City, NM",
|
||||||
|
"identity": "A plumber who inherited his uncle's taxidermy shop...",
|
||||||
|
"situation": "He's been getting calls from people...",
|
||||||
|
"reason_calling": "Someone left a note in his mailbox tonight...",
|
||||||
|
"opening_line": "Luke, I need to ask you something weird.",
|
||||||
|
"secret_want": "Permission to just throw it all away",
|
||||||
|
"specific_details": ["the elk head in the basement", "the note said 'she forgot'", "his uncle's Rolodex"],
|
||||||
|
"emotional_register": "quietly unsettled, trying to sound casual"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_batch_response_returns_caller_list():
|
||||||
|
callers = parse_batch_response(SAMPLE_JSON)
|
||||||
|
assert len(callers) == 1
|
||||||
|
assert callers[0].name == "Danny Ortega"
|
||||||
|
assert callers[0].age == 47
|
||||||
|
assert "taxidermy" in callers[0].identity
|
||||||
|
assert len(callers[0].specific_details) == 3
|
||||||
|
|
||||||
|
|
||||||
|
def test_parse_batch_response_rejects_missing_fields():
|
||||||
|
bad = '{"callers": [{"name": "Jim"}]}'
|
||||||
|
with pytest.raises(ValueError, match="missing"):
|
||||||
|
parse_batch_response(bad)
|
||||||
Reference in New Issue
Block a user