Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

feat(dictation): stats, discard and restore on the REST layer

Ruben Lütke97f522c3d96a
diff --git a/jarvis/ui/web/dictation_routes.py b/jarvis/ui/web/dictation_routes.py
index f6aba09e8f..3d17097d7f 100644
--- a/jarvis/ui/web/dictation_routes.py
+++ b/jarvis/ui/web/dictation_routes.py
@@ -2,15 +2,25 @@
Endpoints (mounted by the WebServer in ``_build_app()``):
- GET /api/dictation/status → capability + live state + the shortcut.
+ GET /api/dictation/status → capability + live state + the shortcuts.
POST /api/dictation/start → begin a dictation ({"target": ...}).
POST /api/dictation/stop → finish the running one.
GET /api/dictation/history → recent dictations (raw + cleaned).
+ GET /api/dictation/stats → lifetime totals, today, day streak.
DELETE /api/dictation/history → purge everything (destructive).
DELETE /api/dictation/history/{id} → drop one entry.
+ POST /api/dictation/history/{id}/discard → soft-delete (recoverable).
+ POST /api/dictation/history/{id}/restore → un-discard, re-transcribe.
GET /api/dictation/settings → the [dictation] block.
PUT /api/dictation/settings → change one or more keys.
+Delete has two shapes on purpose. ``DELETE /history/{id}`` keeps hard-delete
+semantics because that is the contract anyone scripting ``jarvis api dictation``
+already relies on; the UI's trash icon calls ``POST .../discard`` instead, so a
+mis-click stays recoverable. Both of them, and the full purge, take the audio
+sidecar with them — a "deleted" dictation the app still holds a recording of
+would be a quiet lie.
+
Why REST and not only the WebSocket command the chat mic button uses: under the
CLI-first contract (CLAUDE.md §5) a capability that exists only in the UI is not
finished. Mounting this router also makes every action a
@@ -23,16 +33,26 @@ no microphone the status endpoint answers honestly instead of 500-ing.
"""
from __future__ import annotations
+import asyncio
import logging
from typing import Any
-from fastapi import APIRouter, HTTPException, Request
+from fastapi import APIRouter, HTTPException, Query, Request
from pydantic import BaseModel, Field
log = logging.getLogger(__name__)
router = APIRouter(prefix="/api/dictation", tags=["dictation"])
+#: What a Restore says when there is simply no provider to ask. Not an error:
+#: the entry still comes back, it just comes back without its words. Phrased as
+#: a fact about this host rather than as a failure of the request, because a
+#: 500 here would look like a bug in something the user did nothing wrong in.
+_NO_STT_DETAIL = (
+ "No speech-to-text provider is reachable on this computer, so the saved "
+ "audio could not be transcribed again. The entry itself was restored."
+)
+
# ----------------------------------------------------------------------
# Helpers
@@ -63,6 +83,69 @@ def _dictation_cfg(request: Request) -> Any:
return DictationConfig()
+def _as_int(value: Any, fallback: int) -> int:
+ """Best-effort int from a config value that a hand-edit may have mangled."""
+ try:
+ return int(value)
+ except (TypeError, ValueError):
+ return fallback
+
+
+def _pinned_language(request: Request) -> str | None:
+ """``[dictation].language`` as an STT argument — ``None`` means "detect"."""
+ pinned = str(getattr(_dictation_cfg(request), "language", "auto") or "").strip()
+ lowered = pinned.lower()
+ return lowered if lowered and lowered != "auto" else None
+
+
+async def _retranscribe_from_audio(
+ entry: Any, *, language: str | None
+) -> tuple[str, str, str | None]:
+ """Transcribe a kept audio sidecar again. ``(text, language, detail)``.
+
+ ``detail`` is a plain-English explanation of why nothing came back, or
+ ``None`` when it did. Every failure path returns one instead of raising:
+ the caller turns it into a normal 200 with ``retranscribed: false``, so a
+ host without a provider gets an honest sentence rather than a 500.
+ """
+ from jarvis.dictation.audio import load_dictation_audio
+
+ pipeline = _pipeline()
+ # The pipeline's final-transcription provider is the right one to reuse:
+ # it is already wrapped with the user's spoken-vocabulary corrections, so a
+ # restore spells names the same way the original dictation would have.
+ stt = getattr(pipeline, "_utterance_stt", None) if pipeline is not None else None
+ if stt is None:
+ return "", "", _NO_STT_DETAIL
+
+ # Reading and decoding a WAV is blocking work; it must not sit on the event
+ # loop that a live voice turn shares.
+ pcm = await asyncio.to_thread(load_dictation_audio, entry.audio_path)
+ if not pcm:
+ return "", "", "The saved audio for this dictation could not be read."
+
+ try:
+ if language is None:
+ transcript = await stt.transcribe_pcm(pcm)
+ else:
+ try:
+ transcript = await stt.transcribe_pcm(pcm, language=language)
+ except TypeError:
+ # A provider that predates the keyword — the contract allows a
+ # bare ``transcribe_pcm(pcm)``. Falling back beats calling the
+ # user's language pin a failure (precedent: rolling_whisper_wake).
+ transcript = await stt.transcribe_pcm(pcm)
+ except Exception as exc: # noqa: BLE001 — a failed restore is never a 500
+ log.warning("dictation restore transcription failed: %s", exc, exc_info=True)
+ return "", "", f"Transcribing the saved audio failed: {exc}"
+
+ text = str(getattr(transcript, "text", "") or "").strip()
+ detected = str(getattr(transcript, "language", "") or "")
+ if not text:
+ return "", detected, "The saved audio produced no text — it may be silence."
+ return text, detected, None
+
+
# ----------------------------------------------------------------------
# Request models
# ----------------------------------------------------------------------
@@ -97,6 +180,22 @@ class SettingsBody(BaseModel):
history_enabled: bool | None = None
history_max_entries: int | None = None
history_retention_days: int | None = None
+ language: str | None = Field(
+ default=None,
+ description=(
+ "Language dictation is transcribed in: auto (detect per utterance, "
+ "right for almost everyone) or one supported locale"
+ ),
+ )
+ keep_failed_audio: bool | None = Field(
+ default=None,
+ description=(
+ "Keep the raw audio of a dictation that produced nothing usable, so "
+ "it can be transcribed again. Never kept for a successful one."
+ ),
+ )
+ audio_retention_days: int | None = None
+ audio_max_files: int | None = None
persist: bool = Field(
default=True, description="Also write the change to jarvis.toml"
)
@@ -155,6 +254,10 @@ async def get_status(request: Request) -> dict[str, Any]:
"active": active,
"reason": reason,
"hotkey": str(getattr(trigger, "hotkey_dictate", "") or ""),
+ # The hands-free key is its own action, not a mode of the hold key, so
+ # both can be armed at once. Reported separately for the same reason:
+ # a UI that had to infer it from ``mode`` could not show the two rows.
+ "hotkey_toggle": str(getattr(trigger, "hotkey_dictate_toggle", "") or ""),
"mode": str(getattr(dictation, "mode", "hold")),
"target": str(getattr(dictation, "target", "auto")),
"insertion": insertion,
@@ -214,26 +317,177 @@ async def stop() -> dict[str, Any]:
@router.get("/history")
-async def get_history(limit: int = 50) -> dict[str, Any]:
+async def get_history(
+ limit: int = 50,
+ include_discarded: bool = Query(
+ default=False,
+ description=(
+ "Also return entries the user discarded. The UI asks for them "
+ "because they are the ones Restore exists for."
+ ),
+ ),
+) -> dict[str, Any]:
"""Recent dictations, newest first — raw text alongside the cleaned text.
Local-only data. It exists so a filler-cleanup can be audited after the
fact ("did it drop a word I actually said?") and so a transcript survives
an insertion that had to fall back to the clipboard.
+
+ Discarded entries are hidden by default, which is what a script reading
+ "the history" expects. The UI opts back in: filtering them out there would
+ strand the Restore button that makes the soft delete worth having.
+
+ The wire shape never carries ``audio_path`` — a filesystem path in a JSON
+ body is an information leak that buys the client nothing, so the entry
+ reports ``audio_available`` instead.
"""
from jarvis.dictation.history import DictationHistory
capped = max(1, min(int(limit or 50), 500))
- entries = DictationHistory().list_all()[:capped]
+ entries = DictationHistory().list_all(include_discarded=include_discarded)[:capped]
return {"entries": [e.to_dict() for e in entries], "count": len(entries)}
+async def get_stats(request: Request) -> dict[str, Any]:
+ """Lifetime dictation totals, today's numbers and the day streak.
+
+ ``source`` is the honest part and the UI must label the panel from it:
+
+ * ``lifetime`` — the never-pruned counter sidecar answered, so the totals
+ really are all-time.
+ * ``window`` — no sidecar exists yet (an install that predates it), so the
+ numbers were derived from the rolling history window. They are real, they
+ are just bounded by the retention settings, and calling a 30-day slice
+ "all time" would be a lie the user has no way to catch.
+
+ ``window`` reports the retention settings the fallback is bounded by, so
+ the UI can name the period instead of guessing at it.
+ """
+ from jarvis.dictation.history import DictationHistory
+ from jarvis.dictation.stats import DEFAULT_BY_DAY_LIMIT, summarize_entries
+
+ history = DictationHistory()
+ counters = history.stats()
+ if counters.exists:
+ payload = counters.summary(by_day_limit=DEFAULT_BY_DAY_LIMIT)
+ else:
+ payload = summarize_entries(
+ history.list_all(), by_day_limit=DEFAULT_BY_DAY_LIMIT
+ )
+
+ dictation = _dictation_cfg(request)
+ payload["window"] = {
+ "days": _as_int(getattr(dictation, "history_retention_days", 30), 30),
+ "max_entries": _as_int(getattr(dictation, "history_max_entries", 200), 200),
+ }
+ return payload
+
+
[email protected]("/history/{entry_id}/discard")
+async def discard_history_entry(entry_id: str) -> dict[str, Any]:
+ """Hide one entry without deleting it — the recoverable trash icon.
+
+ Soft on purpose. ``discarded`` is a boolean beside the outcome rather than
+ an outcome of its own, because an entry can be both ``inserted`` and
+ discarded, and folding the two into one string makes that unrepresentable.
+ """
+ from jarvis.dictation.history import DictationHistory
+
+ history = DictationHistory()
+ if history.get(entry_id) is None:
+ raise HTTPException(status_code=404, detail="No dictation has that id.")
+ updated = history.set_discarded(entry_id, True)
+ if updated is None:
+ raise HTTPException(
+ status_code=500, detail="The dictation entry could not be updated."
+ )
+ return {"ok": True, "entry": updated.to_dict()}
+
+
[email protected]("/history/{entry_id}/restore")
+async def restore_history_entry(entry_id: str, request: Request) -> dict[str, Any]:
+ """Un-discard one entry and, when there is text to win back, re-transcribe.
+
+ Two different jobs behind one button, because from the user's side they are
+ one thing ("give me that back"):
+
+ 1. A discarded entry that still has its text simply stops being hidden.
+ 2. An entry that ended with nothing — a provider 401, a wedged engine, a
+ transcript that came back empty — is transcribed again from the audio
+ that was kept for exactly this moment.
+
+ Never a 500 on a missing provider. A host with no speech-to-text reachable
+ still restores the entry and says why the words did not come back; that is
+ a disappointment, not a failed request.
+ """
+ from jarvis.dictation.audio import audio_exists
+ from jarvis.dictation.cleanup import count_words
+ from jarvis.dictation.history import DictationHistory
+
+ history = DictationHistory()
+ entry = history.get(entry_id)
+ if entry is None:
+ raise HTTPException(status_code=404, detail="No dictation has that id.")
+
+ has_text = bool(entry.text or entry.raw_text)
+ has_audio = audio_exists(entry.audio_path)
+ if not has_text and not has_audio:
+ raise HTTPException(
+ status_code=409,
+ detail=(
+ "There is nothing to restore: this dictation has no text and no "
+ "saved audio. Keeping audio for failed dictations is what makes "
+ "one recoverable."
+ ),
+ )
+
+ changes: dict[str, Any] = {"discarded": False}
+ detail: str | None = None
+ retranscribed = False
+ if not has_text:
+ text, detected, detail = await _retranscribe_from_audio(
+ entry, language=_pinned_language(request)
+ )
+ if text:
+ retranscribed = True
+ changes.update(
+ raw_text=text,
+ text=text,
+ word_count=count_words(text),
+ language=detected or entry.language,
+ # The recorded failure is over — but the OUTCOME stays what it
+ # was. The dictation really did fail to reach the window the
+ # user was typing in; rewriting it to "inserted" would invent
+ # a delivery that never happened.
+ error=None,
+ )
+
+ updated = history.update(entry_id, **changes)
+ if updated is None:
+ raise HTTPException(
+ status_code=500, detail="The dictation entry could not be updated."
+ )
+ return {
+ "ok": True,
+ "entry": updated.to_dict(),
+ "retranscribed": retranscribed,
+ "detail": detail,
+ }
+
+
@router.delete(
"/history",
openapi_extra={"x-jarvis-dangerous": True},
)
async def clear_history() -> dict[str, Any]:
- """Purge the whole dictation history. Irreversible."""
+ """Purge the whole dictation history. Irreversible.
+
+ Deliberately total: the entries, every kept audio sidecar and the lifetime
+ counters all go, which is why the UI copy has to say the day streak resets.
+ Leaving the counters standing after someone asked for their dictation
+ history to be deleted would be a quiet lie about what the app still knows.
+ """
from jarvis.dictation.history import DictationHistory
return {"ok": bool(DictationHistory().clear())}
@@ -241,7 +495,12 @@ async def clear_history() -> dict[str, Any]:
@router.delete("/history/{entry_id}")
async def delete_history_entry(entry_id: str) -> dict[str, Any]:
- """Drop one entry (idempotent — removing an absent id is not an error)."""
+ """Drop one entry and its audio (idempotent — an absent id is not an error).
+
+ Hard delete, kept that way on purpose: this is the contract anyone
+ scripting ``jarvis api dictation`` already has. The recoverable version the
+ UI's trash icon uses is ``POST /history/{id}/discard``.
+ """
from jarvis.dictation.history import DictationHistory
return {"removed": bool(DictationHistory().delete(entry_id))}
@@ -254,7 +513,15 @@ async def delete_history_entry(entry_id: str) -> dict[str, Any]:
@router.get("/settings")
async def get_settings(request: Request) -> dict[str, Any]:
- """The live ``[dictation]`` block plus the accepted values per key."""
+ """The live ``[dictation]`` block plus the accepted values per key.
+
+ ``choices`` is what every dropdown in the UI is built from, and it is
+ hand-maintained: a key added to ``DICTATION_SETTING_KEYS`` without an entry
+ here renders an empty list the user cannot pick anything out of. The
+ language list is the one exception — it is derived from
+ ``DICTATION_LANGUAGES`` so adding a locale means touching one place.
+ """
+ from jarvis.core.config import DICTATION_LANGUAGES
from jarvis.core.config_writer import DICTATION_SETTING_KEYS
dictation = _dictation_cfg(request)
@@ -266,6 +533,7 @@ async def get_settings(request: Request) -> dict[str, Any]:
"target": ["auto", "insert", "chat"],
"insert_method": ["clipboard", "type"],
"paste_chord": ["auto", "ctrl_v", "ctrl_shift_v", "shift_insert"],
+ "language": list(DICTATION_LANGUAGES),
},
}
diff --git a/tests/unit/ui/web/test_dictation_routes.py b/tests/unit/ui/web/test_dictation_routes.py
new file mode 100644
index 0000000000..066be083d4
--- /dev/null
+++ b/tests/unit/ui/web/test_dictation_routes.py
@@ -0,0 +1,570 @@
+"""REST route tests for the dictation API.
+
+The layer these cover used to be the untested one, which is how the UI ended up
+calling four endpoints that did not exist. So the assertions here are written
+against the contract the frontend actually consumes, not against the
+implementation:
+
+* ``choices`` must carry a language list, or the dropdown renders empty.
+* the wire shape must never contain ``audio_path`` — a filesystem path in a
+ JSON body is an information leak that buys the client nothing.
+* Restore must degrade to an honest sentence on a host with no speech-to-text
+ provider, never a 500.
+* every route must answer sanely with no speech pipeline at all (headless).
+
+The fixture boots a bare ``FastAPI()`` with only this router, and sandboxes
+``LOCALAPPDATA`` so the history, the counter sidecar and the audio directory all
+land in ``tmp_path`` instead of the developer's real profile.
+"""
+
+from __future__ import annotations
+
+from dataclasses import dataclass
+from pathlib import Path
+from types import SimpleNamespace
+from typing import Any
+
+import pytest
+from fastapi import FastAPI
+from fastapi.testclient import TestClient
+
+from jarvis.ui.web.dictation_routes import router as dictation_router
+
+
[email protected](autouse=True)
+def _sandbox_user_data(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
+ """Point ``user_data_dir()`` at the temp directory for the whole test."""
+ monkeypatch.setenv("LOCALAPPDATA", str(tmp_path))
+
+
+def app() -> FastAPI:
+ """A bare app carrying a real config, so no route sees a stand-in model."""
+ from jarvis.core.config import DictationConfig, TriggerConfig
+
+ application = FastAPI()
+ application.include_router(dictation_router)
+ application.state.config = SimpleNamespace(
+ trigger=TriggerConfig(), dictation=DictationConfig()
+ )
+ return application
+
+
+def client(app: FastAPI) -> TestClient:
+ return TestClient(app)
+
+
+# ----------------------------------------------------------------------
+# Fakes — a speech pipeline that only has to answer one question
+# ----------------------------------------------------------------------
+
+
+@dataclass
+class _FakeTranscript:
+ text: str
+ language: str = "en"
+
+
+class _FakeSTT:
+ """The narrow slice of an STT provider a restore actually touches."""
+
+ def __init__(self, text: str = "call the studio", *, accepts_language: bool = True):
+ self._text = text
+ self._accepts_language = accepts_language
+ self.calls: list[tuple[int, str | None]] = []
+
+ async def transcribe_pcm(self, pcm: bytes, language: str | None = None) -> Any:
+ if language is not None and not self._accepts_language:
+ raise TypeError("transcribe_pcm() got an unexpected keyword 'language'")
+ self.calls.append((len(pcm), language))
+ return _FakeTranscript(text=self._text)
+
+
+class _BrokenSTT:
+ async def transcribe_pcm(self, pcm: bytes, language: str | None = None) -> Any:
+ raise RuntimeError("provider returned 401")
+
+
+def _install_pipeline(monkeypatch: pytest.MonkeyPatch, stt: Any) -> Any:
+ """Make ``_pipeline()`` return a pipeline exposing ``stt``."""
+ pipeline = SimpleNamespace(_utterance_stt=stt)
+ monkeypatch.setattr(
+ "jarvis.core.runtime_refs.get_speech_pipeline", lambda: pipeline
+ )
+ return pipeline
+
+
+# ----------------------------------------------------------------------
+# Helpers — build a history the way the pipeline would have
+# ----------------------------------------------------------------------
+
+
+def _history() -> Any:
+ from jarvis.dictation.history import DictationHistory
+
+ return DictationHistory()
+
+
+def _add(**fields: Any) -> Any:
+ """Record one entry through the real store (defaults: a good dictation)."""
+ payload: dict[str, Any] = {
+ "raw_text": "so uh send the report",
+ "text": "send the report",
+ "language": "en",
+ "duration_s": 4.2,
+ "outcome": "inserted",
+ "method": "clipboard",
+ }
+ payload.update(fields)
+ entry = _history().add(**payload)
+ assert entry is not None
+ return entry
+
+
+def _add_failed_with_audio(text: str = "") -> Any:
+ """A dictation that produced nothing, with its audio sidecar kept."""
+ from jarvis.dictation.audio import save_dictation_audio
+
+ history = _history()
+ entry = _add(raw_text=text, text=text, outcome="failed", error="provider 401")
+ # Half a second of (silent) 16 kHz mono int16 — the shape the capture path
+ # hands over. The fake provider does not care what is in it.
+ path = save_dictation_audio(
+ entry.id, b"\x00\x01" * 8_000, directory=history.audio_dir
+ )
+ assert path is not None
+ updated = history.update(entry.id, audio_path=str(path))
+ assert updated is not None
+ return updated
+
+
+# ----------------------------------------------------------------------
+# Settings
+# ----------------------------------------------------------------------
+
+
+def test_settings_report_every_persisted_key(client: TestClient) -> None:
+ from jarvis.core.config_writer import DICTATION_SETTING_KEYS
+
+ body = client.get("/api/dictation/settings").json()
+
+ assert DICTATION_SETTING_KEYS, "the key tuple went empty — the test is blind"
+ missing = [k for k in DICTATION_SETTING_KEYS if k not in body["settings"]]
+ assert missing == []
+ assert body["settings"]["language"] == "auto"
+ assert body["settings"]["keep_failed_audio"] is True
+ assert body["settings"]["audio_retention_days"] == 7
+ assert body["settings"]["audio_max_files"] == 20
+
+
+def test_settings_offer_a_language_list_the_dropdown_can_render(
+ client: TestClient,
+) -> None:
+ """A missing ``choices`` entry is an empty dropdown, not a crash — hence a test."""
+ from jarvis.core.config import DICTATION_LANGUAGES
+
+ choices = client.get("/api/dictation/settings").json()["choices"]
+
+ assert choices["language"] == list(DICTATION_LANGUAGES)
+ assert "auto" in choices["language"]
+ # Every key with a fixed value set has a list; a new one must land here too.
+ for key in ("mode", "target", "insert_method", "paste_chord", "language"):
+ assert choices[key], f"choices.{key} is empty"
+
+
+def test_put_settings_accepts_the_new_keys(client: TestClient) -> None:
+ body = client.put(
+ "/api/dictation/settings",
+ json={
+ "language": "de",
+ "keep_failed_audio": False,
+ "audio_retention_days": 3,
+ "audio_max_files": 5,
+ "persist": False,
+ },
+ ).json()
+
+ assert body["ok"] is True
+ assert body["settings"]["language"] == "de"
+ # False must survive: a filter that drops falsy values would make the
+ # privacy switch impossible to turn OFF.
+ assert body["settings"]["keep_failed_audio"] is False
+ assert body["settings"]["audio_retention_days"] == 3
+ assert body["settings"]["audio_max_files"] == 5
+
+
+def test_put_settings_persists_the_new_keys(
+ client: TestClient, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ written: list[tuple[str, Any]] = []
+ monkeypatch.setattr(
+ "jarvis.core.config_writer.set_dictation_setting",
+ lambda key, value: written.append((key, value)),
+ )
+
+ body = client.put(
+ "/api/dictation/settings", json={"language": "es", "persist": True}
+ ).json()
+
+ assert body["persisted"] is True
+ assert ("language", "es") in written
+
+
+def test_put_settings_rejects_an_unknown_language(client: TestClient) -> None:
+ """The config coerces an unknown value to ``auto`` rather than failing."""
+ body = client.put(
+ "/api/dictation/settings", json={"language": "klingon", "persist": False}
+ ).json()
+
+ assert body["settings"]["language"] == "auto"
+
+
+# ----------------------------------------------------------------------
+# Status
+# ----------------------------------------------------------------------
+
+
+def test_status_reports_the_hands_free_shortcut(app: FastAPI) -> None:
+ app.state.config.trigger.hotkey_dictate_toggle = "ctrl+shift+d"
+ app.state.config.trigger.hotkey_dictate = "ctrl+shift+space"
+
+ body = TestClient(app).get("/api/dictation/status").json()
+
+ assert body["hotkey"] == "ctrl+shift+space"
+ assert body["hotkey_toggle"] == "ctrl+shift+d"
+
+
+def test_status_reports_an_unbound_hands_free_key_as_empty(client: TestClient) -> None:
+ body = client.get("/api/dictation/status").json()
+
+ assert "hotkey_toggle" in body
+ assert isinstance(body["hotkey_toggle"], str)
+
+
+# ----------------------------------------------------------------------
+# History
+# ----------------------------------------------------------------------
+
+
+def test_history_hides_discarded_entries_by_default(client: TestClient) -> None:
+ kept = _add()
+ dropped = _add(text="book the flight", raw_text="book the flight")
+ _history().set_discarded(dropped.id, True)
+
+ body = client.get("/api/dictation/history").json()
+
+ ids = [e["id"] for e in body["entries"]]
+ assert kept.id in ids
+ assert dropped.id not in ids
+ assert body["count"] == 1
+
+
+def test_history_includes_discarded_entries_when_asked(client: TestClient) -> None:
+ """The UI opts in — a filtered-out row could never reach its Restore button."""
+ kept = _add()
+ dropped = _add(text="book the flight", raw_text="book the flight")
+ _history().set_discarded(dropped.id, True)
+
+ body = client.get("/api/dictation/history?include_discarded=true").json()
+
+ ids = [e["id"] for e in body["entries"]]
+ assert kept.id in ids
+ assert dropped.id in ids
+ row = next(e for e in body["entries"] if e["id"] == dropped.id)
+ assert row["discarded"] is True
+
+
+def test_history_never_exposes_the_audio_path(client: TestClient) -> None:
+ entry = _add_failed_with_audio()
+ assert entry.audio_path, "the fixture did not actually store audio"
+
+ body = client.get("/api/dictation/history?include_discarded=true").json()
+ row = next(e for e in body["entries"] if e["id"] == entry.id)
+
+ assert "audio_path" not in row
+ assert row["audio_available"] is True
+ assert row["error"] == "provider 401"
+ assert row["word_count"] == 0
+
+
+def test_history_respects_the_limit(client: TestClient) -> None:
+ for index in range(3):
+ _add(text=f"line {index}", raw_text=f"line {index}")
+
+ body = client.get("/api/dictation/history?limit=2").json()
+
+ assert body["count"] == 2
+
+
+# ----------------------------------------------------------------------
+# Stats
+# ----------------------------------------------------------------------
+
+
+def test_stats_report_lifetime_totals_from_the_sidecar(client: TestClient) -> None:
+ _add() # "send the report" -> three words
+ _add(text="book the flight", raw_text="book the flight", duration_s=2.0)
+
+ body = client.get("/api/dictation/stats").json()
+
+ assert body["source"] == "lifetime"
+ assert body["totals"]["dictations"] == 2
+ assert body["totals"]["words"] == 6
+ assert body["totals"]["wpm"] > 0
+ assert body["today"]["dictations"] == 2
+ assert body["streak"]["current_days"] == 1
+ assert body["by_day"][0]["words"] == 6
+
+
+def test_stats_say_so_when_they_are_only_a_window(client: TestClient) -> None:
+ """An install predating the sidecar gets real numbers, honestly labelled."""
+ _add()
+ history = _history()
+ history.stats_path.unlink() # as if the counters had never existed
+
+ body = client.get("/api/dictation/stats").json()
+
+ assert body["source"] == "window"
+ assert body["totals"]["words"] == 3
+ # The UI names the period from this, so it must reflect the real retention.
+ assert body["window"]["days"] == 30
+ assert body["window"]["max_entries"] == 200
+
+
+def test_stats_answer_on_an_install_that_has_never_dictated(
+ client: TestClient,
+) -> None:
+ body = client.get("/api/dictation/stats").json()
+
+ assert body["source"] == "window"
+ assert body["totals"] == {"dictations": 0, "words": 0, "seconds": 0.0, "wpm": 0.0}
+ assert body["streak"]["current_days"] == 0
+ assert body["by_day"] == []
+
+
+# ----------------------------------------------------------------------
+# Discard
+# ----------------------------------------------------------------------
+
+
+def test_discard_soft_deletes_the_entry(client: TestClient) -> None:
+ entry = _add()
+
+ body = client.post(f"/api/dictation/history/{entry.id}/discard").json()
+
+ assert body["ok"] is True
+ assert body["entry"]["discarded"] is True
+ assert "audio_path" not in body["entry"]
+ # Still on disk — that is the whole difference from DELETE.
+ stored = _history().get(entry.id)
+ assert stored is not None
+ assert stored.discarded is True
+
+
+def test_discard_404s_on_an_unknown_id(client: TestClient) -> None:
+ response = client.post("/api/dictation/history/nope/discard")
+
+ assert response.status_code == 404
+
+
+# ----------------------------------------------------------------------
+# Restore
+# ----------------------------------------------------------------------
+
+
+def test_restore_undiscards_an_entry_that_still_has_its_text(
+ client: TestClient,
+) -> None:
+ entry = _add()
+ _history().set_discarded(entry.id, True)
+
+ body = client.post(f"/api/dictation/history/{entry.id}/restore").json()
+
+ assert body["ok"] is True
+ assert body["entry"]["discarded"] is False
+ assert body["entry"]["text"] == "send the report"
+ # Nothing was re-transcribed: there was nothing to win back.
+ assert body["retranscribed"] is False
+ assert body["detail"] is None
+
+
+def test_restore_retranscribes_from_the_kept_audio(
+ client: TestClient, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ stt = _FakeSTT("call the studio")
+ _install_pipeline(monkeypatch, stt)
+ entry = _add_failed_with_audio()
+ _history().set_discarded(entry.id, True)
+
+ body = client.post(f"/api/dictation/history/{entry.id}/restore").json()
+
+ assert body["retranscribed"] is True
+ assert body["detail"] is None
+ assert body["entry"]["text"] == "call the studio"
+ assert body["entry"]["raw_text"] == "call the studio"
+ assert body["entry"]["word_count"] == 3
+ assert body["entry"]["discarded"] is False
+ assert body["entry"]["error"] is None
+ # The outcome stays "failed": the words came back, the delivery never
+ # happened, and rewriting it would invent an insertion.
+ assert body["entry"]["outcome"] == "failed"
+ assert stt.calls and stt.calls[0][0] > 0
+
+
+def test_restore_passes_the_pinned_language_to_the_provider(
+ app: FastAPI, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ stt = _FakeSTT("ruf das studio an")
+ _install_pipeline(monkeypatch, stt)
+ app.state.config.dictation.language = "de"
+ entry = _add_failed_with_audio()
+
+ TestClient(app).post(f"/api/dictation/history/{entry.id}/restore")
+
+ assert stt.calls[0][1] == "de"
+
+
+def test_restore_falls_back_when_the_provider_predates_the_language_keyword(
+ app: FastAPI, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """A bare ``transcribe_pcm(pcm)`` is allowed by the contract; honour it."""
+ stt = _FakeSTT("call the studio", accepts_language=False)
+ _install_pipeline(monkeypatch, stt)
+ app.state.config.dictation.language = "de"
+ entry = _add_failed_with_audio()
+
+ body = TestClient(app).post(f"/api/dictation/history/{entry.id}/restore").json()
+
+ assert body["retranscribed"] is True
+ assert body["entry"]["text"] == "call the studio"
+
+
+def test_restore_degrades_honestly_with_no_provider_reachable(
+ client: TestClient,
+) -> None:
+ """Headless: the entry comes back, the words do not, and it says why."""
+ entry = _add_failed_with_audio()
+ _history().set_discarded(entry.id, True)
+
+ response = client.post(f"/api/dictation/history/{entry.id}/restore")
+
+ assert response.status_code == 200
+ body = response.json()
+ assert body["ok"] is True
+ assert body["retranscribed"] is False
+ assert body["detail"]
+ assert "speech-to-text" in body["detail"]
+ assert body["entry"]["discarded"] is False
+
+
+def test_restore_reports_a_provider_failure_without_500ing(
+ client: TestClient, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ _install_pipeline(monkeypatch, _BrokenSTT())
+ entry = _add_failed_with_audio()
+
+ response = client.post(f"/api/dictation/history/{entry.id}/restore")
+
+ assert response.status_code == 200
+ body = response.json()
+ assert body["retranscribed"] is False
+ assert "401" in body["detail"]
+ assert body["entry"]["discarded"] is False
+
+
+def test_restore_409s_when_there_is_nothing_to_recover(client: TestClient) -> None:
+ entry = _add(raw_text="", text="", outcome="cancelled")
+
+ response = client.post(f"/api/dictation/history/{entry.id}/restore")
+
+ assert response.status_code == 409
+ assert "nothing to restore" in response.json()["detail"].lower()
+
+
+def test_restore_404s_on_an_unknown_id(client: TestClient) -> None:
+ assert client.post("/api/dictation/history/nope/restore").status_code == 404
+
+
+# ----------------------------------------------------------------------
+# Delete
+# ----------------------------------------------------------------------
+
+
+def test_delete_entry_also_removes_the_audio_sidecar(client: TestClient) -> None:
+ entry = _add_failed_with_audio()
+ sidecar = Path(entry.audio_path)
+ assert sidecar.is_file()
+
+ body = client.delete(f"/api/dictation/history/{entry.id}").json()
+
+ assert body["removed"] is True
+ assert not sidecar.exists()
+
+
+def test_delete_an_absent_entry_is_not_an_error(client: TestClient) -> None:
+ response = client.delete("/api/dictation/history/nope")
+
+ assert response.status_code == 200
+ assert response.json()["removed"] is False
+
+
+def test_clear_history_purges_audio_and_resets_the_counters(
+ client: TestClient,
+) -> None:
+ _add()
+ entry = _add_failed_with_audio()
+ sidecar = Path(entry.audio_path)
+ history = _history()
+ assert history.stats_path.is_file()
+
+ body = client.delete("/api/dictation/history").json()
+
+ assert body["ok"] is True
+ assert client.get("/api/dictation/history").json()["entries"] == []
+ assert not sidecar.exists()
+ # The streak resets with the history — the UI copy promises exactly this.
+ stats = client.get("/api/dictation/stats").json()
+ assert stats["totals"]["words"] == 0
+ assert stats["streak"]["current_days"] == 0
+
+
+# ----------------------------------------------------------------------
+# Headless
+# ----------------------------------------------------------------------
+
+
+def test_every_read_route_answers_without_a_speech_pipeline(
+ client: TestClient,
+) -> None:
+ """A headless VPS has no mic and no pipeline; nothing here may 500."""
+ for path in (
+ "/api/dictation/status",
+ "/api/dictation/history",
+ "/api/dictation/stats",
+ "/api/dictation/settings",
+ ):
+ assert client.get(path).status_code == 200, path
+
+ status = client.get("/api/dictation/status").json()
+ assert status["available"] is False
+ assert status["reason"]
+
+ # Stopping nothing is not an error; starting without a mic says so.
+ assert client.post("/api/dictation/stop").json() == {
+ "ok": True,
+ "stopped": False,
+ "active": False,
+ }
+ assert client.post("/api/dictation/start", json={"target": "auto"}).status_code == 503
+
+
+def test_routes_survive_an_app_with_no_config_at_all() -> None:
+ """The launcher's early window: routes mounted before config lands."""
+ bare = FastAPI()
+ bare.include_router(dictation_router)
+ bare_client = TestClient(bare)
+
+ assert bare_client.get("/api/dictation/status").json()["hotkey_toggle"] == ""
+ assert bare_client.get("/api/dictation/settings").json()["choices"]["language"]
+ assert bare_client.get("/api/dictation/stats").json()["window"]["days"] == 30