80 lines
1.6 KiB
Python
80 lines
1.6 KiB
Python
import json
|
|
|
|
import pytest
|
|
|
|
from voice_assistant.agent.oci import extract_text_delta
|
|
|
|
|
|
def test_extract_text_delta():
|
|
payload = {
|
|
"index": 0,
|
|
"message": {
|
|
"role": "ASSISTANT",
|
|
"content": [
|
|
{
|
|
"type": "TEXT",
|
|
"text": "Olá",
|
|
}
|
|
],
|
|
},
|
|
}
|
|
|
|
result = extract_text_delta(json.dumps(payload))
|
|
|
|
assert result == "Olá"
|
|
|
|
|
|
def test_extract_text_delta_preserves_spaces():
|
|
payload = {
|
|
"message": {
|
|
"content": [
|
|
{
|
|
"type": "TEXT",
|
|
"text": " mundo",
|
|
}
|
|
],
|
|
},
|
|
}
|
|
|
|
assert extract_text_delta(json.dumps(payload)) == " mundo"
|
|
|
|
|
|
def test_extract_text_delta_ignores_empty_text():
|
|
payload = {
|
|
"message": {
|
|
"content": [
|
|
{
|
|
"type": "TEXT",
|
|
"text": "",
|
|
}
|
|
],
|
|
},
|
|
}
|
|
|
|
assert extract_text_delta(json.dumps(payload)) is None
|
|
|
|
|
|
def test_extract_text_delta_done():
|
|
assert extract_text_delta("[DONE]") is None
|
|
|
|
|
|
def test_extract_text_delta_rejects_invalid_json():
|
|
with pytest.raises(ValueError, match="Invalid JSON"):
|
|
extract_text_delta("{invalid")
|
|
|
|
|
|
def test_extract_text_delta_ignores_non_text_events():
|
|
payload = {
|
|
"message": {
|
|
"content": [
|
|
{
|
|
"type": "OTHER",
|
|
"text": "ignored",
|
|
}
|
|
],
|
|
},
|
|
}
|
|
|
|
assert extract_text_delta(json.dumps(payload)) is None
|
|
|