Make browser TTS watchdog checks brace-aware
This commit is contained in:
@@ -5,6 +5,24 @@ import re
|
||||
REPO = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def _extract_function(src: str, name: str) -> str:
|
||||
anchor = f"function {name}("
|
||||
start = src.find(anchor)
|
||||
assert start != -1, f"{name}() must exist"
|
||||
body_start = src.find("{", start)
|
||||
assert body_start != -1, f"{name}() must have a body"
|
||||
depth = 1
|
||||
idx = body_start + 1
|
||||
while depth and idx < len(src):
|
||||
if src[idx] == "{":
|
||||
depth += 1
|
||||
elif src[idx] == "}":
|
||||
depth -= 1
|
||||
idx += 1
|
||||
assert depth == 0, f"{name}() body must balance braces"
|
||||
return src[start:idx]
|
||||
|
||||
|
||||
def test_boot_js_declares_browser_tts_recovery_helpers():
|
||||
src = (REPO / "static" / "boot.js").read_text(encoding="utf-8")
|
||||
assert "let _browserTtsKeepAlive=null;" in src
|
||||
@@ -16,9 +34,7 @@ def test_boot_js_declares_browser_tts_recovery_helpers():
|
||||
|
||||
def test_browser_tts_watchdog_rearms_listening_if_onend_drops():
|
||||
src = (REPO / "static" / "boot.js").read_text(encoding="utf-8")
|
||||
arm_idx = src.find("function _armBrowserTtsRecovery(clean, rate){")
|
||||
assert arm_idx != -1
|
||||
arm_body = src[arm_idx : arm_idx + 1400]
|
||||
arm_body = _extract_function(src, "_armBrowserTtsRecovery")
|
||||
assert "_browserTtsWatchdog=setTimeout" in arm_body
|
||||
assert "_voiceModeState!=='speaking'" in arm_body
|
||||
assert "_browserTtsSuppressNextErrorRearm=true;" in arm_body
|
||||
@@ -31,9 +47,8 @@ def test_browser_tts_watchdog_rearms_listening_if_onend_drops():
|
||||
|
||||
def test_browser_tts_callbacks_and_deactivate_clear_recovery_handles():
|
||||
src = (REPO / "static" / "boot.js").read_text(encoding="utf-8")
|
||||
speak_idx = src.find("const utter=new SpeechSynthesisUtterance(clean);")
|
||||
assert speak_idx != -1
|
||||
speak_body = src[speak_idx : speak_idx + 1200]
|
||||
speak_body = _extract_function(src, "_speakResponse")
|
||||
assert "const utter=new SpeechSynthesisUtterance(clean);" in speak_body
|
||||
assert "utter.onend=()=>{" in speak_body
|
||||
assert "utter.onerror=()=>{" in speak_body
|
||||
assert speak_body.count("_clearBrowserTtsRecovery();") >= 2, (
|
||||
@@ -44,13 +59,7 @@ def test_browser_tts_callbacks_and_deactivate_clear_recovery_handles():
|
||||
assert "if(_browserTtsSuppressNextErrorRearm){" in speak_body
|
||||
assert "_armBrowserTtsRecovery(clean, utter.rate);" in speak_body
|
||||
|
||||
deactivate_match = re.search(
|
||||
r"function _deactivate\(\)\{(.*?)\n \}",
|
||||
src,
|
||||
re.DOTALL,
|
||||
)
|
||||
assert deactivate_match, "_deactivate() must exist"
|
||||
deactivate_body = deactivate_match.group(1)
|
||||
deactivate_body = _extract_function(src, "_deactivate")
|
||||
assert "_clearBrowserTtsRecovery();" in deactivate_body, (
|
||||
"_deactivate() must clear browser TTS watchdog/keep-alive handles."
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user