fix: recordings keep only what was played, newest waiting request kept, video pages opened play

- Playback tells the worker how much of each request was played (tts_played); the worker keeps a
  spoken answer's recording only that far (an answer stopped mid-way no longer keeps its unheard
  rest, and is marked stopped); nothing played or never reported: no file.
- Old recording day folders are also removed with the first recording of each new day.
- response.interrupt off, latest: a request whose final comes after a newer one's is replaced
  instead of replacing it.
- botBrowser presses play on a page's paused main player also after open, not only after a click.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-16 15:56:40 +09:00
parent fdcaae4820
commit ec2672cd4a
12 changed files with 176 additions and 33 deletions

View File

@@ -378,14 +378,17 @@ def test_the_page_says_whether_its_sound_plays_and_opening_waits_for_it_to_start
assert s.open(site + "/second").get("media") == []
def test_clicking_through_to_a_video_page_whose_player_stays_paused_presses_play(chrome):
def test_a_video_page_whose_player_stays_paused_gets_play_pressed_when_clicked_to_or_opened(chrome):
cdp, site, _ = chrome
s = BrowserSession(cdp, time.monotonic() + 15)
s.open(site + "/results")
page = s.click("The song")
assert page["url"].endswith("/watch") and [m["playing"] for m in page["media"]] == [True], page
# Opening the page by its address is not a click on it: the player is left as the page has it.
page = s.open(site + "/watch")
s.open(site + "/results")
page = s.open(site + "/watch") # straight to the video page by its address
assert [m["playing"] for m in page["media"]] == [True], page
# Sound that is not the page's main player (an audio element, no big video) is left as it is.
page = s.open(site + "/button")
assert [m["playing"] for m in page["media"]] == [False]

View File

@@ -402,6 +402,9 @@ def test_with_recordings_on_each_utterance_and_spoken_answer_is_kept_as_a_wav_wi
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "final")
await ws.send(json.dumps({"type": "tts", "id": "r1", "text": "오늘은 맑아요. 기온은 17.5도예요."}))
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "tts_done")
# The app played both sentences to the end.
await ws.send(json.dumps({"type": "tts_played", "id": "r1", "played": 2, "cancelled": False}))
await asyncio.sleep(0.2)
asyncio.run(with_worker(body, tts=FakeTts(), recorder=recorder))
recorder.close()
@@ -446,3 +449,49 @@ def test_recordings_older_than_the_keep_days_are_removed_at_start(tmp_path):
(tmp_path / "notes").mkdir() # not a day folder: left alone
Recorder(tmp_path, now=datetime(2026, 11, 16, 12).timestamp()).close()
assert sorted(p.name for p in tmp_path.iterdir()) == sorted([d.isoformat() for d in keep] + ["notes"])
@pytest.mark.unit
def test_a_spoken_answer_is_kept_only_as_far_as_the_app_played_it(tmp_path):
import wave
from voice_worker.recordings import Recorder
recorder = Recorder(tmp_path)
async def body(url):
async with connect(url) as ws:
await hello(ws)
# Made in full, then stopped while its first sentence played.
await ws.send(json.dumps({"type": "tts", "id": "r1", "text": "오늘은 맑아요. 기온은 17.5도예요."}))
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "tts_done")
await ws.send(json.dumps({"type": "tts_played", "id": "r1", "played": 1, "cancelled": True}))
# Stopped before any of it was heard: nothing to keep.
await ws.send(json.dumps({"type": "tts", "id": "r2", "text": "두 번째 답이에요."}))
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "tts_done")
await ws.send(json.dumps({"type": "tts_played", "id": "r2", "played": 0, "cancelled": True}))
# Never reported as played (an older app): not kept as if it had been heard.
await ws.send(json.dumps({"type": "tts", "id": "r3", "text": "세 번째 답이에요."}))
await recv_until(ws, lambda m: isinstance(m, dict) and m.get("type") == "tts_done")
await asyncio.sleep(0.2)
asyncio.run(with_worker(body, tts=FakeTts(), recorder=recorder))
recorder.close()
day, lines = _index(tmp_path)
assert [(l["id"], l["cancelled"]) for l in lines] == [("r1", True)]
with wave.open(str(day / lines[0]["file"])) as w:
assert w.getnframes() == len("오늘은 맑아요.") * 10, "only the sentence that was played"
@pytest.mark.unit
def test_old_recordings_are_also_removed_while_the_worker_keeps_running(tmp_path):
from datetime import datetime, timedelta
import numpy as np
from voice_worker.recordings import KEEP_DAYS, Recorder
start = datetime(2026, 11, 16, 12)
r = Recorder(tmp_path, now=start.timestamp())
r.said(start.timestamp(), "1", "A", "vc", "첫날", np.zeros(1600))
later = start + timedelta(days=KEEP_DAYS) # the first recording of a day past the keep days
r.said(later.timestamp(), "1", "A", "vc", "나중", np.zeros(1600))
r.close()
assert sorted(p.name for p in tmp_path.iterdir()) == [later.date().isoformat()]