Files
VoiceStudio/tests/test_setup_warmup_task.py
Palash Debnath 012eb41e3a ci(python): gate Ruff and ty on the rules the code already passes (#4614)
* ci(python): gate Ruff and ty on the rules the code already passes

Ruff and ty are pinned in the dev group (never installed for users: the
app syncs with --no-dev and Docker installs without groups). CI's first
Python shard and `bun run lint:py` run the same checks.

- Ruff checks runtime-bug rules: syntax, undefined names and exports,
  f-strings without fields, invalid characters, dropped asyncio tasks.
- ty fails on any rule the code passes today; the 23 rules with a backlog
  warn, so new instances of every other rule fail CI.

Fixes what the rules found:
- The setup warmup task was not referenced and could be garbage-collected
  mid-load; it is now held until it finishes.
- A perf-budget test fake called time.sleep without importing time.
- The scam guard's invisible-character class is written as escapes.
- Type-only names (httpx, Callable) are imported under TYPE_CHECKING.

* changelog: add fragment for #4614

* build(python): let lint:py sync the pinned tools before running them
2026-12-03 07:10:37 -08:00

38 lines
1.0 KiB
Python

"""POST /setup/warmup keeps its background model load alive until it finishes.
The event loop holds tasks only weakly, so a fire-and-forget warmup could be
garbage-collected partway through loading the model.
"""
from __future__ import annotations
import asyncio
import os
import sys
import types
os.environ.setdefault("OMNIVOICE_MODEL", "test")
os.environ.setdefault("OMNIVOICE_DISABLE_FILE_LOG", "1")
def test_warmup_task_is_held_until_it_finishes(monkeypatch):
from api.routers.setup import wizard
async def scenario():
release = asyncio.Event()
async def get_model():
await release.wait()
monkeypatch.setitem(
sys.modules, "services.model_manager", types.SimpleNamespace(get_model=get_model),
)
assert await wizard.setup_warmup() == {"status": "warmup_started"}
(task,) = wizard._warmup_tasks
assert not task.done()
release.set()
await task
await asyncio.sleep(0)
assert not wizard._warmup_tasks
asyncio.run(scenario())