askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_app_talk_object.py · 61.5 KB · 1455 lines
| 1 | """Tests for packages/app-shell's Talk surface (site_talk / /talk). |
| 2 | |
| 3 | Talk is a second projection of the same conversation as the shell: same |
| 4 | POST /api/ai/chat, same prefs/model/tools, same shell_commands history -- |
| 5 | just staged as voice instead of transcript. These tests mirror the |
| 6 | package/renderer testing conventions used in tests/test_app_views_package.py |
| 7 | and add coverage specific to Talk's conversation-mode mic loop and the |
| 8 | places shell.py was patched to stay in sync with it. |
| 9 | """ |
| 10 | |
| 11 | import importlib.util |
| 12 | import json |
| 13 | import re |
| 14 | import shutil |
| 15 | import subprocess |
| 16 | from pathlib import Path |
| 17 | |
| 18 | import object_execution |
| 19 | import object_packages |
| 20 | import object_permissions |
| 21 | import python_object_runtime |
| 22 | |
| 23 | PACKAGES_ROOT = Path(__file__).resolve().parents[1] / "packages" |
| 24 | APP_SHELL_DIR = PACKAGES_ROOT / "app-shell" |
| 25 | TALK_PATH = APP_SHELL_DIR / "objects" / "site" / "talk.py" |
| 26 | TALK_SOURCE = TALK_PATH.read_text() |
| 27 | SHELL_SOURCE = (APP_SHELL_DIR / "objects" / "site" / "shell.py").read_text() |
| 28 | SHELL_PREFS_SCHEMA = json.loads((APP_SHELL_DIR / "schemas" / "shell_preferences.json").read_text()) |
| 29 | |
| 30 | |
| 31 | def _render_talk_html(user_id="u1"): |
| 32 | """Import talk.py fresh and render its GET() body for a signed-in user, |
| 33 | the same way the object runtime would -- used by tests that need the |
| 34 | *actual evaluated* inline <script>, not just the Python source text.""" |
| 35 | spec = importlib.util.spec_from_file_location("site_talk_under_test", TALK_PATH) |
| 36 | module = importlib.util.module_from_spec(spec) |
| 37 | spec.loader.exec_module(module) |
| 38 | |
| 39 | class _NullLogger: |
| 40 | def info(self, *args, **kwargs): |
| 41 | pass |
| 42 | |
| 43 | module._logger = _NullLogger() |
| 44 | return module.GET({"_identity": {"user_id": user_id}})["body"] |
| 45 | |
| 46 | |
| 47 | def _extract_inline_script(html): |
| 48 | """Pull the content of the one bare `<script>...</script>` block (the |
| 49 | page's own inline script, as opposed to the `<script src=...>` tags).""" |
| 50 | blocks = re.findall(r"<script>(.*?)</script>", html, re.S) |
| 51 | assert blocks, "no inline <script> block found in rendered Talk HTML" |
| 52 | return blocks[0] |
| 53 | |
| 54 | |
| 55 | def test_get_package_lists_site_talk_alongside_site_shell(): |
| 56 | package = object_packages.get_package("app-shell", root=PACKAGES_ROOT) |
| 57 | |
| 58 | assert package["id"] == "app-shell" |
| 59 | assert {obj["id"] for obj in package["objects"]} == {"site_shell", "site_talk"} |
| 60 | ids_to_paths = {obj["id"]: obj["path"] for obj in package["objects"]} |
| 61 | assert ids_to_paths["site_talk"] == "objects/site/talk.py" |
| 62 | |
| 63 | |
| 64 | def test_dry_run_app_shell_package_is_safe(tmp_path): |
| 65 | object_root = tmp_path / "objects" |
| 66 | object_root.mkdir() |
| 67 | |
| 68 | plan = object_packages.dry_run_package( |
| 69 | "app-shell", |
| 70 | root=PACKAGES_ROOT, |
| 71 | base_dir=tmp_path / "data", |
| 72 | object_roots=[object_root], |
| 73 | ) |
| 74 | |
| 75 | assert plan["safe_to_install"] is True |
| 76 | assert plan["warnings"] == [] |
| 77 | |
| 78 | |
| 79 | def test_install_app_shell_package_writes_talk_object(tmp_path): |
| 80 | data_dir = tmp_path / "data" |
| 81 | object_root = tmp_path / "objects" |
| 82 | object_root.mkdir() |
| 83 | |
| 84 | object_packages.install_package( |
| 85 | "app-shell", |
| 86 | root=PACKAGES_ROOT, |
| 87 | base_dir=data_dir, |
| 88 | object_roots=[object_root], |
| 89 | ) |
| 90 | |
| 91 | assert (object_root / "site" / "talk.py").is_file() |
| 92 | # Convention routing (/talk -> site_talk, no site_routes record needed) |
| 93 | # is exactly what lets a page-object addition create its own URL, the |
| 94 | # same way /shell -> site_shell already works. |
| 95 | import object_site_routes |
| 96 | |
| 97 | assert object_site_routes.convention_object_id("/talk") == "site_talk" |
| 98 | assert object_site_routes.convention_object_id("/shell") == "site_shell" |
| 99 | |
| 100 | |
| 101 | def _app_shell_policy(): |
| 102 | payload = json.loads((APP_SHELL_DIR / "permissions" / "rules.json").read_text()) |
| 103 | return object_permissions.policy_from_dict({"access_mode": "role_based", "rules": payload["rules"]}) |
| 104 | |
| 105 | |
| 106 | def test_talk_page_execute_is_public_like_shell(): |
| 107 | policy = _app_shell_policy() |
| 108 | |
| 109 | for object_id in ("site_shell", "site_talk"): |
| 110 | decision = object_permissions.check_permission( |
| 111 | None, object_permissions.EXECUTE, policy=policy, object_id=object_id |
| 112 | ) |
| 113 | assert decision.allowed is True, object_id |
| 114 | |
| 115 | |
| 116 | def test_talk_object_serves_sign_in_prompt_when_anonymous(tmp_path): |
| 117 | data_dir = tmp_path / "data" |
| 118 | object_root = tmp_path / "objects" |
| 119 | object_root.mkdir() |
| 120 | object_packages.install_package( |
| 121 | "app-shell", root=PACKAGES_ROOT, base_dir=data_dir, object_roots=[object_root] |
| 122 | ) |
| 123 | |
| 124 | runtime = python_object_runtime.PythonObjectRuntime(base_dir=data_dir) |
| 125 | result = object_execution.execute_object( |
| 126 | runtime, |
| 127 | object_execution.ObjectExecutionRequest("site_talk", payload={"_identity": {}}), |
| 128 | roots=[object_root], |
| 129 | ) |
| 130 | |
| 131 | assert result.ok is True |
| 132 | body = result.result["body"] |
| 133 | assert result.result["content_type"] == "text/html; charset=utf-8" |
| 134 | assert "Sign in" in body |
| 135 | assert "/login?next=/talk" in body |
| 136 | |
| 137 | |
| 138 | def test_talk_object_serves_stage_scaffolding_when_signed_in(tmp_path): |
| 139 | data_dir = tmp_path / "data" |
| 140 | object_root = tmp_path / "objects" |
| 141 | object_root.mkdir() |
| 142 | object_packages.install_package( |
| 143 | "app-shell", root=PACKAGES_ROOT, base_dir=data_dir, object_roots=[object_root] |
| 144 | ) |
| 145 | |
| 146 | runtime = python_object_runtime.PythonObjectRuntime(base_dir=data_dir) |
| 147 | result = object_execution.execute_object( |
| 148 | runtime, |
| 149 | object_execution.ObjectExecutionRequest( |
| 150 | "site_talk", payload={"_identity": {"user_id": "u1"}} |
| 151 | ), |
| 152 | roots=[object_root], |
| 153 | ) |
| 154 | |
| 155 | assert result.ok is True |
| 156 | body = result.result["body"] |
| 157 | assert result.result["content_type"] == "text/html; charset=utf-8" |
| 158 | # Stage/caption/mic scaffolding. |
| 159 | assert 'id="stage"' in body |
| 160 | assert 'id="capUser"' in body and 'id="capAssistant"' in body |
| 161 | assert 'id="mic"' in body |
| 162 | assert 'id="prompt"' in body |
| 163 | assert 'href="/shell"' in body |
| 164 | # Same brain: it is the /api/ai/chat call, not a new endpoint. |
| 165 | assert "/api/ai/chat" in body |
| 166 | # History is written server-side now; the page carries its session id |
| 167 | # instead of a collections URL. |
| 168 | assert "session_id: SESSION_ID" in body |
| 169 | # The talk-mode system prompt marker, plus the base capabilities text |
| 170 | # copied verbatim from shell.py (proves the two are still in sync). |
| 171 | assert "You are in voice mode" in body |
| 172 | assert "NEVER read ids, urls, uuids, or paths aloud" in body |
| 173 | assert "MATERIALIZE PAGES" in body |
| 174 | assert "u1" in body |
| 175 | |
| 176 | |
| 177 | def test_talk_source_pauses_recognition_around_tts_playback(): |
| 178 | """The core anti-feedback-loop requirement: the mic must not hear the |
| 179 | machine talk. Assert the shape directly on source since it's an |
| 180 | interaction between async callbacks that's easiest to prove by |
| 181 | inspecting the wiring: speak() marks `speaking` and stops listening |
| 182 | before playback, both playback-completion paths clear it and resume, |
| 183 | and onend / startListening both respect the `speaking` flag. |
| 184 | """ |
| 185 | assert "speaking = true" in TALK_SOURCE |
| 186 | assert "stopListening();" in TALK_SOURCE |
| 187 | |
| 188 | speak_fn = re.search(r"async function speak\(text\) \{(.*?)\n\}\n", TALK_SOURCE, re.S) |
| 189 | assert speak_fn, "speak() not found in talk.py" |
| 190 | body = speak_fn.group(1) |
| 191 | # speaking flips true and recognition RELEASE IS AWAITED before either |
| 192 | # playback path -- not merely requested. iOS shares one audio session |
| 193 | # between mic and speaker, and recognizer.stop() is a request whose |
| 194 | # teardown completes asynchronously (onend); starting playback before |
| 195 | # that finishes can play into a still-recording session and come out |
| 196 | # silent, with no error anywhere -- Dan's own diagnosis of an iPad |
| 197 | # that spoke on desktop but not there, verbatim: "listening while |
| 198 | # talking, maybe not allowed." |
| 199 | assert body.index("speaking = true") < body.index('await player.play()') |
| 200 | assert body.index("await stopListeningAndWaitForRelease();") < body.index('await player.play()') |
| 201 | # Both the server-TTS path and the speechSynthesis fallback route |
| 202 | # playback-completion through the ONE shared stopSpeaking() (see |
| 203 | # test_talk_stop_speaking_is_shared_by_natural_end_and_interrupt for |
| 204 | # its actual cleanup behavior) rather than each duplicating |
| 205 | # `speaking = false; resumeListeningIfNeeded();` inline -- that |
| 206 | # duplication is exactly what would let a natural end and a |
| 207 | # deliberate interrupt (headphones mode) drift apart. |
| 208 | assert "player.onended = stopSpeaking;" in body |
| 209 | assert "player.onerror = stopSpeaking;" in body |
| 210 | assert "player.onstalled = stopSpeaking;" in body |
| 211 | assert "speaking = false;" in TALK_SOURCE |
| 212 | assert "resumeListeningIfNeeded();" in TALK_SOURCE |
| 213 | |
| 214 | # startListening() itself refuses to start while speaking, and the |
| 215 | # recognizer's own onend only auto-restarts when not speaking. |
| 216 | assert "if (!recognizer || listening || speaking) return;" in TALK_SOURCE |
| 217 | assert "if (conversationMode && !speaking) startListening();" in TALK_SOURCE |
| 218 | |
| 219 | |
| 220 | def test_talk_source_conversation_mode_auto_restarts_on_end(): |
| 221 | assert "conversationMode" in TALK_SOURCE |
| 222 | assert "recognizer.onend = () => {" in TALK_SOURCE |
| 223 | assert "resumeListeningIfNeeded" in TALK_SOURCE |
| 224 | |
| 225 | |
| 226 | def test_talk_stripforspeech_strips_urls_and_view_paths(): |
| 227 | fn = re.search(r"function stripForSpeech\(text\) \{(.*?)\n\}\n", TALK_SOURCE, re.S) |
| 228 | assert fn, "stripForSpeech() not found in talk.py" |
| 229 | body = fn.group(1) |
| 230 | assert "https?" in body |
| 231 | assert "/views" in body |
| 232 | |
| 233 | |
| 234 | def test_talk_base_capabilities_match_shell_verbatim(): |
| 235 | """The views MATERIALIZE PAGES block must be copied verbatim into |
| 236 | talk.py so the two prompts stay in sync, per the module docstring.""" |
| 237 | marker = "You can also MATERIALIZE PAGES" |
| 238 | assert marker in TALK_SOURCE |
| 239 | assert marker in SHELL_SOURCE |
| 240 | |
| 241 | talk_block = TALK_SOURCE[TALK_SOURCE.index(marker):TALK_SOURCE.index(marker) + 900] |
| 242 | shell_block = SHELL_SOURCE[SHELL_SOURCE.index(marker):SHELL_SOURCE.index(marker) + 900] |
| 243 | # Normalize away Python vs. JS string-literal concatenation syntax so |
| 244 | # this compares the actual prose, not the quoting style. |
| 245 | def normalize(s): |
| 246 | s = re.sub(r'["\'+]|\\+', "", s) |
| 247 | return re.sub(r"\s+", " ", s).strip() |
| 248 | |
| 249 | assert normalize(talk_block)[:400] == normalize(shell_block)[:400] |
| 250 | |
| 251 | |
| 252 | def test_talk_marker_instruction_has_worked_example(): |
| 253 | """Live use showed the model ignoring the [[view:<id>]] marker |
| 254 | instruction; a literal worked example was added to the shared |
| 255 | MATERIALIZE-PAGES block (in both files) to reinforce it.""" |
| 256 | example_id = "26b247ed-3b1a-4206-b060-1d92847194de" |
| 257 | expected = f'Example reply: "Here are your open tasks. [[view:{example_id}]]"' |
| 258 | |
| 259 | spec = importlib.util.spec_from_file_location("site_talk_marker_example", TALK_PATH) |
| 260 | module = importlib.util.module_from_spec(spec) |
| 261 | spec.loader.exec_module(module) |
| 262 | assert expected in module.TALK_SYSTEM |
| 263 | |
| 264 | assert f"[[view:{example_id}]]" in SHELL_SOURCE |
| 265 | assert "Example reply" in SHELL_SOURCE |
| 266 | |
| 267 | |
| 268 | def test_talk_marker_example_renders_verbatim_identical_in_shell_js(tmp_path): |
| 269 | """Behavioral verbatim-sync check for the appended example specifically |
| 270 | (the source-level verbatim test above only compares the first 400 |
| 271 | normalized chars of the block, which ends before the example): evaluate |
| 272 | shell.py's actual system-prompt JS expression under node and confirm it |
| 273 | contains the exact same example text as talk.py's TALK_SYSTEM.""" |
| 274 | node = shutil.which("node") |
| 275 | if not node: |
| 276 | return |
| 277 | |
| 278 | example_id = "26b247ed-3b1a-4206-b060-1d92847194de" |
| 279 | expected = f'Example reply: "Here are your open tasks. [[view:{example_id}]]"' |
| 280 | |
| 281 | spec = importlib.util.spec_from_file_location("site_shell_under_test", APP_SHELL_DIR / "objects" / "site" / "shell.py") |
| 282 | module = importlib.util.module_from_spec(spec) |
| 283 | |
| 284 | class _NullLogger: |
| 285 | def info(self, *args, **kwargs): |
| 286 | pass |
| 287 | |
| 288 | module._logger = _NullLogger() |
| 289 | spec.loader.exec_module(module) |
| 290 | |
| 291 | script = module._SCRIPT |
| 292 | start = script.index('system: "You are the shell') |
| 293 | end = script.index("});", start) |
| 294 | expr = script[start + len("system: "):end] |
| 295 | |
| 296 | probe_path = tmp_path / "shell_system_probe.js" |
| 297 | probe_path.write_text(f"const d = {expr};\nconsole.log(JSON.stringify(d));\n") |
| 298 | result = subprocess.run([node, str(probe_path)], capture_output=True, text=True) |
| 299 | assert result.returncode == 0, f"node probe failed:\n{result.stderr}" |
| 300 | shell_system = json.loads(result.stdout) |
| 301 | assert expected in shell_system |
| 302 | |
| 303 | |
| 304 | def test_talk_falls_back_to_tool_calls_for_the_view_path(): |
| 305 | assert "viewPathFromToolCalls" in TALK_SOURCE |
| 306 | assert "create_record" in TALK_SOURCE |
| 307 | assert "update_record" in TALK_SOURCE |
| 308 | assert 'args.collection !== "views"' in TALK_SOURCE |
| 309 | |
| 310 | |
| 311 | # --- fresh model context per session (no shell_commands replay) ---------- |
| 312 | # |
| 313 | # Root cause of the model ignoring the [[view:<id>]] marker in live use: |
| 314 | # Talk used to replay up to 1000 old shell_commands rows into aiHistory on |
| 315 | # every page load, and every historical turn recorded before the marker |
| 316 | # convention existed demonstrated the pre-marker habit -- in-context |
| 317 | # examples override instructions no matter how the system prompt is |
| 318 | # reworded. Talk must start each session with an empty aiHistory and only |
| 319 | # ever grow it from turns submitted in *this* session; shell.py's own |
| 320 | # replay (which drives its visible transcript, not just model context) is |
| 321 | # unaffected. |
| 322 | |
| 323 | |
| 324 | def test_talk_never_auto_resumes_a_session(): |
| 325 | """Talk reads shell_commands now -- but only to BUILD the session |
| 326 | picker. Restoring old turns into model context is a hand action on |
| 327 | the picker, never an automatic on load: a voice page that silently |
| 328 | reloads twenty turns changes what the next answer means without the |
| 329 | user hearing anything change. (The shell auto-resumes inside a |
| 330 | 30-minute window; eyes can see a restored transcript, ears cannot.)""" |
| 331 | assert "loadHistory" not in TALK_SOURCE |
| 332 | assert "let aiHistory = [];" in TALK_SOURCE |
| 333 | load = TALK_SOURCE[TALK_SOURCE.index("async function loadSessions"):] |
| 334 | load = load[:load.index("loadSessions();")] |
| 335 | before_handler = load[:load.index('addEventListener("change"')] |
| 336 | assert "aiHistory.push" not in before_handler |
| 337 | # Turn WRITES are the server's inside /api/ai/chat; the page only |
| 338 | # reads, to fold the picker, and declares its session on each call. |
| 339 | assert "session_id: SESSION_ID" in TALK_SOURCE |
| 340 | |
| 341 | # aiHistory grows in exactly two places: submitTurn (the turn just |
| 342 | # made) and the picker's change handler (an explicit resume). |
| 343 | submit_turn = re.search(r"async function submitTurn\(input\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 344 | assert submit_turn, "submitTurn() not found in talk.py" |
| 345 | push_sites = [m.start() for m in re.finditer(r"aiHistory\.push\(", TALK_SOURCE)] |
| 346 | assert len(push_sites) == 4 # 2 in submitTurn, 2 in the resume handler |
| 347 | handler_at = TALK_SOURCE.index('addEventListener("change"') |
| 348 | for site in push_sites: |
| 349 | inside_submit = submit_turn.start() < site < submit_turn.end() |
| 350 | inside_resume = site > handler_at |
| 351 | assert inside_submit or inside_resume, site |
| 352 | |
| 353 | |
| 354 | def test_talk_object_no_longer_calls_loadhistory_on_page_load(): |
| 355 | html = _render_talk_html() |
| 356 | script = _extract_inline_script(html) |
| 357 | assert "loadHistory" not in script |
| 358 | # initMic() must run AFTER loadPrefs() resolves, not as two independent |
| 359 | # standalone calls -- see test_talk_init_order_loads_prefs_before_deciding_mic_path |
| 360 | # for why the ordering itself is load-bearing, not just style. |
| 361 | assert "loadPrefs().then(() => { initMic(); initCloudVoiceToggle(); initHeadphonesToggle(); });" in script |
| 362 | |
| 363 | |
| 364 | def test_shell_replay_into_model_context_is_unaffected(): |
| 365 | """shell.py's replay behavior stays as-is -- it drives the visible |
| 366 | transcript on load (not just aiHistory), so it is unrelated to the |
| 367 | Talk-specific in-context-examples problem.""" |
| 368 | assert "async function loadHistory()" in SHELL_SOURCE |
| 369 | assert "shell_commands/records?limit=1000" in SHELL_SOURCE |
| 370 | assert "aiHistory.push" in SHELL_SOURCE |
| 371 | assert "loadHistory();" in SHELL_SOURCE |
| 372 | |
| 373 | |
| 374 | # --- shell.py patches (linkify + embed, extended stripForSpeech) --------- |
| 375 | |
| 376 | |
| 377 | def test_shell_finish_linkifies_and_embeds_views_paths(): |
| 378 | assert "function linkifyViews(html)" in SHELL_SOURCE |
| 379 | assert "VIEWS_PATH_RE" in SHELL_SOURCE |
| 380 | assert "viewembed" in SHELL_SOURCE |
| 381 | assert 'target="_blank" rel="noopener">open ↗' in SHELL_SOURCE |
| 382 | |
| 383 | finish_fn = re.search(r"function finish\(out, text,.*?\n\}\n", SHELL_SOURCE, re.S) |
| 384 | assert finish_fn, "finish() not found in shell.py" |
| 385 | body = finish_fn.group(0) |
| 386 | assert "linkifyViews(" in body |
| 387 | assert "insertAdjacentElement(\"afterend\", embed)" in body |
| 388 | |
| 389 | |
| 390 | def test_shell_stripforspeech_strips_urls_and_view_paths(): |
| 391 | fn = re.search(r"function stripForSpeech\(text\) \{(.*?)\n\}\n", SHELL_SOURCE, re.S) |
| 392 | assert fn, "stripForSpeech() not found in shell.py" |
| 393 | body = fn.group(1) |
| 394 | assert "https?" in body |
| 395 | assert "/views" in body |
| 396 | |
| 397 | |
| 398 | # --- bug fixes: placeholder escaping + the dead submit path -------------- |
| 399 | # |
| 400 | # The placeholder bug was a context mismatch: `…` is a JS/Python |
| 401 | # unicode escape, but the placeholder lives in plain HTML markup (the |
| 402 | # `body` string), not inside the `<script>` tag -- so the browser never |
| 403 | # gets a chance to interpret it and shows the six literal characters |
| 404 | # `…` instead of an ellipsis. `node --check` on the *actual evaluated* |
| 405 | # script is the strongest guard against this whole class of Python-string/ |
| 406 | # JS-string escaping fault recurring; it is skipped (not failed) when node |
| 407 | # isn't on PATH, falling back to a source assertion that the placeholder |
| 408 | # is clean. |
| 409 | |
| 410 | |
| 411 | def test_talk_placeholder_has_no_backslash_escape(): |
| 412 | assert 'placeholder="or type...' in TALK_SOURCE |
| 413 | assert "or type\\u2026" not in TALK_SOURCE |
| 414 | assert "or type\\\\u2026" not in TALK_SOURCE |
| 415 | |
| 416 | html = _render_talk_html() |
| 417 | assert 'placeholder="or type...' in html |
| 418 | input_tag = re.search(r'<input name="line"[^>]*>', html) |
| 419 | assert input_tag, "line input not found in rendered Talk HTML" |
| 420 | assert "\\u2026" not in input_tag.group(0) |
| 421 | assert "\\" not in input_tag.group(0) |
| 422 | |
| 423 | |
| 424 | def test_talk_evaluated_script_passes_node_check(tmp_path): |
| 425 | html = _render_talk_html() |
| 426 | script = _extract_inline_script(html) |
| 427 | |
| 428 | node = shutil.which("node") |
| 429 | if not node: |
| 430 | # No node on PATH in this environment -- fall back to the source |
| 431 | # assertion above, which is the part node --check would have caught |
| 432 | # for this specific bug class anyway. |
| 433 | assert 'placeholder="or type...' in html |
| 434 | return |
| 435 | |
| 436 | script_path = tmp_path / "talk_inline.js" |
| 437 | script_path.write_text(script) |
| 438 | result = subprocess.run( |
| 439 | [node, "--check", str(script_path)], capture_output=True, text=True |
| 440 | ) |
| 441 | assert result.returncode == 0, f"node --check failed:\n{result.stderr}" |
| 442 | |
| 443 | |
| 444 | def test_talk_submit_handler_reads_line_input_and_calls_submitturn(): |
| 445 | """The dead-send-button symptom: verify the actual wiring end to end -- |
| 446 | the form's submit listener is attached (not just present as dead code), |
| 447 | it prevents default navigation, reads the named `line` input, and always |
| 448 | calls submitTurn() so voice and typed input share one code path.""" |
| 449 | handler = re.search( |
| 450 | r'document\.getElementById\("prompt"\)\.addEventListener\("submit", \(event\) => \{(.*?)\n\}\);', |
| 451 | TALK_SOURCE, |
| 452 | re.S, |
| 453 | ) |
| 454 | assert handler, "submit listener not found in talk.py" |
| 455 | body = handler.group(1) |
| 456 | assert "event.preventDefault();" in body |
| 457 | assert 'event.target.elements["line"]' in body |
| 458 | assert "submitTurn(input)" in body |
| 459 | # Manual submit folds in the voice buffer regardless of protocol state. |
| 460 | assert "pendingText()" in body |
| 461 | assert "resetTalkState();" in body |
| 462 | |
| 463 | |
| 464 | def test_talk_recognition_final_results_feed_the_same_submit_function(): |
| 465 | onresult = re.search(r"recognizer\.onresult = \(event\) => \{(.*?)\n \};", TALK_SOURCE, re.S) |
| 466 | assert onresult, "recognizer.onresult not found in talk.py" |
| 467 | body = onresult.group(1) |
| 468 | assert "processTranscript(isFinal)" in body |
| 469 | # processTranscript() is the one funnel that can call submitTurn (via |
| 470 | # finalizeVoiceSubmit) -- both the mic path and the form path resolve |
| 471 | # to the same submitTurn() used for /api/ai/chat. |
| 472 | assert "async function submitTurn(input)" in TALK_SOURCE |
| 473 | assert "finalizeVoiceSubmit" in TALK_SOURCE |
| 474 | finalize = re.search(r"function finalizeVoiceSubmit\(text\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 475 | assert finalize |
| 476 | assert "submitTurn(clean)" in finalize.group(1) |
| 477 | |
| 478 | |
| 479 | def test_talk_prefs_reads_go_through_pref_helper_not_raw_field_access(): |
| 480 | """A shell_preferences record written before a field existed in the |
| 481 | schema won't have that key; `prefs.tools.split(",")` on undefined would |
| 482 | throw and silently kill the turn (send button/voice submit both call |
| 483 | submitTurn). pref() with a fallback closes that hole.""" |
| 484 | submit_turn = re.search(r"async function submitTurn\(input\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 485 | assert submit_turn, "submitTurn() not found in talk.py" |
| 486 | body = submit_turn.group(1) |
| 487 | assert "prefs.tools.split" not in body |
| 488 | assert 'pref("tools", DEFAULT_TOOLS)' in body |
| 489 | assert 'pref("ai_model", DEFAULT_MODEL)' in body |
| 490 | |
| 491 | |
| 492 | # --- radio protocol: wake word / end word / VAD endpointing -------------- |
| 493 | |
| 494 | |
| 495 | def test_talk_schema_bumped_with_radio_protocol_fields(): |
| 496 | assert SHELL_PREFS_SCHEMA["version"] == 7 # +talk_tts_engine, +talk_stt_engine, +talk_headphones_mode |
| 497 | |
| 498 | fields = {f["name"]: f for f in SHELL_PREFS_SCHEMA["fields"]} |
| 499 | assert fields["talk_wake_word"]["default"] == "computer" |
| 500 | assert fields["talk_end_word"]["default"] == "over" |
| 501 | assert fields["talk_endpoint"]["type"] == "enum" |
| 502 | assert fields["talk_endpoint"]["default"] == "silence" |
| 503 | assert set(fields["talk_endpoint"]["enum"]) == {"silence", "word", "manual"} |
| 504 | assert fields["talk_silence_ms"]["default"] == "1400" |
| 505 | |
| 506 | # The defaults are mirrored client-side so a first-ever load (before |
| 507 | # loadPrefs() resolves) already behaves per spec. |
| 508 | assert 'talk_wake_word: "computer"' in TALK_SOURCE |
| 509 | assert 'talk_end_word: "over"' in TALK_SOURCE |
| 510 | assert 'talk_endpoint: "silence"' in TALK_SOURCE |
| 511 | assert 'talk_silence_ms: "1400"' in TALK_SOURCE |
| 512 | |
| 513 | |
| 514 | def test_talk_cloud_voice_prefs_default_off(): |
| 515 | fields = {f["name"]: f for f in SHELL_PREFS_SCHEMA["fields"]} |
| 516 | # Both default to the free, local, zero-behavior-change option -- a |
| 517 | # user who never opens settings must see EXACTLY today's behavior. |
| 518 | assert fields["talk_tts_engine"]["type"] == "enum" |
| 519 | assert fields["talk_tts_engine"]["default"] == "local" |
| 520 | assert set(fields["talk_tts_engine"]["enum"]) == {"local", "openai"} |
| 521 | assert fields["talk_stt_engine"]["type"] == "enum" |
| 522 | assert fields["talk_stt_engine"]["default"] == "browser" |
| 523 | assert set(fields["talk_stt_engine"]["enum"]) == {"browser", "openai"} |
| 524 | |
| 525 | assert 'talk_tts_engine: "local"' in TALK_SOURCE |
| 526 | assert 'talk_stt_engine: "browser"' in TALK_SOURCE |
| 527 | |
| 528 | # The settings form must actually surface both, or they're invisible. |
| 529 | default_form_fields = SHELL_PREFS_SCHEMA["forms"]["default"]["fields"] |
| 530 | assert "talk_tts_engine" in default_form_fields |
| 531 | assert "talk_stt_engine" in default_form_fields |
| 532 | |
| 533 | |
| 534 | def test_talk_cloud_voice_url_override(): |
| 535 | # "/talk?stt=openai" (or ?tts=openai) tries cloud voice for one page |
| 536 | # load without touching settings or persisting anything -- both reader |
| 537 | # functions must consult the URL before falling back to the stored |
| 538 | # preference. |
| 539 | override = re.search(r"function urlOverride\(param, allowed\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 540 | assert override, "urlOverride() not found in talk.py" |
| 541 | assert "URLSearchParams(location.search)" in override.group(1) |
| 542 | |
| 543 | tts_fn = re.search(r"function talkTtsEngine\(\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 544 | assert tts_fn and 'urlOverride("tts", ["local", "openai"])' in tts_fn.group(1) |
| 545 | |
| 546 | stt_fn = re.search(r"function talkSttEngine\(\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 547 | assert stt_fn and 'urlOverride("stt", ["browser", "openai"])' in stt_fn.group(1) |
| 548 | |
| 549 | |
| 550 | def test_talk_auto_mode_reliably_reaches_openai_engine(tmp_path): |
| 551 | """Behavioral, not source-string: a real live test hit exactly this -- |
| 552 | "?tts=openai" set, still heard an espeak voice -- because "auto" mode |
| 553 | used to prefer ANY browser-native speechSynthesis voice over the |
| 554 | server unconditionally, before ever consulting the engine choice. |
| 555 | Executes the actual talkTtsMode/talkTtsEngine/urlOverride functions |
| 556 | under node with a handful of real scenarios, not a string match. |
| 557 | """ |
| 558 | node = shutil.which("node") |
| 559 | if not node: |
| 560 | return |
| 561 | |
| 562 | # Pull each function verbatim (brace-counted scan to its own closing |
| 563 | # brace) rather than hand-copying logic into the test -- a future edit |
| 564 | # to talk.py is what this test must catch, not a frozen duplicate of it. |
| 565 | def extract_function(name): |
| 566 | start = TALK_SOURCE.index(f"function {name}(") |
| 567 | depth = 0 |
| 568 | i = TALK_SOURCE.index("{", start) |
| 569 | for j in range(i, len(TALK_SOURCE)): |
| 570 | if TALK_SOURCE[j] == "{": |
| 571 | depth += 1 |
| 572 | elif TALK_SOURCE[j] == "}": |
| 573 | depth -= 1 |
| 574 | if depth == 0: |
| 575 | return TALK_SOURCE[start : j + 1] |
| 576 | raise AssertionError(f"unbalanced braces extracting {name}()") |
| 577 | |
| 578 | functions = "\n".join( |
| 579 | extract_function(name) for name in |
| 580 | ("urlOverride", "talkTtsEngine", "talkSttEngine", "talkTtsMode") |
| 581 | ) |
| 582 | |
| 583 | probe = f""" |
| 584 | const DEFAULT_TALK_TTS = "auto"; |
| 585 | const DEFAULT_TALK_TTS_ENGINE = "local"; |
| 586 | const DEFAULT_TALK_STT_ENGINE = "browser"; |
| 587 | let prefs = {{}}; |
| 588 | let location = {{search: ""}}; |
| 589 | function pref(name, fallback) {{ |
| 590 | const v = prefs[name]; |
| 591 | return v === undefined || v === null ? fallback : v; |
| 592 | }} |
| 593 | {functions} |
| 594 | |
| 595 | const results = []; |
| 596 | // 1. Nobody touched anything: unchanged "auto" behavior (browser wins). |
| 597 | prefs = {{}}; location = {{search: ""}}; |
| 598 | results.push(["default", talkTtsMode(), talkTtsEngine()]); |
| 599 | |
| 600 | // 2. The persisted-setting case: talk_tts_engine=openai, mode left at |
| 601 | // auto -- must now resolve to "server", not silently stay "auto". |
| 602 | prefs = {{talk_tts_engine: "openai"}}; location = {{search: ""}}; |
| 603 | results.push(["persisted-openai", talkTtsMode(), talkTtsEngine()]); |
| 604 | |
| 605 | // 3. The exact bug reported live: no persisted settings, only a |
| 606 | // "?tts=openai" URL override -- must also resolve to "server". |
| 607 | prefs = {{}}; location = {{search: "?tts=openai"}}; |
| 608 | results.push(["url-override", talkTtsMode(), talkTtsEngine()]); |
| 609 | |
| 610 | // 4. Explicit "browser" mode is never overridden by engine choice -- |
| 611 | // it is an unconditional "always the browser's own voice" setting. |
| 612 | prefs = {{talk_tts: "browser", talk_tts_engine: "openai"}}; location = {{search: ""}}; |
| 613 | results.push(["explicit-browser-mode", talkTtsMode(), talkTtsEngine()]); |
| 614 | |
| 615 | console.log(JSON.stringify(results)); |
| 616 | """ |
| 617 | probe_path = tmp_path / "talk_tts_mode_probe.js" |
| 618 | probe_path.write_text(probe) |
| 619 | result = subprocess.run([node, str(probe_path)], capture_output=True, text=True) |
| 620 | assert result.returncode == 0, f"node probe failed:\n{result.stderr}" |
| 621 | outcomes = dict((row[0], (row[1], row[2])) for row in json.loads(result.stdout)) |
| 622 | |
| 623 | assert outcomes["default"] == ("auto", "local") |
| 624 | assert outcomes["persisted-openai"] == ("server", "openai") |
| 625 | assert outcomes["url-override"] == ("server", "openai") |
| 626 | assert outcomes["explicit-browser-mode"] == ("browser", "openai") |
| 627 | |
| 628 | |
| 629 | def test_reader_context_note_grounds_followups_in_real_links(tmp_path): |
| 630 | """Behavioral: a real live session hit exactly this -- read_page |
| 631 | fetched Techmeme, the model summarized it in prose with no link |
| 632 | numbers, and the next turn ("go to that story about Astra") had no |
| 633 | real href to resolve against because only the model's own prose |
| 634 | survives in conversation history, never a tool's actual result. |
| 635 | readerContextNote() re-grounds the NEXT turn in the real (capped) |
| 636 | link list from object_ai.py's new tool_calls[].result echo. |
| 637 | """ |
| 638 | node = shutil.which("node") |
| 639 | if not node: |
| 640 | return |
| 641 | |
| 642 | match = re.search(r"function readerContextNote\(\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 643 | assert match, "readerContextNote() not found in talk.py" |
| 644 | |
| 645 | probe = f""" |
| 646 | let lastReaderPage = null; |
| 647 | function readerContextNote() {{{match.group(1)}\n}} |
| 648 | |
| 649 | const results = {{}}; |
| 650 | results.noPage = readerContextNote(); |
| 651 | |
| 652 | lastReaderPage = {{ |
| 653 | title: "Techmeme", final_url: "https://techmeme.com", |
| 654 | links: [ |
| 655 | {{n: 1, label: "OpenAI announces Astra", href: "https://example.com/astra"}}, |
| 656 | {{n: 2, label: "Other story", href: "https://example.com/other"}}, |
| 657 | ], |
| 658 | }}; |
| 659 | results.withPage = readerContextNote(); |
| 660 | |
| 661 | lastReaderPage = {{title: "Empty page", final_url: "https://example.com", links: []}}; |
| 662 | results.noLinks = readerContextNote(); |
| 663 | |
| 664 | console.log(JSON.stringify(results)); |
| 665 | """ |
| 666 | probe_path = tmp_path / "reader_context_note_probe.js" |
| 667 | probe_path.write_text(probe) |
| 668 | result = subprocess.run([node, str(probe_path)], capture_output=True, text=True) |
| 669 | assert result.returncode == 0, f"node probe failed:\n{result.stderr}" |
| 670 | outcomes = json.loads(result.stdout) |
| 671 | |
| 672 | assert outcomes["noPage"] == "" |
| 673 | assert outcomes["noLinks"] == "" |
| 674 | assert "Techmeme" in outcomes["withPage"] |
| 675 | assert "https://techmeme.com" in outcomes["withPage"] |
| 676 | assert "OpenAI announces Astra" in outcomes["withPage"] |
| 677 | assert "https://example.com/astra" in outcomes["withPage"] |
| 678 | assert "Other story" in outcomes["withPage"] |
| 679 | |
| 680 | |
| 681 | def test_talk_captures_reader_result_and_resets_it_with_session(): |
| 682 | # The capture: a read_page tool call's echoed result (object_ai.py's |
| 683 | # new tool_calls[].result) becomes lastReaderPage for the NEXT turn. |
| 684 | assert ( |
| 685 | 'const readerCall = (body.tool_calls || []).find((c) => c.name === "read_page" && c.result);' |
| 686 | in TALK_SOURCE |
| 687 | ) |
| 688 | assert "if (readerCall) lastReaderPage = readerCall.result;" in TALK_SOURCE |
| 689 | |
| 690 | # The reset: switching sessions must not carry a PREVIOUS session's |
| 691 | # read page into a different conversation's context. |
| 692 | reset_sites = [ |
| 693 | m.start() for m in re.finditer(r"aiHistory = \[\];", TALK_SOURCE) |
| 694 | ] |
| 695 | # One is the top-level declaration-adjacent initial value; the other |
| 696 | # two are the "new session" / "resume a session" handlers, and both |
| 697 | # must reset lastReaderPage right alongside aiHistory. |
| 698 | assert len(reset_sites) >= 2 |
| 699 | for site in reset_sites: |
| 700 | nearby = TALK_SOURCE[site : site + 120] |
| 701 | if TALK_SOURCE[max(0, site - 4) : site] == "let ": |
| 702 | continue # the initial declaration, not a reset handler |
| 703 | assert "lastReaderPage = null;" in nearby, ( |
| 704 | f"an aiHistory reset near offset {site} does not also reset lastReaderPage" |
| 705 | ) |
| 706 | |
| 707 | |
| 708 | def test_talk_page_has_cloud_voice_toggle_button(): |
| 709 | assert '<button type="button" id="cloudvoice" class="backlink" hidden>cloud voice: off</button>' in TALK_SOURCE |
| 710 | assert "#cloudvoice, #headphonestoggle { background: none; border: none;" in TALK_SOURCE |
| 711 | |
| 712 | |
| 713 | def test_talk_init_order_loads_prefs_before_deciding_mic_path(): |
| 714 | # The real bug this guards: initMic() decides browser-vs-cloud ONCE, |
| 715 | # synchronously, at load -- calling it before loadPrefs() resolves |
| 716 | # means it always saw the shipped defaults, never the real stored |
| 717 | # talk_stt_engine, so settings-based cloud STT could never activate |
| 718 | # (only a ?stt= URL override, which doesn't depend on prefs, ever |
| 719 | # could). initCloudVoiceToggle() has the identical dependency for its |
| 720 | # initial label. |
| 721 | assert "loadPrefs().then(() => { initMic(); initCloudVoiceToggle(); initHeadphonesToggle(); });" in TALK_SOURCE |
| 722 | assert re.search(r"^initMic\(\);\s*$", TALK_SOURCE, re.M) is None, ( |
| 723 | "initMic() must not be called standalone before loadPrefs() resolves" |
| 724 | ) |
| 725 | |
| 726 | |
| 727 | def test_talk_cloud_voice_toggle_resolves_and_saves_both_engines_together(tmp_path): |
| 728 | """Behavioral: executes the real resolvedCloudVoiceOn()/initCloudVoiceToggle() |
| 729 | functions under node with a fake DOM/fetch, not a source-text match -- |
| 730 | clicking the toggle must flip BOTH talk_tts_engine and talk_stt_engine |
| 731 | together (the whole point is one switch for "this mode", not two |
| 732 | settings to remember), persist them via the real savePrefs()/api() |
| 733 | call shape, and land on a query-string-free URL afterward so a stale |
| 734 | ?stt=/?tts= link can never look like the toggle silently did nothing. |
| 735 | """ |
| 736 | node = shutil.which("node") |
| 737 | if not node: |
| 738 | return |
| 739 | |
| 740 | def extract_function(name): |
| 741 | start = TALK_SOURCE.index(f"function {name}(") |
| 742 | depth = 0 |
| 743 | i = TALK_SOURCE.index("{", start) |
| 744 | for j in range(i, len(TALK_SOURCE)): |
| 745 | if TALK_SOURCE[j] == "{": |
| 746 | depth += 1 |
| 747 | elif TALK_SOURCE[j] == "}": |
| 748 | depth -= 1 |
| 749 | if depth == 0: |
| 750 | return TALK_SOURCE[start : j + 1] |
| 751 | raise AssertionError(f"unbalanced braces extracting {name}()") |
| 752 | |
| 753 | def extract_async_function(name): |
| 754 | start = TALK_SOURCE.index(f"async function {name}(") |
| 755 | depth = 0 |
| 756 | i = TALK_SOURCE.index("{", start) |
| 757 | for j in range(i, len(TALK_SOURCE)): |
| 758 | if TALK_SOURCE[j] == "{": |
| 759 | depth += 1 |
| 760 | elif TALK_SOURCE[j] == "}": |
| 761 | depth -= 1 |
| 762 | if depth == 0: |
| 763 | return TALK_SOURCE[start : j + 1] |
| 764 | raise AssertionError(f"unbalanced braces extracting {name}()") |
| 765 | |
| 766 | functions = "\n".join([ |
| 767 | extract_function("urlOverride"), |
| 768 | extract_function("talkTtsEngine"), |
| 769 | extract_function("talkSttEngine"), |
| 770 | extract_function("resolvedCloudVoiceOn"), |
| 771 | extract_async_function("api"), |
| 772 | extract_async_function("savePrefs"), |
| 773 | extract_function("initCloudVoiceToggle"), |
| 774 | ]) |
| 775 | |
| 776 | probe = f""" |
| 777 | const DEFAULT_TALK_TTS_ENGINE = "local"; |
| 778 | const DEFAULT_TALK_STT_ENGINE = "browser"; |
| 779 | const OWNER_ID = "dan"; |
| 780 | let prefs = {{talk_tts_engine: "local", talk_stt_engine: "browser"}}; |
| 781 | let location = {{search: "", pathname: "/talk", href: "/talk"}}; |
| 782 | function pref(name, fallback) {{ |
| 783 | const v = prefs[name]; |
| 784 | return v === undefined || v === null ? fallback : v; |
| 785 | }} |
| 786 | |
| 787 | const putCalls = []; |
| 788 | function fetch(path, opts) {{ |
| 789 | const body = opts.body ? JSON.parse(opts.body) : undefined; |
| 790 | putCalls.push({{method: opts.method, path, body}}); |
| 791 | return Promise.resolve({{ok: true, json: () => Promise.resolve({{status: "ok"}})}}); |
| 792 | }} |
| 793 | |
| 794 | class FakeButton {{ |
| 795 | constructor() {{ this.hidden = true; this.textContent = ""; this.disabled = false; this._handlers = {{}}; }} |
| 796 | addEventListener(name, fn) {{ this._handlers[name] = fn; }} |
| 797 | click() {{ return this._handlers["click"](); }} |
| 798 | }} |
| 799 | const btn = new FakeButton(); |
| 800 | const document = {{getElementById: (id) => id === "cloudvoice" ? btn : null}}; |
| 801 | |
| 802 | {functions} |
| 803 | |
| 804 | const results = {{}}; |
| 805 | initCloudVoiceToggle(); |
| 806 | results.initialHidden = btn.hidden; |
| 807 | results.initialText = btn.textContent; |
| 808 | |
| 809 | (async () => {{ |
| 810 | await btn.click(); |
| 811 | results.afterClickText = btn.textContent; |
| 812 | results.putCalls = putCalls; |
| 813 | results.finalHref = location.href; |
| 814 | results.finalPrefs = prefs; |
| 815 | console.log(JSON.stringify(results)); |
| 816 | }})(); |
| 817 | """ |
| 818 | probe_path = tmp_path / "talk_cloud_toggle_probe.js" |
| 819 | probe_path.write_text(probe) |
| 820 | result = subprocess.run([node, str(probe_path)], capture_output=True, text=True) |
| 821 | assert result.returncode == 0, f"node probe failed:\n{result.stderr}" |
| 822 | outcome = json.loads(result.stdout) |
| 823 | |
| 824 | assert outcome["initialHidden"] is False |
| 825 | assert outcome["initialText"] == "cloud voice: off" |
| 826 | |
| 827 | put_calls = outcome["putCalls"] |
| 828 | assert len(put_calls) == 1 |
| 829 | call = put_calls[0] |
| 830 | assert call["method"] == "PUT" |
| 831 | assert call["path"] == "/collections/shell_preferences/records/dan" |
| 832 | # Both engines flip TOGETHER in one call -- not two separate saves a |
| 833 | # user could half-apply. |
| 834 | assert call["body"] == {"talk_tts_engine": "openai", "talk_stt_engine": "openai"} |
| 835 | |
| 836 | assert outcome["finalPrefs"]["talk_tts_engine"] == "openai" |
| 837 | assert outcome["finalPrefs"]["talk_stt_engine"] == "openai" |
| 838 | # Lands on a clean, query-string-free URL -- no leftover ?tts=/?stt= |
| 839 | # that could make the freshly-saved preference look inert. |
| 840 | assert outcome["finalHref"] == "/talk" |
| 841 | |
| 842 | |
| 843 | def test_talk_headphones_mode_pref_defaults_off(): |
| 844 | fields = {f["name"]: f for f in SHELL_PREFS_SCHEMA["fields"]} |
| 845 | assert fields["talk_headphones_mode"]["type"] == "boolean" |
| 846 | assert fields["talk_headphones_mode"]["default"] == "false" |
| 847 | default_form_fields = SHELL_PREFS_SCHEMA["forms"]["default"]["fields"] |
| 848 | assert "talk_headphones_mode" in default_form_fields |
| 849 | |
| 850 | assert 'function talkHeadphonesMode() {' in TALK_SOURCE |
| 851 | assert 'String(pref("talk_headphones_mode", "false")).trim() === "true"' in TALK_SOURCE |
| 852 | |
| 853 | |
| 854 | def test_talk_headphones_mode_wired_into_cloud_mic_click_handler(): |
| 855 | fn = re.search(r"function initCloudMic\(mic\) \{(.*?)\n\}\n", TALK_SOURCE, re.S) |
| 856 | assert fn, "initCloudMic() not found in talk.py" |
| 857 | body = fn.group(1) |
| 858 | assert "if (talkHeadphonesMode()) {" in body |
| 859 | assert "if (headphonesArmed) stopHeadphonesMode(mic); else await startHeadphonesMode(mic);" in body |
| 860 | |
| 861 | |
| 862 | def test_talk_headphones_mode_interrupts_speech_not_just_ignores_it(): |
| 863 | """The one genuinely new behavior versus push-to-talk: real speech |
| 864 | detected WHILE the assistant is talking must interrupt it (safe only |
| 865 | because headphones mean the mic can't be hearing the assistant's own |
| 866 | voice), not be silently dropped or queued.""" |
| 867 | frame = re.search(r"function frame\(\) \{(.*?)\n \}\n headphonesRafId", TALK_SOURCE, re.S) |
| 868 | assert frame, "headphones mode's frame() loop not found in talk.py" |
| 869 | body = frame.group(1) |
| 870 | assert "if (speaking) stopSpeaking();" in body |
| 871 | |
| 872 | |
| 873 | def test_talk_headphones_mode_rms_matches_the_browser_paths_technique(tmp_path): |
| 874 | """Not a shared function (see the comment above headphonesRms in |
| 875 | talk.py for why: the browser path's meter is tightly coupled to |
| 876 | SpeechRecognition-specific state), but the same amplitude-analysis |
| 877 | math -- getByteTimeDomainData -> normalize to [-1, 1] -> RMS.""" |
| 878 | node = shutil.which("node") |
| 879 | if not node: |
| 880 | return |
| 881 | |
| 882 | match = re.search(r"function headphonesRms\(analyser, data\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 883 | assert match, "headphonesRms() not found in talk.py" |
| 884 | |
| 885 | probe = f""" |
| 886 | function headphonesRms(analyser, data) {{{match.group(1)}\n}} |
| 887 | |
| 888 | const silent = {{getByteTimeDomainData: (arr) => arr.fill(128)}}; |
| 889 | const loud = {{getByteTimeDomainData: (arr) => {{ for (let i = 0; i < arr.length; i++) arr[i] = i % 2 === 0 ? 0 : 255; }}}}; |
| 890 | |
| 891 | console.log(JSON.stringify({{ |
| 892 | silent: headphonesRms(silent, new Uint8Array(8)), |
| 893 | loud: headphonesRms(loud, new Uint8Array(8)), |
| 894 | }})); |
| 895 | """ |
| 896 | probe_path = tmp_path / "rms_probe.js" |
| 897 | probe_path.write_text(probe) |
| 898 | result = subprocess.run([node, str(probe_path)], capture_output=True, text=True) |
| 899 | assert result.returncode == 0, f"node probe failed:\n{result.stderr}" |
| 900 | outcome = json.loads(result.stdout) |
| 901 | assert outcome["silent"] == 0 |
| 902 | assert outcome["loud"] > 0.9 |
| 903 | |
| 904 | |
| 905 | def test_talk_headphones_mode_resets_alongside_session_switches(): |
| 906 | # A previous session's live, armed headphones mode must not survive a |
| 907 | # session switch -- it holds an open mic stream that belongs to the |
| 908 | # conversation that just ended. |
| 909 | reset_sites = [ |
| 910 | m.start() for m in re.finditer(r"lastReaderPage = null;", TALK_SOURCE) |
| 911 | ] |
| 912 | handler_sites = [ |
| 913 | site for site in reset_sites |
| 914 | if TALK_SOURCE[max(0, site - 4) : site] != "let " |
| 915 | ] |
| 916 | assert len(handler_sites) >= 2, "expected at least the new-session and resume-session handlers" |
| 917 | for site in handler_sites: |
| 918 | nearby = TALK_SOURCE[site : site + 200] |
| 919 | assert "if (headphonesArmed) stopHeadphonesMode(document.getElementById(\"mic\"));" in nearby |
| 920 | |
| 921 | |
| 922 | def test_talk_page_has_headphones_toggle_button(): |
| 923 | assert '<button type="button" id="headphonestoggle" class="backlink" hidden>headphones: off</button>' in TALK_SOURCE |
| 924 | assert "#cloudvoice, #headphonestoggle { background: none;" in TALK_SOURCE |
| 925 | |
| 926 | |
| 927 | def test_talk_headphones_toggle_hidden_unless_cloud_stt_and_saves_live_without_reload(tmp_path): |
| 928 | """Behavioral: unlike the cloud-voice toggle (which reloads, since it |
| 929 | changes which mic implementation initMic() wires up), this one must |
| 930 | NOT reload -- it only changes behavior read fresh on each tap inside |
| 931 | the already-running initCloudMic click handler.""" |
| 932 | node = shutil.which("node") |
| 933 | if not node: |
| 934 | return |
| 935 | |
| 936 | def extract_function(name): |
| 937 | start = TALK_SOURCE.index(f"function {name}(") |
| 938 | depth = 0 |
| 939 | i = TALK_SOURCE.index("{", start) |
| 940 | for j in range(i, len(TALK_SOURCE)): |
| 941 | if TALK_SOURCE[j] == "{": |
| 942 | depth += 1 |
| 943 | elif TALK_SOURCE[j] == "}": |
| 944 | depth -= 1 |
| 945 | if depth == 0: |
| 946 | return TALK_SOURCE[start : j + 1] |
| 947 | raise AssertionError(f"unbalanced braces extracting {name}()") |
| 948 | |
| 949 | def extract_async_function(name): |
| 950 | start = TALK_SOURCE.index(f"async function {name}(") |
| 951 | depth = 0 |
| 952 | i = TALK_SOURCE.index("{", start) |
| 953 | for j in range(i, len(TALK_SOURCE)): |
| 954 | if TALK_SOURCE[j] == "{": |
| 955 | depth += 1 |
| 956 | elif TALK_SOURCE[j] == "}": |
| 957 | depth -= 1 |
| 958 | if depth == 0: |
| 959 | return TALK_SOURCE[start : j + 1] |
| 960 | raise AssertionError(f"unbalanced braces extracting {name}()") |
| 961 | |
| 962 | functions = "\n".join([ |
| 963 | extract_function("urlOverride"), |
| 964 | extract_function("talkSttEngine"), |
| 965 | extract_function("talkHeadphonesMode"), |
| 966 | extract_async_function("api"), |
| 967 | extract_async_function("savePrefs"), |
| 968 | extract_function("initHeadphonesToggle"), |
| 969 | ]) |
| 970 | |
| 971 | probe = f""" |
| 972 | const DEFAULT_TALK_STT_ENGINE = "browser"; |
| 973 | const OWNER_ID = "dan"; |
| 974 | let prefs = {{talk_stt_engine: "browser"}}; |
| 975 | let location = {{search: ""}}; |
| 976 | function pref(name, fallback) {{ |
| 977 | const v = prefs[name]; |
| 978 | return v === undefined || v === null ? fallback : v; |
| 979 | }} |
| 980 | const putCalls = []; |
| 981 | function fetch(path, opts) {{ |
| 982 | putCalls.push({{path, body: JSON.parse(opts.body)}}); |
| 983 | return Promise.resolve({{ok: true, json: () => Promise.resolve({{status: "ok"}})}}); |
| 984 | }} |
| 985 | class FakeButton {{ |
| 986 | constructor() {{ this.hidden = true; this.textContent = ""; this.disabled = false; this._handlers = {{}}; }} |
| 987 | addEventListener(name, fn) {{ this._handlers[name] = fn; }} |
| 988 | click() {{ return this._handlers["click"](); }} |
| 989 | }} |
| 990 | const btn = new FakeButton(); |
| 991 | const document = {{getElementById: (id) => id === "headphonestoggle" ? btn : null}}; |
| 992 | |
| 993 | {functions} |
| 994 | |
| 995 | const results = {{}}; |
| 996 | initHeadphonesToggle(); |
| 997 | results.hiddenWhenBrowserStt = btn.hidden; |
| 998 | |
| 999 | prefs = {{talk_stt_engine: "openai", talk_headphones_mode: "false"}}; |
| 1000 | initHeadphonesToggle(); |
| 1001 | results.shownWhenCloudStt = btn.hidden; |
| 1002 | results.initialText = btn.textContent; |
| 1003 | |
| 1004 | (async () => {{ |
| 1005 | await btn.click(); |
| 1006 | results.afterClickText = btn.textContent; |
| 1007 | results.putCalls = putCalls; |
| 1008 | results.finalPrefs = prefs; |
| 1009 | console.log(JSON.stringify(results)); |
| 1010 | }})(); |
| 1011 | """ |
| 1012 | probe_path = tmp_path / "headphones_toggle_probe.js" |
| 1013 | probe_path.write_text(probe) |
| 1014 | result = subprocess.run([node, str(probe_path)], capture_output=True, text=True) |
| 1015 | assert result.returncode == 0, f"node probe failed:\n{result.stderr}" |
| 1016 | outcome = json.loads(result.stdout) |
| 1017 | |
| 1018 | assert outcome["hiddenWhenBrowserStt"] is True |
| 1019 | assert outcome["shownWhenCloudStt"] is False |
| 1020 | assert outcome["initialText"] == "headphones: off" |
| 1021 | assert outcome["afterClickText"] == "headphones: on" |
| 1022 | assert outcome["putCalls"] == [ |
| 1023 | {"path": "/collections/shell_preferences/records/dan", "body": {"talk_headphones_mode": "true"}} |
| 1024 | ] |
| 1025 | assert outcome["finalPrefs"]["talk_headphones_mode"] == "true" |
| 1026 | |
| 1027 | |
| 1028 | def test_talk_stop_speaking_is_shared_by_natural_end_and_interrupt(tmp_path): |
| 1029 | """Behavioral: stopSpeaking() must be reachable both as a normal |
| 1030 | playback-ended handler AND as a deliberate interrupt call from |
| 1031 | outside speak() -- executes the real function under node against a |
| 1032 | fake Audio element and fake speechSynthesis. |
| 1033 | """ |
| 1034 | node = shutil.which("node") |
| 1035 | if not node: |
| 1036 | return |
| 1037 | |
| 1038 | def extract_function(name): |
| 1039 | start = TALK_SOURCE.index(f"function {name}(") |
| 1040 | depth = 0 |
| 1041 | i = TALK_SOURCE.index("{", start) |
| 1042 | for j in range(i, len(TALK_SOURCE)): |
| 1043 | if TALK_SOURCE[j] == "{": |
| 1044 | depth += 1 |
| 1045 | elif TALK_SOURCE[j] == "}": |
| 1046 | depth -= 1 |
| 1047 | if depth == 0: |
| 1048 | return TALK_SOURCE[start : j + 1] |
| 1049 | raise AssertionError(f"unbalanced braces extracting {name}()") |
| 1050 | |
| 1051 | probe = f""" |
| 1052 | let speaking = true; |
| 1053 | let currentTtsObjectUrl = "blob:fake-url"; |
| 1054 | let revoked = null; |
| 1055 | let cancelled = false; |
| 1056 | let resumeCalled = false; |
| 1057 | function resumeListeningIfNeeded() {{ resumeCalled = true; }} |
| 1058 | const URL = {{revokeObjectURL: (u) => {{ revoked = u; }}}}; |
| 1059 | const fakePlayer = {{onended: "x", onerror: "x", onstalled: "x", paused: false, pause() {{ this.paused = true; }}}}; |
| 1060 | function sharedTtsAudio() {{ return fakePlayer; }} |
| 1061 | const window = {{speechSynthesis: {{cancel: () => {{ cancelled = true; }}}}}}; |
| 1062 | |
| 1063 | {extract_function("stopSpeaking")} |
| 1064 | |
| 1065 | stopSpeaking(); |
| 1066 | console.log(JSON.stringify({{ |
| 1067 | speaking, revoked, cancelled, resumeCalled, |
| 1068 | playerPaused: fakePlayer.paused, |
| 1069 | handlersCleared: fakePlayer.onended === null && fakePlayer.onerror === null && fakePlayer.onstalled === null, |
| 1070 | }})); |
| 1071 | """ |
| 1072 | probe_path = tmp_path / "stop_speaking_probe.js" |
| 1073 | probe_path.write_text(probe) |
| 1074 | result = subprocess.run([node, str(probe_path)], capture_output=True, text=True) |
| 1075 | assert result.returncode == 0, f"node probe failed:\n{result.stderr}" |
| 1076 | outcome = json.loads(result.stdout) |
| 1077 | |
| 1078 | assert outcome["speaking"] is False |
| 1079 | assert outcome["revoked"] == "blob:fake-url" |
| 1080 | assert outcome["cancelled"] is True |
| 1081 | assert outcome["resumeCalled"] is True |
| 1082 | assert outcome["playerPaused"] is True |
| 1083 | assert outcome["handlersCleared"] is True |
| 1084 | |
| 1085 | # Both call sites must actually use it -- a duplicate inline cleanup |
| 1086 | # anywhere would mean an interrupt and a natural end could drift apart. |
| 1087 | assert "player.onended = stopSpeaking;" in TALK_SOURCE |
| 1088 | assert "utter.addEventListener(\"end\", stopSpeaking);" in TALK_SOURCE |
| 1089 | |
| 1090 | |
| 1091 | def test_talk_wake_word_gating_discards_until_heard(): |
| 1092 | process = re.search(r"function processTranscript\(isFinal\) \{(.*?)\n\}\n", TALK_SOURCE, re.S) |
| 1093 | assert process, "processTranscript() not found in talk.py" |
| 1094 | body = process.group(1) |
| 1095 | assert "if (armed) {" in body |
| 1096 | assert "findWakeSplit(combinedRaw, w)" in body |
| 1097 | # Nothing found yet -> stays armed and shows the dim hint, does not fold |
| 1098 | # into the active buffer. |
| 1099 | # Per-event splitting: cumulative interims restate the wake word |
| 1100 | # every event, so the split runs every event too (the recorded turn |
| 1101 | # "Show me my notes computer show me my notes" is the old behaviour). |
| 1102 | assert "if (w && split === null) {" in body |
| 1103 | assert "updateCaptionArmed();" in body |
| 1104 | |
| 1105 | armed_caption = re.search(r"function updateCaptionArmed\(\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 1106 | assert armed_caption |
| 1107 | assert "to address me" in armed_caption.group(1) |
| 1108 | assert '.add("armed")' in armed_caption.group(1) |
| 1109 | |
| 1110 | |
| 1111 | def test_talk_wake_word_match_is_case_insensitive_word_boundary(): |
| 1112 | find_split = re.search(r"function findWakeSplit\(text, word\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 1113 | assert find_split, "findWakeSplit() not found in talk.py" |
| 1114 | body = find_split.group(1) |
| 1115 | assert ".toLowerCase()" in body |
| 1116 | assert "tokenize(text)" in body |
| 1117 | # Whole-token compare, not substring -- via matchesWakeWord, which is |
| 1118 | # the exact match PLUS a bounded Damerau tolerance ("compture" arrived |
| 1119 | # for "computer" and the strict version invisibly discarded everything |
| 1120 | # said after it; see tests/test_talk_mobile.py for that contract). |
| 1121 | assert "matchesWakeWord(key, target)" in body |
| 1122 | |
| 1123 | |
| 1124 | def test_talk_end_word_tail_match_and_strip(): |
| 1125 | tail_match = re.search(r"function tailMatchesEndWord\(text, word\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 1126 | assert tail_match, "tailMatchesEndWord() not found in talk.py" |
| 1127 | body = tail_match.group(1) |
| 1128 | assert ".toLowerCase()" in body |
| 1129 | assert "tokens[tokens.length - 1]" in body # only the tail token |
| 1130 | assert "tokens.slice(0, -1).join" in body # strips it from the remainder |
| 1131 | |
| 1132 | word_key = re.search(r"function wordKey\(token\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 1133 | assert word_key, "wordKey() not found in talk.py" |
| 1134 | assert "[.,!?;:]" in word_key.group(1) # tolerant of trailing punctuation |
| 1135 | |
| 1136 | process = re.search(r"function processTranscript\(isFinal\) \{(.*?)\n\}\n", TALK_SOURCE, re.S) |
| 1137 | assert process |
| 1138 | body = process.group(1) |
| 1139 | assert 'endpoint === "word" || endpoint === "silence"' in body |
| 1140 | assert "tailMatchesEndWord(active, ew)" in body |
| 1141 | assert "finalizeVoiceSubmit(stripped)" in body |
| 1142 | |
| 1143 | |
| 1144 | def test_talk_empty_wake_word_captures_everything(): |
| 1145 | process = re.search(r"function processTranscript\(isFinal\) \{(.*?)\n\}\n", TALK_SOURCE, re.S) |
| 1146 | assert process |
| 1147 | body = process.group(1) |
| 1148 | assert "armed = false; // empty wake word: capture everything while the mic is on" in body |
| 1149 | |
| 1150 | |
| 1151 | def test_talk_empty_end_word_falls_back_to_debounced_isfinal(): |
| 1152 | process = re.search(r"function processTranscript\(isFinal\) \{(.*?)\n\}\n", TALK_SOURCE, re.S) |
| 1153 | assert process |
| 1154 | body = process.group(1) |
| 1155 | assert 'endpoint === "word" && !ew' in body |
| 1156 | assert "setTimeout(() => {" in body |
| 1157 | assert "}, 300);" in body # short: stability endpointing owns the long wait now |
| 1158 | assert "finalizeVoiceSubmit(buffer);" in body |
| 1159 | |
| 1160 | |
| 1161 | def test_talk_buffer_cleared_and_rearmed_after_any_submit(): |
| 1162 | finalize = re.search(r"function finalizeVoiceSubmit\(text\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 1163 | assert finalize, "finalizeVoiceSubmit() not found in talk.py" |
| 1164 | body = finalize.group(1) |
| 1165 | assert "resetTalkState();" in body |
| 1166 | |
| 1167 | reset = re.search(r"function resetTalkState\(\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 1168 | assert reset, "resetTalkState() not found in talk.py" |
| 1169 | body = reset.group(1) |
| 1170 | assert 'buffer = "";' in body |
| 1171 | assert 'sessionLive = "";' in body |
| 1172 | assert "armed = true;" in body |
| 1173 | assert "clearTimeout(finalDebounceTimer);" in body |
| 1174 | |
| 1175 | # Manual submit also resets before/around submitTurn, per "after any |
| 1176 | # submit: clear the buffer and return to armed state". |
| 1177 | handler = re.search( |
| 1178 | r'document\.getElementById\("prompt"\)\.addEventListener\("submit", \(event\) => \{(.*?)\n\}\);', |
| 1179 | TALK_SOURCE, |
| 1180 | re.S, |
| 1181 | ) |
| 1182 | assert handler |
| 1183 | assert "resetTalkState();" in handler.group(1) |
| 1184 | |
| 1185 | |
| 1186 | def test_talk_manual_submit_bypasses_arming_and_joins_voice_plus_typed(): |
| 1187 | handler = re.search( |
| 1188 | r'document\.getElementById\("prompt"\)\.addEventListener\("submit", \(event\) => \{(.*?)\n\}\);', |
| 1189 | TALK_SOURCE, |
| 1190 | re.S, |
| 1191 | ) |
| 1192 | assert handler |
| 1193 | body = handler.group(1) |
| 1194 | assert "const typed = box.value.trim();" in body |
| 1195 | assert "const voice = conversationMode ? pendingText() : " in body |
| 1196 | assert "[voice, typed].filter(Boolean).join" in body |
| 1197 | |
| 1198 | |
| 1199 | def test_talk_tts_pause_keeps_buffer_from_accumulating_machine_speech(): |
| 1200 | """Recognition is already stopped for the whole speaking span (asserted |
| 1201 | elsewhere); this just confirms the VAD path independently refuses to run |
| 1202 | while speaking, since its getUserMedia stream is not gated by the |
| 1203 | recognizer's own stop/start.""" |
| 1204 | vad = re.search(r"function evaluateVAD\(rms, now\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 1205 | assert vad, "evaluateVAD() not found in talk.py" |
| 1206 | assert "if (speaking || !conversationMode) {" in vad.group(1) |
| 1207 | |
| 1208 | meter = re.search(r"function updateMeterVisual\(rms\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 1209 | assert meter, "updateMeterVisual() not found in talk.py" |
| 1210 | assert "speaking ? 0 :" in meter.group(1) # ring idles during playback |
| 1211 | |
| 1212 | |
| 1213 | # --- mic level metering + VAD silence endpointing ------------------------- |
| 1214 | |
| 1215 | |
| 1216 | def test_talk_meter_uses_getusermedia_and_analysernode(): |
| 1217 | start_meter = re.search(r"async function startMeter\(\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 1218 | assert start_meter, "startMeter() not found in talk.py" |
| 1219 | body = start_meter.group(1) |
| 1220 | assert "navigator.mediaDevices.getUserMedia({audio: true})" in body |
| 1221 | assert "createMediaStreamSource(micStream)" in body |
| 1222 | assert "createAnalyser()" in body |
| 1223 | assert "requestAnimationFrame(frame)" in body |
| 1224 | |
| 1225 | |
| 1226 | def test_talk_noise_floor_calibration_then_threshold(): |
| 1227 | start_meter = re.search(r"async function startMeter\(\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 1228 | assert start_meter |
| 1229 | body = start_meter.group(1) |
| 1230 | assert "CALIBRATION_MS" in body |
| 1231 | assert "calibrationSamples.push(rms)" in body |
| 1232 | assert "noiseFloor = avg;" in body |
| 1233 | assert "speechThreshold = Math.max(avg * 3, MIN_SPEECH_THRESHOLD);" in body |
| 1234 | assert "const CALIBRATION_MS = 800;" in TALK_SOURCE |
| 1235 | |
| 1236 | |
| 1237 | def test_talk_silence_timer_submits_buffer_respecting_wake_gate(): |
| 1238 | vad = re.search(r"function evaluateVAD\(rms, now\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 1239 | assert vad |
| 1240 | body = vad.group(1) |
| 1241 | assert "utteranceStarted" in body |
| 1242 | assert "bufferHasContent()" in body |
| 1243 | assert "now - silenceStartTs < silenceMs()) return;" in body |
| 1244 | assert "if (armed) {" in body |
| 1245 | assert "resetTalkState();" in body # discard, not submit, if never woken |
| 1246 | assert "finalizeVoiceSubmit(pendingText());" in body # submit once woken |
| 1247 | |
| 1248 | |
| 1249 | def test_talk_meter_stream_released_when_conversation_mode_turns_off(): |
| 1250 | stop_meter = re.search(r"function stopMeter\(\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 1251 | assert stop_meter, "stopMeter() not found in talk.py" |
| 1252 | body = stop_meter.group(1) |
| 1253 | assert "cancelAnimationFrame(meterRafId)" in body |
| 1254 | assert "audioCtx.close()" in body |
| 1255 | assert "micStream.getTracks().forEach((t) => t.stop())" in body |
| 1256 | assert 'ring.hidden = true;' in body # visibly dead -- the privacy indicator |
| 1257 | |
| 1258 | mic_click = re.search(r'mic\.addEventListener\("click", \(\) => \{(.*?)\n \}\);', TALK_SOURCE, re.S) |
| 1259 | assert mic_click, "mic click handler not found in talk.py" |
| 1260 | body = mic_click.group(1) |
| 1261 | # The OFF path must still release the stream from here -- that is what |
| 1262 | # this test is for, and the privacy indicator depends on it. |
| 1263 | assert "stopMeter();" in body |
| 1264 | # The ON path starts the meter AT THE CLICK -- deliberately, again, |
| 1265 | # after the deferred design killed desktop endpointing (a meter |
| 1266 | # started mid-speech calibrates on the voice itself). The iOS |
| 1267 | # exclusive-mic theft is handled by the conviction instead, and the |
| 1268 | # conviction flag gates the restart (see tests/test_talk_mobile.py's |
| 1269 | # design history note). |
| 1270 | assert "startMeter()" in body |
| 1271 | assert "!meterContended" in body |
| 1272 | |
| 1273 | |
| 1274 | def test_talk_endpoint_mode_helper_validates_and_defaults(): |
| 1275 | fn = re.search(r"function endpointMode\(\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 1276 | assert fn, "endpointMode() not found in talk.py" |
| 1277 | body = fn.group(1) |
| 1278 | assert '"word"' in body and '"manual"' in body and '"silence"' in body |
| 1279 | assert "DEFAULT_ENDPOINT" in body |
| 1280 | |
| 1281 | |
| 1282 | def test_talk_helper_functions_behave_correctly_under_node(): |
| 1283 | """Behavioral (not just source-string) coverage for the pure word-match |
| 1284 | helpers, run against the actual evaluated script when node is available.""" |
| 1285 | node = shutil.which("node") |
| 1286 | if not node: |
| 1287 | return |
| 1288 | |
| 1289 | html = _render_talk_html() |
| 1290 | script = _extract_inline_script(html) |
| 1291 | start = script.index("function tokenize(text)") |
| 1292 | end = script.index("function resetTalkState()") |
| 1293 | helpers = script[start:end] |
| 1294 | |
| 1295 | probe = ( |
| 1296 | helpers |
| 1297 | + "\nconsole.log(JSON.stringify({" |
| 1298 | + 'wake: findWakeSplit("hey computer turn on the lights", "computer"),' |
| 1299 | + 'noWake: findWakeSplit("turn on the lights", "computer"),' |
| 1300 | + 'wakeCasePunct: findWakeSplit("Computer, whats the time", "computer"),' |
| 1301 | + 'endStrip: tailMatchesEndWord("turn on the lights over", "over"),' |
| 1302 | + 'endStripPunct: tailMatchesEndWord("turn on the lights, over.", "over"),' |
| 1303 | + 'noEnd: tailMatchesEndWord("turn on the lights", "over"),' |
| 1304 | + "}));\n" |
| 1305 | ) |
| 1306 | result = subprocess.run([node, "-e", probe], capture_output=True, text=True) |
| 1307 | assert result.returncode == 0, f"node probe failed:\n{result.stderr}" |
| 1308 | out = json.loads(result.stdout) |
| 1309 | assert out["wake"] == "turn on the lights" |
| 1310 | assert out["noWake"] is None |
| 1311 | assert out["wakeCasePunct"] == "whats the time" |
| 1312 | assert out["endStrip"] == "turn on the lights" |
| 1313 | assert out["endStripPunct"] == "turn on the lights," |
| 1314 | assert out["noEnd"] is None |
| 1315 | |
| 1316 | |
| 1317 | # --- voice engine preference (talk_tts: auto / server / browser) --------- |
| 1318 | |
| 1319 | |
| 1320 | def test_talk_tts_schema_field_added_with_default_auto(): |
| 1321 | fields = {f["name"]: f for f in SHELL_PREFS_SCHEMA["fields"]} |
| 1322 | assert fields["talk_tts"]["type"] == "enum" |
| 1323 | assert fields["talk_tts"]["default"] == "auto" |
| 1324 | assert set(fields["talk_tts"]["enum"]) == {"auto", "server", "browser"} |
| 1325 | assert "talk_tts" in SHELL_PREFS_SCHEMA["forms"]["default"]["fields"] |
| 1326 | |
| 1327 | # Client-side defaults mirror the schema default in both objects. |
| 1328 | assert 'talk_tts: "auto"' in TALK_SOURCE |
| 1329 | assert 'talk_tts: "auto"' in SHELL_SOURCE |
| 1330 | |
| 1331 | |
| 1332 | def test_talk_speak_routes_by_talk_tts_preference(): |
| 1333 | fn = re.search(r"async function speak\(text\) \{(.*?)\n\}\n", TALK_SOURCE, re.S) |
| 1334 | assert fn, "speak() not found in talk.py" |
| 1335 | body = fn.group(1) |
| 1336 | assert "talkTtsMode()" in body |
| 1337 | assert 'mode === "browser"' in body |
| 1338 | assert 'mode === "auto" && window.speechSynthesis && hasLocalVoice()' in body |
| 1339 | assert "speakBrowser(spoken)" in body |
| 1340 | # "server" (and "auto" without a local voice) keep the original POST |
| 1341 | # /api/tts-first, speechSynthesis-fallback behavior. |
| 1342 | assert '/api/tts' in body |
| 1343 | assert "catch (e) {" in body |
| 1344 | |
| 1345 | mode_fn = re.search(r"function talkTtsMode\(\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 1346 | assert mode_fn, "talkTtsMode() not found in talk.py" |
| 1347 | assert '"server"' in mode_fn.group(1) and '"browser"' in mode_fn.group(1) |
| 1348 | assert "DEFAULT_TALK_TTS" in mode_fn.group(1) |
| 1349 | assert 'const DEFAULT_TALK_TTS = "auto";' in TALK_SOURCE |
| 1350 | |
| 1351 | |
| 1352 | def test_talk_speak_browser_sets_and_clears_speaking_flag(): |
| 1353 | """The browser-voice path must uphold the same recognition-pause |
| 1354 | contract as the server path: set `speaking` and stop listening before |
| 1355 | the utterance, clear it and resume only once the utterance ends.""" |
| 1356 | fn = re.search(r"async function speakBrowser\(text\) \{(.*?)\n\}\n", TALK_SOURCE, re.S) |
| 1357 | assert fn, "speakBrowser() not found in talk.py" |
| 1358 | body = fn.group(1) |
| 1359 | assert "speaking = true;" in body |
| 1360 | # Awaits the real mic release, same as the server-TTS path, and for |
| 1361 | # the same iOS shared-audio-session reason. |
| 1362 | assert "await stopListeningAndWaitForRelease();" in body |
| 1363 | assert "speaking = false; resumeListeningIfNeeded();" in body |
| 1364 | assert body.index("speaking = true;") < body.index("window.speechSynthesis.speak(utter);") |
| 1365 | assert "utter.addEventListener(\"end\"," in body |
| 1366 | |
| 1367 | # "browser" mode, the "auto"-with-a-local-voice shortcut, and the |
| 1368 | # server-mode fallback all funnel through this one function -- no |
| 1369 | # separate untracked speechSynthesis path. |
| 1370 | speak_fn = re.search(r"async function speak\(text\) \{(.*?)\n\}\n", TALK_SOURCE, re.S) |
| 1371 | assert speak_fn |
| 1372 | assert speak_fn.group(1).count("speakBrowser(spoken)") == 3 |
| 1373 | |
| 1374 | |
| 1375 | def test_talk_pick_voice_prefers_named_then_local_english_else_default(): |
| 1376 | assert "const PREFERRED_VOICE_RE = /Samantha|Ava|Karen|Daniel/;" in TALK_SOURCE |
| 1377 | |
| 1378 | fn = re.search(r"function pickVoice\(\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 1379 | assert fn, "pickVoice() not found in talk.py" |
| 1380 | body = fn.group(1) |
| 1381 | assert "PREFERRED_VOICE_RE.test(v.name)" in body |
| 1382 | assert "v.localService && /^en/i.test(v.lang)" in body |
| 1383 | assert "return localEn || null;" in body |
| 1384 | |
| 1385 | has_local = re.search(r"function hasLocalVoice\(\) \{(.*?)\n\}", TALK_SOURCE, re.S) |
| 1386 | assert has_local, "hasLocalVoice() not found in talk.py" |
| 1387 | assert "v.localService" in has_local.group(1) |
| 1388 | |
| 1389 | |
| 1390 | def test_talk_voice_picking_behaves_correctly_under_node(tmp_path): |
| 1391 | node = shutil.which("node") |
| 1392 | if not node: |
| 1393 | return |
| 1394 | |
| 1395 | html = _render_talk_html() |
| 1396 | script = _extract_inline_script(html) |
| 1397 | start = script.index("const PREFERRED_VOICE_RE") |
| 1398 | end = script.index("async function speakBrowser(text)") |
| 1399 | helpers = script[start:end] |
| 1400 | |
| 1401 | probe = ( |
| 1402 | "let voicesFixture = [];\n" |
| 1403 | "const window = {speechSynthesis: {getVoices: () => voicesFixture}};\n" |
| 1404 | + helpers |
| 1405 | + "\n" |
| 1406 | "voicesFixture = [\n" |
| 1407 | " {name: 'Generic Voice', lang: 'en-US', localService: true},\n" |
| 1408 | " {name: 'Karen', lang: 'en-AU', localService: true},\n" |
| 1409 | "];\n" |
| 1410 | "const named = pickVoice();\n" |
| 1411 | "voicesFixture = [{name: 'Generic Voice', lang: 'en-US', localService: true}];\n" |
| 1412 | "const localEn = pickVoice();\n" |
| 1413 | "voicesFixture = [{name: 'Generic Voice', lang: 'fr-FR', localService: false}];\n" |
| 1414 | "const none = pickVoice();\n" |
| 1415 | "voicesFixture = [{name: 'Generic Voice', lang: 'en-US', localService: true}];\n" |
| 1416 | "const anyLocalTrue = hasLocalVoice();\n" |
| 1417 | "voicesFixture = [];\n" |
| 1418 | "const anyLocalFalse = hasLocalVoice();\n" |
| 1419 | "console.log(JSON.stringify({\n" |
| 1420 | " named: named && named.name,\n" |
| 1421 | " localEn: localEn && localEn.name,\n" |
| 1422 | " none,\n" |
| 1423 | " anyLocalTrue,\n" |
| 1424 | " anyLocalFalse,\n" |
| 1425 | "}));\n" |
| 1426 | ) |
| 1427 | probe_path = tmp_path / "voice_probe.js" |
| 1428 | probe_path.write_text(probe) |
| 1429 | result = subprocess.run([node, str(probe_path)], capture_output=True, text=True) |
| 1430 | assert result.returncode == 0, f"node probe failed:\n{result.stderr}" |
| 1431 | out = json.loads(result.stdout) |
| 1432 | assert out["named"] == "Karen" |
| 1433 | assert out["localEn"] == "Generic Voice" |
| 1434 | assert out["none"] is None |
| 1435 | assert out["anyLocalTrue"] is True |
| 1436 | assert out["anyLocalFalse"] is False |
| 1437 | |
| 1438 | |
| 1439 | def test_shell_speak_routes_by_talk_tts_preference(): |
| 1440 | fn = re.search(r"async function speak\(text\) \{(.*?)\n\}\n", SHELL_SOURCE, re.S) |
| 1441 | assert fn, "speak() not found in shell.py" |
| 1442 | body = fn.group(1) |
| 1443 | assert "talkTtsMode()" in body |
| 1444 | assert 'mode === "browser"' in body |
| 1445 | assert 'mode === "auto" && window.speechSynthesis && hasLocalVoice()' in body |
| 1446 | assert "speakBrowser(spoken)" in body |
| 1447 | assert '/api/tts' in body |
| 1448 | assert "catch (e) {" in body |
| 1449 | |
| 1450 | assert "function talkTtsMode() {" in SHELL_SOURCE |
| 1451 | assert "function speakBrowser(text) {" in SHELL_SOURCE |
| 1452 | assert "function pickVoice() {" in SHELL_SOURCE |
| 1453 | assert "function hasLocalVoice() {" in SHELL_SOURCE |
| 1454 | assert "const PREFERRED_VOICE_RE = /Samantha|Ava|Karen|Daniel/;" in SHELL_SOURCE |
| 1455 | assert 'const DEFAULT_TALK_TTS = "auto";' in SHELL_SOURCE |