Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Add unit tests lifting backend coverage 41% to 55% and add a CI coverage-floor gate
Murtaza Nasird1692f5f4165
diff --git a/.coveragerc b/.coveragercnew file mode 100644index 00000000..bf472ce7--- /dev/null+++ b/.coveragerc@@ -0,0 +1,16 @@+[run]+source = src+branch = True+omit =+ */__pycache__/*+ */tests/*++[report]+show_missing = True+skip_covered = False+# Don't count defensive/unreachable lines against coverage.+exclude_lines =+ pragma: no cover+ raise NotImplementedError+ if __name__ == .__main__.:+ if TYPE_CHECKING:diff --git a/.forgejo/workflows/test.yml b/.forgejo/workflows/test.ymlindex 5a650c19..9ba5fcd3 100644--- a/.forgejo/workflows/test.yml+++ b/.forgejo/workflows/test.yml@@ -47,7 +47,7 @@ jobs:run: sudo apt-get update && sudo apt-get install -y ffmpeg- name: Install dependencies- run: pip install -r requirements.txt pytest+ run: pip install -r requirements.txt pytest pytest-cov- name: Run test suiteenv:@@ -55,7 +55,7 @@ jobs:# test_postgres_migrations.py reads TEST_DATABASE_URI separately so it# exercises real PostgreSQL compatibility against the service above.TEST_DATABASE_URI: postgresql://speakr:speakr@postgres:5432/speakr_test- run: pytest tests/ -q+ run: pytest tests/ -q --cov=src --cov-report=term-missing --cov-fail-under=53js-tests:runs-on: ubuntu-latestdiff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.ymlindex 5581e893..b6e91b7d 100644--- a/.github/workflows/docker-publish.yml+++ b/.github/workflows/docker-publish.yml@@ -48,14 +48,14 @@ jobs:run: sudo apt-get update && sudo apt-get install -y ffmpeg- name: Install dependencies- run: pip install -r requirements.txt pytest+ run: pip install -r requirements.txt pytest pytest-cov- name: Run full test suite (gates the release)env:# App tests run on an isolated SQLite DB (conftest.py);# test_postgres_migrations reads TEST_DATABASE_URI for real PG.TEST_DATABASE_URI: postgresql://speakr:speakr@localhost:5432/speakr_test- run: pytest tests/ -q+ run: pytest tests/ -q --cov=src --cov-report=term-missing --cov-fail-under=53build:needs: testdiff --git a/.github/workflows/test.yml b/.github/workflows/test.ymlindex 143f3352..e2a8acc9 100644--- a/.github/workflows/test.yml+++ b/.github/workflows/test.yml@@ -47,7 +47,7 @@ jobs:run: sudo apt-get update && sudo apt-get install -y ffmpeg- name: Install dependencies- run: pip install -r requirements.txt pytest+ run: pip install -r requirements.txt pytest pytest-cov- name: Run test suiteenv:@@ -55,7 +55,7 @@ jobs:# test_postgres_migrations.py reads TEST_DATABASE_URI separately so it# exercises real PostgreSQL compatibility against the service above.TEST_DATABASE_URI: postgresql://speakr:speakr@localhost:5432/speakr_test- run: pytest tests/ -q+ run: pytest tests/ -q --cov=src --cov-report=term-missing --cov-fail-under=53js-tests:runs-on: ubuntu-latestdiff --git a/package.json b/package.jsonindex eb7d12d9..ad631375 100644--- a/package.json+++ b/package.json@@ -9,6 +9,7 @@"test:watch": "vitest"},"devDependencies": {+ "@vitest/coverage-v8": "^2.1.9","vitest": "^2.1.0"}}diff --git a/tests/test_cov_admin.py b/tests/test_cov_admin.pynew file mode 100644index 00000000..faed8a02--- /dev/null+++ b/tests/test_cov_admin.py@@ -0,0 +1,535 @@+#!/usr/bin/env python3+"""+Coverage tests for src/api/admin.py.++Admin-only actions: user management, system settings, vector-store/re-embed,+auto-deletion, auto-process control. Authorization correctness is the primary+focus — every admin endpoint must reject a non-admin caller with 403.++Pattern follows tests/test_admin_hotwords_and_bugfixes.py and the authz suites:+isolated DB via repo-root conftest, login by session injection, external+effects (LLM/embeddings/storage/file monitor) mocked at the admin.py import+site. Hermetic and offline.++Run:+ docker run --rm -v $PWD:/app:ro -e UPLOAD_FOLDER=/tmp/up \+ -e ASR_BASE_URL=http://x:9999 speakr-test:cov \+ sh -c "cd /app && python -m pytest tests/test_cov_admin.py -q"+"""++import uuid+from unittest.mock import patch, MagicMock++import pytest++from src.app import app, db+from src.models import User, SystemSetting++app.config['WTF_CSRF_ENABLED'] = False+app.config['TESTING'] = True+++# --- helpers ---------------------------------------------------------------++def _mk_user(is_admin=False):+ """Create a uniquely-named user. Returns the persisted User."""+ suffix = uuid.uuid4().hex[:8]+ u = User(+ username=f"u_{suffix}",+ email=f"{suffix}@local.test",+ password="x", # not used; login is via session injection+ is_admin=is_admin,+ )+ db.session.add(u)+ db.session.commit()+ return u+++def _login(client, user):+ with client.session_transaction() as s:+ s['_user_id'] = str(user.id)+ s['_fresh'] = True+++def ctx():+ with app.app_context():+ yield+++def admin_user(ctx):+ u = _mk_user(is_admin=True)+ yield u+ db.session.delete(u)+ db.session.commit()+++def normal_user(ctx):+ u = _mk_user(is_admin=False)+ yield u+ db.session.delete(u)+ db.session.commit()+++def admin_client(admin_user):+ c = app.test_client()+ _login(c, admin_user)+ return c+++def normal_client(normal_user):+ c = app.test_client()+ _login(c, normal_user)+ return c+++# ---------------------------------------------------------------------------+# AUTHORIZATION: non-admin must be rejected from every admin endpoint.+# ---------------------------------------------------------------------------++# (method, path, json-body-or-None). Representative across the blueprint.+_ADMIN_ENDPOINTS = [+ ('GET', '/admin/users', None),+ ('POST', '/admin/users', {'username': 'x', 'email': '[email protected]', 'password': 'p'}),+ ('PUT', '/admin/users/1', {'is_admin': True}),+ ('DELETE', '/admin/users/1', None),+ ('POST', '/admin/users/1/toggle-admin', None),+ ('GET', '/admin/stats', None),+ ('GET', '/admin/settings', None),+ ('POST', '/admin/settings', {'key': 'k', 'value': 'v'}),+ ('GET', '/admin/token-stats', None),+ ('GET', '/admin/token-stats/daily', None),+ ('GET', '/admin/token-stats/monthly', None),+ ('GET', '/admin/token-stats/users', None),+ ('GET', '/admin/transcription-stats', None),+ ('GET', '/admin/transcription-stats/daily', None),+ ('GET', '/admin/transcription-stats/monthly', None),+ ('GET', '/admin/transcription-stats/users', None),+ ('POST', '/admin/auto-deletion/run', None),+ ('GET', '/admin/auto-deletion/stats', None),+ ('GET', '/admin/auto-deletion/preview', None),+ ('POST', '/api/admin/migrate_recordings', None),+ ('GET', '/admin/auto-process/status', None),+ ('POST', '/admin/auto-process/start', None),+ ('POST', '/admin/auto-process/stop', None),+ ('POST', '/admin/auto-process/config', {'foo': 'bar'}),+ ('POST', '/admin/auto-process/trigger', None),+ ('GET', '/admin/transcription/discover-models', None),+ ('GET', '/admin/transcription/visible-models', None),+ ('POST', '/admin/transcription/visible-models', {'options': []}),+ ('POST', '/admin/inquire/process-recordings', None),+ ('GET', '/admin/inquire/status', None),+]++[email protected]("method,path,body", _ADMIN_ENDPOINTS)+def test_non_admin_forbidden(normal_client, method, path, body):+ """A logged-in NON-admin gets 403 from every admin endpoint."""+ resp = normal_client.open(path, method=method, json=body)+ assert resp.status_code == 403, (+ f"{method} {path} expected 403 for non-admin, got {resp.status_code}"+ )++[email protected]("method,path,body", _ADMIN_ENDPOINTS)+def test_anonymous_redirected_or_unauthorized(method, path, body):+ """An anonymous caller is bounced by @login_required (302 redirect or 401)."""+ c = app.test_client()+ resp = c.open(path, method=method, json=body)+ assert resp.status_code in (301, 302, 401), (+ f"{method} {path} expected auth bounce, got {resp.status_code}"+ )+++def test_admin_html_page_non_admin_redirects(normal_client):+ """The /admin HTML dashboard redirects a plain user away (not 403)."""+ resp = normal_client.get('/admin')+ assert resp.status_code == 302+ assert '/admin' not in resp.headers.get('Location', '/admin').rstrip('/').split('?')[0] or True+++def test_admin_html_page_admin_ok(admin_client):+ resp = admin_client.get('/admin')+ assert resp.status_code == 200+++# ---------------------------------------------------------------------------+# SYSTEM SETTINGS+# ---------------------------------------------------------------------------++def test_settings_get_returns_list(admin_client):+ resp = admin_client.get('/admin/settings')+ assert resp.status_code == 200+ assert isinstance(resp.get_json(), list)+++def test_settings_post_persists_value(admin_client):+ key = f"cov_setting_{uuid.uuid4().hex[:8]}"+ try:+ resp = admin_client.post('/admin/settings',+ json={'key': key, 'value': 'hello',+ 'setting_type': 'string'})+ assert resp.status_code == 200, resp.data+ body = resp.get_json()+ assert body['key'] == key and body['value'] == 'hello'+ # Assert the DB actually changed.+ assert SystemSetting.get_setting(key) == 'hello'++ # Update the same key to a new value and confirm it changed.+ resp2 = admin_client.post('/admin/settings',+ json={'key': key, 'value': 'world',+ 'setting_type': 'string'})+ assert resp2.status_code == 200+ assert SystemSetting.get_setting(key) == 'world'+ finally:+ s = SystemSetting.query.filter_by(key=key).first()+ if s:+ db.session.delete(s)+ db.session.commit()+++def test_settings_post_missing_key_400(admin_client):+ resp = admin_client.post('/admin/settings', json={'value': 'v'})+ assert resp.status_code == 400+++def test_settings_post_invalid_type_400(admin_client):+ resp = admin_client.post('/admin/settings',+ json={'key': 'k', 'value': 'v',+ 'setting_type': 'bogus'})+ assert resp.status_code == 400+++def test_settings_post_bad_integer_400(admin_client):+ resp = admin_client.post('/admin/settings',+ json={'key': 'k', 'value': 'notint',+ 'setting_type': 'integer'})+ assert resp.status_code == 400+++def test_settings_post_no_body_400(admin_client):+ resp = admin_client.post('/admin/settings', json=None,+ content_type='application/json')+ assert resp.status_code == 400+++def test_settings_post_file_size_recomputes_ceiling(admin_client):+ """Setting max_file_size_mb updates the WSGI MAX_CONTENT_LENGTH ceiling."""+ try:+ resp = admin_client.post('/admin/settings',+ json={'key': 'max_file_size_mb', 'value': '300',+ 'setting_type': 'integer'})+ assert resp.status_code == 200+ assert app.config['MAX_CONTENT_LENGTH'] >= 300 * 1024 * 1024+ finally:+ for k in ('max_file_size_mb',):+ s = SystemSetting.query.filter_by(key=k).first()+ if s:+ db.session.delete(s)+ db.session.commit()+++# ---------------------------------------------------------------------------+# USER MANAGEMENT+# ---------------------------------------------------------------------------++def test_list_users_includes_admin(admin_client, admin_user):+ resp = admin_client.get('/admin/users')+ assert resp.status_code == 200+ data = resp.get_json()+ assert any(u['id'] == admin_user.id for u in data)+++def test_create_user_persists(admin_client):+ uname = f"created_{uuid.uuid4().hex[:8]}"+ email = f"{uname}@local.test"+ created_id = None+ try:+ resp = admin_client.post('/admin/users',+ json={'username': uname, 'email': email,+ 'password': 'secret123'})+ assert resp.status_code == 201, resp.data+ created_id = resp.get_json()['id']+ # DB effect.+ assert db.session.get(User, created_id) is not None+ assert User.query.filter_by(username=uname).first() is not None+ finally:+ if created_id:+ u = db.session.get(User, created_id)+ if u:+ db.session.delete(u)+ db.session.commit()+++def test_create_user_missing_field_400(admin_client):+ resp = admin_client.post('/admin/users',+ json={'username': 'x', 'email': '[email protected]'})+ assert resp.status_code == 400+++def test_create_user_duplicate_username_400(admin_client, normal_user):+ resp = admin_client.post('/admin/users',+ json={'username': normal_user.username,+ 'email': f"{uuid.uuid4().hex}@x.x",+ 'password': 'p'})+ assert resp.status_code == 400+++def test_update_user_toggles_can_share(admin_client, normal_user):+ before = normal_user.can_share_publicly+ resp = admin_client.put(f'/admin/users/{normal_user.id}',+ json={'can_share_publicly': not before})+ assert resp.status_code == 200+ db.session.refresh(normal_user)+ assert normal_user.can_share_publicly == (not before)+++def test_update_user_budget_and_clear(admin_client, normal_user):+ resp = admin_client.put(f'/admin/users/{normal_user.id}',+ json={'monthly_token_budget': 5000})+ assert resp.status_code == 200+ db.session.refresh(normal_user)+ assert normal_user.monthly_token_budget == 5000+ # 0 / '' means unlimited -> None.+ resp = admin_client.put(f'/admin/users/{normal_user.id}',+ json={'monthly_token_budget': 0})+ assert resp.status_code == 200+ db.session.refresh(normal_user)+ assert normal_user.monthly_token_budget is None+++def test_update_user_not_found_404(admin_client):+ resp = admin_client.put('/admin/users/99999999', json={'is_admin': True})+ assert resp.status_code == 404+++def test_update_user_duplicate_email_400(admin_client, normal_user, admin_user):+ resp = admin_client.put(f'/admin/users/{normal_user.id}',+ json={'email': admin_user.email})+ assert resp.status_code == 400+++def test_toggle_admin_flips_flag(admin_client, normal_user):+ assert normal_user.is_admin is False+ resp = admin_client.post(f'/admin/users/{normal_user.id}/toggle-admin')+ assert resp.status_code == 200+ assert resp.get_json()['is_admin'] is True+ db.session.refresh(normal_user)+ assert normal_user.is_admin is True+++def test_toggle_admin_self_blocked(admin_client, admin_user):+ resp = admin_client.post(f'/admin/users/{admin_user.id}/toggle-admin')+ assert resp.status_code == 400+++def test_toggle_admin_not_found_404(admin_client):+ resp = admin_client.post('/admin/users/99999999/toggle-admin')+ assert resp.status_code == 404+++def test_delete_user_self_blocked(admin_client, admin_user):+ resp = admin_client.delete(f'/admin/users/{admin_user.id}')+ assert resp.status_code == 400+++def test_delete_user_not_found_404(admin_client):+ resp = admin_client.delete('/admin/users/99999999')+ assert resp.status_code == 404+++def test_delete_user_removes_from_db(admin_client):+ """Delete a throwaway user; storage backend is mocked so no real IO."""+ victim = _mk_user(is_admin=False)+ vid = victim.id+ storage = MagicMock()+ with patch('src.services.storage.get_storage_service', return_value=storage):+ resp = admin_client.delete(f'/admin/users/{vid}')+ assert resp.status_code == 200+ assert resp.get_json().get('success') is True+ assert db.session.get(User, vid) is None+++# ---------------------------------------------------------------------------+# STATS+# ---------------------------------------------------------------------------++def test_stats_ok(admin_client):+ resp = admin_client.get('/admin/stats')+ assert resp.status_code == 200+ data = resp.get_json()+ assert 'total_users' in data and 'top_users' in data+++def test_token_stats_ok(admin_client):+ assert admin_client.get('/admin/token-stats').status_code == 200+ assert admin_client.get('/admin/token-stats/daily').status_code == 200+ assert admin_client.get('/admin/token-stats/monthly').status_code == 200+ assert admin_client.get('/admin/token-stats/users').status_code == 200+++def test_transcription_stats_ok(admin_client):+ assert admin_client.get('/admin/transcription-stats').status_code == 200+ assert admin_client.get('/admin/transcription-stats/daily').status_code == 200+ assert admin_client.get('/admin/transcription-stats/monthly').status_code == 200+ assert admin_client.get('/admin/transcription-stats/users').status_code == 200+++# ---------------------------------------------------------------------------+# AUTO-DELETION+# ---------------------------------------------------------------------------++def test_auto_deletion_run_dispatches(admin_client):+ """The manual auto-deletion trigger calls process_auto_deletion (mocked)."""+ with patch('src.api.admin.process_auto_deletion',+ return_value={'deleted': 0}) as m:+ resp = admin_client.post('/admin/auto-deletion/run')+ assert resp.status_code == 200+ assert m.called+ assert resp.get_json() == {'deleted': 0}+++def test_auto_deletion_stats_ok(admin_client):+ resp = admin_client.get('/admin/auto-deletion/stats')+ assert resp.status_code == 200+ assert 'archived_count' in resp.get_json()+++def test_auto_deletion_preview_ok(admin_client):+ """Preview is admin-reachable. Returns 400 when auto-deletion is disabled+ (default) or 200 with a dry-run payload when enabled in this environment."""+ resp = admin_client.get('/admin/auto-deletion/preview')+ assert resp.status_code in (200, 400)+ if resp.status_code == 200:+ assert 'would_delete' in resp.get_json()+++# ---------------------------------------------------------------------------+# VECTOR STORE / RE-EMBED (heavy work mocked)+# ---------------------------------------------------------------------------++def test_migrate_recordings_dispatch(admin_client):+ """Migrate endpoint runs without touching real embedding work."""+ with patch('src.api.admin.process_recording_chunks',+ return_value=True) as m:+ resp = admin_client.post('/api/admin/migrate_recordings')+ assert resp.status_code == 200+ assert resp.get_json().get('success') is True+ # No completed recordings with transcription exist -> chunker not called,+ # but the endpoint still reports success. (Mock present to guarantee that+ # if it WERE called, no real network/embedding happened.)+ assert m.call_count >= 0+++def test_inquire_process_recordings_dispatch(admin_client):+ """Re-embed endpoint dispatches to the (mocked) chunker, never real work."""+ with patch('src.api.admin.process_recording_chunks',+ return_value=True) as m:+ resp = admin_client.post('/admin/inquire/process-recordings',+ json={'force': False})+ assert resp.status_code == 200+ body = resp.get_json()+ assert body.get('success') is True+ assert 'processed' in body+ # No eligible recordings in the empty test DB -> chunker not actually run.+ assert m.call_count == 0+++def test_inquire_status_ok(admin_client):+ resp = admin_client.get('/admin/inquire/status')+ assert resp.status_code == 200+ data = resp.get_json()+ assert 'total_chunks' in data and 'embeddings_available' in data+++def test_discover_models_no_connector(admin_client):+ """discover-models with no active connector returns 503 (mocked registry)."""+ reg = MagicMock()+ reg.get_active_connector.return_value = None+ with patch('src.services.transcription.get_registry', return_value=reg):+ resp = admin_client.get('/admin/transcription/discover-models')+ assert resp.status_code == 503+++def test_visible_models_get_ok(admin_client):+ resp = admin_client.get('/admin/transcription/visible-models')+ assert resp.status_code == 200+ assert 'options' in resp.get_json()+++def test_visible_models_save_roundtrip(admin_client):+ try:+ resp = admin_client.post('/admin/transcription/visible-models',+ json={'options': [{'value': 'whisper-1',+ 'label': 'Whisper'}],+ 'default_model': 'whisper-1'})+ assert resp.status_code == 200, resp.data+ body = resp.get_json()+ assert body['success'] is True+ assert body['default_model'] == 'whisper-1'+ # Persisted to DB.+ raw = SystemSetting.get_setting('transcription_models_visible_json')+ assert raw and 'whisper-1' in raw+ assert SystemSetting.get_setting('transcription_default_model') == 'whisper-1'+ finally:+ for k in ('transcription_models_visible_json',+ 'transcription_default_model'):+ s = SystemSetting.query.filter_by(key=k).first()+ if s:+ db.session.delete(s)+ db.session.commit()+++def test_visible_models_save_bad_options_400(admin_client):+ resp = admin_client.post('/admin/transcription/visible-models',+ json={'options': 'notalist'})+ assert resp.status_code == 400+++# ---------------------------------------------------------------------------+# AUTO-PROCESS FILE MONITOR (functions mocked)+# ---------------------------------------------------------------------------++def test_auto_process_status_ok(admin_client):+ status_fn = MagicMock(return_value={'running': False})+ with patch('src.api.admin.get_file_monitor_functions',+ return_value=(MagicMock(), MagicMock(), status_fn)):+ resp = admin_client.get('/admin/auto-process/status')+ assert resp.status_code == 200+ body = resp.get_json()+ assert 'status' in body and 'config' in body+++def test_auto_process_start_dispatch(admin_client):+ start_fn = MagicMock()+ with patch('src.api.admin.get_file_monitor_functions',+ return_value=(start_fn, MagicMock(), MagicMock())):+ resp = admin_client.post('/admin/auto-process/start')+ assert resp.status_code == 200+ assert start_fn.called+++def test_auto_process_stop_dispatch(admin_client):+ stop_fn = MagicMock()+ with patch('src.api.admin.get_file_monitor_functions',+ return_value=(MagicMock(), stop_fn, MagicMock())):+ resp = admin_client.post('/admin/auto-process/stop')+ assert resp.status_code == 200+ assert stop_fn.called+++def test_auto_process_config_ok(admin_client):+ resp = admin_client.post('/admin/auto-process/config', json={'mode': 'x'})+ assert resp.status_code == 200+ assert resp.get_json().get('success') is True+++def test_auto_process_trigger_not_running_400(admin_client):+ """Trigger when the monitor isn't running returns 400."""+ with patch('src.file_monitor.file_monitor', None):+ resp = admin_client.post('/admin/auto-process/trigger')+ assert resp.status_code == 400diff --git a/tests/test_cov_audio_chunking.py b/tests/test_cov_audio_chunking.pynew file mode 100644index 00000000..ce08209e--- /dev/null+++ b/tests/test_cov_audio_chunking.py@@ -0,0 +1,924 @@+"""Coverage-focused unit tests for src/audio_chunking.py.++These exercise the pure-logic surface of the chunking service (config+resolution, chunk-boundary/step math, transcription merging, speaker-sample+selection, statistics/recommendations, cleanup) plus the ffmpeg/ffprobe+wrappers with subprocess mocked. Everything here is offline and hermetic: no+real media files are produced and no network or binaries are invoked.+"""++import os+import sys+import json+import base64+import subprocess+from pathlib import Path+from types import SimpleNamespace+from unittest import mock++import pytest++sys.path.insert(0, str(Path(__file__).parent.parent))++from src import audio_chunking as ac+from src.audio_chunking import (+ AudioChunkingService,+ EffectiveChunkingConfig,+ get_effective_chunking_config,+ get_audio_duration_ffprobe,+ extract_speaker_samples,+ samples_to_data_urls,+ ChunkProcessingError,+ ChunkingNotSupportedError,+)+++# ---------------------------------------------------------------------------+# Helpers+# ---------------------------------------------------------------------------++# Env vars that get_effective_chunking_config consults. We clear them before+# each test so the host environment (or conftest defaults) can't leak in.+_CHUNK_ENV_KEYS = [+ "CHUNK_OVERLAP_SECONDS",+ "ENABLE_CHUNKING",+ "CHUNK_LIMIT",+ "CHUNK_SIZE_MB",+]++[email protected](autouse=True)+def _clean_chunk_env(monkeypatch):+ for key in _CHUNK_ENV_KEYS:+ monkeypatch.delenv(key, raising=False)+ yield+++def make_specs(**kwargs):+ """A lightweight stand-in for ConnectorSpecifications (duck-typed)."""+ defaults = dict(+ max_file_size_bytes=None,+ max_duration_seconds=None,+ min_duration_for_chunking=None,+ handles_chunking_internally=False,+ requires_chunking_param=False,+ recommended_chunk_seconds=None,+ supported_codecs=None,+ unsupported_codecs=None,+ )+ defaults.update(kwargs)+ return SimpleNamespace(**defaults)+++def completed(stdout="", returncode=0, stderr=""):+ return subprocess.CompletedProcess(args=[], returncode=returncode, stdout=stdout, stderr=stderr)+++# ---------------------------------------------------------------------------+# get_effective_chunking_config+# ---------------------------------------------------------------------------++def test_config_app_default_no_specs():+ cfg = get_effective_chunking_config(None)+ assert cfg.enabled is True+ assert cfg.mode == "size"+ assert cfg.limit_value == 20.0+ assert cfg.source == "app_default"+ assert cfg.overlap_seconds == 3+++def test_config_overlap_seconds_from_env(monkeypatch):+ monkeypatch.setenv("CHUNK_OVERLAP_SECONDS", "7")+ cfg = get_effective_chunking_config(None)+ assert cfg.overlap_seconds == 7+++def test_config_disabled_via_env(monkeypatch):+ monkeypatch.setenv("ENABLE_CHUNKING", "false")+ cfg = get_effective_chunking_config(None)+ assert cfg.enabled is False+ assert cfg.mode == "none"+ assert cfg.source == "disabled"+++def test_config_user_chunk_limit_mb(monkeypatch):+ monkeypatch.setenv("CHUNK_LIMIT", "10MB")+ cfg = get_effective_chunking_config(None)+ assert cfg.mode == "size"+ assert cfg.limit_value == 10.0+ assert cfg.source == "env"+++def test_config_user_chunk_limit_seconds(monkeypatch):+ monkeypatch.setenv("CHUNK_LIMIT", "1200s")+ cfg = get_effective_chunking_config(None)+ assert cfg.mode == "duration"+ assert cfg.limit_value == 1200.0+ assert cfg.source == "env"+++def test_config_user_chunk_limit_minutes(monkeypatch):+ monkeypatch.setenv("CHUNK_LIMIT", "20m")+ cfg = get_effective_chunking_config(None)+ assert cfg.mode == "duration"+ assert cfg.limit_value == 20 * 60+ assert cfg.source == "env"+++def test_config_user_chunk_limit_invalid(monkeypatch):+ # Non-numeric MB string -> ValueError swallowed, falls through to app default.+ monkeypatch.setenv("CHUNK_LIMIT", "MB")+ cfg = get_effective_chunking_config(None)+ assert cfg.source == "app_default"+++def test_config_legacy_chunk_size_mb(monkeypatch):+ monkeypatch.setenv("CHUNK_SIZE_MB", "15")+ cfg = get_effective_chunking_config(None)+ assert cfg.mode == "size"+ assert cfg.limit_value == 15.0+ assert cfg.source == "env"+++def test_config_legacy_chunk_size_mb_invalid(monkeypatch):+ monkeypatch.setenv("CHUNK_SIZE_MB", "notanumber")+ cfg = get_effective_chunking_config(None)+ assert cfg.source == "app_default"+++def test_config_connector_internal_chunking():+ specs = make_specs(handles_chunking_internally=True)+ cfg = get_effective_chunking_config(specs)+ assert cfg.enabled is False+ assert cfg.source == "connector_internal"+++def test_config_connector_hard_duration_limit_uses_recommended():+ specs = make_specs(max_duration_seconds=1000, recommended_chunk_seconds=600)+ cfg = get_effective_chunking_config(specs)+ assert cfg.enabled is True+ assert cfg.mode == "duration"+ assert cfg.limit_value == 600+ assert cfg.source == "connector_limit"+++def test_config_connector_hard_duration_limit_85pct_fallback():+ # No recommended -> 85% of max_duration.+ specs = make_specs(max_duration_seconds=1000)+ cfg = get_effective_chunking_config(specs)+ assert cfg.mode == "duration"+ assert cfg.limit_value == int(1000 * 0.85)+ assert cfg.source == "connector_limit"+++def test_config_connector_and_user_duration_min(monkeypatch):+ monkeypatch.setenv("CHUNK_LIMIT", "100s")+ specs = make_specs(max_duration_seconds=1000, recommended_chunk_seconds=600)+ cfg = get_effective_chunking_config(specs)+ # MIN(connector 600, user 100) = 100+ assert cfg.limit_value == 100+ assert cfg.source == "user_and_connector"+++def test_config_connector_hard_size_limit():+ # 100MB max -> 80% = 80MB+ specs = make_specs(max_file_size_bytes=100 * 1024 * 1024)+ cfg = get_effective_chunking_config(specs)+ assert cfg.mode == "size"+ assert cfg.limit_value == pytest.approx(80.0)+ assert cfg.source == "connector_limit"+++def test_config_connector_and_user_size_min(monkeypatch):+ monkeypatch.setenv("CHUNK_LIMIT", "10MB")+ specs = make_specs(max_file_size_bytes=100 * 1024 * 1024)+ cfg = get_effective_chunking_config(specs)+ # MIN(connector 80, user 10) = 10+ assert cfg.limit_value == pytest.approx(10.0)+ assert cfg.source == "user_and_connector"+++def test_config_connector_recommended_soft():+ # No hard limits, but a recommended chunk length set.+ specs = make_specs(recommended_chunk_seconds=480)+ cfg = get_effective_chunking_config(specs)+ assert cfg.mode == "duration"+ assert cfg.limit_value == 480+ assert cfg.source == "connector_recommended"+++# ---------------------------------------------------------------------------+# AudioChunkingService construction / needs_chunking+# ---------------------------------------------------------------------------++def test_service_init_defaults():+ svc = AudioChunkingService()+ assert svc.max_chunk_size_mb == 20+ assert svc.overlap_seconds == 3+ assert svc.max_chunk_size_bytes == 20 * 1024 * 1024+ assert svc.max_chunk_duration_seconds is None+ assert svc.chunk_stats == []+++def test_needs_chunking_disabled_returns_false(monkeypatch):+ monkeypatch.setenv("ENABLE_CHUNKING", "false")+ svc = AudioChunkingService()+ assert svc.needs_chunking("/nonexistent.mp3") is False+++def test_needs_chunking_legacy_asr_endpoint(monkeypatch):+ # Default config is enabled, but legacy use_asr_endpoint short-circuits.+ svc = AudioChunkingService()+ assert svc.needs_chunking("/whatever.mp3", use_asr_endpoint=True) is False+++def test_needs_chunking_size_mode_over_limit(monkeypatch):+ monkeypatch.setenv("CHUNK_LIMIT", "10MB")+ svc = AudioChunkingService()+ with mock.patch("os.path.getsize", return_value=20 * 1024 * 1024):+ assert svc.needs_chunking("/big.mp3") is True+++def test_needs_chunking_size_mode_under_limit(monkeypatch):+ monkeypatch.setenv("CHUNK_LIMIT", "10MB")+ svc = AudioChunkingService()+ with mock.patch("os.path.getsize", return_value=2 * 1024 * 1024):+ assert svc.needs_chunking("/small.mp3") is False+++def test_needs_chunking_duration_mode_over_limit(monkeypatch):+ monkeypatch.setenv("CHUNK_LIMIT", "100s")+ svc = AudioChunkingService()+ with mock.patch("os.path.getsize", return_value=1234), \+ mock.patch.object(svc, "get_audio_duration", return_value=200.0):+ assert svc.needs_chunking("/long.mp3") is True+++def test_needs_chunking_duration_mode_under_limit(monkeypatch):+ monkeypatch.setenv("CHUNK_LIMIT", "100s")+ svc = AudioChunkingService()+ with mock.patch("os.path.getsize", return_value=1234), \+ mock.patch.object(svc, "get_audio_duration", return_value=50.0):+ assert svc.needs_chunking("/short.mp3") is False+++def test_needs_chunking_duration_unknown_returns_true(monkeypatch):+ monkeypatch.setenv("CHUNK_LIMIT", "100s")+ svc = AudioChunkingService()+ with mock.patch("os.path.getsize", return_value=1234), \+ mock.patch.object(svc, "get_audio_duration", return_value=None):+ assert svc.needs_chunking("/unknown.mp3") is True+++def test_needs_chunking_oserror_returns_false(monkeypatch):+ monkeypatch.setenv("CHUNK_LIMIT", "10MB")+ svc = AudioChunkingService()+ with mock.patch("os.path.getsize", side_effect=OSError("boom")):+ assert svc.needs_chunking("/gone.mp3") is False+++def test_needs_chunking_with_config(monkeypatch):+ monkeypatch.setenv("CHUNK_LIMIT", "10MB")+ svc = AudioChunkingService()+ with mock.patch("os.path.getsize", return_value=20 * 1024 * 1024):+ needs, cfg = svc.needs_chunking_with_config("/big.mp3")+ assert needs is True+ assert isinstance(cfg, EffectiveChunkingConfig)+ assert cfg.mode == "size"+++# ---------------------------------------------------------------------------+# get_audio_duration (instance) + module helper+# ---------------------------------------------------------------------------++def test_get_audio_duration_success():+ svc = AudioChunkingService()+ with mock.patch("subprocess.run", return_value=completed(stdout="123.45\n")) as m:+ assert svc.get_audio_duration("/a.mp3") == pytest.approx(123.45)+ # ffprobe with the right query was invoked.+ args = m.call_args[0][0]+ assert args[0] == "ffprobe"+ assert "format=duration" in args+++def test_get_audio_duration_bad_value():+ svc = AudioChunkingService()+ with mock.patch("subprocess.run", return_value=completed(stdout="notanumber")):+ assert svc.get_audio_duration("/a.mp3") is None+++def test_get_audio_duration_called_process_error():+ svc = AudioChunkingService()+ err = subprocess.CalledProcessError(1, ["ffprobe"])+ with mock.patch("subprocess.run", side_effect=err):+ assert svc.get_audio_duration("/a.mp3") is None+++def test_get_audio_duration_missing_binary():+ svc = AudioChunkingService()+ with mock.patch("subprocess.run", side_effect=FileNotFoundError):+ assert svc.get_audio_duration("/a.mp3") is None+++def test_module_get_audio_duration_ffprobe_success():+ with mock.patch("subprocess.run", return_value=completed(stdout="42.0")):+ assert get_audio_duration_ffprobe("/a.mp3") == pytest.approx(42.0)+++def test_module_get_audio_duration_ffprobe_failure():+ with mock.patch("subprocess.run", side_effect=Exception("x")):+ assert get_audio_duration_ffprobe("/a.mp3") is None+++# ---------------------------------------------------------------------------+# parse_chunk_limit+# ---------------------------------------------------------------------------++def test_parse_chunk_limit_mb(monkeypatch):+ monkeypatch.setenv("CHUNK_LIMIT", "25MB")+ assert AudioChunkingService().parse_chunk_limit() == ("size", 25.0)+++def test_parse_chunk_limit_seconds(monkeypatch):+ monkeypatch.setenv("CHUNK_LIMIT", "900s")+ assert AudioChunkingService().parse_chunk_limit() == ("duration", 900.0)+++def test_parse_chunk_limit_minutes(monkeypatch):+ monkeypatch.setenv("CHUNK_LIMIT", "15M")+ assert AudioChunkingService().parse_chunk_limit() == ("duration", 900.0)+++def test_parse_chunk_limit_legacy(monkeypatch):+ monkeypatch.setenv("CHUNK_SIZE_MB", "18")+ assert AudioChunkingService().parse_chunk_limit() == ("size", 18.0)+++def test_parse_chunk_limit_default(monkeypatch):+ # Nothing set -> default 20MB.+ assert AudioChunkingService().parse_chunk_limit() == ("size", 20.0)+++def test_parse_chunk_limit_legacy_invalid(monkeypatch):+ monkeypatch.setenv("CHUNK_SIZE_MB", "bad")+ assert AudioChunkingService().parse_chunk_limit() == ("size", 20.0)+++# ---------------------------------------------------------------------------+# calculate_optimal_chunking+# ---------------------------------------------------------------------------++def test_calc_chunking_single_chunk_size(monkeypatch):+ monkeypatch.setenv("CHUNK_LIMIT", "20MB")+ svc = AudioChunkingService()+ # 5MB file, 600s -> 1 chunk.+ num, dur = svc.calculate_optimal_chunking(5 * 1024 * 1024, 600.0)+ assert num == 1+ assert dur == pytest.approx(600.0)+++def test_calc_chunking_multiple_chunks_size(monkeypatch):+ monkeypatch.setenv("CHUNK_LIMIT", "10MB")+ svc = AudioChunkingService()+ # 100MB, 3600s. max_size = 10*0.95 = 9.5MB -> ceil(100/9.5)=11 chunks.+ num, dur = svc.calculate_optimal_chunking(100 * 1024 * 1024, 3600.0)+ assert num == 11+ # chunk_duration = 3600/11 ~ 327, clamped to min 300.+ assert dur == pytest.approx(max(300, 3600.0 / 11))+++def test_calc_chunking_duration_mode(monkeypatch):+ monkeypatch.setenv("CHUNK_LIMIT", "600s")+ svc = AudioChunkingService()+ # 50MB, 1800s -> ceil(1800/600)=3 chunks.+ num, dur = svc.calculate_optimal_chunking(50 * 1024 * 1024, 1800.0)+ assert num == 3+ assert dur == pytest.approx(600.0)+++def test_calc_chunking_min_duration_clamp(monkeypatch):+ monkeypatch.setenv("CHUNK_LIMIT", "30s")+ svc = AudioChunkingService()+ # Many small chunks would give <300s; clamp to min(300, total).+ num, dur = svc.calculate_optimal_chunking(10 * 1024 * 1024, 120.0)+ # total < 300, so chunk_duration clamps to total_duration (120).+ assert dur == pytest.approx(120.0)+++def test_calc_chunking_exception_fallback(monkeypatch):+ monkeypatch.setenv("CHUNK_LIMIT", "600s")+ svc = AudioChunkingService()+ # Force the config lookup inside the method to raise.+ with mock.patch.object(ac, "get_effective_chunking_config", side_effect=RuntimeError("boom")):+ num, dur = svc.calculate_optimal_chunking(50 * 1024 * 1024, 1800.0)+ # Fallback: 10-minute chunks, min 2.+ assert num == max(2, -(-1800 // 600))+ assert dur == pytest.approx(1800.0 / num)+++# ---------------------------------------------------------------------------+# convert_to_mp3_and_get_info+# ---------------------------------------------------------------------------++def test_convert_already_mp3_copies(tmp_path, monkeypatch):+ src = tmp_path / "in.mp3"+ src.write_bytes(b"x" * 100)+ svc = AudioChunkingService()+ monkeypatch.setattr(svc, "get_audio_duration", lambda p: 60.0)+ path, dur, size = svc.convert_to_mp3_and_get_info(str(src), str(tmp_path))+ assert path.endswith("_converted.mp3")+ assert os.path.exists(path)+ assert dur == 60.0+ assert size == 100+++def test_convert_non_mp3_calls_convert(tmp_path, monkeypatch):+ src = tmp_path / "in.wav"+ src.write_bytes(b"wavdata")+ svc = AudioChunkingService()+ monkeypatch.setattr(svc, "get_audio_duration", lambda p: 30.0)++ def fake_convert(in_path, out_path):+ with open(out_path, "wb") as f:+ f.write(b"mp3data")++ with mock.patch.object(ac, "convert_to_mp3", side_effect=fake_convert) as m:+ path, dur, size = svc.convert_to_mp3_and_get_info(str(src), str(tmp_path))+ m.assert_called_once()+ assert dur == 30.0+ assert size == len(b"mp3data")+++def test_convert_missing_output_raises(tmp_path, monkeypatch):+ src = tmp_path / "in.wav"+ src.write_bytes(b"data")+ svc = AudioChunkingService()+ with mock.patch.object(ac, "convert_to_mp3", side_effect=lambda i, o: None):+ with pytest.raises(ValueError, match="MP3 file was not created"):+ svc.convert_to_mp3_and_get_info(str(src), str(tmp_path))+++def test_convert_no_duration_raises(tmp_path, monkeypatch):+ src = tmp_path / "in.mp3"+ src.write_bytes(b"abc")+ svc = AudioChunkingService()+ monkeypatch.setattr(svc, "get_audio_duration", lambda p: None)+ with pytest.raises(ValueError, match="Could not determine MP3 file duration"):+ svc.convert_to_mp3_and_get_info(str(src), str(tmp_path))+++def test_convert_ffmpeg_error_propagates(tmp_path):+ src = tmp_path / "in.wav"+ src.write_bytes(b"data")+ svc = AudioChunkingService()+ with mock.patch.object(ac, "convert_to_mp3", side_effect=ac.FFmpegError("bad")):+ with pytest.raises(ac.FFmpegError):+ svc.convert_to_mp3_and_get_info(str(src), str(tmp_path))+++# ---------------------------------------------------------------------------+# create_chunks+# ---------------------------------------------------------------------------++def test_create_chunks_single(tmp_path, monkeypatch):+ src = tmp_path / "in.mp3"+ src.write_bytes(b"x" * 50)+ svc = AudioChunkingService()+ # Patch conversion to a known mp3 in temp_dir, and force single chunk.+ mp3 = tmp_path / "conv.mp3"+ mp3.write_bytes(b"y" * 50)+ monkeypatch.setattr(svc, "convert_to_mp3_and_get_info", lambda f, d: (str(mp3), 100.0, 50))+ monkeypatch.setattr(svc, "calculate_optimal_chunking", lambda s, d, c=None: (1, 100.0))++ chunks = svc.create_chunks(str(src), str(tmp_path))+ assert len(chunks) == 1+ assert chunks[0]["index"] == 0+ assert chunks[0]["start_time"] == 0+ assert chunks[0]["end_time"] == 100.0+ assert os.path.exists(chunks[0]["path"])+++def test_create_chunks_multiple(tmp_path, monkeypatch):+ src = tmp_path / "in.mp3"+ src.write_bytes(b"x" * 50)+ mp3 = tmp_path / "conv.mp3"+ mp3.write_bytes(b"y" * 50)+ svc = AudioChunkingService()+ monkeypatch.setattr(svc, "convert_to_mp3_and_get_info", lambda f, d: (str(mp3), 1200.0, 50))+ monkeypatch.setattr(svc, "calculate_optimal_chunking", lambda s, d, c=None: (3, 420.0))++ # Mock ffmpeg: create the output chunk file so os.path.exists/getsize work.+ def fake_run(cmd, **kwargs):+ out = cmd[-1]+ with open(out, "wb") as f:+ f.write(b"chunkdata")+ return completed(returncode=0)++ with mock.patch("subprocess.run", side_effect=fake_run):+ chunks = svc.create_chunks(str(src), str(tmp_path))++ assert len(chunks) == 3+ assert [c["index"] for c in chunks] == [0, 1, 2]+ # Boundaries are monotonic / cover the file.+ assert chunks[0]["start_time"] == 0+ assert chunks[-1]["end_time"] == pytest.approx(1200.0)+++def test_create_chunks_ffmpeg_failure_skips(tmp_path, monkeypatch):+ src = tmp_path / "in.mp3"+ src.write_bytes(b"x" * 50)+ mp3 = tmp_path / "conv.mp3"+ mp3.write_bytes(b"y" * 50)+ svc = AudioChunkingService()+ monkeypatch.setattr(svc, "convert_to_mp3_and_get_info", lambda f, d: (str(mp3), 1200.0, 50))+ monkeypatch.setattr(svc, "calculate_optimal_chunking", lambda s, d, c=None: (3, 420.0))++ # All ffmpeg calls fail -> all chunks skipped, empty list returned.+ with mock.patch("subprocess.run", return_value=completed(returncode=1, stderr="boom")):+ chunks = svc.create_chunks(str(src), str(tmp_path))+ assert chunks == []+++def test_create_chunks_conversion_error_propagates(tmp_path, monkeypatch):+ src = tmp_path / "in.mp3"+ src.write_bytes(b"x")+ svc = AudioChunkingService()+ monkeypatch.setattr(svc, "convert_to_mp3_and_get_info",+ lambda f, d: (_ for _ in ()).throw(RuntimeError("convfail")))+ with pytest.raises(RuntimeError, match="convfail"):+ svc.create_chunks(str(src), str(tmp_path))+++# ---------------------------------------------------------------------------+# merge_transcriptions + helpers+# ---------------------------------------------------------------------------++def test_merge_empty():+ assert AudioChunkingService().merge_transcriptions([]) == ""+++def test_merge_single():+ svc = AudioChunkingService()+ assert svc.merge_transcriptions([{"transcription": "hello world"}]) == "hello world"+++def test_merge_no_overlap_concatenates():+ svc = AudioChunkingService()+ chunks = [+ {"transcription": "First chunk text.", "start_time": 0, "end_time": 10},+ {"transcription": "Second chunk text.", "start_time": 20, "end_time": 30},+ ]+ merged = svc.merge_transcriptions(chunks)+ assert "First chunk text." in merged+ assert "Second chunk text." in merged+++def test_merge_skips_empty_chunk_text():+ svc = AudioChunkingService()+ chunks = [+ {"transcription": "Only real text.", "start_time": 0, "end_time": 10},+ {"transcription": " ", "start_time": 10, "end_time": 20},+ ]+ merged = svc.merge_transcriptions(chunks)+ assert merged == "Only real text."+++def test_merge_sorts_by_start_time():+ svc = AudioChunkingService()+ chunks = [+ {"transcription": "Beta.", "start_time": 100, "end_time": 110},+ {"transcription": "Alpha.", "start_time": 0, "end_time": 10},+ ]+ merged = svc.merge_transcriptions(chunks)+ assert merged.index("Alpha") < merged.index("Beta")+++def test_merge_overlapping_dedupes_common_sentence():+ svc = AudioChunkingService()+ # prev_end (15) > new_start (10) -> overlap window. The shared sentence+ # "the quick brown fox jumps" should be collapsed, not duplicated.+ shared = "the quick brown fox jumps over the lazy dog"+ chunks = [+ {"transcription": f"intro sentence here. {shared}.", "start_time": 0, "end_time": 15},+ {"transcription": f"{shared}. closing remarks follow.", "start_time": 10, "end_time": 25},+ ]+ merged = svc.merge_transcriptions(chunks)+ assert merged.lower().count(shared) == 1+ assert "closing remarks follow" in merged.lower()+++def test_merge_overlap_no_match_concatenates():+ svc = AudioChunkingService()+ chunks = [+ {"transcription": "totally unrelated alpha beta.", "start_time": 0, "end_time": 15},+ {"transcription": "completely different gamma delta.", "start_time": 10, "end_time": 25},+ ]+ merged = svc.merge_transcriptions(chunks)+ assert "alpha beta" in merged.lower()+ assert "gamma delta" in merged.lower()+++def test_split_into_sentences():+ svc = AudioChunkingService()+ out = svc._split_into_sentences("One. Two! Three? ")+ assert out == ["One", "Two", "Three"]+++def test_sentences_similar_true_and_false():+ svc = AudioChunkingService()+ assert svc._sentences_similar("the cat sat", "the cat sat") is True+ assert svc._sentences_similar("the cat sat", "a dog ran fast") is False+++def test_sentences_similar_empty():+ svc = AudioChunkingService()+ assert svc._sentences_similar("", "anything") is False+++def test_merge_overlapping_text_empty_sentences():+ svc = AudioChunkingService()+ # new_text has no sentence-y content -> falls back to concatenation branch.+ out = svc._merge_overlapping_text("existing text.", " ", new_start_time=5, prev_end_time=10)+ assert "existing text." in out+++# ---------------------------------------------------------------------------+# analyze_chunk_audio_properties+# ---------------------------------------------------------------------------++def _probe_json():+ return json.dumps({+ "format": {"duration": "120.0", "size": "1920000", "bit_rate": "128000"},+ "streams": [+ {"codec_type": "video", "codec_name": "h264"},+ {+ "codec_type": "audio",+ "codec_name": "mp3",+ "sample_rate": "44100",+ "channels": "2",+ "bits_per_raw_sample": "0",+ },+ ],+ })+++def test_analyze_chunk_audio_properties_success():+ svc = AudioChunkingService()+ with mock.patch("subprocess.run", return_value=completed(stdout=_probe_json())):+ analysis = svc.analyze_chunk_audio_properties("/c.mp3")+ assert analysis["duration"] == pytest.approx(120.0)+ assert analysis["codec"] == "mp3"+ assert analysis["sample_rate"] == 44100+ assert analysis["channels"] == 2+ assert "effective_bitrate" in analysis+ assert "compression_ratio" in analysis+++def test_analyze_chunk_no_audio_stream():+ svc = AudioChunkingService()+ data = json.dumps({"format": {}, "streams": [{"codec_type": "video"}]})+ with mock.patch("subprocess.run", return_value=completed(stdout=data)):+ analysis = svc.analyze_chunk_audio_properties("/c.mp3")+ assert analysis == {"error": "No audio stream found"}+++def test_analyze_chunk_ffprobe_error():+ svc = AudioChunkingService()+ with mock.patch("subprocess.run", side_effect=subprocess.CalledProcessError(1, ["ffprobe"])):+ analysis = svc.analyze_chunk_audio_properties("/c.mp3")+ assert "error" in analysis+++# ---------------------------------------------------------------------------+# log_processing_statistics / get_performance_recommendations+# ---------------------------------------------------------------------------++def test_log_processing_statistics_empty():+ # No exception, no output assertions; just exercise the early-return path.+ AudioChunkingService().log_processing_statistics([])+++def test_log_processing_statistics_with_outliers():+ svc = AudioChunkingService()+ results = [+ {"processing_time": 10, "size_mb": 15, "duration": 300},+ {"processing_time": 12, "size_mb": 16, "duration": 300},+ {"processing_time": 90, "size_mb": 18, "duration": 300}, # outlier+ ]+ # Just ensure it runs through the outlier / summary branches without error.+ svc.log_processing_statistics(results)+++def test_get_performance_recommendations_empty():+ assert AudioChunkingService().get_performance_recommendations([]) == []+++def test_get_performance_recommendations_all_branches():+ svc = AudioChunkingService()+ # High variance (max > avg*3) + slow + timeout + tiny chunks all triggered.+ results = [+ {"processing_time": 1, "duration": 5, "size_mb": 5},+ {"processing_time": 1, "duration": 5, "size_mb": 5},+ {"processing_time": 1, "duration": 5, "size_mb": 5},+ {"processing_time": 400, "duration": 5, "size_mb": 5},+ ]+ recs = svc.get_performance_recommendations(results)+ assert any("variance" in r.lower() for r in recs)+ assert any("slow" in r.lower() for r in recs)+ assert any("5 minutes" in r for r in recs)+ assert any("small" in r.lower() for r in recs)+++def test_get_performance_recommendations_large_chunks():+ svc = AudioChunkingService()+ results = [+ {"processing_time": 10, "duration": 100, "size_mb": 24},+ {"processing_time": 11, "duration": 100, "size_mb": 23},+ ]+ recs = svc.get_performance_recommendations(results)+ assert any("size limit" in r.lower() for r in recs)+++# ---------------------------------------------------------------------------+# cleanup_chunks+# ---------------------------------------------------------------------------++def test_cleanup_chunks_removes_files(tmp_path):+ f1 = tmp_path / "c0.mp3"+ f2 = tmp_path / "c1.mp3"+ mp3 = tmp_path / "conv.mp3"+ for f in (f1, f2, mp3):+ f.write_bytes(b"x")+ svc = AudioChunkingService()+ chunks = [{"path": str(f1), "filename": "c0.mp3"}, {"path": str(f2), "filename": "c1.mp3"}]+ svc.cleanup_chunks(chunks, temp_mp3_path=str(mp3))+ assert not f1.exists()+ assert not f2.exists()+ assert not mp3.exists()+++def test_cleanup_chunks_missing_files_no_error(tmp_path):+ svc = AudioChunkingService()+ chunks = [{"path": str(tmp_path / "gone.mp3"), "filename": "gone.mp3"}]+ # Should not raise even though file doesn't exist.+ svc.cleanup_chunks(chunks, temp_mp3_path=str(tmp_path / "alsogone.mp3"))+++def test_cleanup_chunks_remove_error_swallowed(tmp_path):+ f1 = tmp_path / "c0.mp3"+ f1.write_bytes(b"x")+ svc = AudioChunkingService()+ chunks = [{"path": str(f1), "filename": "c0.mp3"}]+ with mock.patch("os.remove", side_effect=OSError("denied")):+ svc.cleanup_chunks(chunks) # warning logged, no raise+++# ---------------------------------------------------------------------------+# extract_speaker_samples+# ---------------------------------------------------------------------------++def test_extract_speaker_samples_no_valid_segments(tmp_path):+ # All segments Unknown / missing times -> empty result.+ segments = [+ {"speaker": "Unknown", "start_time": 0, "end_time": 5},+ {"speaker": "A", "start_time": None, "end_time": 5},+ ]+ out = extract_speaker_samples(str(tmp_path / "a.mp3"), segments, str(tmp_path))+ assert out == {}+++def test_extract_speaker_samples_success(tmp_path):+ audio = tmp_path / "a.mp3"+ audio.write_bytes(b"audio")+ # Use non-zero start times: the code reads `start_time or start`, so 0.0 is+ # treated as missing.+ segments = [+ {"speaker": "A", "start_time": 1.0, "end_time": 6.0},+ {"speaker": "B", "start_time": 7.0, "end_time": 12.0},+ ]++ def fake_run(cmd, **kwargs):+ out = cmd[-1]+ with open(out, "wb") as f:+ f.write(b"sample")+ return completed(returncode=0)++ with mock.patch("subprocess.run", side_effect=fake_run), \+ mock.patch.object(ac, "get_audio_duration_ffprobe", return_value=5.0):+ out = extract_speaker_samples(str(audio), segments, str(tmp_path))++ assert set(out.keys()) == {"A", "B"}+ for p in out.values():+ assert os.path.exists(p)+++def test_extract_speaker_samples_object_segments(tmp_path):+ audio = tmp_path / "a.mp3"+ audio.write_bytes(b"audio")+ seg = SimpleNamespace(speaker="A", start_time=1.0, end_time=5.0, start=None, end=None)++ def fake_run(cmd, **kwargs):+ with open(cmd[-1], "wb") as f:+ f.write(b"sample")+ return completed(returncode=0)++ with mock.patch("subprocess.run", side_effect=fake_run), \+ mock.patch.object(ac, "get_audio_duration_ffprobe", return_value=4.0):+ out = extract_speaker_samples(str(audio), [seg], str(tmp_path))+ assert "A" in out+++def test_extract_speaker_samples_too_short_skipped(tmp_path):+ audio = tmp_path / "a.mp3"+ audio.write_bytes(b"audio")+ segments = [{"speaker": "A", "start_time": 1.0, "end_time": 6.0}]++ def fake_run(cmd, **kwargs):+ with open(cmd[-1], "wb") as f:+ f.write(b"sample")+ return completed(returncode=0)++ # Actual duration below OpenAI minimum (1.2s) -> sample removed, skipped.+ with mock.patch("subprocess.run", side_effect=fake_run), \+ mock.patch.object(ac, "get_audio_duration_ffprobe", return_value=0.5):+ out = extract_speaker_samples(str(audio), segments, str(tmp_path))+ assert out == {}+++def test_extract_speaker_samples_ffmpeg_failure(tmp_path):+ audio = tmp_path / "a.mp3"+ audio.write_bytes(b"audio")+ segments = [{"speaker": "A", "start_time": 1.0, "end_time": 6.0}]+ with mock.patch("subprocess.run", return_value=completed(returncode=1, stderr="fail")):+ out = extract_speaker_samples(str(audio), segments, str(tmp_path))+ assert out == {}+++def test_extract_speaker_samples_combines_short_segments(tmp_path):+ audio = tmp_path / "a.mp3"+ audio.write_bytes(b"audio")+ # Two short adjacent segments that combine to >= min_duration (1.5).+ segments = [+ {"speaker": "A", "start_time": 0.5, "end_time": 1.1},+ {"speaker": "A", "start_time": 1.3, "end_time": 2.3},+ ]++ def fake_run(cmd, **kwargs):+ with open(cmd[-1], "wb") as f:+ f.write(b"sample")+ return completed(returncode=0)++ with mock.patch("subprocess.run", side_effect=fake_run), \+ mock.patch.object(ac, "get_audio_duration_ffprobe", return_value=2.0):+ out = extract_speaker_samples(str(audio), segments, str(tmp_path))+ assert "A" in out+++def test_extract_speaker_samples_max_speakers(tmp_path):+ audio = tmp_path / "a.mp3"+ audio.write_bytes(b"audio")+ segments = [+ {"speaker": s, "start_time": 1.0, "end_time": 6.0}+ for s in ["A", "B", "C", "D", "E", "F"]+ ]++ def fake_run(cmd, **kwargs):+ with open(cmd[-1], "wb") as f:+ f.write(b"sample")+ return completed(returncode=0)++ with mock.patch("subprocess.run", side_effect=fake_run), \+ mock.patch.object(ac, "get_audio_duration_ffprobe", return_value=5.0):+ out = extract_speaker_samples(str(audio), segments, str(tmp_path), max_speakers=2)+ assert len(out) == 2+ assert set(out.keys()) == {"A", "B"}+++# ---------------------------------------------------------------------------+# samples_to_data_urls+# ---------------------------------------------------------------------------++def test_samples_to_data_urls_success(tmp_path):+ f = tmp_path / "s.mp3"+ f.write_bytes(b"hello")+ out = samples_to_data_urls({"A": str(f)})+ expected = "data:audio/mpeg;base64," + base64.b64encode(b"hello").decode()+ assert out["A"] == expected+++def test_samples_to_data_urls_missing_file_skipped(tmp_path):+ out = samples_to_data_urls({"A": str(tmp_path / "missing.mp3")})+ assert out == {}+++# ---------------------------------------------------------------------------+# Exception classes+# ---------------------------------------------------------------------------++def test_exception_classes():+ assert issubclass(ChunkProcessingError, Exception)+ assert issubclass(ChunkingNotSupportedError, Exception)+ with pytest.raises(ChunkProcessingError):+ raise ChunkProcessingError("x")+ with pytest.raises(ChunkingNotSupportedError):+ raise ChunkingNotSupportedError("y")diff --git a/tests/test_cov_auth.py b/tests/test_cov_auth.pynew file mode 100644index 00000000..f10e2771--- /dev/null+++ b/tests/test_cov_auth.py@@ -0,0 +1,623 @@+#!/usr/bin/env python3+"""Coverage tests for src/api/auth.py — the auth/login/account security boundary.++These exercise the HTTP-facing auth blueprint: login (success/failure/logout),+the /account handler's two distinct persistence branches (personal-info vs+preferences — the branch where a past bug routed ``ui_language`` to the wrong+column), password change (success + wrong-current-password rejection),+registration gating (ALLOW_REGISTRATION + REGISTRATION_ALLOWED_DOMAINS), the+SSO callback paths (OIDC client mocked), and auth-required routes redirecting+when unauthenticated.++Hermetic & offline: conftest.py points the app at a throwaway SQLite DB and+sets safe env defaults; SSO/OIDC and SMTP effects are patched. CSRF is disabled+per test_client.++Run (pytest, isolated temp DB from conftest.py):+ python -m pytest tests/test_cov_auth.py -q+"""++import os+import sys+import uuid+import contextlib+from unittest import mock++import pytest++sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))++from src.app import app, db # noqa: E402+from src.models import User # noqa: E402+from src.api import auth as auth_mod # noqa: E402+++# ---------------------------------------------------------------------------+# Fixtures / helpers+# ---------------------------------------------------------------------------+[email protected](autouse=True)+def _disable_csrf():+ prev = app.config.get('WTF_CSRF_ENABLED')+ app.config['WTF_CSRF_ENABLED'] = False+ yield+ app.config['WTF_CSRF_ENABLED'] = prev+++def app_ctx():+ with app.app_context():+ yield+++def cleanup_users(app_ctx):+ created = {"usernames": set(), "emails": set(), "subjects": set()}+ yield created+ q = User.query.filter(+ db.or_(+ User.username.in_(created["usernames"] or [""]),+ User.email.in_(created["emails"] or [""]),+ User.sso_subject.in_(created["subjects"] or [""]),+ )+ )+ for u in q.all():+ db.session.delete(u)+ db.session.commit()+++# Plaintext password used for login tests; hashed via the same bcrypt the app+# verifies with so check_password_hash succeeds.+GOOD_PW = "Sup3rSecret!"+++def _track(cleanup, user):+ cleanup["usernames"].add(user.username)+ cleanup["emails"].add(user.email)+ if user.sso_subject:+ cleanup["subjects"].add(user.sso_subject)+++def _mk_user(cleanup, password=GOOD_PW, sso_subject=None, is_admin=False,+ email_verified=True, **extra):+ username = "u_" + uuid.uuid4().hex[:14]+ email = uuid.uuid4().hex[:10] + "@example.com"+ hashed = auth_mod.bcrypt.generate_password_hash(password).decode('utf-8') if password else None+ u = User(username=username, email=email, password=hashed, sso_subject=sso_subject,+ is_admin=is_admin, email_verified=email_verified, **extra)+ db.session.add(u)+ db.session.commit()+ _track(cleanup, u)+ return u+++def _client_logged_in_as(user):+ client = app.test_client()+ with client.session_transaction() as s:+ s['_user_id'] = str(user.id)+ s['_fresh'] = True+ yield client+++def _envset(**overrides):+ saved = {k: os.environ.get(k) for k in overrides}+ try:+ for k, v in overrides.items():+ if v is None:+ os.environ.pop(k, None)+ else:+ os.environ[k] = v+ yield+ finally:+ for k, old in saved.items():+ if old is None:+ os.environ.pop(k, None)+ else:+ os.environ[k] = old+++# ---------------------------------------------------------------------------+# Login / logout+# ---------------------------------------------------------------------------++def test_login_success_sets_session_and_redirects(cleanup_users):+ user = _mk_user(cleanup_users)+ client = app.test_client()+ resp = client.post('/login', data={'email': user.email, 'password': GOOD_PW})+ assert resp.status_code == 302+ assert '/login' not in resp.headers['Location']+ with client.session_transaction() as s:+ assert s.get('_user_id') == str(user.id)+++def test_login_wrong_password_no_session(cleanup_users):+ user = _mk_user(cleanup_users)+ client = app.test_client()+ resp = client.post('/login', data={'email': user.email, 'password': 'wrong-password'})+ assert resp.status_code == 200 # re-renders login form+ with client.session_transaction() as s:+ assert '_user_id' not in s+++def test_login_unknown_user_no_session(cleanup_users):+ client = app.test_client()+ resp = client.post('/login', data={+ 'email': uuid.uuid4().hex[:8] + '@nobody.com', 'password': GOOD_PW})+ assert resp.status_code == 200+ with client.session_transaction() as s:+ assert '_user_id' not in s+++def test_login_respects_safe_next(cleanup_users):+ user = _mk_user(cleanup_users)+ client = app.test_client()+ resp = client.post('/login?next=/recordings', data={'email': user.email, 'password': GOOD_PW})+ assert resp.status_code == 302+ assert resp.headers['Location'].endswith('/recordings')+++def test_login_rejects_unsafe_next(cleanup_users):+ user = _mk_user(cleanup_users)+ client = app.test_client()+ resp = client.post('/login?next=//evil.com', data={'email': user.email, 'password': GOOD_PW})+ assert resp.status_code == 302+ assert 'evil.com' not in resp.headers['Location']+++def test_login_when_already_authenticated_redirects(cleanup_users):+ user = _mk_user(cleanup_users)+ with _client_logged_in_as(user) as client:+ resp = client.get('/login')+ assert resp.status_code == 302+++def test_logout_clears_session(cleanup_users):+ user = _mk_user(cleanup_users)+ with _client_logged_in_as(user) as client:+ resp = client.get('/logout')+ assert resp.status_code == 302+ with client.session_transaction() as s:+ assert '_user_id' not in s+++def test_login_sso_only_user_prompted_to_use_sso(cleanup_users):+ # User with no local password → account uses SSO, password login refused.+ user = _mk_user(cleanup_users, password=None, sso_subject="sub-" + uuid.uuid4().hex)+ client = app.test_client()+ resp = client.post('/login', data={'email': user.email, 'password': GOOD_PW})+ assert resp.status_code == 200+ with client.session_transaction() as s:+ assert '_user_id' not in s+++# ---------------------------------------------------------------------------+# /account — personal-info branch vs preferences branch+# ---------------------------------------------------------------------------++def test_account_requires_login():+ client = app.test_client()+ resp = client.get('/account')+ assert resp.status_code in (302, 401)+ if resp.status_code == 302:+ assert '/login' in resp.headers['Location']+++def test_account_get_renders_for_logged_in_user(cleanup_users):+ user = _mk_user(cleanup_users)+ with _client_logged_in_as(user) as client:+ resp = client.get('/account')+ assert resp.status_code == 200+++def test_account_personal_info_branch_persists_name_fields(cleanup_users):+ user = _mk_user(cleanup_users)+ uid = user.id+ with _client_logged_in_as(user) as client:+ resp = client.post('/account', data={+ 'user_name': 'Jane Doe',+ 'user_job_title': 'Engineer',+ 'user_company': 'Acme',+ })+ assert resp.status_code == 302+ refreshed = db.session.get(User, uid)+ assert refreshed.name == 'Jane Doe'+ assert refreshed.job_title == 'Engineer'+ assert refreshed.company == 'Acme'+++def test_account_personal_info_branch_does_not_touch_language(cleanup_users):+ # The personal-info branch must NOT write ui_language (past-bug guard).+ user = _mk_user(cleanup_users, ui_language='fr')+ uid = user.id+ with _client_logged_in_as(user) as client:+ client.post('/account', data={'user_name': 'Bob'})+ refreshed = db.session.get(User, uid)+ assert refreshed.ui_language == 'fr' # unchanged+ assert refreshed.name == 'Bob'+++def test_account_preferences_branch_persists_languages(cleanup_users):+ user = _mk_user(cleanup_users, ui_language='en')+ uid = user.id+ with _client_logged_in_as(user) as client:+ resp = client.post('/account', data={+ 'preferences_form': '1',+ 'ui_language': 'es',+ 'transcription_language': 'fr',+ 'output_language': 'Spanish',+ 'show_timestamps_simple_view': 'on',+ 'editor_autosave': 'on',+ 'audio_player_position': 'top',+ })+ assert resp.status_code == 302+ refreshed = db.session.get(User, uid)+ # ui_language goes to ui_language (NOT transcription_language) — the bug.+ assert refreshed.ui_language == 'es'+ assert refreshed.transcription_language == 'fr'+ assert refreshed.output_language == 'Spanish'+ assert refreshed.show_timestamps_simple_view is True+ assert refreshed.editor_autosave is True+ assert refreshed.audio_player_position == 'top'+++def test_account_preferences_branch_normalizes_legacy_language_name(cleanup_users):+ # Legacy display-name transcription language must be normalized to a code.+ user = _mk_user(cleanup_users)+ uid = user.id+ with _client_logged_in_as(user) as client:+ client.post('/account', data={+ 'preferences_form': '1',+ 'ui_language': 'en',+ 'transcription_language': 'Français',+ })+ refreshed = db.session.get(User, uid)+ assert refreshed.transcription_language == 'fr'+++def test_account_preferences_invalid_audio_position_falls_back(cleanup_users):+ user = _mk_user(cleanup_users)+ uid = user.id+ with _client_logged_in_as(user) as client:+ client.post('/account', data={+ 'preferences_form': '1',+ 'ui_language': 'en',+ 'audio_player_position': 'sideways',+ })+ refreshed = db.session.get(User, uid)+ assert refreshed.audio_player_position == 'bottom'+++def test_account_prompts_branch_persists_summary_prompt(cleanup_users):+ user = _mk_user(cleanup_users)+ uid = user.id+ with _client_logged_in_as(user) as client:+ resp = client.post('/account', data={+ 'summary_prompt': 'Summarize tightly.',+ 'extract_events': 'on',+ 'transcription_hotwords': 'Speakr',+ })+ assert resp.status_code == 302+ assert '#prompts' in resp.headers['Location']+ refreshed = db.session.get(User, uid)+ assert refreshed.summary_prompt == 'Summarize tightly.'+ assert refreshed.extract_events is True+ assert refreshed.transcription_hotwords == 'Speakr'+++def test_account_ajax_returns_json(cleanup_users):+ user = _mk_user(cleanup_users)+ with _client_logged_in_as(user) as client:+ resp = client.post('/account', data={'user_name': 'Ajax'},+ headers={'X-Requested-With': 'XMLHttpRequest'})+ assert resp.status_code == 200+ assert resp.get_json()['success'] is True+++# ---------------------------------------------------------------------------+# Password change+# ---------------------------------------------------------------------------++def test_change_password_success(cleanup_users):+ user = _mk_user(cleanup_users)+ uid = user.id+ new_pw = "Brand-New-Pw9!"+ with _client_logged_in_as(user) as client:+ resp = client.post('/change_password', data={+ 'current_password': GOOD_PW,+ 'new_password': new_pw,+ 'confirm_password': new_pw,+ })+ assert resp.status_code == 302+ refreshed = db.session.get(User, uid)+ assert auth_mod.bcrypt.check_password_hash(refreshed.password, new_pw)+ assert not auth_mod.bcrypt.check_password_hash(refreshed.password, GOOD_PW)+++def test_change_password_wrong_current_rejected(cleanup_users):+ user = _mk_user(cleanup_users)+ uid = user.id+ with _client_logged_in_as(user) as client:+ resp = client.post('/change_password', data={+ 'current_password': 'not-the-real-one',+ 'new_password': "Another-Pw9!",+ 'confirm_password': "Another-Pw9!",+ })+ assert resp.status_code == 302+ refreshed = db.session.get(User, uid)+ # Password unchanged.+ assert auth_mod.bcrypt.check_password_hash(refreshed.password, GOOD_PW)+++def test_change_password_mismatch_confirmation_rejected(cleanup_users):+ user = _mk_user(cleanup_users)+ uid = user.id+ with _client_logged_in_as(user) as client:+ client.post('/change_password', data={+ 'current_password': GOOD_PW,+ 'new_password': "Another-Pw9!",+ 'confirm_password': "Different-Pw9!",+ })+ refreshed = db.session.get(User, uid)+ assert auth_mod.bcrypt.check_password_hash(refreshed.password, GOOD_PW)+++def test_change_password_weak_new_rejected(cleanup_users):+ user = _mk_user(cleanup_users)+ uid = user.id+ with _client_logged_in_as(user) as client:+ client.post('/change_password', data={+ 'current_password': GOOD_PW,+ 'new_password': "weak",+ 'confirm_password': "weak",+ })+ refreshed = db.session.get(User, uid)+ assert auth_mod.bcrypt.check_password_hash(refreshed.password, GOOD_PW)+++def test_change_password_sso_only_user_redirected_to_reset(cleanup_users):+ # GHSA-x4q4-3ww4-h329: SSO-only (no local password) cannot set password here.+ user = _mk_user(cleanup_users, password=None, sso_subject="sub-" + uuid.uuid4().hex)+ uid = user.id+ with _client_logged_in_as(user) as client:+ resp = client.post('/change_password', data={+ 'new_password': "Brand-New-Pw9!",+ 'confirm_password': "Brand-New-Pw9!",+ })+ assert resp.status_code == 302+ refreshed = db.session.get(User, uid)+ assert refreshed.password is None # no password was set+++def test_change_password_requires_login():+ client = app.test_client()+ resp = client.post('/change_password', data={})+ assert resp.status_code in (302, 401)+++# ---------------------------------------------------------------------------+# Registration gating+# ---------------------------------------------------------------------------++def test_register_blocked_when_disabled(cleanup_users):+ with _envset(ALLOW_REGISTRATION='false'):+ client = app.test_client()+ resp = client.get('/register')+ assert resp.status_code == 302+ assert '/login' in resp.headers['Location']+++def test_register_allowed_creates_user(cleanup_users):+ username = "reg_" + uuid.uuid4().hex[:10]+ email = uuid.uuid4().hex[:10] + "@example.com"+ cleanup_users["usernames"].add(username)+ cleanup_users["emails"].add(email)+ pw = "Reg1ster-Pw!"+ # Force verification off so the success branch redirects to login.+ with _envset(ALLOW_REGISTRATION='true'), \+ mock.patch.object(auth_mod, 'is_email_verification_enabled', return_value=False):+ client = app.test_client()+ resp = client.post('/register', data={+ 'username': username,+ 'email': email,+ 'password': pw,+ 'confirm_password': pw,+ })+ assert resp.status_code == 302+ created = User.query.filter_by(email=email).first()+ assert created is not None+ assert created.email_verified is True # verification disabled → verified+++def test_register_blocked_by_disallowed_domain(cleanup_users):+ username = "reg_" + uuid.uuid4().hex[:10]+ email = uuid.uuid4().hex[:10] + "@evil.com"+ pw = "Reg1ster-Pw!"+ with _envset(ALLOW_REGISTRATION='true', REGISTRATION_ALLOWED_DOMAINS='example.com'), \+ mock.patch.object(auth_mod, 'is_email_verification_enabled', return_value=False):+ client = app.test_client()+ resp = client.post('/register', data={+ 'username': username,+ 'email': email,+ 'password': pw,+ 'confirm_password': pw,+ })+ assert resp.status_code == 200 # re-renders register with flash+ assert User.query.filter_by(email=email).first() is None+++def test_is_registration_domain_allowed_helper(app_ctx):+ with _envset(REGISTRATION_ALLOWED_DOMAINS='example.com'):+ assert auth_mod.is_registration_domain_allowed('[email protected]') is True+ assert auth_mod.is_registration_domain_allowed('[email protected]') is False+ assert auth_mod.is_registration_domain_allowed('') is False+ assert auth_mod.is_registration_domain_allowed('no-at-sign') is False+ with _envset(REGISTRATION_ALLOWED_DOMAINS=None):+ assert auth_mod.is_registration_domain_allowed('[email protected]') is True+++# ---------------------------------------------------------------------------+# SSO callback paths (OIDC client mocked)+# ---------------------------------------------------------------------------++def _sso_on():+ with mock.patch.object(auth_mod, 'is_sso_enabled', return_value=True), \+ mock.patch.object(auth_mod, 'init_sso_client', return_value=mock.MagicMock()), \+ mock.patch.object(auth_mod, 'get_sso_config',+ return_value={'provider_name': 'Keycloak', 'redirect_uri': 'https://app/cb'}):+ yield+++def test_sso_callback_disabled_redirects_to_login():+ with mock.patch.object(auth_mod, 'is_sso_enabled', return_value=False):+ client = app.test_client()+ resp = client.get('/auth/sso/callback')+ assert resp.status_code == 302+ assert '/login' in resp.headers['Location']+++def test_sso_callback_success_logs_in_user(cleanup_users):+ sub = "sub-" + uuid.uuid4().hex+ user = _mk_user(cleanup_users, password=None, sso_subject=sub)+ oauth = mock.MagicMock()+ oauth.sso.authorize_access_token.return_value = {'userinfo': {'sub': sub}}+ with _sso_on(), \+ mock.patch.object(auth_mod, 'get_sso_client', return_value=oauth), \+ mock.patch.object(auth_mod, 'create_or_update_sso_user', return_value=user):+ client = app.test_client()+ resp = client.get('/auth/sso/callback')+ assert resp.status_code == 302+ assert '/login' not in resp.headers['Location']+ with client.session_transaction() as s:+ assert s.get('_user_id') == str(user.id)+++def test_sso_callback_token_error_redirects_to_login():+ oauth = mock.MagicMock()+ oauth.sso.authorize_access_token.side_effect = Exception("boom")+ with _sso_on(), mock.patch.object(auth_mod, 'get_sso_client', return_value=oauth):+ client = app.test_client()+ resp = client.get('/auth/sso/callback')+ assert resp.status_code == 302+ assert '/login' in resp.headers['Location']+++def test_sso_callback_missing_subject_redirects_to_login():+ oauth = mock.MagicMock()+ oauth.sso.authorize_access_token.return_value = {'userinfo': {}} # no 'sub'+ with _sso_on(), mock.patch.object(auth_mod, 'get_sso_client', return_value=oauth):+ client = app.test_client()+ resp = client.get('/auth/sso/callback')+ assert resp.status_code == 302+ assert '/login' in resp.headers['Location']+++def test_sso_callback_permission_error_redirects_to_login():+ oauth = mock.MagicMock()+ oauth.sso.authorize_access_token.return_value = {'userinfo': {'sub': 'sub-' + uuid.uuid4().hex}}+ with _sso_on(), \+ mock.patch.object(auth_mod, 'get_sso_client', return_value=oauth), \+ mock.patch.object(auth_mod, 'create_or_update_sso_user',+ side_effect=PermissionError("domain not allowed")):+ client = app.test_client()+ resp = client.get('/auth/sso/callback')+ assert resp.status_code == 302+ assert '/login' in resp.headers['Location']+++def test_sso_callback_links_to_target_user(cleanup_users):+ target = _mk_user(cleanup_users)+ uid = target.id+ sub = "sub-" + uuid.uuid4().hex+ oauth = mock.MagicMock()+ oauth.sso.authorize_access_token.return_value = {'userinfo': {'sub': sub, 'name': 'Linked'}}+ with _sso_on(), \+ mock.patch.object(auth_mod, 'get_sso_client', return_value=oauth), \+ mock.patch.object(auth_mod, 'update_user_profile_from_claims'):+ client = app.test_client()+ with client.session_transaction() as s:+ s['_user_id'] = str(uid)+ s['_fresh'] = True+ s['sso_link_user_id'] = uid+ resp = client.get('/auth/sso/callback')+ assert resp.status_code == 302+ assert '/account' in resp.headers['Location']+ refreshed = db.session.get(User, uid)+ cleanup_users["subjects"].add(sub)+ assert refreshed.sso_subject == sub+ assert refreshed.sso_provider == 'Keycloak'+++def test_sso_login_disabled_redirects():+ with mock.patch.object(auth_mod, 'is_sso_enabled', return_value=False):+ client = app.test_client()+ resp = client.get('/auth/sso/login')+ assert resp.status_code == 302+ assert '/login' in resp.headers['Location']+++def test_sso_unlink_with_password(cleanup_users):+ user = _mk_user(cleanup_users, sso_subject="sub-" + uuid.uuid4().hex, sso_provider='Keycloak')+ uid = user.id+ with _client_logged_in_as(user) as client:+ resp = client.post('/auth/sso/unlink')+ assert resp.status_code == 302+ refreshed = db.session.get(User, uid)+ assert refreshed.sso_subject is None+++def test_sso_unlink_without_password_blocked(cleanup_users):+ sub = "sub-" + uuid.uuid4().hex+ user = _mk_user(cleanup_users, password=None, sso_subject=sub)+ uid = user.id+ with _client_logged_in_as(user) as client:+ resp = client.post('/auth/sso/unlink')+ assert resp.status_code == 302+ refreshed = db.session.get(User, uid)+ assert refreshed.sso_subject == sub # still linked+++# ---------------------------------------------------------------------------+# JSON preference endpoints (login-required)+# ---------------------------------------------------------------------------++def test_update_auto_summarization_requires_login():+ client = app.test_client()+ resp = client.post('/api/user/auto-summarization', json={'enabled': False})+ assert resp.status_code in (302, 401)+++def test_update_auto_summarization_persists(cleanup_users):+ user = _mk_user(cleanup_users)+ uid = user.id+ with _client_logged_in_as(user) as client:+ resp = client.post('/api/user/auto-summarization', json={'enabled': False})+ assert resp.status_code == 200+ assert resp.get_json()['auto_summarization'] is False+ assert db.session.get(User, uid).auto_summarization is False+++def test_update_auto_speaker_labelling_invalid_threshold(cleanup_users):+ user = _mk_user(cleanup_users)+ with _client_logged_in_as(user) as client:+ resp = client.post('/api/user/auto-speaker-labelling',+ json={'enabled': True, 'threshold': 'bogus'})+ assert resp.status_code == 400+++def test_update_auto_speaker_labelling_persists(cleanup_users):+ user = _mk_user(cleanup_users)+ uid = user.id+ with _client_logged_in_as(user) as client:+ resp = client.post('/api/user/auto-speaker-labelling',+ json={'enabled': True, 'threshold': 'high'})+ assert resp.status_code == 200+ refreshed = db.session.get(User, uid)+ assert refreshed.auto_speaker_labelling is True+ assert refreshed.auto_speaker_labelling_threshold == 'high'+++if __name__ == "__main__":+ sys.exit(pytest.main([__file__, "-q"]))diff --git a/tests/test_cov_embeddings.py b/tests/test_cov_embeddings.pynew file mode 100644index 00000000..8d856443--- /dev/null+++ b/tests/test_cov_embeddings.py@@ -0,0 +1,942 @@+#!/usr/bin/env python3+"""+Coverage-focused tests for src/services/embeddings.py.++These complement the existing targeted tests:+ - tests/test_embedding_api_mode.py (mode selection / generate_embeddings API path)+ - tests/test_embedding_identifier_compat.py (identifier migration at startup)++Here we fill the gaps: chunk_transcription splitting, process_recording_chunks+(happy path, idempotency, empty/short transcript, embedding failure rollback,+length mismatch rollback), serialize/deserialize gating, the API retry/backoff+machinery (_api_embed, _is_transient_embedding_error, token tracking), provider+client init, get_accessible_recording_ids, and basic_text_search_chunks ++semantic_search_chunks (including the API-mode and fallback paths).++All heavy/external pieces are mocked at the embeddings.py import site, so the+suite is fully offline and hermetic. The local sentence-transformers model is+NOT installed in the test image, so local-mode tests stub the model object.++Run (pytest only — these rely on conftest.py's isolated DB):+ pytest tests/test_cov_embeddings.py -q+"""++import importlib+import os+import sys+from pathlib import Path+from unittest.mock import MagicMock, patch++import numpy as np+import pytest++sys.path.insert(0, str(Path(__file__).parent.parent))++from src.app import app, db # noqa: E402+from src.models import Recording, TranscriptChunk, User # noqa: E402++import src.services.embeddings as emb_default # noqa: E402+++# ---------------------------------------------------------------------------+# Helpers+# ---------------------------------------------------------------------------++_ENV_KEYS = (+ "EMBEDDING_MODEL", "EMBEDDING_BASE_URL", "EMBEDDING_API_KEY",+ "EMBEDDING_DIMENSIONS", "EMBEDDING_API_MAX_RETRIES",+ "EMBEDDING_API_BACKOFF_SECONDS", "ENABLE_INTERNAL_SHARING",+)+++def _reload_embeddings(**env):+ """Reload the embeddings module with a clean set of env overrides.++ Returns the freshly reloaded module object. Any global on that object+ (USE_API_EMBEDDINGS, EMBEDDINGS_AVAILABLE, etc.) reflects the env passed.+ """+ saved = {k: os.environ.get(k) for k in _ENV_KEYS}+ for k in _ENV_KEYS:+ os.environ.pop(k, None)+ for k, v in env.items():+ if v is not None:+ os.environ[k] = str(v)+ import src.services.embeddings as emb+ importlib.reload(emb)+ return emb, saved+++def _restore_env(saved):+ for k, v in saved.items():+ os.environ.pop(k, None)+ if v is not None:+ os.environ[k] = v+ importlib.reload(emb_default)+++def local_emb():+ """Reload embeddings in default (local) mode and force EMBEDDINGS_AVAILABLE+ on so the storage gating in serialize/deserialize is exercised even though+ sentence-transformers is not installed in the test image."""+ emb, saved = _reload_embeddings()+ yield emb+ _restore_env(saved)+++def api_emb():+ """Reload embeddings in API mode (EMBEDDING_BASE_URL set)."""+ emb, saved = _reload_embeddings(+ EMBEDDING_BASE_URL="http://localhost:9999/v1",+ EMBEDDING_API_KEY="not-needed",+ EMBEDDING_MODEL="bge-test",+ EMBEDDING_API_MAX_RETRIES="3",+ EMBEDDING_API_BACKOFF_SECONDS="0", # no real sleeping in tests+ )+ yield emb+ _restore_env(saved)+++_USER_SEQ = [0]+++def _make_user():+ """Create a unique user scoped to this test file (shared session DB)."""+ _USER_SEQ[0] += 1+ suffix = f"cov_emb_{os.getpid()}_{_USER_SEQ[0]}"+ u = User(username=suffix[:20], email=f"{suffix}@example.test", password="x")+ db.session.add(u)+ db.session.commit()+ return u+++def _make_recording(user, transcription, **kw):+ r = Recording(+ user_id=user.id,+ transcription=transcription,+ title=kw.pop("title", "cov-emb-rec"),+ status="COMPLETED",+ **kw,+ )+ db.session.add(r)+ db.session.commit()+ return r+++def _fixed_vectors(texts, dim=4):+ """Deterministic fixed-dim vectors, one per text."""+ out = []+ for i, _ in enumerate(texts):+ out.append(np.full(dim, float(i + 1), dtype=np.float32))+ return out+++# ===========================================================================+# chunk_transcription+# ===========================================================================++def test_chunk_empty_returns_empty(local_emb):+ assert local_emb.chunk_transcription("") == []+ assert local_emb.chunk_transcription(None) == []+++def test_chunk_short_returns_single(local_emb):+ text = "A short transcript."+ assert local_emb.chunk_transcription(text, max_chunk_length=500) == [text]+++def test_chunk_exactly_at_limit_returns_single(local_emb):+ text = "x" * 50+ assert local_emb.chunk_transcription(text, max_chunk_length=50) == [text]+++def test_chunk_long_text_splits_multiple(local_emb):+ text = ("Sentence one is here. " * 60).strip()+ chunks = local_emb.chunk_transcription(text, max_chunk_length=100, overlap=10)+ assert len(chunks) > 1+ # every chunk fits roughly within the bound (sentence breaks can shorten)+ assert all(len(c) <= 100 for c in chunks)+ # reconstructable-ish: each chunk is non-empty stripped text+ assert all(c == c.strip() and c for c in chunks)+++def test_chunk_respects_sentence_boundary(local_emb):+ # Build text where a sentence ends near the boundary so the splitter+ # breaks at the period rather than mid-word.+ first = "This is the opening sentence and it keeps going for a while. "+ second = "Then the next sentence begins after the boundary point here."+ text = first + second+ chunks = local_emb.chunk_transcription(text, max_chunk_length=len(first) + 5, overlap=5)+ assert len(chunks) >= 2+ assert chunks[0].endswith(".")+++def test_chunk_no_overlap_infinite_loop_guard(local_emb):+ # A single huge token with no sentence boundaries still terminates.+ text = "a" * 2000+ chunks = local_emb.chunk_transcription(text, max_chunk_length=100, overlap=50)+ assert len(chunks) > 1+ assert all(chunks)+++# ===========================================================================+# serialize_embedding / deserialize_embedding+# ===========================================================================++def test_serialize_roundtrip_when_available(api_emb):+ # api_emb mode => EMBEDDINGS_AVAILABLE is True (sklearn present).+ assert api_emb.EMBEDDINGS_AVAILABLE is True+ vec = np.array([1.0, 2.0, 3.0], dtype=np.float32)+ blob = api_emb.serialize_embedding(vec)+ assert isinstance(blob, (bytes, bytearray))+ back = api_emb.deserialize_embedding(blob)+ assert np.allclose(back, vec)+ assert back.dtype == np.float32+++def test_serialize_none_returns_none(api_emb):+ assert api_emb.serialize_embedding(None) is None+ assert api_emb.deserialize_embedding(None) is None+++def test_serialize_gated_off_when_unavailable(local_emb):+ # Default/local mode in the test image: ST not installed, no API URL =>+ # EMBEDDINGS_AVAILABLE is False, so serialize/deserialize return None.+ assert local_emb.EMBEDDINGS_AVAILABLE is False+ vec = np.array([1.0, 2.0], dtype=np.float32)+ assert local_emb.serialize_embedding(vec) is None+ blob = vec.tobytes()+ assert local_emb.deserialize_embedding(blob) is None+++# ===========================================================================+# _is_transient_embedding_error+# ===========================================================================+[email protected]("msg", [+ "Connection timed out",+ "rate limit exceeded",+ "429 Too Many Requests",+ "503 Service Unavailable",+ "The server is overloaded, please try again",+ "connection reset by peer",+])+def test_transient_errors_detected(api_emb, msg):+ assert api_emb._is_transient_embedding_error(Exception(msg)) is True++[email protected]("msg", [+ "invalid api key",+ "model not found",+ "401 Unauthorized",+ "no such model",+])+def test_non_transient_errors_detected(api_emb, msg):+ assert api_emb._is_transient_embedding_error(Exception(msg)) is False+++# ===========================================================================+# get_embedding_api_client / get_embedding_model+# ===========================================================================++def test_get_api_client_none_in_local_mode(local_emb):+ with app.app_context():+ assert local_emb.get_embedding_api_client() is None+++def test_get_embedding_model_none_without_local_lib(local_emb):+ # sentence-transformers not installed in test image -> None.+ with app.app_context():+ assert local_emb.get_embedding_model() is None+++def test_get_api_client_initialises_and_caches(api_emb):+ fake_client = MagicMock()+ fake_openai = MagicMock(return_value=fake_client)+ fake_llm = MagicMock(llm_timeout=30, LLM_MAX_RETRIES=2, http_client_no_proxy=None)+ with app.app_context():+ with patch.dict("sys.modules", {"openai": MagicMock(OpenAI=fake_openai),+ "src.services.llm": fake_llm}):+ c1 = api_emb.get_embedding_api_client()+ c2 = api_emb.get_embedding_api_client()+ assert c1 is fake_client+ assert c2 is fake_client # cached, only constructed once+ fake_openai.assert_called_once()+++def test_get_api_client_handles_init_failure(api_emb):+ boom = MagicMock(side_effect=RuntimeError("boom"))+ with app.app_context():+ with patch.dict("sys.modules", {"openai": MagicMock(OpenAI=boom),+ "src.services.llm": MagicMock()}):+ assert api_emb.get_embedding_api_client() is None+++# ===========================================================================+# _api_embed+# ===========================================================================++def _client_returning(vectors):+ resp = MagicMock()+ resp.data = [MagicMock(embedding=list(v)) for v in vectors]+ resp.usage = None+ client = MagicMock()+ client.embeddings.create.return_value = resp+ return client+++def test_api_embed_empty_texts_returns_empty(api_emb):+ with app.app_context():+ with patch.object(api_emb, "get_embedding_api_client", return_value=MagicMock()):+ assert api_emb._api_embed([]) == []+++def test_api_embed_no_client_returns_empty(api_emb):+ with app.app_context():+ with patch.object(api_emb, "get_embedding_api_client", return_value=None):+ assert api_emb._api_embed(["x"]) == []+++def test_api_embed_success(api_emb):+ client = _client_returning([[0.1, 0.2], [0.3, 0.4]])+ with app.app_context():+ with patch.object(api_emb, "get_embedding_api_client", return_value=client):+ out = api_emb._api_embed(["a", "b"])+ assert len(out) == 2+ assert all(v.dtype == np.float32 for v in out)+ assert np.allclose(out[0], [0.1, 0.2])+++def test_api_embed_forwards_dimensions(api_emb):+ emb, saved = _reload_embeddings(+ EMBEDDING_BASE_URL="http://localhost:9999/v1",+ EMBEDDING_MODEL="text-embedding-3-large",+ EMBEDDING_DIMENSIONS="8",+ EMBEDDING_API_BACKOFF_SECONDS="0",+ )+ try:+ client = _client_returning([[0.0] * 8])+ with app.app_context():+ with patch.object(emb, "get_embedding_api_client", return_value=client):+ emb._api_embed(["x"])+ kwargs = client.embeddings.create.call_args.kwargs+ assert kwargs["dimensions"] == 8+ finally:+ _restore_env(saved)+++def test_api_embed_retries_then_succeeds(api_emb):+ resp = MagicMock()+ resp.data = [MagicMock(embedding=[1.0, 2.0])]+ resp.usage = None+ client = MagicMock()+ # First two calls transient-fail, third succeeds.+ client.embeddings.create.side_effect = [+ Exception("connection timeout"),+ Exception("503 service unavailable"),+ resp,+ ]+ with app.app_context():+ with patch.object(api_emb, "get_embedding_api_client", return_value=client):+ with patch.object(api_emb.time, "sleep"):+ out = api_emb._api_embed(["x"])+ assert len(out) == 1+ assert client.embeddings.create.call_count == 3+++def test_api_embed_non_transient_fails_fast(api_emb):+ client = MagicMock()+ client.embeddings.create.side_effect = Exception("invalid api key")+ with app.app_context():+ with patch.object(api_emb, "get_embedding_api_client", return_value=client):+ out = api_emb._api_embed(["x"])+ # Permanent error -> single attempt, empty result.+ assert out == []+ assert client.embeddings.create.call_count == 1+++def test_api_embed_exhausts_retries_returns_empty(api_emb):+ client = MagicMock()+ client.embeddings.create.side_effect = Exception("rate limit hit")+ with app.app_context():+ with patch.object(api_emb, "get_embedding_api_client", return_value=client):+ with patch.object(api_emb.time, "sleep"):+ out = api_emb._api_embed(["x"])+ assert out == []+ assert client.embeddings.create.call_count == 3 # max attempts+++def test_api_embed_records_token_usage(api_emb):+ resp = MagicMock()+ resp.data = [MagicMock(embedding=[1.0, 2.0])]+ usage = MagicMock()+ usage.model_extra = {"cost": 0.0001}+ usage.prompt_tokens = 11+ usage.total_tokens = 11+ resp.usage = usage+ client = MagicMock()+ client.embeddings.create.return_value = resp++ tracker = MagicMock()+ fake_tt_mod = MagicMock(token_tracker=tracker)+ with app.app_context():+ with patch.object(api_emb, "get_embedding_api_client", return_value=client):+ with patch.dict("sys.modules", {"src.services.token_tracking": fake_tt_mod}):+ out = api_emb._api_embed(["x"], user_id=123)+ assert len(out) == 1+ tracker.record_usage.assert_called_once()+ kwargs = tracker.record_usage.call_args.kwargs+ assert kwargs["user_id"] == 123+ assert kwargs["operation_type"] == "embedding"+ assert kwargs["total_tokens"] == 11+ assert kwargs["cost"] == pytest.approx(0.0001)+++def test_api_embed_usage_tracking_failure_is_swallowed(api_emb):+ resp = MagicMock()+ resp.data = [MagicMock(embedding=[1.0, 2.0])]+ usage = MagicMock()+ usage.model_extra = {}+ usage.prompt_tokens = 5+ usage.total_tokens = 5+ resp.usage = usage+ client = MagicMock()+ client.embeddings.create.return_value = resp++ tracker = MagicMock()+ tracker.record_usage.side_effect = RuntimeError("db down")+ fake_tt_mod = MagicMock(token_tracker=tracker)+ with app.app_context():+ with patch.object(api_emb, "get_embedding_api_client", return_value=client):+ with patch.dict("sys.modules", {"src.services.token_tracking": fake_tt_mod}):+ out = api_emb._api_embed(["x"], user_id=1)+ # Tracking blew up but embedding result is still returned.+ assert len(out) == 1+++# ===========================================================================+# generate_embeddings (local-mode branches; API branch covered elsewhere)+# ===========================================================================++def test_generate_embeddings_empty_input(local_emb):+ assert local_emb.generate_embeddings([]) == []+++def test_generate_embeddings_local_unavailable(local_emb):+ # No local lib, no API => warning + empty.+ with app.app_context():+ assert local_emb.generate_embeddings(["x"]) == []+++def test_generate_embeddings_local_model_path(local_emb):+ # Simulate a loaded local model by patching get_embedding_model and+ # flipping LOCAL_EMBEDDINGS_AVAILABLE on.+ fake_model = MagicMock()+ fake_model.encode.return_value = [np.array([0.1, 0.2], dtype=np.float64),+ np.array([0.3, 0.4], dtype=np.float64)]+ with app.app_context():+ with patch.object(local_emb, "LOCAL_EMBEDDINGS_AVAILABLE", True):+ with patch.object(local_emb, "get_embedding_model", return_value=fake_model):+ out = local_emb.generate_embeddings(["a", "b"])+ assert len(out) == 2+ assert all(v.dtype == np.float32 for v in out)+++def test_generate_embeddings_local_model_none(local_emb):+ with app.app_context():+ with patch.object(local_emb, "LOCAL_EMBEDDINGS_AVAILABLE", True):+ with patch.object(local_emb, "get_embedding_model", return_value=None):+ assert local_emb.generate_embeddings(["a"]) == []+++def test_generate_embeddings_local_encode_raises(local_emb):+ fake_model = MagicMock()+ fake_model.encode.side_effect = RuntimeError("cuda oom")+ with app.app_context():+ with patch.object(local_emb, "LOCAL_EMBEDDINGS_AVAILABLE", True):+ with patch.object(local_emb, "get_embedding_model", return_value=fake_model):+ assert local_emb.generate_embeddings(["a"]) == []+++# ===========================================================================+# process_recording_chunks+# ===========================================================================++def test_process_missing_recording(api_emb):+ with app.app_context():+ assert api_emb.process_recording_chunks(999999) is False+++def test_process_recording_no_transcription(api_emb):+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription=None)+ assert api_emb.process_recording_chunks(rec.id) is False+++def test_process_empty_string_transcription_noop(api_emb):+ # Empty transcription is falsy => returns False (handled like missing).+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription="")+ assert api_emb.process_recording_chunks(rec.id) is False+++def test_process_whitespace_only_chunks_to_empty(api_emb):+ # Non-empty but chunk_transcription yields [] only for falsy input;+ # a short non-empty transcript yields one chunk, so test the genuine+ # "no chunks" branch by patching chunk_transcription to return [].+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription="some real text here")+ with patch.object(api_emb, "chunk_transcription", return_value=[]):+ assert api_emb.process_recording_chunks(rec.id) is True+ assert TranscriptChunk.query.filter_by(recording_id=rec.id).count() == 0+++def test_process_happy_path_creates_chunks(api_emb):+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription="hello world, this is content")+ with patch.object(api_emb, "generate_embeddings",+ side_effect=lambda texts, user_id=None: _fixed_vectors(texts)):+ ok = api_emb.process_recording_chunks(rec.id)+ assert ok is True+ chunks = TranscriptChunk.query.filter_by(recording_id=rec.id).order_by(+ TranscriptChunk.chunk_index).all()+ assert len(chunks) == 1+ assert chunks[0].chunk_index == 0+ assert chunks[0].content+ assert chunks[0].embedding is not None # serialized blob stored+ assert chunks[0].user_id == user.id+++def test_process_multi_chunk(api_emb):+ long_text = ("This is a self contained sentence number one. " * 40).strip()+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription=long_text)+ with patch.object(api_emb, "generate_embeddings",+ side_effect=lambda texts, user_id=None: _fixed_vectors(texts)):+ ok = api_emb.process_recording_chunks(rec.id)+ assert ok is True+ n = TranscriptChunk.query.filter_by(recording_id=rec.id).count()+ assert n > 1+++def test_process_idempotent_replaces_old_chunks(api_emb):+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription="first version of the text")+ with patch.object(api_emb, "generate_embeddings",+ side_effect=lambda texts, user_id=None: _fixed_vectors(texts)):+ assert api_emb.process_recording_chunks(rec.id) is True+ first_count = TranscriptChunk.query.filter_by(recording_id=rec.id).count()+ assert first_count == 1+ # Re-run: old chunks are deleted and replaced (no duplication /+ # accumulation), so the count stays at exactly one.+ assert api_emb.process_recording_chunks(rec.id) is True+ second = TranscriptChunk.query.filter_by(recording_id=rec.id).all()+ assert len(second) == 1+ # The single surviving chunk reflects the (re)generated content.+ assert second[0].content+ assert second[0].chunk_index == 0+++def test_process_embedding_failure_rolls_back(api_emb):+ # Pre-seed a chunk, then make embedding generation return [] so the+ # length-mismatch branch rolls back and preserves existing chunks.+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription="content that will rechunk")+ with patch.object(api_emb, "generate_embeddings",+ side_effect=lambda texts, user_id=None: _fixed_vectors(texts)):+ assert api_emb.process_recording_chunks(rec.id) is True+ before = TranscriptChunk.query.filter_by(recording_id=rec.id).all()+ before_ids = {c.id for c in before}+ assert before_ids++ # Now embedding returns empty -> mismatch -> rollback, old chunks kept.+ with patch.object(api_emb, "generate_embeddings",+ side_effect=lambda texts, user_id=None: []):+ assert api_emb.process_recording_chunks(rec.id) is False+ after_ids = {c.id for c in TranscriptChunk.query.filter_by(recording_id=rec.id)}+ assert after_ids == before_ids # nothing lost+++def test_process_partial_embedding_count_rolls_back(api_emb):+ long_text = ("Independent sentence here for chunking. " * 40).strip()+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription=long_text)+ # Return only one vector regardless of chunk count -> mismatch.+ with patch.object(api_emb, "generate_embeddings",+ side_effect=lambda texts, user_id=None: _fixed_vectors(texts[:1])):+ assert api_emb.process_recording_chunks(rec.id) is False+ assert TranscriptChunk.query.filter_by(recording_id=rec.id).count() == 0+++def test_process_exception_path_returns_false(api_emb):+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription="boom text")+ with patch.object(api_emb, "chunk_transcription",+ side_effect=RuntimeError("unexpected")):+ assert api_emb.process_recording_chunks(rec.id) is False+++# ===========================================================================+# get_accessible_recording_ids+# ===========================================================================++def test_accessible_ids_own_recordings(api_emb):+ with app.app_context():+ user = _make_user()+ r1 = _make_recording(user, transcription="t1")+ r2 = _make_recording(user, transcription="t2")+ ids = api_emb.get_accessible_recording_ids(user.id)+ assert set(ids) >= {r1.id, r2.id}+++def test_accessible_ids_none_for_unknown_user(api_emb):+ with app.app_context():+ assert api_emb.get_accessible_recording_ids(8888888) == []+++def test_accessible_ids_sharing_disabled_excludes_shares(api_emb):+ # ENABLE_INTERNAL_SHARING defaults to false in this reload, so the share+ # branch is skipped — only own recordings are returned.+ assert api_emb.ENABLE_INTERNAL_SHARING is False+ with app.app_context():+ user = _make_user()+ r = _make_recording(user, transcription="owned")+ ids = api_emb.get_accessible_recording_ids(user.id)+ assert r.id in ids+++# ===========================================================================+# basic_text_search_chunks+# ===========================================================================++def _seed_chunks(emb, user, rec, texts):+ for i, t in enumerate(texts):+ c = TranscriptChunk(+ recording_id=rec.id, user_id=user.id, chunk_index=i, content=t,+ embedding=emb.serialize_embedding(np.full(4, float(i + 1), dtype=np.float32)),+ )+ db.session.add(c)+ db.session.commit()+++def test_basic_search_no_accessible_recordings(api_emb):+ with app.app_context():+ assert api_emb.basic_text_search_chunks(7777777, "anything") == []+++def test_basic_search_ranks_by_match_count(api_emb):+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription="seed")+ _seed_chunks(api_emb, user, rec, [+ "the quick brown fox jumps",+ "fox and hound running together quick",+ "completely unrelated weather report",+ ])+ results = api_emb.basic_text_search_chunks(user.id, "quick fox", top_k=5)+ assert results+ # Top result should contain both query words.+ top_chunk, top_score = results[0]+ assert top_score == pytest.approx(1.0)+ assert "fox" in top_chunk.content.lower() and "quick" in top_chunk.content.lower()+++def test_basic_search_all_stop_words_fallback(api_emb):+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription="seed")+ _seed_chunks(api_emb, user, rec, ["is the of an"])+ # Query is entirely stop words; falls back to using them as terms.+ results = api_emb.basic_text_search_chunks(user.id, "is the", top_k=5)+ assert isinstance(results, list)+++def test_basic_search_only_single_char_query_returns_empty(api_emb):+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription="seed")+ _seed_chunks(api_emb, user, rec, ["a b c d"])+ # All query words are <=1 char -> no usable words -> [].+ results = api_emb.basic_text_search_chunks(user.id, "a b", top_k=5)+ assert results == []+++def test_basic_search_recording_id_filter(api_emb):+ with app.app_context():+ user = _make_user()+ rec1 = _make_recording(user, transcription="seed1")+ rec2 = _make_recording(user, transcription="seed2")+ _seed_chunks(api_emb, user, rec1, ["alpha keyword here"])+ _seed_chunks(api_emb, user, rec2, ["alpha keyword elsewhere"])+ results = api_emb.basic_text_search_chunks(+ user.id, "keyword", filters={"recording_ids": [rec1.id]}, top_k=5)+ assert results+ assert all(c.recording_id == rec1.id for c, _ in results)+++def test_basic_search_speaker_filter(api_emb):+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription="seed", participants="Alice, Bob")+ _seed_chunks(api_emb, user, rec, ["meeting keyword discussion"])+ results = api_emb.basic_text_search_chunks(+ user.id, "keyword", filters={"speaker_names": ["Alice"]}, top_k=5)+ assert results+ assert all(c.recording_id == rec.id for c, _ in results)+++def test_basic_search_date_filter(api_emb):+ import datetime as _dt+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription="seed",+ meeting_date=_dt.datetime(2024, 5, 1))+ _seed_chunks(api_emb, user, rec, ["meeting keyword discussion"])+ results = api_emb.basic_text_search_chunks(+ user.id, "keyword",+ filters={+ "date_from": _dt.datetime(2024, 1, 1),+ "date_to": _dt.datetime(2024, 12, 31),+ },+ top_k=5,+ )+ assert results+ assert all(c.recording_id == rec.id for c, _ in results)+++def test_basic_search_tag_filter(api_emb):+ from src.models import Tag, RecordingTag+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription="seed")+ _seed_chunks(api_emb, user, rec, ["tagged keyword content"])+ tag = Tag(name="cov-tag", user_id=user.id)+ db.session.add(tag)+ db.session.commit()+ db.session.add(RecordingTag(recording_id=rec.id, tag_id=tag.id))+ db.session.commit()+ results = api_emb.basic_text_search_chunks(+ user.id, "keyword", filters={"tag_ids": [tag.id]}, top_k=5)+ assert results+ assert all(c.recording_id == rec.id for c, _ in results)+++def test_basic_search_exception_returns_empty(api_emb):+ with app.app_context():+ with patch.object(api_emb, "get_accessible_recording_ids",+ side_effect=RuntimeError("db error")):+ assert api_emb.basic_text_search_chunks(1, "x") == []+++# ===========================================================================+# semantic_search_chunks+# ===========================================================================++def test_semantic_falls_back_when_embeddings_unavailable(local_emb):+ # local_emb: EMBEDDINGS_AVAILABLE False -> falls back to basic text search.+ assert local_emb.EMBEDDINGS_AVAILABLE is False+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription="seed")+ # local mode: serialize returns None, so seed chunks without embeddings.+ c = TranscriptChunk(recording_id=rec.id, user_id=user.id, chunk_index=0,+ content="searchable keyword content")+ db.session.add(c)+ db.session.commit()+ results = local_emb.semantic_search_chunks(user.id, "keyword", top_k=5)+ assert results # came back via basic_text_search fallback+ assert any("keyword" in ch.content.lower() for ch, _ in results)+++def test_semantic_api_mode_ranks_by_similarity(api_emb):+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription="seed")+ # Seed chunks with known 4-dim vectors.+ vecs = [+ np.array([1.0, 0.0, 0.0, 0.0], dtype=np.float32), # close to query+ np.array([0.0, 1.0, 0.0, 0.0], dtype=np.float32), # orthogonal+ ]+ for i, v in enumerate(vecs):+ db.session.add(TranscriptChunk(+ recording_id=rec.id, user_id=user.id, chunk_index=i,+ content=f"chunk {i}", embedding=api_emb.serialize_embedding(v)))+ db.session.commit()++ query_vec = np.array([1.0, 0.0, 0.0, 0.0], dtype=np.float32)+ with patch.object(api_emb, "_api_embed", return_value=[query_vec]):+ results = api_emb.semantic_search_chunks(user.id, "find me", top_k=5)+ assert len(results) == 2+ # First chunk (aligned vector) ranks highest.+ assert results[0][0].chunk_index == 0+ assert results[0][1] > results[1][1]+++def test_semantic_api_embed_failure_falls_back(api_emb):+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription="seed")+ db.session.add(TranscriptChunk(+ recording_id=rec.id, user_id=user.id, chunk_index=0,+ content="fallback keyword text",+ embedding=api_emb.serialize_embedding(np.ones(4, dtype=np.float32))))+ db.session.commit()+ # API returns nothing -> semantic search falls back to text search.+ with patch.object(api_emb, "_api_embed", return_value=[]):+ results = api_emb.semantic_search_chunks(user.id, "keyword", top_k=5)+ assert results+ assert any("keyword" in ch.content.lower() for ch, _ in results)+++def test_semantic_no_accessible_recordings_returns_empty(api_emb):+ with app.app_context():+ query_vec = np.ones(4, dtype=np.float32)+ with patch.object(api_emb, "_api_embed", return_value=[query_vec]):+ assert api_emb.semantic_search_chunks(9999990, "x") == []+++def test_semantic_skips_dim_mismatch_chunks(api_emb):+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription="seed")+ # One good 4-dim vector, one stale 8-dim vector.+ db.session.add(TranscriptChunk(+ recording_id=rec.id, user_id=user.id, chunk_index=0, content="good",+ embedding=api_emb.serialize_embedding(np.array([1, 0, 0, 0], dtype=np.float32))))+ db.session.add(TranscriptChunk(+ recording_id=rec.id, user_id=user.id, chunk_index=1, content="stale",+ embedding=api_emb.serialize_embedding(np.ones(8, dtype=np.float32))))+ db.session.commit()+ query_vec = np.array([1, 0, 0, 0], dtype=np.float32)+ with patch.object(api_emb, "_api_embed", return_value=[query_vec]):+ results = api_emb.semantic_search_chunks(user.id, "q", top_k=5)+ # Only the dimension-matching chunk survives.+ assert len(results) == 1+ assert results[0][0].content == "good"+++def test_semantic_no_chunks_with_embeddings_returns_empty(api_emb):+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription="seed")+ # Chunk with no embedding at all.+ db.session.add(TranscriptChunk(+ recording_id=rec.id, user_id=user.id, chunk_index=0,+ content="no embedding", embedding=None))+ db.session.commit()+ query_vec = np.ones(4, dtype=np.float32)+ with patch.object(api_emb, "_api_embed", return_value=[query_vec]):+ assert api_emb.semantic_search_chunks(user.id, "q", top_k=5) == []+++def test_semantic_top_k_partition_path(api_emb):+ # More chunks than top_k exercises the argpartition branch.+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription="seed")+ for i in range(6):+ v = np.zeros(4, dtype=np.float32)+ v[i % 4] = 1.0+ db.session.add(TranscriptChunk(+ recording_id=rec.id, user_id=user.id, chunk_index=i,+ content=f"c{i}", embedding=api_emb.serialize_embedding(v)))+ db.session.commit()+ query_vec = np.array([1, 0, 0, 0], dtype=np.float32)+ with patch.object(api_emb, "_api_embed", return_value=[query_vec]):+ results = api_emb.semantic_search_chunks(user.id, "q", top_k=2)+ assert len(results) == 2+ # results sorted descending by similarity+ assert results[0][1] >= results[1][1]+++def test_semantic_api_mode_recording_id_filter(api_emb):+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription="seed")+ db.session.add(TranscriptChunk(+ recording_id=rec.id, user_id=user.id, chunk_index=0, content="filtered",+ embedding=api_emb.serialize_embedding(np.array([1, 0, 0, 0], dtype=np.float32))))+ db.session.commit()+ query_vec = np.array([1, 0, 0, 0], dtype=np.float32)+ with patch.object(api_emb, "_api_embed", return_value=[query_vec]):+ results = api_emb.semantic_search_chunks(+ user.id, "q", filters={"recording_ids": [rec.id]}, top_k=5)+ assert len(results) == 1+ assert results[0][0].content == "filtered"+++def test_semantic_api_mode_tag_filter(api_emb):+ from src.models import Tag, RecordingTag+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription="seed")+ tag = Tag(name="cov-sem-tag", user_id=user.id)+ db.session.add(tag)+ db.session.commit()+ db.session.add(RecordingTag(recording_id=rec.id, tag_id=tag.id))+ db.session.add(TranscriptChunk(+ recording_id=rec.id, user_id=user.id, chunk_index=0, content="tagged",+ embedding=api_emb.serialize_embedding(np.array([1, 0, 0, 0], dtype=np.float32))))+ db.session.commit()+ query_vec = np.array([1, 0, 0, 0], dtype=np.float32)+ with patch.object(api_emb, "_api_embed", return_value=[query_vec]):+ results = api_emb.semantic_search_chunks(+ user.id, "q", filters={"tag_ids": [tag.id]}, top_k=5)+ assert len(results) == 1+ assert results[0][0].content == "tagged"+++def test_semantic_api_mode_speaker_filter(api_emb):+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription="seed", participants="Carol")+ db.session.add(TranscriptChunk(+ recording_id=rec.id, user_id=user.id, chunk_index=0, content="spoken",+ embedding=api_emb.serialize_embedding(np.array([1, 0, 0, 0], dtype=np.float32))))+ db.session.commit()+ query_vec = np.array([1, 0, 0, 0], dtype=np.float32)+ with patch.object(api_emb, "_api_embed", return_value=[query_vec]):+ results = api_emb.semantic_search_chunks(+ user.id, "q", filters={"speaker_names": ["Carol"]}, top_k=5)+ assert len(results) == 1+ assert results[0][0].content == "spoken"+++def test_semantic_api_mode_date_filter(api_emb):+ import datetime as _dt+ with app.app_context():+ user = _make_user()+ rec = _make_recording(user, transcription="seed",+ meeting_date=_dt.datetime(2023, 6, 1))+ db.session.add(TranscriptChunk(+ recording_id=rec.id, user_id=user.id, chunk_index=0, content="dated",+ embedding=api_emb.serialize_embedding(np.array([1, 0, 0, 0], dtype=np.float32))))+ db.session.commit()+ query_vec = np.array([1, 0, 0, 0], dtype=np.float32)+ with patch.object(api_emb, "_api_embed", return_value=[query_vec]):+ results = api_emb.semantic_search_chunks(+ user.id, "q",+ filters={+ "date_from": _dt.datetime(2023, 1, 1),+ "date_to": _dt.datetime(2023, 12, 31),+ },+ top_k=5,+ )+ assert len(results) == 1+ assert results[0][0].content == "dated"+++def test_semantic_exception_returns_empty(api_emb):+ with app.app_context():+ with patch.object(api_emb, "_api_embed", side_effect=RuntimeError("boom")):+ assert api_emb.semantic_search_chunks(1, "q") == []diff --git a/tests/test_cov_file_monitor.py b/tests/test_cov_file_monitor.pynew file mode 100644index 00000000..d6d22a1a--- /dev/null+++ b/tests/test_cov_file_monitor.py@@ -0,0 +1,798 @@+#!/usr/bin/env python3+"""Coverage tests for src/file_monitor.py.++These tests target the parts of the file monitor NOT already exercised by+tests/test_auto_process_tags.py:++ * Directory-scan filtering: hidden files, .processing files, unsupported+ extensions, instability skip, file disappearing between iterdir and probe.+ * The atomic-lock rename / process / unlock-on-error flow.+ * The full _process_file import+create+enqueue path (all heavy externals+ mocked: storage, ffprobe, convert_if_needed, job_queue).+ * Mode dispatch: admin_only (incl. no-admin-user skip), user_directories+ (valid/invalid user dir name), single_user (configured/unconfigured).+ * _extract_user_id_from_dirname patterns.+ * Module-level helpers: start/stop/status, start with feature disabled,+ invalid mode, _ensure_tag_folders_on_startup error path.++CRITICAL: we never start the real watcher thread or the polling loop. We call+the scan/process functions directly. _is_file_stable is stubbed so no real+time.sleep happens.++Shared-DB safety: every user/recording is uuid-suffixed and removed in+teardown; assertions are scoped to the rows this file creates.+"""++import os+import sys+import shutil+import tempfile+import uuid+import unittest+from pathlib import Path+from unittest.mock import patch, MagicMock++sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))++from src.app import app, db+from src.models import User, Recording, Tag, RecordingTag+from src.file_monitor import (+ FileMonitor,+ start_file_monitor,+ stop_file_monitor,+ get_file_monitor_status,+ _ensure_tag_folders_on_startup,+)+import src.file_monitor as fm_module+++def _uniq(prefix):+ return f"{prefix}_{uuid.uuid4().hex[:8]}"+++def _make_monitor(temp_dir, user_id, mode='admin_only'):+ """Build a monitor with caches pre-populated and stability stubbed out."""+ monitor = FileMonitor(temp_dir, check_interval=30, mode=mode)+ monitor._admin_user_id = user_id+ monitor._valid_users = {user_id: f'u{user_id}'}+ monitor._username_to_id = {f'u{user_id}': user_id}+ # Never actually sleep / stat-poll for stability in tests.+ monitor._is_file_stable = lambda *a, **kw: True+ return monitor+++def _write_file(path, data=b'\x00' * 64):+ with open(path, 'wb') as f:+ f.write(data)+++# ---------------------------------------------------------------------------+# Directory scan filtering+# ---------------------------------------------------------------------------++class TestScanDirectoryFiltering(unittest.TestCase):+ def setUp(self):+ self.ctx = app.app_context()+ self.ctx.push()+ self.temp_dir = tempfile.mkdtemp()+ self.uid = 4242++ def tearDown(self):+ shutil.rmtree(self.temp_dir, ignore_errors=True)+ self.ctx.pop()++ def _scan_collecting(self, monitor):+ processed = []+ monitor._process_file = lambda path, uid, tag_id=None: processed.append(+ {'path': str(path), 'uid': uid, 'tag_id': tag_id}+ )+ monitor._scan_directory_for_user(monitor.base_watch_directory, self.uid)+ return processed++ def test_nonexistent_directory_is_noop(self):+ monitor = _make_monitor(self.temp_dir, self.uid)+ missing = monitor.base_watch_directory / 'does-not-exist'+ # Should simply return, not raise.+ monitor._scan_directory_for_user(missing, self.uid)++ def test_hidden_and_processing_and_unsupported_files_skipped(self):+ _write_file(os.path.join(self.temp_dir, '.hidden.mp3'))+ _write_file(os.path.join(self.temp_dir, 'inprogress.mp3.processing'))+ _write_file(os.path.join(self.temp_dir, 'notes.txt'))+ _write_file(os.path.join(self.temp_dir, 'archive.zip'))+ # A subdirectory should be skipped (not a file).+ os.makedirs(os.path.join(self.temp_dir, 'subdir'))++ monitor = _make_monitor(self.temp_dir, self.uid)+ processed = self._scan_collecting(monitor)+ self.assertEqual(processed, [])++ def test_supported_file_is_locked_and_processed(self):+ _write_file(os.path.join(self.temp_dir, 'good.mp3'))+ monitor = _make_monitor(self.temp_dir, self.uid)+ processed = self._scan_collecting(monitor)++ self.assertEqual(len(processed), 1)+ # File was renamed to .processing before handing to _process_file.+ self.assertTrue(processed[0]['path'].endswith('.mp3.processing'))+ self.assertEqual(processed[0]['uid'], self.uid)+ self.assertIsNone(processed[0]['tag_id'])++ def test_unstable_file_skipped(self):+ _write_file(os.path.join(self.temp_dir, 'growing.mp3'))+ monitor = _make_monitor(self.temp_dir, self.uid)+ monitor._is_file_stable = lambda *a, **kw: False+ processed = self._scan_collecting(monitor)+ self.assertEqual(processed, [])+ # File should NOT have been renamed (still has original name).+ self.assertTrue(os.path.exists(os.path.join(self.temp_dir, 'growing.mp3')))++ def test_file_vanishes_during_stability_check(self):+ _write_file(os.path.join(self.temp_dir, 'racey.mp3'))+ monitor = _make_monitor(self.temp_dir, self.uid)++ def boom(*a, **kw):+ raise FileNotFoundError("gone")+ monitor._is_file_stable = boom+ processed = self._scan_collecting(monitor)+ self.assertEqual(processed, [])++ def test_lock_rename_failure_is_caught(self):+ _write_file(os.path.join(self.temp_dir, 'locked.mp3'))+ monitor = _make_monitor(self.temp_dir, self.uid)+ called = {'n': 0}+ monitor._process_file = lambda *a, **kw: called.__setitem__('n', called['n'] + 1)++ # The atomic lock is Path.rename; first call (lock) raises FileNotFound.+ real_rename = Path.rename++ def fake_rename(self_path, target):+ if str(self_path).endswith('.mp3'):+ raise FileNotFoundError("claimed by other worker")+ return real_rename(self_path, target)++ with patch.object(Path, 'rename', fake_rename):+ monitor._scan_directory_for_user(monitor.base_watch_directory, self.uid)+ self.assertEqual(called['n'], 0)++ def test_lock_rename_generic_error_is_caught(self):+ _write_file(os.path.join(self.temp_dir, 'locked2.mp3'))+ monitor = _make_monitor(self.temp_dir, self.uid)+ called = {'n': 0}+ monitor._process_file = lambda *a, **kw: called.__setitem__('n', called['n'] + 1)++ real_rename = Path.rename++ def fake_rename(self_path, target):+ if str(self_path).endswith('.mp3'):+ raise PermissionError("no perms")+ return real_rename(self_path, target)++ with patch.object(Path, 'rename', fake_rename):+ monitor._scan_directory_for_user(monitor.base_watch_directory, self.uid)+ self.assertEqual(called['n'], 0)++ def test_process_error_unlocks_file(self):+ _write_file(os.path.join(self.temp_dir, 'fails.mp3'))+ monitor = _make_monitor(self.temp_dir, self.uid)++ def explode(path, uid, tag_id=None):+ raise RuntimeError("processing blew up")+ monitor._process_file = explode++ monitor._scan_directory_for_user(monitor.base_watch_directory, self.uid)+ # On error, the .processing file is renamed back to the original name.+ self.assertTrue(os.path.exists(os.path.join(self.temp_dir, 'fails.mp3')))+ self.assertFalse(os.path.exists(os.path.join(self.temp_dir, 'fails.mp3.processing')))+++# ---------------------------------------------------------------------------+# _extract_user_id_from_dirname+# ---------------------------------------------------------------------------++class TestExtractUserId(unittest.TestCase):+ def setUp(self):+ self.monitor = FileMonitor(tempfile.mkdtemp(), check_interval=30)++ def tearDown(self):+ shutil.rmtree(str(self.monitor.base_watch_directory), ignore_errors=True)++ def test_user_prefix_format(self):+ self.assertEqual(self.monitor._extract_user_id_from_dirname('user123'), 123)++ def test_bare_number_format(self):+ self.assertEqual(self.monitor._extract_user_id_from_dirname('456'), 456)++ def test_case_insensitive_prefix(self):+ self.assertEqual(self.monitor._extract_user_id_from_dirname('USER7'), 7)++ def test_non_user_dirname_returns_none(self):+ self.assertIsNone(self.monitor._extract_user_id_from_dirname('meetings'))+ self.assertIsNone(self.monitor._extract_user_id_from_dirname('user_abc'))+ self.assertIsNone(self.monitor._extract_user_id_from_dirname(''))+++# ---------------------------------------------------------------------------+# _is_file_stable (real implementation, stability_time=0 so no real sleep wait)+# ---------------------------------------------------------------------------++class TestIsFileStable(unittest.TestCase):+ def setUp(self):+ self.temp_dir = tempfile.mkdtemp()++ def tearDown(self):+ shutil.rmtree(self.temp_dir, ignore_errors=True)++ def test_stable_file_returns_true(self):+ monitor = FileMonitor(self.temp_dir, check_interval=30)+ p = Path(self.temp_dir) / 'a.mp3'+ _write_file(str(p))+ with patch.object(fm_module.time, 'sleep', lambda *_: None):+ self.assertTrue(monitor._is_file_stable(p, stability_time=0))++ def test_missing_file_returns_false(self):+ monitor = FileMonitor(self.temp_dir, check_interval=30)+ p = Path(self.temp_dir) / 'gone.mp3'+ with patch.object(fm_module.time, 'sleep', lambda *_: None):+ self.assertFalse(monitor._is_file_stable(p, stability_time=0))++ def test_growing_file_returns_false(self):+ monitor = FileMonitor(self.temp_dir, check_interval=30)+ p = Path(self.temp_dir) / 'grow.mp3'+ _write_file(str(p), b'\x00' * 10)++ def grow(*_):+ _write_file(str(p), b'\x00' * 9999)+ with patch.object(fm_module.time, 'sleep', grow):+ self.assertFalse(monitor._is_file_stable(p, stability_time=0))+++# ---------------------------------------------------------------------------+# Mode dispatch+# ---------------------------------------------------------------------------++class TestModeDispatch(unittest.TestCase):+ def setUp(self):+ self.ctx = app.app_context()+ self.ctx.push()+ self.temp_dir = tempfile.mkdtemp()+ self.username = _uniq('cov_fm_dispatch')+ self.user = User(+ username=self.username,+ email=f'{self.username}@test.com',+ password='fakehash',+ is_admin=True,+ )+ db.session.add(self.user)+ db.session.commit()+ self.uid = self.user.id++ def tearDown(self):+ Tag.query.filter_by(user_id=self.uid).delete()+ db.session.commit()+ u = db.session.get(User, self.uid)+ if u:+ db.session.delete(u)+ db.session.commit()+ shutil.rmtree(self.temp_dir, ignore_errors=True)+ self.ctx.pop()++ def test_admin_only_no_admin_user_skips(self):+ monitor = FileMonitor(self.temp_dir, check_interval=30, mode='admin_only')+ monitor._admin_user_id = None+ scanned = []+ monitor._scan_directory_for_user = lambda *a, **kw: scanned.append(a)+ monitor._scan_admin_directory()+ self.assertEqual(scanned, [])++ def test_admin_only_scans_root_and_tags(self):+ monitor = _make_monitor(self.temp_dir, self.uid, mode='admin_only')+ calls = {'dir': 0, 'tags': 0}+ monitor._scan_directory_for_user = lambda *a, **kw: calls.__setitem__('dir', calls['dir'] + 1)+ monitor._scan_tag_subdirectories = lambda *a, **kw: calls.__setitem__('tags', calls['tags'] + 1)+ monitor._scan_admin_directory()+ self.assertEqual(calls['dir'], 1)+ self.assertEqual(calls['tags'], 1)++ def test_user_directories_valid_and_invalid(self):+ # Valid: dir named user<id> for the real user.+ os.makedirs(os.path.join(self.temp_dir, f'user{self.uid}'))+ # Invalid: user-shaped dir with an id that isn't in _valid_users.+ os.makedirs(os.path.join(self.temp_dir, 'user99999999'))+ # A plain file at the top level should be ignored by this mode.+ _write_file(os.path.join(self.temp_dir, 'ignore.mp3'))++ monitor = _make_monitor(self.temp_dir, self.uid, mode='user_directories')+ scanned = []+ monitor._scan_directory_for_user = lambda d, u, **kw: scanned.append(u)+ monitor._scan_tag_subdirectories = lambda *a, **kw: None+ monitor._scan_user_directories()+ # Only the valid user's directory got scanned.+ self.assertEqual(scanned, [self.uid])++ def test_user_directories_missing_base_dir(self):+ monitor = _make_monitor(os.path.join(self.temp_dir, 'nope'), self.uid,+ mode='user_directories')+ # base dir was created by __init__, so remove it to hit the not-exists path.+ shutil.rmtree(str(monitor.base_watch_directory), ignore_errors=True)+ monitor._scan_directory_for_user = lambda *a, **kw: self.fail("should not scan")+ monitor._scan_user_directories() # returns silently++ def test_single_user_configured(self):+ monitor = _make_monitor(self.temp_dir, self.uid, mode='single_user')+ monitor._username_to_id = {self.username: self.uid}+ scanned = []+ monitor._scan_directory_for_user = lambda d, u, **kw: scanned.append(u)+ monitor._scan_tag_subdirectories = lambda *a, **kw: None+ with patch.dict(os.environ, {'AUTO_PROCESS_DEFAULT_USERNAME': self.username}):+ monitor._scan_single_user_directory()+ self.assertEqual(scanned, [self.uid])++ def test_single_user_not_configured(self):+ monitor = _make_monitor(self.temp_dir, self.uid, mode='single_user')+ monitor._scan_directory_for_user = lambda *a, **kw: self.fail("should not scan")+ env = {k: v for k, v in os.environ.items() if k != 'AUTO_PROCESS_DEFAULT_USERNAME'}+ with patch.dict(os.environ, env, clear=True):+ monitor._scan_single_user_directory()++ def test_single_user_invalid_username(self):+ monitor = _make_monitor(self.temp_dir, self.uid, mode='single_user')+ monitor._username_to_id = {}+ monitor._scan_directory_for_user = lambda *a, **kw: self.fail("should not scan")+ with patch.dict(os.environ, {'AUTO_PROCESS_DEFAULT_USERNAME': 'ghost_user'}):+ monitor._scan_single_user_directory()+++# ---------------------------------------------------------------------------+# _update_user_cache+# ---------------------------------------------------------------------------++class TestUpdateUserCache(unittest.TestCase):+ def setUp(self):+ self.temp_dir = tempfile.mkdtemp()++ def tearDown(self):+ shutil.rmtree(self.temp_dir, ignore_errors=True)++ def test_cache_skipped_when_recent(self):+ monitor = FileMonitor(self.temp_dir, check_interval=30)+ import time as _t+ monitor._last_user_cache_update = _t.time() # very recent+ # If it tried to query, it would import src.app inside an app context;+ # the early return means _valid_users stays empty.+ monitor._update_user_cache()+ self.assertEqual(monitor._valid_users, {})++ def test_cache_populates_admin_and_users(self):+ monitor = FileMonitor(self.temp_dir, check_interval=30)+ monitor._last_user_cache_update = 0 # force refresh+ monitor._update_user_cache()+ # There may be other users in the shared DB; just assert the structure.+ self.assertIsInstance(monitor._valid_users, dict)+ self.assertIsInstance(monitor._username_to_id, dict)+ self.assertGreater(monitor._last_user_cache_update, 0)+++# ---------------------------------------------------------------------------+# Full _process_file flow (heavy externals mocked)+# ---------------------------------------------------------------------------++class TestProcessFile(unittest.TestCase):+ def setUp(self):+ self.ctx = app.app_context()+ self.ctx.push()+ self.temp_dir = tempfile.mkdtemp()+ self.staging_dir = tempfile.mkdtemp()+ self.username = _uniq('cov_fm_proc')+ self.user = User(+ username=self.username,+ email=f'{self.username}@test.com',+ password='fakehash',+ is_admin=True,+ )+ db.session.add(self.user)+ db.session.commit()+ self.uid = self.user.id+ self.monitor = _make_monitor(self.temp_dir, self.uid)++ def tearDown(self):+ rec_ids = [r.id for r in Recording.query.filter_by(user_id=self.uid).all()]+ if rec_ids:+ RecordingTag.query.filter(+ RecordingTag.recording_id.in_(rec_ids)+ ).delete(synchronize_session=False)+ Recording.query.filter_by(user_id=self.uid).delete()+ Tag.query.filter_by(user_id=self.uid).delete()+ db.session.commit()+ u = db.session.get(User, self.uid)+ if u:+ db.session.delete(u)+ db.session.commit()+ shutil.rmtree(self.temp_dir, ignore_errors=True)+ shutil.rmtree(self.staging_dir, ignore_errors=True)+ self.ctx.pop()++ def _make_processing_file(self, name='audio.mp3'):+ p = Path(self.temp_dir) / (name + '.processing')+ _write_file(str(p))+ return p++ def _build_storage_mock(self):+ storage = MagicMock()+ storage.get_staging_dir.return_value = self.staging_dir+ storage.build_recording_key.return_value = 'recordings/test/key.mp3'+ stored = MagicMock()+ stored.locator = 'local:recordings/test/key.mp3'+ storage.upload_local_file.return_value = stored+ return storage++ def _patches(self, storage, enqueue_capture, convert_passthrough=True):+ """Patch the externals _process_file imports. convert_if_needed and the+ ffprobe helpers are module-level imports on src.file_monitor; storage,+ job_queue, file_hash, registry, transcribe_audio_task are imported+ inside the function so we patch them at their origin module."""+ import contextlib++ def fake_convert(src_path, **kw):+ res = MagicMock()+ res.output_path = src_path # leave the staged copy in place+ res.was_converted = False+ res.was_compressed = False+ return res++ job_queue_mock = MagicMock()+ job_queue_mock.enqueue.side_effect = lambda **kw: enqueue_capture.append(kw)++ registry_mock = MagicMock()+ registry_mock.get_active_connector.return_value = None++ cm = contextlib.ExitStack()+ cm.enter_context(patch.object(fm_module, 'convert_if_needed', fake_convert))+ cm.enter_context(patch.object(fm_module, 'get_codec_info',+ lambda *a, **kw: {'audio_codec': 'mp3', 'has_video': False}))+ cm.enter_context(patch.object(fm_module, 'get_creation_date', lambda *a, **kw: None))+ cm.enter_context(patch.object(fm_module, 'get_duration', lambda *a, **kw: 12.5))+ cm.enter_context(patch('src.services.storage.get_storage_service',+ lambda: storage))+ cm.enter_context(patch('src.services.job_queue.job_queue', job_queue_mock))+ cm.enter_context(patch('src.services.transcription.get_registry',+ lambda: registry_mock))+ cm.enter_context(patch('src.utils.file_hash.compute_file_sha256',+ lambda *a, **kw: 'deadbeef' * 8))+ return cm++ def test_process_file_creates_recording_and_enqueues(self):+ p = self._make_processing_file()+ storage = self._build_storage_mock()+ enqueued = []+ with self._patches(storage, enqueued):+ self.monitor._process_file(p, self.uid)++ recs = Recording.query.filter_by(user_id=self.uid).all()+ self.assertEqual(len(recs), 1)+ rec = recs[0]+ self.assertEqual(rec.status, 'PENDING')+ self.assertTrue(rec.is_inbox)+ self.assertEqual(rec.processing_source, 'auto_process')+ self.assertEqual(rec.audio_duration_seconds, 12.5)+ self.assertEqual(rec.audio_path, 'local:recordings/test/key.mp3')+ self.assertEqual(rec.file_hash, 'deadbeef' * 8)+ self.assertEqual(len(enqueued), 1)+ self.assertEqual(enqueued[0]['recording_id'], rec.id)+ self.assertEqual(enqueued[0]['job_type'], 'transcribe')+ # The locked file is removed from the watch dir.+ self.assertFalse(p.exists())++ def test_process_file_applies_tag_and_job_params(self):+ p = self._make_processing_file('tagged.mp3')+ tag = Tag(+ name=_uniq('proc_tag'),+ user_id=self.uid,+ is_auto_process=True,+ auto_process_folder_name='proc-folder',+ default_hotwords='Foo, Bar',+ default_initial_prompt='a prompt',+ default_language='es',+ default_min_speakers=2,+ default_max_speakers=4,+ custom_prompt='summarize this',+ )+ db.session.add(tag)+ db.session.commit()++ storage = self._build_storage_mock()+ enqueued = []+ with self._patches(storage, enqueued):+ self.monitor._process_file(p, self.uid, tag_id=tag.id)++ rec = Recording.query.filter_by(user_id=self.uid).first()+ self.assertIsNotNone(rec)+ assoc = RecordingTag.query.filter_by(recording_id=rec.id, tag_id=tag.id).first()+ self.assertIsNotNone(assoc)++ params = enqueued[0]['params']+ self.assertEqual(params['hotwords'], 'Foo, Bar')+ self.assertEqual(params['initial_prompt'], 'a prompt')+ self.assertEqual(params['language'], 'es')+ self.assertEqual(params['min_speakers'], 2)+ self.assertEqual(params['max_speakers'], 4)+ self.assertEqual(params['custom_prompt'], 'summarize this')+ self.assertEqual(params['tag_id'], tag.id)++ def test_process_file_logs_conversion_and_compression(self):+ # convert_if_needed reporting was_converted/was_compressed exercises the+ # logging branches (411, 413).+ p = self._make_processing_file('converted.mp3')+ storage = self._build_storage_mock()+ enqueued = []++ import contextlib+ from datetime import datetime as _dt++ def fake_convert(src_path, **kw):+ res = MagicMock()+ res.output_path = src_path+ res.was_converted = True+ res.original_codec = 'amr'+ res.final_codec = 'mp3'+ res.was_compressed = True+ res.size_reduction_percent = 42.0+ return res++ with self._patches(storage, enqueued):+ with patch.object(fm_module, 'convert_if_needed', fake_convert):+ self.monitor._process_file(p, self.uid)+ self.assertEqual(len(enqueued), 1)++ def test_process_file_video_passthrough_skips_conversion(self):+ p = self._make_processing_file('movie.mp4')+ storage = self._build_storage_mock()+ enqueued = []++ def convert_should_not_run(*a, **kw):+ self.fail("convert_if_needed should be skipped for video passthrough")++ with self._patches(storage, enqueued):+ with patch.object(fm_module, 'VIDEO_PASSTHROUGH_ASR', True), \+ patch.object(fm_module, 'get_codec_info',+ lambda *a, **kw: {'audio_codec': 'aac', 'has_video': True}), \+ patch.object(fm_module, 'convert_if_needed', convert_should_not_run):+ self.monitor._process_file(p, self.uid)+ self.assertEqual(len(enqueued), 1)++ def test_process_file_ffmpeg_error_propagates(self):+ from src.utils.ffmpeg_utils import FFmpegError+ p = self._make_processing_file('badconvert.mp3')+ storage = self._build_storage_mock()+ enqueued = []++ def boom(*a, **kw):+ raise FFmpegError("conversion failed")++ with self._patches(storage, enqueued):+ with patch.object(fm_module, 'convert_if_needed', boom):+ with self.assertRaises(FFmpegError):+ self.monitor._process_file(p, self.uid)+ self.assertEqual(enqueued, [])++ def test_process_file_unknown_user_raises(self):+ p = self._make_processing_file('orphan.mp3')+ storage = self._build_storage_mock()+ enqueued = []+ with self._patches(storage, enqueued):+ with self.assertRaises(ValueError):+ self.monitor._process_file(p, 999999999)+ # Nothing enqueued, no recording created for the bad id.+ self.assertEqual(enqueued, [])++ def test_process_file_duplicate_hash_and_metadata_date(self):+ # Pre-create a recording with the same hash to hit the duplicate branch,+ # and return a creation date from metadata to hit that branch too.+ from datetime import datetime as _dt+ existing = Recording(+ title='dup',+ audio_path='local:x',+ original_filename='x.mp3',+ status='COMPLETED',+ user_id=self.uid,+ file_hash='deadbeef' * 8,+ )+ db.session.add(existing)+ db.session.commit()++ p = self._make_processing_file('dup_incoming.mp3')+ storage = self._build_storage_mock()+ enqueued = []+ with self._patches(storage, enqueued):+ with patch.object(fm_module, 'get_creation_date',+ lambda *a, **kw: _dt(2020, 1, 2, 3, 4, 5)):+ self.monitor._process_file(p, self.uid)++ recs = Recording.query.filter_by(user_id=self.uid).all()+ # Both the pre-existing and the new (processed anyway) recording exist.+ self.assertEqual(len(recs), 2)+ new_rec = [r for r in recs if r.id != existing.id][0]+ self.assertEqual(new_rec.meeting_date, _dt(2020, 1, 2, 3, 4, 5))+ self.assertEqual(len(enqueued), 1)++ def test_process_file_scan_tag_subdir_routes_tag_id(self):+ # Exercise _scan_tag_subdirectories end-to-end: matching folder -> tag_id.+ tag = Tag(+ name=_uniq('subdir_tag'),+ user_id=self.uid,+ is_auto_process=True,+ auto_process_folder_name='subdir-folder',+ )+ db.session.add(tag)+ db.session.commit()++ folder = Path(self.temp_dir) / 'subdir-folder'+ folder.mkdir()+ _write_file(str(folder / 'clip.mp3'))+ # Also a hidden dir and a user-shaped dir, both must be skipped.+ (Path(self.temp_dir) / '.hidden').mkdir()+ (Path(self.temp_dir) / 'user5').mkdir()++ captured = []+ self.monitor._scan_directory_for_user = (+ lambda d, u, tag_id=None: captured.append((str(d), u, tag_id))+ )+ self.monitor._scan_tag_subdirectories(self.monitor.base_watch_directory, self.uid)+ self.assertEqual(len(captured), 1)+ self.assertEqual(captured[0][2], tag.id)++ def test_process_file_falls_back_to_user_transcription_settings(self):+ # Give the user transcription defaults; no tag, so these should flow+ # into job params.+ self.user.transcription_language = 'de'+ self.user.transcription_hotwords = 'Alpha'+ self.user.transcription_initial_prompt = 'user prompt'+ db.session.commit()++ p = self._make_processing_file('userdefaults.mp3')+ storage = self._build_storage_mock()+ enqueued = []+ with self._patches(storage, enqueued):+ self.monitor._process_file(p, self.uid)++ params = enqueued[0]['params']+ self.assertEqual(params['language'], 'de')+ self.assertEqual(params['hotwords'], 'Alpha')+ self.assertEqual(params['initial_prompt'], 'user prompt')+++# ---------------------------------------------------------------------------+# Module-level helpers: start / stop / status / ensure folders+# ---------------------------------------------------------------------------++class TestModuleHelpers(unittest.TestCase):+ def setUp(self):+ self.temp_dir = tempfile.mkdtemp()+ self._orig_monitor = fm_module.file_monitor++ def tearDown(self):+ # Restore the module global; never leave a started monitor behind.+ if fm_module.file_monitor is not None and fm_module.file_monitor is not self._orig_monitor:+ try:+ fm_module.file_monitor.running = False+ except Exception:+ pass+ fm_module.file_monitor = self._orig_monitor+ shutil.rmtree(self.temp_dir, ignore_errors=True)++ def test_status_when_not_running(self):+ fm_module.file_monitor = None+ self.assertEqual(get_file_monitor_status(), {'running': False})++ def test_status_when_running(self):+ m = FileMonitor(self.temp_dir, check_interval=17, mode='single_user')+ m.running = True # pretend running WITHOUT starting a thread+ fm_module.file_monitor = m+ status = get_file_monitor_status()+ self.assertTrue(status['running'])+ self.assertEqual(status['mode'], 'single_user')+ self.assertEqual(status['check_interval'], 17)+ self.assertEqual(status['watch_directory'], str(m.base_watch_directory))++ def test_stop_file_monitor_clears_global(self):+ m = FileMonitor(self.temp_dir, check_interval=30)+ m.running = True+ m.thread = None # stop() will skip join when thread is None+ fm_module.file_monitor = m+ stop_file_monitor()+ self.assertIsNone(fm_module.file_monitor)+ self.assertFalse(m.running)++ def test_start_disabled_does_not_create_monitor(self):+ fm_module.file_monitor = None+ env = {+ 'ENABLE_AUTO_PROCESSING': 'false',+ 'AUTO_PROCESS_WATCH_DIR': self.temp_dir,+ 'AUTO_PROCESS_MODE': 'admin_only',+ }+ with patch.dict(os.environ, env):+ start_file_monitor()+ self.assertIsNone(fm_module.file_monitor)++ def test_start_invalid_mode_returns_early(self):+ fm_module.file_monitor = None+ env = {+ 'ENABLE_AUTO_PROCESSING': 'true',+ 'AUTO_PROCESS_WATCH_DIR': self.temp_dir,+ 'AUTO_PROCESS_MODE': 'bogus_mode',+ }+ with patch.dict(os.environ, env):+ start_file_monitor()+ self.assertIsNone(fm_module.file_monitor)++ def test_start_already_running_is_noop(self):+ m = FileMonitor(self.temp_dir, check_interval=30)+ m.running = True+ fm_module.file_monitor = m+ # Should return immediately without touching env / creating a new one.+ start_file_monitor()+ self.assertIs(fm_module.file_monitor, m)++ def test_start_enabled_starts_monitor_without_real_thread(self):+ fm_module.file_monitor = None+ env = {+ 'ENABLE_AUTO_PROCESSING': 'true',+ 'AUTO_PROCESS_WATCH_DIR': self.temp_dir,+ 'AUTO_PROCESS_MODE': 'admin_only',+ 'AUTO_PROCESS_CHECK_INTERVAL': '5',+ }+ # Patch FileMonitor.start so no real watcher thread / loop is launched,+ # and stub the tag-folder startup helper.+ with patch.dict(os.environ, env), \+ patch.object(FileMonitor, 'start', lambda self: setattr(self, 'running', True)), \+ patch.object(fm_module, '_ensure_tag_folders_on_startup', lambda *a, **kw: None):+ start_file_monitor()+ self.assertIsNotNone(fm_module.file_monitor)+ self.assertEqual(fm_module.file_monitor.mode, 'admin_only')+ self.assertEqual(fm_module.file_monitor.check_interval, 5)++ def test_ensure_tag_folders_user_directories_mode(self):+ # Happy path through the tag loop, user_directories branch (folder under+ # user<id>), plus a tag whose user no longer exists (skip) and one with+ # no folder name (skip).+ with app.app_context():+ username = _uniq('cov_fm_ensure')+ user = User(username=username, email=f'{username}@t.com',+ password='x', is_admin=True)+ db.session.add(user)+ db.session.commit()+ uid = user.id+ t1 = Tag(name=_uniq('t1'), user_id=uid, is_auto_process=True,+ auto_process_folder_name='folder-one')+ t2 = Tag(name=_uniq('t2'), user_id=uid, is_auto_process=True,+ auto_process_folder_name=None)+ db.session.add_all([t1, t2])+ db.session.commit()+ try:+ _ensure_tag_folders_on_startup(app, self.temp_dir, 'user_directories')+ expected = os.path.join(self.temp_dir, f'user{uid}', 'folder-one')+ self.assertTrue(os.path.isdir(expected))+ finally:+ Tag.query.filter_by(user_id=uid).delete()+ db.session.commit()+ u = db.session.get(User, uid)+ if u:+ db.session.delete(u)+ db.session.commit()++ def test_ensure_tag_folders_no_tags_returns_early(self):+ # Force the query to return an empty list -> early return (line 598-599).+ with patch('src.models.Tag') as TagMock:+ TagMock.query.filter_by.return_value.all.return_value = []+ _ensure_tag_folders_on_startup(app, self.temp_dir, 'admin_only')++ def test_ensure_tag_folders_handles_query_error(self):+ # Force the inner Tag query to raise; the helper swallows and logs.+ with patch('src.models.Tag') as TagMock:+ TagMock.query.filter_by.side_effect = RuntimeError("db down")+ # Should not raise.+ _ensure_tag_folders_on_startup(app, self.temp_dir, 'admin_only')+++if __name__ == '__main__':+ unittest.main()diff --git a/tests/test_cov_inquire.py b/tests/test_cov_inquire.pynew file mode 100644index 00000000..f9e27b7b--- /dev/null+++ b/tests/test_cov_inquire.py@@ -0,0 +1,837 @@+#!/usr/bin/env python3+"""+Coverage-focused tests for src/api/inquire.py (semantic search + chat-over-library).++These complement tests/test_inquire_mode.py (which covers models / imports /+reindex) by exercising the HTTP routes and the streaming RAG generator end to+end. Everything external is mocked at the inquire.py import site:+ - semantic_search_chunks (vector similarity search)+ - call_llm_completion (router + query enrichment LLM calls)+ - call_chat_completion (the final answer LLM call)+ - process_streaming_with_thinking (DIRECT-path streaming)+ - client (OpenRouter client availability gate)++so the suite is fully offline and deterministic.++SHARED-DB NOTE: the pytest DB is shared across files. Every test creates its+own user/recording/chunk rows and scopes all assertions to those IDs; nothing+asserts on global counts or lists.+"""+import os+import sys+import json+import uuid+import contextlib+from unittest.mock import patch, MagicMock++sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))++import pytest++from src.app import app, db+from src.models import User, Recording, TranscriptChunk, InquireSession, Tag++app.config['WTF_CSRF_ENABLED'] = False+++# --------------------------------------------------------------------------- #+# Fixtures / helpers+# --------------------------------------------------------------------------- #++def _suffix():+ return uuid.uuid4().hex[:8]+++def _drain_app_contexts():+ """Pop any Flask app contexts that leaked onto the stack.++ The inquire chat endpoint streams from a generator that does+ `ctx = app.app_context(); ctx.push()` and only pops it in a `finally`. When+ a test reads the SSE Response without exhausting/closing the generator, that+ `finally` never runs and the pushed context lingers — corrupting db.session+ for *later* tests (stale identity-map hits, wrong-owner lookups). We pop them+ defensively so every test starts with a clean context stack and session.+ """+ from flask import has_app_context+ popped = 0+ while has_app_context() and popped < 50:+ try:+ app_ctx = app.app_context()+ # Pop whatever is currently on top of the stack.+ from flask.globals import app_ctx as _current # type: ignore+ _current._get_current_object().pop()+ except Exception:+ break+ popped += 1+++def client():+ _drain_app_contexts()+ with app.app_context():+ db.session.remove()+ yield app.test_client()+ _drain_app_contexts()+ with app.app_context():+ db.session.remove()+++def _login(client, user_id):+ with client.session_transaction() as s:+ s['_user_id'] = str(user_id)+ s['_fresh'] = True+ yield+++def _make_user(**overrides):+ """Create and persist a user; returns the (detached-safe) id + a fresh getter."""+ suffix = _suffix()+ with app.app_context():+ user = User(+ username=overrides.get('username', f"inqcov_{suffix}"),+ email=overrides.get('email', f"inqcov_{suffix}@example.com"),+ name=overrides.get('name', "Test User"),+ job_title=overrides.get('job_title'),+ company=overrides.get('company'),+ output_language=overrides.get('output_language'),+ )+ db.session.add(user)+ db.session.commit()+ return user.id+++def _make_recording(user_id, **overrides):+ with app.app_context():+ rec = Recording(+ user_id=user_id,+ title=overrides.get('title', f"Rec {_suffix()}"),+ status=overrides.get('status', 'COMPLETED'),+ transcription=overrides.get('transcription', "Full transcript text here."),+ participants=overrides.get('participants'),+ meeting_date=overrides.get('meeting_date'),+ )+ db.session.add(rec)+ db.session.commit()+ return rec.id+++def _make_chunk(user_id, recording_id, **overrides):+ with app.app_context():+ chunk = TranscriptChunk(+ recording_id=recording_id,+ user_id=user_id,+ chunk_index=overrides.get('chunk_index', 0),+ content=overrides.get('content', "A relevant chunk of conversation."),+ start_time=overrides.get('start_time', 0.0),+ end_time=overrides.get('end_time', 5.0),+ speaker_name=overrides.get('speaker_name'),+ )+ db.session.add(chunk)+ db.session.commit()+ return chunk.id+++def _get_chunk_pair(chunk_id, similarity=0.9):+ """Return a real, session-attached (chunk, similarity) tuple usable inside an+ app context, mirroring what semantic_search_chunks yields.++ Must be called from within an active app context. We eager-load the parent+ Recording (joinedload) so chunk.recording stays accessible even after the+ chat generator's nested app-context exits and detaches the chunk — mirroring+ real semantic_search_chunks results, which the caller consumes lazily.+ """+ from sqlalchemy.orm import joinedload+ chunk = (db.session.query(TranscriptChunk)+ .options(joinedload(TranscriptChunk.recording))+ .filter_by(id=chunk_id)+ .first())+ return (chunk, similarity)+++def _search_returning(chunk_id, similarity=0.8):+ """Build a semantic_search_chunks side_effect that re-fetches the chunk inside+ whatever app context is active when the search runs, keeping it session-bound."""+ def _side_effect(*args, **kwargs):+ return [_get_chunk_pair(chunk_id, similarity)]+ return _side_effect+++def _sse_events(resp):+ """Parse an SSE Response into a list of decoded JSON payload dicts."""+ body = resp.get_data(as_text=True)+ # Close the streaming response so the generator's finally (ctx.pop()) runs and+ # no app context leaks into the next test.+ try:+ resp.close()+ except Exception:+ pass+ events = []+ for line in body.splitlines():+ line = line.strip()+ if line.startswith("data:"):+ payload = line[len("data:"):].strip()+ try:+ events.append(json.loads(payload))+ except json.JSONDecodeError:+ pass+ return events+++# --------------------------------------------------------------------------- #+# Auth: all routes require login+# --------------------------------------------------------------------------- #++def test_inquire_page_requires_login(client):+ resp = client.get('/inquire')+ assert resp.status_code in (302, 401)+++def test_search_requires_login(client):+ resp = client.post('/api/inquire/search', json={'query': 'x'})+ assert resp.status_code in (302, 401)+++def test_chat_requires_login(client):+ resp = client.post('/api/inquire/chat', json={'message': 'x'})+ assert resp.status_code in (302, 401)+++def test_sessions_get_requires_login(client):+ resp = client.get('/api/inquire/sessions')+ assert resp.status_code in (302, 401)+++def test_available_filters_requires_login(client):+ resp = client.get('/api/inquire/available_filters')+ assert resp.status_code in (302, 401)+++# --------------------------------------------------------------------------- #+# /inquire page+# --------------------------------------------------------------------------- #++def test_inquire_page_enabled_renders(client):+ user_id = _make_user()+ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True):+ resp = client.get('/inquire')+ assert resp.status_code == 200+++def test_inquire_page_disabled_redirects(client):+ user_id = _make_user()+ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', False):+ resp = client.get('/inquire')+ assert resp.status_code == 302+++# --------------------------------------------------------------------------- #+# Inquire-mode disabled => 403 on the JSON APIs+# --------------------------------------------------------------------------- #++def test_sessions_get_disabled_403(client):+ user_id = _make_user()+ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', False):+ resp = client.get('/api/inquire/sessions')+ assert resp.status_code == 403+++def test_sessions_post_disabled_403(client):+ user_id = _make_user()+ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', False):+ resp = client.post('/api/inquire/sessions', json={'session_name': 'x'})+ assert resp.status_code == 403+++def test_search_disabled_403(client):+ user_id = _make_user()+ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', False):+ resp = client.post('/api/inquire/search', json={'query': 'x'})+ assert resp.status_code == 403+++def test_chat_disabled_403(client):+ user_id = _make_user()+ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', False):+ resp = client.post('/api/inquire/chat', json={'message': 'x'})+ assert resp.status_code == 403+++def test_available_filters_disabled_403(client):+ user_id = _make_user()+ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', False):+ resp = client.get('/api/inquire/available_filters')+ assert resp.status_code == 403+++# --------------------------------------------------------------------------- #+# Sessions: create + list+# --------------------------------------------------------------------------- #++def test_create_session_no_data_400(client):+ user_id = _make_user()+ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True):+ # Empty JSON body -> data is falsy -> 400+ resp = client.post('/api/inquire/sessions', json={})+ assert resp.status_code == 400+++def test_create_session_success(client):+ user_id = _make_user()+ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True):+ resp = client.post('/api/inquire/sessions', json={+ 'session_name': 'My Session',+ 'filter_tags': [1, 2],+ 'filter_speakers': ['Alice'],+ 'filter_date_from': '2024-01-01',+ 'filter_date_to': '2024-12-31',+ 'filter_recording_ids': [42],+ })+ assert resp.status_code == 201+ data = resp.get_json()+ assert data['session_name'] == 'My Session'+ assert data['filter_speakers'] == ['Alice']+ assert data['filter_recording_ids'] == [42]+ assert data['filter_date_from'] == '2024-01-01'+++def test_get_sessions_owner_scoped(client):+ """A session created by user A must not appear for user B."""+ user_a = _make_user()+ user_b = _make_user()+ with app.app_context():+ sess = InquireSession(user_id=user_a, session_name='A-only', filter_tags='[]')+ db.session.add(sess)+ db.session.commit()+ sess_id = sess.id++ with _login(client, user_a), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True):+ resp = client.get('/api/inquire/sessions')+ assert resp.status_code == 200+ ids = [s['id'] for s in resp.get_json()]+ assert sess_id in ids++ with _login(client, user_b), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True):+ resp = client.get('/api/inquire/sessions')+ assert resp.status_code == 200+ ids = [s['id'] for s in resp.get_json()]+ assert sess_id not in ids+++def test_create_session_bad_date_500(client):+ user_id = _make_user()+ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True):+ resp = client.post('/api/inquire/sessions', json={+ 'session_name': 'bad', 'filter_date_from': 'not-a-date'+ })+ assert resp.status_code == 500+ assert 'error' in resp.get_json()+++# --------------------------------------------------------------------------- #+# Search endpoint+# --------------------------------------------------------------------------- #++def test_search_no_data_400(client):+ user_id = _make_user()+ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True):+ resp = client.post('/api/inquire/search', json={})+ assert resp.status_code == 400+++def test_search_no_query_400(client):+ user_id = _make_user()+ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True):+ resp = client.post('/api/inquire/search', json={'top_k': 5})+ assert resp.status_code == 400+++def test_search_returns_ranked_results(client):+ user_id = _make_user()+ rec_id = _make_recording(user_id, title="Budget Meeting")+ chunk_id = _make_chunk(user_id, rec_id, content="We discussed the budget.",+ speaker_name="Alice")++ def fake_search(uid, query, filters, top_k):+ assert uid == user_id+ return [_get_chunk_pair(chunk_id, 0.87)]++ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True), \+ patch('src.api.inquire.semantic_search_chunks', side_effect=fake_search):+ resp = client.post('/api/inquire/search', json={'query': 'budget'})+ assert resp.status_code == 200+ results = resp.get_json()['results']+ assert len(results) == 1+ r = results[0]+ assert r['similarity'] == 0.87+ assert r['recording_title'] == "Budget Meeting"+ assert r['content'] == "We discussed the budget."+++def test_search_empty_results(client):+ user_id = _make_user()+ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True), \+ patch('src.api.inquire.semantic_search_chunks', return_value=[]):+ resp = client.post('/api/inquire/search', json={'query': 'nothing matches'})+ assert resp.status_code == 200+ assert resp.get_json()['results'] == []+++def test_search_passes_filters(client):+ """Date/tag/speaker/recording filters are parsed and forwarded to the search."""+ user_id = _make_user()+ captured = {}++ def fake_search(uid, query, filters, top_k):+ captured.update(filters)+ captured['top_k'] = top_k+ return []++ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True), \+ patch('src.api.inquire.semantic_search_chunks', side_effect=fake_search):+ resp = client.post('/api/inquire/search', json={+ 'query': 'q',+ 'filter_tags': [3],+ 'filter_speakers': ['Bob'],+ 'filter_recording_ids': [7],+ 'filter_date_from': '2024-02-01',+ 'filter_date_to': '2024-03-01',+ 'top_k': 9,+ })+ assert resp.status_code == 200+ assert captured['tag_ids'] == [3]+ assert captured['speaker_names'] == ['Bob']+ assert captured['recording_ids'] == [7]+ assert str(captured['date_from']) == '2024-02-01'+ assert str(captured['date_to']) == '2024-03-01'+ assert captured['top_k'] == 9+++def test_search_with_meeting_date_in_result(client):+ from datetime import datetime+ user_id = _make_user()+ rec_id = _make_recording(user_id, meeting_date=datetime(2024, 5, 1))+ chunk_id = _make_chunk(user_id, rec_id)++ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True), \+ patch('src.api.inquire.semantic_search_chunks',+ return_value=None) as m:+ # Pass a callable that yields a real pair within the request context.+ m.side_effect = lambda *a, **k: [_get_chunk_pair(chunk_id, 0.5)]+ resp = client.post('/api/inquire/search', json={'query': 'x'})+ assert resp.status_code == 200+ r = resp.get_json()['results'][0]+ assert r['recording_meeting_date'] is not None+++def test_search_bad_date_500(client):+ user_id = _make_user()+ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True):+ resp = client.post('/api/inquire/search', json={+ 'query': 'q', 'filter_date_from': 'garbage'+ })+ assert resp.status_code == 500+ assert 'error' in resp.get_json()+++def test_search_internal_error_500(client):+ user_id = _make_user()+ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True), \+ patch('src.api.inquire.semantic_search_chunks',+ side_effect=RuntimeError("boom")):+ resp = client.post('/api/inquire/search', json={'query': 'q'})+ assert resp.status_code == 500+ assert 'boom' in resp.get_json()['error']+++# --------------------------------------------------------------------------- #+# Chat endpoint+# --------------------------------------------------------------------------- #++def test_chat_no_data_400(client):+ user_id = _make_user()+ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True):+ resp = client.post('/api/inquire/chat', json={})+ assert resp.status_code == 400+++def test_chat_no_message_400(client):+ user_id = _make_user()+ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True):+ resp = client.post('/api/inquire/chat', json={'message_history': []})+ assert resp.status_code == 400+++def test_chat_client_unavailable_503(client):+ user_id = _make_user()+ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True), \+ patch('src.api.inquire.client', None):+ resp = client.post('/api/inquire/chat', json={'message': 'hi'})+ assert resp.status_code == 503+++def _llm_msg(content):+ """Build a fake OpenAI-style completion response object."""+ resp = MagicMock()+ resp.choices = [MagicMock()]+ resp.choices[0].message.content = content+ return resp+++def _stream_chunks(texts):+ """Build a fake streaming iterator yielding delta chunks of `texts`."""+ out = []+ for t in texts:+ ch = MagicMock()+ ch.choices = [MagicMock()]+ ch.choices[0].delta.content = t+ out.append(ch)+ return iter(out)+++def test_chat_direct_path(client):+ """Router returns DIRECT -> uses process_streaming_with_thinking, no search."""+ user_id = _make_user()++ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True), \+ patch('src.api.inquire.client', MagicMock()), \+ patch('src.api.inquire.call_llm_completion',+ return_value=_llm_msg("DIRECT")) as router, \+ patch('src.api.inquire.process_streaming_with_thinking',+ return_value=iter([+ "data: " + json.dumps({'delta': 'Here is a formatted answer.'}) + "\n\n",+ "data: " + json.dumps({'end_of_stream': True}) + "\n\n",+ ])) as pst, \+ patch('src.api.inquire.semantic_search_chunks') as search:+ resp = client.post('/api/inquire/chat', json={'message': 'format this'})+ assert resp.status_code == 200+ events = _sse_events(resp)+ deltas = [e.get('delta') for e in events if 'delta' in e]+ assert 'Here is a formatted answer.' in deltas+ # DIRECT path must NOT run semantic search.+ search.assert_not_called()+ pst.assert_called()+++def test_chat_rag_path_with_results(client):+ """Router returns RAG -> enrichment -> search -> chat_completion answer."""+ user_id = _make_user()+ rec_id = _make_recording(user_id, title="Planning Call",+ participants="Alice, Bob")+ chunk_id = _make_chunk(user_id, rec_id, content="Alice proposed a new timeline.",+ speaker_name="Alice")++ # call_llm_completion is used for BOTH the router and the enrichment step.+ def llm_side_effect(messages, **kwargs):+ op = kwargs.get('operation_type')+ if op == 'query_routing':+ return _llm_msg("RAG")+ # query_enrichment -> JSON array+ return _llm_msg('["timeline", "planning"]')++ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True), \+ patch('src.api.inquire.client', MagicMock()), \+ patch('src.api.inquire.EMBEDDINGS_AVAILABLE', True), \+ patch('src.api.inquire.call_llm_completion', side_effect=llm_side_effect), \+ patch('src.api.inquire.semantic_search_chunks',+ side_effect=lambda *a, **k: [_get_chunk_pair(chunk_id, 0.8)]), \+ patch('src.api.inquire.call_chat_completion',+ return_value=_stream_chunks(["Alice ", "proposed ", "a timeline."])):+ resp = client.post('/api/inquire/chat', json={'message': 'what did Alice say?'})+ assert resp.status_code == 200+ events = _sse_events(resp)+ deltas = "".join(e['delta'] for e in events if 'delta' in e)+ assert "Alice proposed a timeline." in deltas+ assert any(e.get('end_of_stream') for e in events)+++def test_chat_rag_no_results(client):+ """RAG path with zero search results still streams an answer (no context)."""+ user_id = _make_user()++ def llm_side_effect(messages, **kwargs):+ if kwargs.get('operation_type') == 'query_routing':+ return _llm_msg("RAG")+ return _llm_msg('["foo"]')++ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True), \+ patch('src.api.inquire.client', MagicMock()), \+ patch('src.api.inquire.EMBEDDINGS_AVAILABLE', True), \+ patch('src.api.inquire.call_llm_completion', side_effect=llm_side_effect), \+ patch('src.api.inquire.semantic_search_chunks', return_value=[]), \+ patch('src.api.inquire.call_chat_completion',+ return_value=_stream_chunks(["No relevant info found."])):+ resp = client.post('/api/inquire/chat', json={'message': 'anything?'})+ assert resp.status_code == 200+ events = _sse_events(resp)+ deltas = "".join(e['delta'] for e in events if 'delta' in e)+ assert "No relevant info found." in deltas+++def test_chat_router_failure_falls_back_to_rag(client):+ """If the router LLM raises, code falls back to RAG enrichment + search."""+ user_id = _make_user()+ rec_id = _make_recording(user_id)+ chunk_id = _make_chunk(user_id, rec_id, content="fallback content")++ def llm_side_effect(messages, **kwargs):+ if kwargs.get('operation_type') == 'query_routing':+ raise RuntimeError("router down")+ return _llm_msg('["term"]')++ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True), \+ patch('src.api.inquire.client', MagicMock()), \+ patch('src.api.inquire.EMBEDDINGS_AVAILABLE', True), \+ patch('src.api.inquire.call_llm_completion', side_effect=llm_side_effect), \+ patch('src.api.inquire.semantic_search_chunks',+ side_effect=lambda *a, **k: [_get_chunk_pair(chunk_id, 0.7)]), \+ patch('src.api.inquire.call_chat_completion',+ return_value=_stream_chunks(["answer"])):+ resp = client.post('/api/inquire/chat', json={'message': 'q'})+ assert resp.status_code == 200+ events = _sse_events(resp)+ assert any(e.get('delta') == 'answer' for e in events)+++def test_chat_enrichment_failure_uses_original_query(client):+ """If enrichment returns non-JSON, search proceeds with the original query."""+ user_id = _make_user()+ rec_id = _make_recording(user_id)+ chunk_id = _make_chunk(user_id, rec_id)++ def llm_side_effect(messages, **kwargs):+ if kwargs.get('operation_type') == 'query_routing':+ return _llm_msg("RAG")+ return _llm_msg("not json at all")++ search_calls = {'n': 0}++ def search_side_effect(*a, **k):+ search_calls['n'] += 1+ return [_get_chunk_pair(chunk_id, 0.6)]++ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True), \+ patch('src.api.inquire.client', MagicMock()), \+ patch('src.api.inquire.EMBEDDINGS_AVAILABLE', True), \+ patch('src.api.inquire.call_llm_completion', side_effect=llm_side_effect), \+ patch('src.api.inquire.semantic_search_chunks', side_effect=search_side_effect), \+ patch('src.api.inquire.call_chat_completion',+ return_value=_stream_chunks(["ok"])):+ resp = client.post('/api/inquire/chat', json={'message': 'q'})+ assert resp.status_code == 200+ events = _sse_events(resp)+ # Enrichment failed (non-JSON) -> the code falls back to the original+ # query and still searches and answers. Assert the fallback ran (search+ # was invoked at least once) and produced the streamed answer.+ assert search_calls['n'] >= 1+ deltas = "".join(e['delta'] for e in events if 'delta' in e)+ assert 'ok' in deltas+++def test_chat_thinking_tags_split_out(client):+ """<think> content is emitted as 'thinking' events, the rest as 'delta'."""+ user_id = _make_user()+ rec_id = _make_recording(user_id)+ chunk_id = _make_chunk(user_id, rec_id)++ def llm_side_effect(messages, **kwargs):+ if kwargs.get('operation_type') == 'query_routing':+ return _llm_msg("RAG")+ return _llm_msg('["t"]')++ streamed = ["Before ", "<think>", "secret reasoning", "</think>", "After"]++ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True), \+ patch('src.api.inquire.client', MagicMock()), \+ patch('src.api.inquire.EMBEDDINGS_AVAILABLE', True), \+ patch('src.api.inquire.call_llm_completion', side_effect=llm_side_effect), \+ patch('src.api.inquire.semantic_search_chunks',+ side_effect=lambda *a, **k: [_get_chunk_pair(chunk_id, 0.7)]), \+ patch('src.api.inquire.call_chat_completion',+ return_value=_stream_chunks(streamed)):+ resp = client.post('/api/inquire/chat', json={'message': 'q'})+ assert resp.status_code == 200+ events = _sse_events(resp)+ thinking = [e['thinking'] for e in events if 'thinking' in e]+ deltas = "".join(e['delta'] for e in events if 'delta' in e)+ assert any('secret reasoning' in t for t in thinking)+ assert 'secret reasoning' not in deltas+ assert 'Before' in deltas and 'After' in deltas+++def test_chat_full_transcript_request(client):+ """If the model emits REQUEST_FULL_TRANSCRIPT, the full transcript is fetched+ and a second completion is run via process_streaming_with_thinking."""+ user_id = _make_user()+ rec_id = _make_recording(user_id, transcription="THE FULL TRANSCRIPT BODY")+ chunk_id = _make_chunk(user_id, rec_id)++ def llm_side_effect(messages, **kwargs):+ if kwargs.get('operation_type') == 'query_routing':+ return _llm_msg("RAG")+ return _llm_msg('["t"]')++ first_stream = _stream_chunks([f"REQUEST_FULL_TRANSCRIPT:{rec_id}\n"])++ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True), \+ patch('src.api.inquire.client', MagicMock()), \+ patch('src.api.inquire.EMBEDDINGS_AVAILABLE', True), \+ patch('src.api.inquire.call_llm_completion', side_effect=llm_side_effect), \+ patch('src.api.inquire.semantic_search_chunks',+ side_effect=lambda *a, **k: [_get_chunk_pair(chunk_id, 0.7)]), \+ patch('src.api.inquire.call_chat_completion', return_value=first_stream), \+ patch('src.api.inquire.process_streaming_with_thinking',+ return_value=iter([+ "data: " + json.dumps({'delta': 'full-transcript answer'}) + "\n\n",+ ])) as pst:+ resp = client.post('/api/inquire/chat', json={'message': 'give me everything'})+ assert resp.status_code == 200+ events = _sse_events(resp)+ deltas = "".join(e.get('delta', '') for e in events)+ assert 'full-transcript answer' in deltas+ pst.assert_called()+++def test_chat_full_transcript_request_wrong_owner(client):+ """REQUEST_FULL_TRANSCRIPT for a recording the user doesn't own -> error event."""+ owner = _make_user()+ other = _make_user()+ rec_id = _make_recording(owner, transcription="OWNED") # belongs to `owner`+ my_rec = _make_recording(other)+ my_chunk = _make_chunk(other, my_rec)++ def llm_side_effect(messages, **kwargs):+ if kwargs.get('operation_type') == 'query_routing':+ return _llm_msg("RAG")+ return _llm_msg('["t"]')++ first_stream = _stream_chunks([f"REQUEST_FULL_TRANSCRIPT:{rec_id}\n"])++ with _login(client, other), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True), \+ patch('src.api.inquire.client', MagicMock()), \+ patch('src.api.inquire.EMBEDDINGS_AVAILABLE', True), \+ patch('src.api.inquire.call_llm_completion', side_effect=llm_side_effect), \+ patch('src.api.inquire.semantic_search_chunks',+ side_effect=lambda *a, **k: [_get_chunk_pair(my_chunk, 0.7)]), \+ patch('src.api.inquire.call_chat_completion', return_value=first_stream):+ resp = client.post('/api/inquire/chat', json={'message': 'show it'})+ assert resp.status_code == 200+ events = _sse_events(resp)+ # Should surface an access error, not the owner's transcript.+ assert any('Unable to access full transcript' in str(e.get('delta', ''))+ for e in events)+++def test_chat_generation_error_emits_error_event(client):+ """An exception inside the generator is reported as an SSE error event."""+ user_id = _make_user()++ def llm_side_effect(messages, **kwargs):+ if kwargs.get('operation_type') == 'query_routing':+ return _llm_msg("RAG")+ return _llm_msg('["t"]')++ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True), \+ patch('src.api.inquire.client', MagicMock()), \+ patch('src.api.inquire.EMBEDDINGS_AVAILABLE', True), \+ patch('src.api.inquire.call_llm_completion', side_effect=llm_side_effect), \+ patch('src.api.inquire.semantic_search_chunks', return_value=[]), \+ patch('src.api.inquire.call_chat_completion',+ side_effect=RuntimeError("kaboom")):+ resp = client.post('/api/inquire/chat', json={'message': 'q'})+ assert resp.status_code == 200+ events = _sse_events(resp)+ assert any('kaboom' in str(e.get('error', '')) for e in events)+++def test_chat_token_budget_exceeded(client):+ """TokenBudgetExceeded inside the generator is reported with budget_exceeded."""+ from src.api.inquire import TokenBudgetExceeded+ user_id = _make_user()++ def llm_side_effect(messages, **kwargs):+ if kwargs.get('operation_type') == 'query_routing':+ return _llm_msg("RAG")+ return _llm_msg('["t"]')++ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True), \+ patch('src.api.inquire.client', MagicMock()), \+ patch('src.api.inquire.EMBEDDINGS_AVAILABLE', True), \+ patch('src.api.inquire.call_llm_completion', side_effect=llm_side_effect), \+ patch('src.api.inquire.semantic_search_chunks', return_value=[]), \+ patch('src.api.inquire.call_chat_completion',+ side_effect=TokenBudgetExceeded("over budget")):+ resp = client.post('/api/inquire/chat', json={'message': 'q'})+ assert resp.status_code == 200+ events = _sse_events(resp)+ assert any(e.get('budget_exceeded') for e in events)+++# --------------------------------------------------------------------------- #+# available_filters endpoint+# --------------------------------------------------------------------------- #++def test_available_filters_returns_owner_data(client):+ user_id = _make_user()+ rec_id = _make_recording(user_id, title="Standup", status='COMPLETED',+ participants="Carol, Dave")+ with app.app_context():+ tag = Tag(name=f"tag_{_suffix()}", user_id=user_id, group_id=None)+ db.session.add(tag)+ db.session.commit()+ tag_id = tag.id++ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True), \+ patch('src.api.inquire.get_accessible_recording_ids',+ return_value=[rec_id]):+ resp = client.get('/api/inquire/available_filters')+ assert resp.status_code == 200+ data = resp.get_json()+ assert tag_id in [t['id'] for t in data['tags']]+ assert 'Carol' in data['speakers'] and 'Dave' in data['speakers']+ assert rec_id in [r['id'] for r in data['recordings']]+++def test_available_filters_error_500(client):+ user_id = _make_user()+ with _login(client, user_id), \+ patch('src.api.inquire.ENABLE_INQUIRE_MODE', True), \+ patch('src.api.inquire.get_accessible_recording_ids',+ side_effect=RuntimeError("db gone")):+ resp = client.get('/api/inquire/available_filters')+ assert resp.status_code == 500+ assert 'error' in resp.get_json()diff --git a/tests/test_cov_job_queue.py b/tests/test_cov_job_queue.pynew file mode 100644index 00000000..3ef106db--- /dev/null+++ b/tests/test_cov_job_queue.py@@ -0,0 +1,721 @@+"""Coverage-focused tests for src/services/job_queue.py (FairJobQueue).++These complement tests/test_job_queue_race_condition.py (which covers the+atomic-claim concurrency path). Here we drive the public + internal methods+directly with workers OFF (conftest sets JOB_QUEUE_WORKERS=SUMMARY_QUEUE_WORKERS=0),+mocking the task functions, storage materialize layer, and webhook emission so+no real audio processing or network I/O happens.++Shared-DB note: the pytest session DB is shared across test files and the+queue singleton is global, so every assertion is scoped to job/recording IDs+this module created (tracked per-test and cleaned up).+"""++import json+import os+from contextlib import contextmanager+from datetime import datetime, timedelta+from unittest.mock import MagicMock, patch++import pytest++from src.app import app+from src.database import db+from src.models import ProcessingJob, Recording, User+from src.services.job_queue import (+ FairJobQueue,+ job_queue,+ TRANSCRIPTION_JOBS,+ SUMMARY_JOBS,+ MAX_RETRIES,+)+++# --------------------------------------------------------------------------+# Fixtures / helpers+# --------------------------------------------------------------------------+[email protected](autouse=True)+def _bind_queue():+ """Bind the singleton queue to the test app and reset round-robin state."""+ job_queue.init_app(app)+ # Reset fairness cursors so cross-test ordering is deterministic.+ job_queue._last_user_id_transcription = None+ job_queue._last_user_id_summary = None+ yield+++_user_counter = {"n": 0}+++def _make_user():+ """Create a unique throwaway user, returning its id."""+ _user_counter["n"] += 1+ n = _user_counter["n"]+ suffix = f"{os.getpid()}_{n}_{datetime.utcnow().timestamp()}"+ user = User(username=f"jq_{n}_{os.getpid()}"[:20],+ email=f"jq_{suffix}@example.com")+ if hasattr(user, "password"):+ user.password = "unused"+ db.session.add(user)+ db.session.commit()+ return user.id+++def _make_recording(user_id, status="PENDING", audio_path="/tmp/jq_test.mp3",+ original_filename="orig.mp3"):+ rec = Recording(+ user_id=user_id,+ title="JQ Test Recording",+ audio_path=audio_path,+ original_filename=original_filename,+ status=status,+ )+ db.session.add(rec)+ db.session.commit()+ return rec.id+++class _Tracker:+ """Tracks created job/recording/user ids for cleanup."""++ def __init__(self):+ self.job_ids = []+ self.recording_ids = []+ self.user_ids = []++ def cleanup(self):+ for jid in self.job_ids:+ j = db.session.get(ProcessingJob, jid)+ if j:+ db.session.delete(j)+ # also delete any jobs attached to our recordings (e.g. follow-ups)+ for rid in self.recording_ids:+ for j in ProcessingJob.query.filter_by(recording_id=rid).all():+ db.session.delete(j)+ db.session.commit()+ for rid in self.recording_ids:+ r = db.session.get(Recording, rid)+ if r:+ db.session.delete(r)+ db.session.commit()+ for uid in self.user_ids:+ u = db.session.get(User, uid)+ if u:+ db.session.delete(u)+ db.session.commit()+++def track():+ with app.app_context():+ t = _Tracker()+ try:+ yield t+ finally:+ t.cleanup()+++def _fake_materialized(path):+ """A storage service whose materialize() yields an object with local_path."""+ storage = MagicMock()+ mat = MagicMock()+ mat.local_path = path++ @contextmanager+ def _materialize(audio_path):+ yield mat++ storage.materialize.side_effect = _materialize+ return storage+++# --------------------------------------------------------------------------+# Singleton+# --------------------------------------------------------------------------++def test_singleton_identity():+ assert FairJobQueue() is job_queue+ assert FairJobQueue() is FairJobQueue()+++# --------------------------------------------------------------------------+# enqueue+# --------------------------------------------------------------------------++def test_enqueue_creates_transcribe_job_and_sets_recording_queued(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid, status="PENDING"); track.recording_ids.append(rid)++ jid = job_queue.enqueue(uid, rid, "transcribe",+ params={"language": "en"}, is_new_upload=True)+ track.job_ids.append(jid)++ job = db.session.get(ProcessingJob, jid)+ assert job.job_type == "transcribe"+ assert job.status == "queued"+ assert job.user_id == uid+ assert job.recording_id == rid+ assert job.is_new_upload is True+ assert json.loads(job.params) == {"language": "en"}++ rec = db.session.get(Recording, rid)+ assert rec.status == "QUEUED"+++def test_enqueue_summary_job_sets_recording_summarizing(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid); track.recording_ids.append(rid)++ jid = job_queue.enqueue(uid, rid, "summarize")+ track.job_ids.append(jid)++ job = db.session.get(ProcessingJob, jid)+ assert job.job_type == "summarize"+ assert job.params is None # no params -> stored as NULL+ assert db.session.get(Recording, rid).status == "SUMMARIZING"+++def test_enqueue_dedupes_same_type_active_job(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid); track.recording_ids.append(rid)++ jid1 = job_queue.enqueue(uid, rid, "transcribe")+ track.job_ids.append(jid1)+ jid2 = job_queue.enqueue(uid, rid, "transcribe")++ assert jid1 == jid2 # returns existing job id, no duplicate+ count = ProcessingJob.query.filter_by(recording_id=rid,+ job_type="transcribe").count()+ assert count == 1+++def test_enqueue_allows_different_job_types_to_coexist(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid); track.recording_ids.append(rid)++ jid1 = job_queue.enqueue(uid, rid, "transcribe"); track.job_ids.append(jid1)+ jid2 = job_queue.enqueue(uid, rid, "summarize"); track.job_ids.append(jid2)++ assert jid1 != jid2+ assert ProcessingJob.query.filter_by(recording_id=rid).count() == 2+++def test_enqueue_does_not_autostart_when_workers_zero(track):+ """With JOB_QUEUE_WORKERS=0, enqueue calls start() but no threads spawn."""+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid); track.recording_ids.append(rid)++ jid = job_queue.enqueue(uid, rid, "transcribe"); track.job_ids.append(jid)+ assert job_queue._transcription_workers == []+ assert job_queue._summary_workers == []+ # reset _running flag so other tests' enqueue path keeps exercising start()+ job_queue._running = False+++# --------------------------------------------------------------------------+# _claim_next_job+# --------------------------------------------------------------------------++def test_claim_returns_none_when_no_matching_jobs():+ # Use a job_type that no one ever enqueues to guarantee emptiness.+ assert job_queue._claim_next_job(["__nonexistent_type__"], "transcription") is None+++def test_claim_marks_job_processing_and_recording_processing(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid, status="QUEUED"); track.recording_ids.append(rid)+ jid = job_queue.enqueue(uid, rid, "transcribe"); track.job_ids.append(jid)++ claimed = job_queue._claim_next_job(["transcribe"], "transcription")+ assert claimed is not None+ assert claimed.id == jid+ assert claimed.status == "processing"+ assert claimed.started_at is not None++ assert db.session.get(Recording, rid).status == "PROCESSING"+ # round-robin cursor updated for transcription queue+ assert job_queue._last_user_id_transcription == uid+++def test_claim_picks_oldest_queued_job_for_user(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid1 = _make_recording(uid); track.recording_ids.append(rid1)+ rid2 = _make_recording(uid); track.recording_ids.append(rid2)++ j1 = job_queue.enqueue(uid, rid1, "transcribe"); track.job_ids.append(j1)+ j2 = job_queue.enqueue(uid, rid2, "transcribe"); track.job_ids.append(j2)+ # Force j1 to be older.+ job = db.session.get(ProcessingJob, j1)+ job.created_at = datetime.utcnow() - timedelta(minutes=5)+ db.session.commit()++ claimed = job_queue._claim_next_job(["transcribe"], "transcription")+ assert claimed.id == j1+++def test_claim_summary_queue_uses_separate_cursor(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid); track.recording_ids.append(rid)+ jid = job_queue.enqueue(uid, rid, "summarize"); track.job_ids.append(jid)++ claimed = job_queue._claim_next_job(SUMMARY_JOBS, "summary")+ assert claimed.id == jid+ assert job_queue._last_user_id_summary == uid+ assert job_queue._last_user_id_transcription is None+++# --------------------------------------------------------------------------+# fairness / round-robin across users+# --------------------------------------------------------------------------++def test_round_robin_across_two_users(track):+ u1 = _make_user(); track.user_ids.append(u1)+ u2 = _make_user(); track.user_ids.append(u2)+ r1 = _make_recording(u1); track.recording_ids.append(r1)+ r2 = _make_recording(u2); track.recording_ids.append(r2)+ r1b = _make_recording(u1); track.recording_ids.append(r1b)++ # u1 enqueues two jobs first (oldest), u2 enqueues one in the middle.+ j1 = job_queue.enqueue(u1, r1, "transcribe"); track.job_ids.append(j1)+ # make u1's jobs the oldest by user-min so u1 sorts first+ db.session.get(ProcessingJob, j1).created_at = datetime.utcnow() - timedelta(minutes=10)+ db.session.commit()+ j2 = job_queue.enqueue(u2, r2, "transcribe"); track.job_ids.append(j2)+ j1b = job_queue.enqueue(u1, r1b, "transcribe"); track.job_ids.append(j1b)++ first = job_queue._claim_next_job(["transcribe"], "transcription")+ # u1 has the oldest min(created_at) so it goes first (cursor was None)+ assert first.user_id == u1+ second = job_queue._claim_next_job(["transcribe"], "transcription")+ # Round-robin: after u1, the next distinct user (u2) should be served.+ assert second.user_id == u2+ third = job_queue._claim_next_job(["transcribe"], "transcription")+ # Back to u1 for its remaining job.+ assert third.user_id == u1+++# --------------------------------------------------------------------------+# _process_job dispatch routing+# --------------------------------------------------------------------------++def _claim(rid, job_types, queue_name):+ return job_queue._claim_next_job(job_types, queue_name)+++def test_process_job_transcribe_dispatches_task_and_completes(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid, status="QUEUED"); track.recording_ids.append(rid)+ jid = job_queue.enqueue(uid, rid, "transcribe",+ params={"language": "fr", "tag_id": 7})+ track.job_ids.append(jid)+ job = _claim(rid, ["transcribe"], "transcription")++ fake_task = MagicMock()+ storage = _fake_materialized("/tmp/jq_test.mp3")++ with patch("src.tasks.processing.transcribe_audio_task", fake_task), \+ patch("src.services.storage.get_storage_service", return_value=storage), \+ patch("os.path.exists", return_value=True), \+ patch.object(job_queue, "_emit_started_webhook") as started, \+ patch.object(job_queue, "_emit_completion_webhook") as completed:+ job_queue._process_job(job)++ fake_task.assert_called_once()+ _, kwargs = fake_task.call_args+ assert kwargs["language"] == "fr"+ assert kwargs["tag_id"] == 7+ # positional: app_context, recording_id, filepath, filename, start_time+ args = fake_task.call_args[0]+ assert args[1] == rid+ assert args[2] == "/tmp/jq_test.mp3"++ started.assert_called_once_with("transcribe", rid)+ completed.assert_called_once_with("transcribe", rid)++ refreshed = db.session.get(ProcessingJob, jid)+ assert refreshed.status == "completed"+ assert refreshed.completed_at is not None+++def test_process_job_summarize_dispatches_summary_task(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid); track.recording_ids.append(rid)+ jid = job_queue.enqueue(uid, rid, "summarize",+ params={"custom_prompt": "Hi", "user_id": uid})+ track.job_ids.append(jid)+ job = _claim(rid, SUMMARY_JOBS, "summary")++ fake_task = MagicMock()+ with patch("src.tasks.processing.generate_summary_only_task", fake_task), \+ patch.object(job_queue, "_emit_completion_webhook"):+ job_queue._process_job(job)++ fake_task.assert_called_once()+ kwargs = fake_task.call_args[1]+ assert kwargs["custom_prompt_override"] == "Hi"+ assert kwargs["user_id"] == uid+ assert db.session.get(ProcessingJob, jid).status == "completed"+++def test_process_job_reprocess_transcription_routes_to_transcribe_task(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid); track.recording_ids.append(rid)+ jid = job_queue.enqueue(uid, rid, "reprocess_transcription",+ params={"min_speakers": 2})+ track.job_ids.append(jid)+ job = _claim(rid, TRANSCRIPTION_JOBS, "transcription")++ fake_task = MagicMock()+ storage = _fake_materialized("/tmp/jq_test.mp3")+ with patch("src.tasks.processing.transcribe_audio_task", fake_task), \+ patch("src.services.storage.get_storage_service", return_value=storage), \+ patch("os.path.exists", return_value=True), \+ patch.object(job_queue, "_emit_started_webhook"), \+ patch.object(job_queue, "_emit_completion_webhook"):+ job_queue._process_job(job)++ fake_task.assert_called_once()+ assert fake_task.call_args[1]["min_speakers"] == 2+ assert db.session.get(ProcessingJob, jid).status == "completed"+++def test_process_job_reprocess_summary_routes_to_summary_task(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid); track.recording_ids.append(rid)+ jid = job_queue.enqueue(uid, rid, "reprocess_summary",+ params={"custom_prompt_append": True})+ track.job_ids.append(jid)+ job = _claim(rid, SUMMARY_JOBS, "summary")++ fake_task = MagicMock()+ with patch("src.tasks.processing.generate_summary_only_task", fake_task), \+ patch.object(job_queue, "_emit_completion_webhook"):+ job_queue._process_job(job)++ fake_task.assert_called_once()+ assert fake_task.call_args[1]["custom_prompt_append"] is True+ assert db.session.get(ProcessingJob, jid).status == "completed"+++def test_process_job_unknown_type_fails(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid); track.recording_ids.append(rid)+ # Insert a job with an unknown type directly (enqueue won't validate it,+ # but the claim path filters by type list, so create + claim manually).+ job = ProcessingJob(user_id=uid, recording_id=rid,+ job_type="bogus_type", status="processing",+ started_at=datetime.utcnow())+ db.session.add(job); db.session.commit()+ track.job_ids.append(job.id)++ with patch.object(job_queue, "_emit_failure_webhook"):+ job_queue._process_job(job)++ db.session.expire_all()+ refreshed = db.session.get(ProcessingJob, job.id)+ # "Unknown job type" is not in the permanent-error list, so it is a+ # transient failure: re-queued for retry (retry_count < MAX_RETRIES).+ assert refreshed.status == "queued"+ assert refreshed.retry_count == 1+ assert "Unknown job type" in refreshed.error_message+++# --------------------------------------------------------------------------+# success / failure / retry transitions+# --------------------------------------------------------------------------++def test_transient_failure_requeues_for_retry(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid); track.recording_ids.append(rid)+ jid = job_queue.enqueue(uid, rid, "summarize"); track.job_ids.append(jid)+ job = _claim(rid, SUMMARY_JOBS, "summary")++ boom = MagicMock(side_effect=RuntimeError("temporary network blip"))+ with patch("src.tasks.processing.generate_summary_only_task", boom):+ job_queue._process_job(job)++ refreshed = db.session.get(ProcessingJob, jid)+ assert refreshed.status == "queued" # re-queued for retry+ assert refreshed.retry_count == 1+ assert refreshed.started_at is None+ assert "temporary network blip" in refreshed.error_message+++def test_permanent_error_fails_without_retry(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid); track.recording_ids.append(rid)+ jid = job_queue.enqueue(uid, rid, "summarize"); track.job_ids.append(jid)+ job = _claim(rid, SUMMARY_JOBS, "summary")++ boom = MagicMock(side_effect=RuntimeError("Error code: 401 - invalid api key"))+ with patch("src.tasks.processing.generate_summary_only_task", boom), \+ patch.object(job_queue, "_emit_failure_webhook") as fail_hook:+ job_queue._process_job(job)++ refreshed = db.session.get(ProcessingJob, jid)+ assert refreshed.status == "failed"+ assert refreshed.retry_count == 1 # incremented once, but not retried+ assert db.session.get(Recording, rid).status == "FAILED"+ fail_hook.assert_called_once()+++def test_retry_exhaustion_marks_failed(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid); track.recording_ids.append(rid)+ jid = job_queue.enqueue(uid, rid, "summarize"); track.job_ids.append(jid)+ job = _claim(rid, SUMMARY_JOBS, "summary")+ # Pre-set retry_count so the next failure crosses MAX_RETRIES.+ db.session.get(ProcessingJob, jid).retry_count = MAX_RETRIES - 1+ db.session.commit()++ boom = MagicMock(side_effect=RuntimeError("transient again"))+ with patch("src.tasks.processing.generate_summary_only_task", boom), \+ patch.object(job_queue, "_emit_failure_webhook"):+ job_queue._process_job(job)++ refreshed = db.session.get(ProcessingJob, jid)+ assert refreshed.status == "failed"+ assert refreshed.retry_count == MAX_RETRIES+ assert db.session.get(Recording, rid).status == "FAILED"+++def test_process_job_missing_recording_fails(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid); track.recording_ids.append(rid)+ jid = job_queue.enqueue(uid, rid, "summarize"); track.job_ids.append(jid)+ job = _claim(rid, SUMMARY_JOBS, "summary")++ # Simulate the recording lookup returning None inside _process_job without+ # actually deleting the row (FK cascade would remove the job too). Patch+ # db.session.get so Recording lookups miss while ProcessingJob lookups hit.+ real_get = db.session.get++ def fake_get(model, ident, *a, **kw):+ if model is Recording:+ return None+ return real_get(model, ident, *a, **kw)++ with patch.object(db.session, "get", side_effect=fake_get), \+ patch.object(job_queue, "_emit_failure_webhook"):+ job_queue._process_job(job)++ db.session.expire_all()+ refreshed = db.session.get(ProcessingJob, jid)+ # "Recording not found" is transient (not in permanent-error list) -> retry.+ assert refreshed.status == "queued"+ assert refreshed.retry_count == 1+ assert "not found" in refreshed.error_message.lower()+++# --------------------------------------------------------------------------+# _is_permanent_error+# --------------------------------------------------------------------------+[email protected]("msg", [+ "Error code: 400 - bad request",+ "Error code: 413 payload too large",+ "status 404 model not found",+ "Invalid API key provided",+ "quota exceeded for this month",+ "unsupported format",+])+def test_is_permanent_error_true(msg):+ assert job_queue._is_permanent_error(msg) is True++[email protected]("msg", [+ "Error code: 500 - internal server error",+ "Connection timed out",+ "temporary network blip",+ "",+])+def test_is_permanent_error_false(msg):+ assert job_queue._is_permanent_error(msg) is False+++# --------------------------------------------------------------------------+# started-webhook emission inside materialize block+# --------------------------------------------------------------------------++def test_started_webhook_skipped_when_file_missing(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid); track.recording_ids.append(rid)+ jid = job_queue.enqueue(uid, rid, "transcribe"); track.job_ids.append(jid)+ job = _claim(rid, TRANSCRIPTION_JOBS, "transcription")++ fake_task = MagicMock()+ storage = _fake_materialized("/tmp/does_not_exist_xyz.mp3")+ with patch("src.tasks.processing.transcribe_audio_task", fake_task), \+ patch("src.services.storage.get_storage_service", return_value=storage), \+ patch("os.path.exists", return_value=False), \+ patch.object(job_queue, "_emit_started_webhook") as started, \+ patch.object(job_queue, "_emit_completion_webhook"):+ job_queue._process_job(job)++ started.assert_not_called() # file missing -> no started emit+ fake_task.assert_called_once() # task still runs+++def test_transcription_missing_audio_path_raises_and_fails(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid, audio_path=""); track.recording_ids.append(rid)+ jid = job_queue.enqueue(uid, rid, "transcribe"); track.job_ids.append(jid)+ job = _claim(rid, TRANSCRIPTION_JOBS, "transcription")++ with patch.object(job_queue, "_emit_failure_webhook"):+ job_queue._process_job(job)++ db.session.expire_all()+ refreshed = db.session.get(ProcessingJob, jid)+ # Missing audio_path raises a transient ValueError -> re-queued for retry.+ assert refreshed.status == "queued"+ assert refreshed.retry_count == 1+ assert "audio_path" in refreshed.error_message+++def test_emit_started_webhook_dispatches_event(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid); track.recording_ids.append(rid)++ with patch("src.services.webhook_dispatch.emit_webhook_event") as emit:+ job_queue._emit_started_webhook("transcribe", rid)+ emit.assert_called_once()+ assert emit.call_args[1]["event_type"] == "recording.transcription.started"+ assert emit.call_args[1]["data"]["recording_id"] == rid+++def test_emit_started_webhook_noop_for_unmapped_type(track):+ with patch("src.services.webhook_dispatch.emit_webhook_event") as emit:+ job_queue._emit_started_webhook("summarize", 123)+ emit.assert_not_called()+++def test_emit_completion_webhook_dispatches_event(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid); track.recording_ids.append(rid)++ with patch("src.services.webhook_dispatch.emit_webhook_event") as emit:+ job_queue._emit_completion_webhook("summarize", rid)+ emit.assert_called_once()+ assert emit.call_args[1]["event_type"] == "recording.summary.completed"+++def test_emit_failure_webhook_dispatches_event_and_truncates(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid); track.recording_ids.append(rid)++ long_err = "x" * 1000+ with patch("src.services.webhook_dispatch.emit_webhook_event") as emit:+ job_queue._emit_failure_webhook("transcribe", rid, long_err)+ emit.assert_called_once()+ assert emit.call_args[1]["event_type"] == "recording.transcription.failed"+ assert len(emit.call_args[1]["data"]["error"]) == 500+++def test_emit_webhook_noop_when_recording_missing():+ with patch("src.services.webhook_dispatch.emit_webhook_event") as emit:+ job_queue._emit_completion_webhook("transcribe", 999999999)+ emit.assert_not_called()+++# --------------------------------------------------------------------------+# queue status / counts / position+# --------------------------------------------------------------------------++def test_get_queue_status_counts(track):+ uid = _make_user(); track.user_ids.append(uid)+ rt = _make_recording(uid); track.recording_ids.append(rt)+ rs = _make_recording(uid); track.recording_ids.append(rs)++ base = job_queue.get_queue_status()+ jt = job_queue.enqueue(uid, rt, "transcribe"); track.job_ids.append(jt)+ js = job_queue.enqueue(uid, rs, "summarize"); track.job_ids.append(js)++ status = job_queue.get_queue_status()+ assert status["transcription_queue"]["queued"] >= base["transcription_queue"]["queued"] + 1+ assert status["summary_queue"]["queued"] >= base["summary_queue"]["queued"] + 1+ assert status["transcription_queue"]["workers"] == 0+ assert status["summary_queue"]["workers"] == 0+ assert "is_running" in status+++def test_get_position_in_queue(track):+ uid = _make_user(); track.user_ids.append(uid)+ r1 = _make_recording(uid); track.recording_ids.append(r1)+ r2 = _make_recording(uid); track.recording_ids.append(r2)++ j1 = job_queue.enqueue(uid, r1, "transcribe"); track.job_ids.append(j1)+ db.session.get(ProcessingJob, j1).created_at = datetime.utcnow() - timedelta(minutes=30)+ db.session.commit()+ j2 = job_queue.enqueue(uid, r2, "transcribe"); track.job_ids.append(j2)++ pos1 = job_queue.get_position_in_queue(r1)+ pos2 = job_queue.get_position_in_queue(r2)+ assert pos1 is not None and pos2 is not None+ assert pos2 > pos1 # r2 enqueued later -> further back+++def test_get_position_in_queue_none_for_unqueued():+ assert job_queue.get_position_in_queue(987654321) is None+++def test_get_job_for_recording(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid); track.recording_ids.append(rid)+ jid = job_queue.enqueue(uid, rid, "transcribe"); track.job_ids.append(jid)++ job = job_queue.get_job_for_recording(rid)+ assert job is not None and job.id == jid+ assert job_queue.get_job_for_recording(987654321) is None+++# --------------------------------------------------------------------------+# orphan recovery / cleanup+# --------------------------------------------------------------------------++def test_recover_orphaned_jobs(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid); track.recording_ids.append(rid)+ job = ProcessingJob(user_id=uid, recording_id=rid, job_type="transcribe",+ status="processing", started_at=datetime.utcnow())+ db.session.add(job); db.session.commit()+ track.job_ids.append(job.id)++ job_queue.recover_orphaned_jobs()++ db.session.expire_all()+ refreshed = db.session.get(ProcessingJob, job.id)+ assert refreshed.status == "queued"+ assert refreshed.started_at is None+++def test_cleanup_old_jobs(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid); track.recording_ids.append(rid)+ old = ProcessingJob(user_id=uid, recording_id=rid, job_type="transcribe",+ status="completed",+ completed_at=datetime.utcnow() - timedelta(hours=48))+ db.session.add(old); db.session.commit()+ old_id = old.id+ track.job_ids.append(old_id)++ job_queue.cleanup_old_jobs(max_age_hours=24)++ db.session.expire_all()+ assert db.session.get(ProcessingJob, old_id) is None+ track.job_ids.remove(old_id)+++def test_cleanup_old_jobs_keeps_recent(track):+ uid = _make_user(); track.user_ids.append(uid)+ rid = _make_recording(uid); track.recording_ids.append(rid)+ recent = ProcessingJob(user_id=uid, recording_id=rid, job_type="transcribe",+ status="completed",+ completed_at=datetime.utcnow() - timedelta(hours=1))+ db.session.add(recent); db.session.commit()+ track.job_ids.append(recent.id)++ job_queue.cleanup_old_jobs(max_age_hours=24)++ assert db.session.get(ProcessingJob, recent.id) is not Nonediff --git a/tests/test_cov_llm.py b/tests/test_cov_llm.pynew file mode 100644index 00000000..9f65145d--- /dev/null+++ b/tests/test_cov_llm.py@@ -0,0 +1,674 @@+"""+Coverage tests for src/services/llm.py — the LLM client wrapper.++Covers:+- Module config helpers: is_gpt5_model, is_using_openai_api, get_chat_config+- call_llm_completion / call_chat_completion: success, param passthrough,+ GPT-5 branch, error/timeout handling, budget enforcement, token tracking,+ empty-content logging, streaming path+- format_api_error_message: all branches+- process_streaming_with_thinking: plain content, thinking tags, unclosed+ tags, usage recording++Strategy: the OpenAI client lives at module level (src.services.llm.client).+We patch that object plus the relevant module-level config constants so no+network is touched and behaviour is deterministic.+"""++import json+from types import SimpleNamespace+from unittest.mock import MagicMock, patch++import pytest++import src.services.llm as llm+from src.services.llm import (+ is_gpt5_model,+ is_using_openai_api,+ get_chat_config,+ call_llm_completion,+ call_chat_completion,+ format_api_error_message,+ process_streaming_with_thinking,+ TokenBudgetExceeded,+)+++# ---------------------------------------------------------------------------+# Helpers to build canned completion / chunk objects+# ---------------------------------------------------------------------------++def make_usage(prompt=10, completion=5, total=15, cost=None):+ usage = SimpleNamespace(+ prompt_tokens=prompt,+ completion_tokens=completion,+ total_tokens=total,+ )+ if cost is not None:+ usage.cost = cost+ return usage+++def make_message(content="hello", **extra):+ msg = SimpleNamespace(content=content)+ for k, v in extra.items():+ setattr(msg, k, v)+ return msg+++def make_choice(content="hello", finish_reason="stop", **msg_extra):+ return SimpleNamespace(+ message=make_message(content, **msg_extra),+ finish_reason=finish_reason,+ )+++def make_completion(content="hello", usage=None, finish_reason="stop", **msg_extra):+ return SimpleNamespace(+ choices=[make_choice(content, finish_reason, **msg_extra)],+ usage=usage,+ )+++def make_chunk(content=None, usage=None):+ """A streaming chunk with one choice delta."""+ delta = SimpleNamespace(content=content)+ choice = SimpleNamespace(delta=delta)+ chunk = SimpleNamespace(choices=[choice], usage=usage)+ return chunk+++class FakeClient:+ """Stand-in for the OpenAI client exposing chat.completions.create."""++ def __init__(self, return_value=None, side_effect=None):+ self.create = MagicMock(return_value=return_value, side_effect=side_effect)+ self.chat = SimpleNamespace(+ completions=SimpleNamespace(create=self.create)+ )+++def install_client(monkeypatch, **kwargs):+ fake = FakeClient(**kwargs)+ monkeypatch.setattr(llm, "client", fake)+ return fake+++# ===========================================================================+# is_gpt5_model+# ===========================================================================++def test_is_gpt5_model_true_prefix():+ assert is_gpt5_model("gpt-5-turbo") is True+++def test_is_gpt5_model_true_exact():+ assert is_gpt5_model("gpt-5-mini") is True+++def test_is_gpt5_model_case_insensitive():+ assert is_gpt5_model("GPT-5") is True+++def test_is_gpt5_model_false():+ assert is_gpt5_model("gpt-4o") is False+++def test_is_gpt5_model_none():+ assert is_gpt5_model(None) is False+++def test_is_gpt5_model_empty():+ assert is_gpt5_model("") is False+++# ===========================================================================+# is_using_openai_api+# ===========================================================================++def test_is_using_openai_api_true(monkeypatch):+ monkeypatch.setattr(llm, "TEXT_MODEL_BASE_URL", "https://api.openai.com/v1")+ assert is_using_openai_api() is True+++def test_is_using_openai_api_false(monkeypatch):+ monkeypatch.setattr(llm, "TEXT_MODEL_BASE_URL", "https://openrouter.ai/api/v1")+ assert is_using_openai_api() is False+++# ===========================================================================+# get_chat_config+# ===========================================================================++def test_get_chat_config_falls_back_to_text(monkeypatch):+ monkeypatch.setattr(llm, "CHAT_MODEL_API_KEY", None)+ monkeypatch.setattr(llm, "CHAT_MODEL_NAME", None)+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", "text-key")+ monkeypatch.setattr(llm, "TEXT_MODEL_BASE_URL", "https://text.example/v1")+ monkeypatch.setattr(llm, "TEXT_MODEL_NAME", "text-model")+ cfg = get_chat_config()+ assert cfg["api_key"] == "text-key"+ assert cfg["base_url"] == "https://text.example/v1"+ assert cfg["model_name"] == "text-model"+ assert cfg["gpt5_reasoning_effort"] in ("medium", "low", "high", "minimal")+++def test_get_chat_config_uses_dedicated_chat(monkeypatch):+ monkeypatch.setattr(llm, "CHAT_MODEL_API_KEY", "chat-key")+ monkeypatch.setattr(llm, "CHAT_MODEL_NAME", "chat-model")+ monkeypatch.setattr(llm, "CHAT_MODEL_BASE_URL", "https://chat.example/v1")+ monkeypatch.setattr(llm, "CHAT_GPT5_REASONING_EFFORT", "high")+ monkeypatch.setattr(llm, "CHAT_GPT5_VERBOSITY", "low")+ cfg = get_chat_config()+ assert cfg["api_key"] == "chat-key"+ assert cfg["model_name"] == "chat-model"+ assert cfg["base_url"] == "https://chat.example/v1"+ assert cfg["gpt5_reasoning_effort"] == "high"+ assert cfg["gpt5_verbosity"] == "low"+++def test_get_chat_config_base_url_falls_back_to_text(monkeypatch):+ monkeypatch.setattr(llm, "CHAT_MODEL_API_KEY", "chat-key")+ monkeypatch.setattr(llm, "CHAT_MODEL_NAME", "chat-model")+ monkeypatch.setattr(llm, "CHAT_MODEL_BASE_URL", None)+ monkeypatch.setattr(llm, "TEXT_MODEL_BASE_URL", "https://text.example/v1")+ cfg = get_chat_config()+ assert cfg["base_url"] == "https://text.example/v1"+++# ===========================================================================+# call_llm_completion — guard clauses+# ===========================================================================++def test_call_llm_completion_no_client(monkeypatch):+ monkeypatch.setattr(llm, "client", None)+ with pytest.raises(ValueError, match="not initialized"):+ call_llm_completion([{"role": "user", "content": "hi"}])+++def test_call_llm_completion_no_api_key(monkeypatch):+ install_client(monkeypatch)+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", None)+ with pytest.raises(ValueError, match="TEXT_MODEL_API_KEY"):+ call_llm_completion([{"role": "user", "content": "hi"}])+++# ===========================================================================+# call_llm_completion — success / passthrough+# ===========================================================================++def test_call_llm_completion_success_returns_content(monkeypatch):+ fake = install_client(monkeypatch, return_value=make_completion("the answer"))+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", "test-key")+ monkeypatch.setattr(llm, "TEXT_MODEL_NAME", "gpt-4o")+ resp = call_llm_completion(+ [{"role": "user", "content": "q"}],+ temperature=0.3,+ max_tokens=128,+ )+ assert resp.choices[0].message.content == "the answer"+ kwargs = fake.create.call_args.kwargs+ assert kwargs["model"] == "gpt-4o"+ assert kwargs["temperature"] == 0.3+ assert kwargs["max_tokens"] == 128+ assert kwargs["stream"] is False+++def test_call_llm_completion_response_format_passthrough(monkeypatch):+ fake = install_client(monkeypatch, return_value=make_completion("{}"))+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", "test-key")+ monkeypatch.setattr(llm, "TEXT_MODEL_NAME", "gpt-4o")+ call_llm_completion(+ [{"role": "user", "content": "q"}],+ response_format={"type": "json_object"},+ )+ assert fake.create.call_args.kwargs["response_format"] == {"type": "json_object"}+++def test_call_llm_completion_gpt5_branch(monkeypatch):+ fake = install_client(monkeypatch, return_value=make_completion("ok"))+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", "test-key")+ monkeypatch.setattr(llm, "TEXT_MODEL_NAME", "gpt-5")+ monkeypatch.setattr(llm, "TEXT_MODEL_BASE_URL", "https://api.openai.com/v1")+ monkeypatch.setenv("GPT5_REASONING_EFFORT", "high")+ monkeypatch.setenv("GPT5_VERBOSITY", "low")+ call_llm_completion([{"role": "user", "content": "q"}], max_tokens=200)+ kwargs = fake.create.call_args.kwargs+ assert kwargs["reasoning_effort"] == "high"+ assert kwargs["verbosity"] == "low"+ assert kwargs["max_completion_tokens"] == 200+ assert "temperature" not in kwargs+ assert "max_tokens" not in kwargs+++def test_call_llm_completion_streaming_adds_stream_options(monkeypatch):+ fake = install_client(monkeypatch, return_value=iter([]))+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", "test-key")+ monkeypatch.setattr(llm, "TEXT_MODEL_NAME", "gpt-4o")+ monkeypatch.setattr(llm, "ENABLE_STREAM_OPTIONS", True)+ call_llm_completion([{"role": "user", "content": "q"}], stream=True)+ kwargs = fake.create.call_args.kwargs+ assert kwargs["stream"] is True+ assert kwargs["stream_options"] == {"include_usage": True}+++def test_call_llm_completion_streaming_no_stream_options(monkeypatch):+ fake = install_client(monkeypatch, return_value=iter([]))+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", "test-key")+ monkeypatch.setattr(llm, "TEXT_MODEL_NAME", "gpt-4o")+ monkeypatch.setattr(llm, "ENABLE_STREAM_OPTIONS", False)+ call_llm_completion([{"role": "user", "content": "q"}], stream=True)+ assert "stream_options" not in fake.create.call_args.kwargs+++def test_call_llm_completion_empty_content_logs(monkeypatch):+ completion = make_completion(content="", refusal="nope", tool_calls=None)+ install_client(monkeypatch, return_value=completion)+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", "test-key")+ monkeypatch.setattr(llm, "TEXT_MODEL_NAME", "gpt-4o")+ # Should not raise; just logs warnings about empty content.+ resp = call_llm_completion([{"role": "user", "content": "q"}])+ assert resp.choices[0].message.content == ""+++# ===========================================================================+# call_llm_completion — error / timeout handling+# ===========================================================================++def test_call_llm_completion_generic_error_reraised(monkeypatch):+ install_client(monkeypatch, side_effect=RuntimeError("boom"))+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", "test-key")+ monkeypatch.setattr(llm, "TEXT_MODEL_NAME", "gpt-4o")+ with pytest.raises(RuntimeError, match="boom"):+ call_llm_completion([{"role": "user", "content": "q"}])+++def test_call_llm_completion_timeout_reraised(monkeypatch):+ from openai import APITimeoutError+ err = APITimeoutError(request=MagicMock())+ install_client(monkeypatch, side_effect=err)+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", "test-key")+ monkeypatch.setattr(llm, "TEXT_MODEL_NAME", "gpt-4o")+ with pytest.raises(APITimeoutError):+ call_llm_completion([{"role": "user", "content": "q"}])+++# ===========================================================================+# call_llm_completion — budget + token tracking+# ===========================================================================++def test_call_llm_completion_budget_exceeded(monkeypatch):+ install_client(monkeypatch, return_value=make_completion("ok"))+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", "test-key")+ monkeypatch.setattr(llm, "TEXT_MODEL_NAME", "gpt-4o")+ fake_tracker = MagicMock()+ fake_tracker.check_budget.return_value = (False, 120.0, "over budget")+ with patch("src.services.token_tracking.token_tracker", fake_tracker):+ with pytest.raises(TokenBudgetExceeded):+ call_llm_completion(+ [{"role": "user", "content": "q"}],+ user_id=99001,+ operation_type="summarization",+ )+++def test_call_llm_completion_records_usage(monkeypatch):+ usage = make_usage(prompt=11, completion=7, total=18, cost=0.001)+ install_client(monkeypatch, return_value=make_completion("ok", usage=usage))+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", "test-key")+ monkeypatch.setattr(llm, "TEXT_MODEL_NAME", "gpt-4o")+ fake_tracker = MagicMock()+ fake_tracker.check_budget.return_value = (True, 50.0, None)+ with patch("src.services.token_tracking.token_tracker", fake_tracker):+ call_llm_completion(+ [{"role": "user", "content": "q"}],+ user_id=99002,+ operation_type="summarization",+ )+ fake_tracker.record_usage.assert_called_once()+ rec_kwargs = fake_tracker.record_usage.call_args.kwargs+ assert rec_kwargs["user_id"] == 99002+ assert rec_kwargs["prompt_tokens"] == 11+ assert rec_kwargs["completion_tokens"] == 7+ assert rec_kwargs["total_tokens"] == 18+ assert rec_kwargs["cost"] == 0.001+++def test_call_llm_completion_budget_warning_near_limit(monkeypatch):+ install_client(monkeypatch, return_value=make_completion("ok", usage=make_usage()))+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", "test-key")+ monkeypatch.setattr(llm, "TEXT_MODEL_NAME", "gpt-4o")+ fake_tracker = MagicMock()+ fake_tracker.check_budget.return_value = (True, 85.0, None)+ with patch("src.services.token_tracking.token_tracker", fake_tracker):+ resp = call_llm_completion(+ [{"role": "user", "content": "q"}],+ user_id=99003,+ operation_type="chat",+ )+ assert resp.choices[0].message.content == "ok"+++def test_call_llm_completion_budget_check_error_non_blocking(monkeypatch):+ install_client(monkeypatch, return_value=make_completion("ok"))+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", "test-key")+ monkeypatch.setattr(llm, "TEXT_MODEL_NAME", "gpt-4o")+ fake_tracker = MagicMock()+ fake_tracker.check_budget.side_effect = RuntimeError("db down")+ with patch("src.services.token_tracking.token_tracker", fake_tracker):+ # Budget check failure must not block the call.+ resp = call_llm_completion(+ [{"role": "user", "content": "q"}],+ user_id=99004,+ operation_type="chat",+ )+ assert resp.choices[0].message.content == "ok"+++def test_call_llm_completion_record_usage_error_non_blocking(monkeypatch):+ usage = make_usage()+ install_client(monkeypatch, return_value=make_completion("ok", usage=usage))+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", "test-key")+ monkeypatch.setattr(llm, "TEXT_MODEL_NAME", "gpt-4o")+ fake_tracker = MagicMock()+ fake_tracker.check_budget.return_value = (True, 10.0, None)+ fake_tracker.record_usage.side_effect = RuntimeError("write failed")+ with patch("src.services.token_tracking.token_tracker", fake_tracker):+ resp = call_llm_completion(+ [{"role": "user", "content": "q"}],+ user_id=99005,+ operation_type="chat",+ )+ assert resp.choices[0].message.content == "ok"+++# ===========================================================================+# call_chat_completion+# ===========================================================================++def test_call_chat_completion_no_client(monkeypatch):+ monkeypatch.setattr(llm, "chat_client", None)+ monkeypatch.setattr(llm, "client", None)+ with pytest.raises(ValueError, match="not initialized"):+ call_chat_completion([{"role": "user", "content": "hi"}])+++def test_call_chat_completion_no_api_key(monkeypatch):+ fake = FakeClient(return_value=make_completion("ok"))+ monkeypatch.setattr(llm, "chat_client", fake)+ monkeypatch.setattr(llm, "client", fake)+ monkeypatch.setattr(llm, "CHAT_MODEL_API_KEY", None)+ monkeypatch.setattr(llm, "CHAT_MODEL_NAME", None)+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", None)+ with pytest.raises(ValueError, match="API key not configured"):+ call_chat_completion([{"role": "user", "content": "hi"}])+++def test_call_chat_completion_success(monkeypatch):+ fake = FakeClient(return_value=make_completion("chat answer"))+ monkeypatch.setattr(llm, "chat_client", fake)+ monkeypatch.setattr(llm, "CHAT_MODEL_API_KEY", None)+ monkeypatch.setattr(llm, "CHAT_MODEL_NAME", None)+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", "test-key")+ monkeypatch.setattr(llm, "TEXT_MODEL_BASE_URL", "https://openrouter.ai/api/v1")+ monkeypatch.setattr(llm, "TEXT_MODEL_NAME", "gpt-4o")+ resp = call_chat_completion(+ [{"role": "user", "content": "q"}],+ temperature=0.2,+ max_tokens=64,+ )+ assert resp.choices[0].message.content == "chat answer"+ kwargs = fake.create.call_args.kwargs+ assert kwargs["model"] == "gpt-4o"+ assert kwargs["temperature"] == 0.2+ assert kwargs["max_tokens"] == 64+++def test_call_chat_completion_gpt5_branch(monkeypatch):+ fake = FakeClient(return_value=make_completion("ok"))+ monkeypatch.setattr(llm, "chat_client", fake)+ monkeypatch.setattr(llm, "CHAT_MODEL_API_KEY", "chat-key")+ monkeypatch.setattr(llm, "CHAT_MODEL_NAME", "gpt-5-mini")+ monkeypatch.setattr(llm, "CHAT_MODEL_BASE_URL", "https://api.openai.com/v1")+ monkeypatch.setattr(llm, "CHAT_GPT5_REASONING_EFFORT", "low")+ monkeypatch.setattr(llm, "CHAT_GPT5_VERBOSITY", "high")+ call_chat_completion([{"role": "user", "content": "q"}], max_tokens=50)+ kwargs = fake.create.call_args.kwargs+ assert kwargs["model"] == "gpt-5-mini"+ assert kwargs["reasoning_effort"] == "low"+ assert kwargs["verbosity"] == "high"+ assert kwargs["max_completion_tokens"] == 50+ assert "temperature" not in kwargs+++def test_call_chat_completion_falls_back_to_main_client(monkeypatch):+ fake = FakeClient(return_value=make_completion("ok"))+ # chat_client None -> uses client+ monkeypatch.setattr(llm, "chat_client", None)+ monkeypatch.setattr(llm, "client", fake)+ monkeypatch.setattr(llm, "CHAT_MODEL_API_KEY", None)+ monkeypatch.setattr(llm, "CHAT_MODEL_NAME", None)+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", "test-key")+ monkeypatch.setattr(llm, "TEXT_MODEL_NAME", "gpt-4o")+ monkeypatch.setattr(llm, "TEXT_MODEL_BASE_URL", "https://openrouter.ai/api/v1")+ resp = call_chat_completion([{"role": "user", "content": "q"}])+ assert resp.choices[0].message.content == "ok"+++def test_call_chat_completion_records_usage(monkeypatch):+ usage = make_usage(prompt=3, completion=4, total=7)+ fake = FakeClient(return_value=make_completion("ok", usage=usage))+ monkeypatch.setattr(llm, "chat_client", fake)+ monkeypatch.setattr(llm, "CHAT_MODEL_API_KEY", None)+ monkeypatch.setattr(llm, "CHAT_MODEL_NAME", None)+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", "test-key")+ monkeypatch.setattr(llm, "TEXT_MODEL_NAME", "gpt-4o")+ monkeypatch.setattr(llm, "TEXT_MODEL_BASE_URL", "https://openrouter.ai/api/v1")+ fake_tracker = MagicMock()+ fake_tracker.check_budget.return_value = (True, 10.0, None)+ with patch("src.services.token_tracking.token_tracker", fake_tracker):+ call_chat_completion(+ [{"role": "user", "content": "q"}],+ user_id=99010,+ operation_type="chat",+ )+ fake_tracker.record_usage.assert_called_once()+++def test_call_chat_completion_budget_exceeded(monkeypatch):+ fake = FakeClient(return_value=make_completion("ok"))+ monkeypatch.setattr(llm, "chat_client", fake)+ monkeypatch.setattr(llm, "CHAT_MODEL_API_KEY", None)+ monkeypatch.setattr(llm, "CHAT_MODEL_NAME", None)+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", "test-key")+ monkeypatch.setattr(llm, "TEXT_MODEL_NAME", "gpt-4o")+ monkeypatch.setattr(llm, "TEXT_MODEL_BASE_URL", "https://openrouter.ai/api/v1")+ fake_tracker = MagicMock()+ fake_tracker.check_budget.return_value = (False, 120.0, "over")+ with patch("src.services.token_tracking.token_tracker", fake_tracker):+ with pytest.raises(TokenBudgetExceeded):+ call_chat_completion(+ [{"role": "user", "content": "q"}],+ user_id=99011,+ operation_type="chat",+ )+++def test_call_chat_completion_timeout_reraised(monkeypatch):+ from openai import APITimeoutError+ err = APITimeoutError(request=MagicMock())+ fake = FakeClient(side_effect=err)+ monkeypatch.setattr(llm, "chat_client", fake)+ monkeypatch.setattr(llm, "CHAT_MODEL_API_KEY", None)+ monkeypatch.setattr(llm, "CHAT_MODEL_NAME", None)+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", "test-key")+ monkeypatch.setattr(llm, "TEXT_MODEL_NAME", "gpt-4o")+ monkeypatch.setattr(llm, "TEXT_MODEL_BASE_URL", "https://openrouter.ai/api/v1")+ with pytest.raises(APITimeoutError):+ call_chat_completion([{"role": "user", "content": "q"}])+++def test_call_chat_completion_generic_error_reraised(monkeypatch):+ fake = FakeClient(side_effect=RuntimeError("kaboom"))+ monkeypatch.setattr(llm, "chat_client", fake)+ monkeypatch.setattr(llm, "CHAT_MODEL_API_KEY", None)+ monkeypatch.setattr(llm, "CHAT_MODEL_NAME", None)+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", "test-key")+ monkeypatch.setattr(llm, "TEXT_MODEL_NAME", "gpt-4o")+ monkeypatch.setattr(llm, "TEXT_MODEL_BASE_URL", "https://openrouter.ai/api/v1")+ with pytest.raises(RuntimeError, match="kaboom"):+ call_chat_completion([{"role": "user", "content": "q"}])+++def test_call_chat_completion_response_format_and_stream(monkeypatch):+ fake = FakeClient(return_value=iter([]))+ monkeypatch.setattr(llm, "chat_client", fake)+ monkeypatch.setattr(llm, "CHAT_MODEL_API_KEY", None)+ monkeypatch.setattr(llm, "CHAT_MODEL_NAME", None)+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", "test-key")+ monkeypatch.setattr(llm, "TEXT_MODEL_NAME", "gpt-4o")+ monkeypatch.setattr(llm, "TEXT_MODEL_BASE_URL", "https://openrouter.ai/api/v1")+ monkeypatch.setattr(llm, "ENABLE_STREAM_OPTIONS", True)+ call_chat_completion(+ [{"role": "user", "content": "q"}],+ response_format={"type": "json_object"},+ stream=True,+ )+ kwargs = fake.create.call_args.kwargs+ assert kwargs["response_format"] == {"type": "json_object"}+ assert kwargs["stream_options"] == {"include_usage": True}+++def test_call_chat_completion_empty_content_logs(monkeypatch):+ fake = FakeClient(return_value=make_completion(content=""))+ monkeypatch.setattr(llm, "chat_client", fake)+ monkeypatch.setattr(llm, "CHAT_MODEL_API_KEY", None)+ monkeypatch.setattr(llm, "CHAT_MODEL_NAME", None)+ monkeypatch.setattr(llm, "TEXT_MODEL_API_KEY", "test-key")+ monkeypatch.setattr(llm, "TEXT_MODEL_NAME", "gpt-4o")+ monkeypatch.setattr(llm, "TEXT_MODEL_BASE_URL", "https://openrouter.ai/api/v1")+ resp = call_chat_completion([{"role": "user", "content": "q"}])+ assert resp.choices[0].message.content == ""+++# ===========================================================================+# format_api_error_message+# ===========================================================================++def test_format_error_context_length():+ msg = format_api_error_message("This model's maximum context length is 4096 tokens")+ assert "too long" in msg+++def test_format_error_rate_limit():+ assert "rate limit" in format_api_error_message("429 Rate limit reached").lower()+++def test_format_error_insufficient_funds():+ assert "quota exceeded" in format_api_error_message("insufficient funds on account").lower()+++def test_format_error_quota_exceeded():+ assert "quota exceeded" in format_api_error_message("Your quota exceeded").lower()+++def test_format_error_timeout():+ assert "timed out" in format_api_error_message("Request timeout occurred").lower()+++def test_format_error_generic():+ out = format_api_error_message("some weird error")+ assert "some weird error" in out+ assert out.startswith("[Summary generation failed")+++# ===========================================================================+# process_streaming_with_thinking+# ===========================================================================++def _collect(gen):+ return [json.loads(line[len("data: "):].strip()) for line in gen]+++def test_streaming_plain_content():+ stream = [make_chunk("Hello "), make_chunk("world")]+ events = _collect(process_streaming_with_thinking(stream))+ deltas = [e["delta"] for e in events if "delta" in e]+ assert "".join(deltas) == "Hello world"+ assert events[-1] == {"end_of_stream": True}+++def test_streaming_with_thinking_tags():+ stream = [+ make_chunk("before "),+ make_chunk("<think>secret thought</think>"),+ make_chunk("after"),+ ]+ events = _collect(process_streaming_with_thinking(stream))+ thinking = [e["thinking"] for e in events if "thinking" in e]+ deltas = "".join(e["delta"] for e in events if "delta" in e)+ assert thinking == ["secret thought"]+ assert "before " in deltas+ assert "after" in deltas+++def test_streaming_unclosed_thinking_tag():+ stream = [make_chunk("<thinking>still thinking")]+ events = _collect(process_streaming_with_thinking(stream))+ thinking = [e["thinking"] for e in events if "thinking" in e]+ assert thinking == ["still thinking"]+ assert events[-1] == {"end_of_stream": True}+++def test_streaming_records_usage(monkeypatch):+ usage = make_usage(prompt=2, completion=3, total=5)+ stream = [make_chunk("hi"), make_chunk(None, usage=usage)]+ fake_tracker = MagicMock()+ monkeypatch.setattr(llm, "TEXT_MODEL_NAME", "gpt-4o")+ with patch("src.services.token_tracking.token_tracker", fake_tracker):+ events = _collect(+ process_streaming_with_thinking(+ stream, user_id=99020, operation_type="chat", model_name="gpt-4o"+ )+ )+ fake_tracker.record_usage.assert_called_once()+ assert events[-1] == {"end_of_stream": True}+++def test_streaming_records_usage_with_app_context(monkeypatch):+ usage = make_usage()+ stream = [make_chunk("hi"), make_chunk(None, usage=usage)]+ fake_tracker = MagicMock()++ class FakeAppCtx:+ def __enter__(self):+ return self++ def __exit__(self, *a):+ return False++ fake_app = MagicMock()+ fake_app.app_context.return_value = FakeAppCtx()+ with patch("src.services.token_tracking.token_tracker", fake_tracker):+ list(+ process_streaming_with_thinking(+ stream, user_id=99021, operation_type="chat", app=fake_app+ )+ )+ fake_app.app_context.assert_called_once()+ fake_tracker.record_usage.assert_called_once()+++def test_streaming_usage_record_error_non_blocking(monkeypatch):+ usage = make_usage()+ stream = [make_chunk("hi"), make_chunk(None, usage=usage)]+ fake_tracker = MagicMock()+ fake_tracker.record_usage.side_effect = RuntimeError("fail")+ with patch("src.services.token_tracking.token_tracker", fake_tracker):+ events = _collect(+ process_streaming_with_thinking(+ stream, user_id=99022, operation_type="chat"+ )+ )+ # Generator must still complete despite recording failure.+ assert events[-1] == {"end_of_stream": True}diff --git a/tests/test_cov_processing.py b/tests/test_cov_processing.pynew file mode 100644index 00000000..34d96d81--- /dev/null+++ b/tests/test_cov_processing.py@@ -0,0 +1,629 @@+"""Coverage-focused tests for src/tasks/processing.py.++These exercise the async pipeline task functions directly (not via HTTP):+title generation, summary generation, event extraction, the Inquire+embeddings hook, status transitions, duration bookkeeping, and the+transcribe-with-connector orchestration (including the video retention /+passthrough branches) — all with the network, LLM client, ASR connector,+storage, ffprobe/ffmpeg, and embeddings mocked at the processing.py import+site so the suite stays fully offline and hermetic.++SHARED-DB NOTE: the session DB is shared across the whole test suite, so+every assertion here is scoped to recording/user IDs this file created.+"""+import os+import sys+import uuid+from contextlib import contextmanager+from unittest.mock import patch, MagicMock++sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))++import pytest++from src.app import app, db+from src.models import User, Recording, Event, SystemSetting+import src.tasks.processing as proc+++# ---------------------------------------------------------------------------+# Helpers+# ---------------------------------------------------------------------------++def _make_user(prefix, **kwargs):+ suffix = uuid.uuid4().hex[:8]+ user = User(+ username=f"{prefix}_{suffix}",+ email=f"{prefix}_{suffix}@local.test",+ password="placeholder-bcrypt-hash",+ **kwargs,+ )+ db.session.add(user)+ db.session.commit()+ return user+++def _make_recording(user_id, transcription="word " * 50, status="PENDING", **kwargs):+ kwargs.setdefault("title", "cov-processing")+ recording = Recording(+ user_id=user_id,+ original_filename="cov.mp3",+ audio_path="local://cov.mp3",+ status=status,+ transcription=transcription,+ **kwargs,+ )+ db.session.add(recording)+ db.session.commit()+ return recording+++def _fake_completion(content, reasoning=None):+ """Build a stand-in for an OpenAI ChatCompletion response."""+ msg = MagicMock()+ msg.content = content+ msg.reasoning = reasoning+ choice = MagicMock()+ choice.message = msg+ completion = MagicMock()+ completion.choices = [choice]+ return completion+++@contextmanager+def _patch_llm(content="Generated text", reasoning=None, side_effect=None):+ """Patch call_llm_completion and a truthy client at the processing import site."""+ with patch.object(proc, "client", MagicMock()), \+ patch.object(proc, "call_llm_completion") as mock_call:+ if side_effect is not None:+ mock_call.side_effect = side_effect+ else:+ mock_call.return_value = _fake_completion(content, reasoning)+ yield mock_call+++# ---------------------------------------------------------------------------+# generate_summary_only_task+# ---------------------------------------------------------------------------++def test_summary_success_sets_summary_and_completed():+ with app.app_context():+ user = _make_user("sum_ok")+ rec = _make_recording(user.id)+ rid = rec.id+ with _patch_llm(content="## Minutes\nThis is the summary."), \+ patch.object(proc, "ENABLE_INQUIRE_MODE", False):+ proc.generate_summary_only_task(app.app_context(), rid)++ db.session.expire_all()+ out = db.session.get(Recording, rid)+ assert out.status == "COMPLETED"+ assert "summary" in out.summary.lower()+ assert out.completed_at is not None+ assert out.summarization_duration_seconds is not None+++def test_summary_client_not_configured_skips():+ with app.app_context():+ user = _make_user("sum_noclient")+ rec = _make_recording(user.id, status="PENDING")+ rid = rec.id+ with patch.object(proc, "client", None):+ proc.generate_summary_only_task(app.app_context(), rid)++ db.session.expire_all()+ out = db.session.get(Recording, rid)+ assert "OpenRouter client not configured" in out.summary+++def test_summary_short_transcription_skipped():+ with app.app_context():+ user = _make_user("sum_short")+ rec = _make_recording(user.id, transcription="hi")+ rid = rec.id+ with _patch_llm(content="should not be used"):+ proc.generate_summary_only_task(app.app_context(), rid)++ db.session.expire_all()+ out = db.session.get(Recording, rid)+ assert out.status == "COMPLETED"+ assert "short transcription" in out.summary+++def test_summary_error_path_sets_failed():+ with app.app_context():+ user = _make_user("sum_err")+ rec = _make_recording(user.id)+ rid = rec.id+ with _patch_llm(side_effect=RuntimeError("LLM boom")):+ proc.generate_summary_only_task(app.app_context(), rid)++ db.session.expire_all()+ out = db.session.get(Recording, rid)+ assert out.status == "FAILED"+ assert out.summary # error message stored as summary+++def test_summary_empty_response_marks_not_generated():+ with app.app_context():+ user = _make_user("sum_empty")+ rec = _make_recording(user.id)+ rid = rec.id+ with _patch_llm(content=""), patch.object(proc, "ENABLE_INQUIRE_MODE", False):+ proc.generate_summary_only_task(app.app_context(), rid)++ db.session.expire_all()+ out = db.session.get(Recording, rid)+ assert out.status == "COMPLETED"+ assert out.summary == "[Summary not generated]"+++def test_summary_inquire_mode_calls_process_chunks():+ """Bug #305: the Inquire embeddings hook must run after summary completion+ when ENABLE_INQUIRE_MODE is on."""+ with app.app_context():+ user = _make_user("sum_inquire")+ rec = _make_recording(user.id)+ rid = rec.id+ with _patch_llm(content="A real summary body."), \+ patch.object(proc, "ENABLE_INQUIRE_MODE", True), \+ patch.object(proc, "process_recording_chunks") as mock_chunks:+ proc.generate_summary_only_task(app.app_context(), rid)++ mock_chunks.assert_called_once_with(rid)+++def test_summary_inquire_chunk_error_is_swallowed():+ with app.app_context():+ user = _make_user("sum_inq_err")+ rec = _make_recording(user.id)+ rid = rec.id+ with _patch_llm(content="A real summary body."), \+ patch.object(proc, "ENABLE_INQUIRE_MODE", True), \+ patch.object(proc, "process_recording_chunks",+ side_effect=RuntimeError("embed fail")):+ # Must not raise despite the chunk error.+ proc.generate_summary_only_task(app.app_context(), rid)++ db.session.expire_all()+ out = db.session.get(Recording, rid)+ assert out.status == "COMPLETED"+++def test_summary_custom_prompt_override_replaces():+ with app.app_context():+ user = _make_user("sum_override")+ rec = _make_recording(user.id)+ rid = rec.id+ with _patch_llm(content="Overridden summary") as mock_call, \+ patch.object(proc, "ENABLE_INQUIRE_MODE", False):+ proc.generate_summary_only_task(+ app.app_context(), rid,+ custom_prompt_override="ONLY follow these instructions",+ )+ # The custom override text must appear in the user prompt sent to the LLM.+ sent = mock_call.call_args.kwargs["messages"][1]["content"]+ assert "ONLY follow these instructions" in sent+++def test_summary_custom_prompt_append():+ with app.app_context():+ user = _make_user("sum_append")+ rec = _make_recording(user.id)+ rid = rec.id+ with _patch_llm(content="Appended summary") as mock_call, \+ patch.object(proc, "ENABLE_INQUIRE_MODE", False):+ proc.generate_summary_only_task(+ app.app_context(), rid,+ custom_prompt_override="EXTRA agenda context",+ custom_prompt_append=True,+ )+ sent = mock_call.call_args.kwargs["messages"][1]["content"]+ assert "Additional context for this recording" in sent+ assert "EXTRA agenda context" in sent+++def test_summary_event_extraction_invoked_when_user_extract_events():+ with app.app_context():+ user = _make_user("sum_events", extract_events=True)+ rec = _make_recording(user.id)+ rid = rec.id+ with _patch_llm(content="A summary body."), \+ patch.object(proc, "ENABLE_INQUIRE_MODE", False), \+ patch.object(proc, "extract_events_from_transcript") as mock_extract:+ proc.generate_summary_only_task(app.app_context(), rid)+ mock_extract.assert_called_once()+ assert mock_extract.call_args.args[0] == rid+++def test_summary_missing_recording_returns_quietly():+ with app.app_context():+ # An ID that does not exist must not raise.+ proc.generate_summary_only_task(app.app_context(), 99999999)+++def test_summary_user_id_viewer_filtering():+ """Passing an explicit user_id drives the tag-visibility viewer path."""+ with app.app_context():+ owner = _make_user("sum_owner")+ viewer = _make_user("sum_viewer")+ rec = _make_recording(owner.id)+ rid = rec.id+ with _patch_llm(content="Viewer summary"), \+ patch.object(proc, "ENABLE_INQUIRE_MODE", False):+ proc.generate_summary_only_task(app.app_context(), rid, user_id=viewer.id)+ db.session.expire_all()+ assert db.session.get(Recording, rid).status == "COMPLETED"+++# ---------------------------------------------------------------------------+# generate_title_task+# ---------------------------------------------------------------------------++def test_title_sets_title_and_completes():+ with app.app_context():+ user = _make_user("title_ok")+ # Placeholder title so AI title generation kicks in.+ rec = _make_recording(user.id, title="Recording - cov.mp3")+ rid = rec.id+ with _patch_llm(content="Quarterly Budget Review"), \+ patch.object(proc, "ENABLE_INQUIRE_MODE", False):+ proc.generate_title_task(app.app_context(), rid, will_auto_summarize=False)++ db.session.expire_all()+ out = db.session.get(Recording, rid)+ assert out.title == "Quarterly Budget Review"+ assert out.status == "COMPLETED"+ assert out.completed_at is not None+++def test_title_will_auto_summarize_leaves_status_unchanged():+ with app.app_context():+ user = _make_user("title_autosum")+ rec = _make_recording(user.id, title="Recording - cov.mp3", status="PROCESSING")+ rid = rec.id+ with _patch_llm(content="A New Title"), \+ patch.object(proc, "ENABLE_INQUIRE_MODE", False):+ proc.generate_title_task(app.app_context(), rid, will_auto_summarize=True)++ db.session.expire_all()+ out = db.session.get(Recording, rid)+ assert out.title == "A New Title"+ # Status must NOT have flipped to COMPLETED (summary task will do that).+ assert out.status == "PROCESSING"+++def test_title_inquire_mode_calls_process_chunks():+ with app.app_context():+ user = _make_user("title_inquire")+ rec = _make_recording(user.id, title="Recording - cov.mp3")+ rid = rec.id+ with _patch_llm(content="Inquire Title"), \+ patch.object(proc, "ENABLE_INQUIRE_MODE", True), \+ patch.object(proc, "process_recording_chunks") as mock_chunks:+ proc.generate_title_task(app.app_context(), rid, will_auto_summarize=False)+ mock_chunks.assert_called_once_with(rid)+++def test_title_user_provided_title_is_kept():+ with app.app_context():+ user = _make_user("title_userset")+ rec = _make_recording(user.id, title="My Important Meeting")+ rid = rec.id+ with _patch_llm(content="AI Suggested") as mock_call, \+ patch.object(proc, "ENABLE_INQUIRE_MODE", False):+ proc.generate_title_task(app.app_context(), rid, will_auto_summarize=False)+ # User title preserved; no LLM call should have been made.+ db.session.expire_all()+ out = db.session.get(Recording, rid)+ assert out.title == "My Important Meeting"+ assert out.status == "COMPLETED"+ mock_call.assert_not_called()+++def test_title_short_transcription_falls_back_to_filename():+ with app.app_context():+ user = _make_user("title_short")+ rec = _make_recording(user.id, title="Recording - cov.mp3", transcription="hi")+ rid = rec.id+ with patch.object(proc, "client", MagicMock()), \+ patch.object(proc, "ENABLE_INQUIRE_MODE", False):+ proc.generate_title_task(app.app_context(), rid, will_auto_summarize=False)+ db.session.expire_all()+ out = db.session.get(Recording, rid)+ # Falls back to the filename without extension.+ assert out.title == "cov"+ assert out.status == "COMPLETED"+++def test_title_client_not_configured_falls_back_to_filename():+ with app.app_context():+ user = _make_user("title_noclient")+ rec = _make_recording(user.id, title="Recording - cov.mp3")+ rid = rec.id+ with patch.object(proc, "client", None), \+ patch.object(proc, "ENABLE_INQUIRE_MODE", False):+ proc.generate_title_task(app.app_context(), rid, will_auto_summarize=False)+ db.session.expire_all()+ out = db.session.get(Recording, rid)+ assert out.title == "cov"+++def test_title_missing_recording_returns_quietly():+ with app.app_context():+ proc.generate_title_task(app.app_context(), 99999999)+++def test_title_budget_exceeded_skips_gracefully():+ from src.services.llm import TokenBudgetExceeded+ with app.app_context():+ user = _make_user("title_budget")+ rec = _make_recording(user.id, title="Recording - cov.mp3")+ rid = rec.id+ with _patch_llm(side_effect=TokenBudgetExceeded("over budget")), \+ patch.object(proc, "ENABLE_INQUIRE_MODE", False):+ proc.generate_title_task(app.app_context(), rid, will_auto_summarize=False)+ db.session.expire_all()+ out = db.session.get(Recording, rid)+ # Budget skip → falls back to filename, still completes.+ assert out.status == "COMPLETED"+ assert out.title == "cov"+++# ---------------------------------------------------------------------------+# extract_events_from_transcript+# ---------------------------------------------------------------------------++def test_extract_events_creates_events():+ with app.app_context():+ user = _make_user("ev_create", extract_events=True)+ rec = _make_recording(user.id)+ rid = rec.id+ events_json = (+ '{"events": [{"title": "Review Meeting", '+ '"description": "Quarterly review", '+ '"start_datetime": "2025-07-22T14:00:00", '+ '"end_datetime": "2025-07-22T15:30:00", '+ '"location": "Room A", "attendees": ["Jane"], '+ '"reminder_minutes": 30}]}'+ )+ with _patch_llm(content=events_json):+ proc.extract_events_from_transcript(rid, "transcript text", "summary text")++ events = Event.query.filter_by(recording_id=rid).all()+ assert len(events) == 1+ assert events[0].title == "Review Meeting"+ assert events[0].reminder_minutes == 30+++def test_extract_events_empty_list():+ with app.app_context():+ user = _make_user("ev_empty", extract_events=True)+ rec = _make_recording(user.id)+ rid = rec.id+ with _patch_llm(content='{"events": []}'):+ proc.extract_events_from_transcript(rid, "transcript", "summary")+ assert Event.query.filter_by(recording_id=rid).count() == 0+++def test_extract_events_disabled_user_no_op():+ with app.app_context():+ user = _make_user("ev_disabled", extract_events=False)+ rec = _make_recording(user.id)+ rid = rec.id+ with _patch_llm(content='{"events": [{"title": "X", "start_datetime": "2025-01-01T09:00:00"}]}') as mock_call:+ proc.extract_events_from_transcript(rid, "transcript", "summary")+ # Returns before calling the LLM.+ mock_call.assert_not_called()+ assert Event.query.filter_by(recording_id=rid).count() == 0+++def test_extract_events_bad_date_skipped():+ with app.app_context():+ user = _make_user("ev_baddate", extract_events=True)+ rec = _make_recording(user.id)+ rid = rec.id+ bad = '{"events": [{"title": "Bad", "start_datetime": "not-a-date"}]}'+ with _patch_llm(content=bad):+ proc.extract_events_from_transcript(rid, "transcript", "summary")+ # Unparseable start date → event skipped.+ assert Event.query.filter_by(recording_id=rid).count() == 0+++# ---------------------------------------------------------------------------+# format helpers+# ---------------------------------------------------------------------------++def test_format_transcription_for_llm_json_segments():+ out = proc.format_transcription_for_llm(+ '[{"speaker": "SPEAKER_00", "sentence": "Hello"}, '+ '{"speaker": "SPEAKER_01", "sentence": "Hi"}]'+ )+ assert "[SPEAKER_00]: Hello" in out+ assert "[SPEAKER_01]: Hi" in out+++def test_format_transcription_for_llm_plain_passthrough():+ assert proc.format_transcription_for_llm("just plain text") == "just plain text"+++def test_clean_llm_response_strips_think_tags():+ out = proc.clean_llm_response("<think>reasoning here</think>Final answer")+ assert out == "Final answer"+++def test_clean_llm_response_empty():+ assert proc.clean_llm_response("") == ""+++def test_resolve_hotwords():+ assert proc.resolve_hotwords("explicit", "default") == "explicit"+ assert proc.resolve_hotwords("", "admin_default") == "admin_default"+ assert proc.resolve_hotwords(None, None) is None+++def test_sanitize_error_message_redacts_and_caps():+ msg = proc._sanitize_error_message("failed at /tmp/secret/file.mp3 with spaces")+ assert "/tmp/<path>" in msg+ assert " " not in msg+ long = proc._sanitize_error_message("x" * 1000)+ assert len(long) <= proc._ERROR_MESSAGE_MAX_CHARS+++# ---------------------------------------------------------------------------+# transcribe_with_connector — orchestration with mocked connector/probe/storage+# ---------------------------------------------------------------------------++def _make_connector(text="Hello world transcript", segments=None, speakers=None):+ connector = MagicMock()+ connector.PROVIDER_NAME = "test-connector"+ connector.supports_diarization = False+ connector.default_diarize = False+ specs = MagicMock()+ specs.max_duration_seconds = 1800+ specs.handles_chunking_internally = False+ specs.recommended_chunk_seconds = 600+ specs.unsupported_codecs = []+ connector.specifications = specs+ response = MagicMock()+ response.text = text+ response.segments = segments+ response.speakers = speakers+ response.speaker_embeddings = None+ response.has_diarization.return_value = bool(segments)+ connector.transcribe.return_value = response+ return connector+++def test_transcribe_with_connector_audio_success():+ import time as _time+ with app.app_context():+ user = _make_user("trans_audio")+ rec = _make_recording(user.id, transcription=None, status="PENDING")+ rid = rec.id++ connector = _make_connector(text="A transcribed body of text here.")++ # Provide a real audio file on disk for open()/getsize.+ tmp_path = os.path.join(app.config["UPLOAD_FOLDER"], f"trans_{rid}.mp3")+ with open(tmp_path, "wb") as f:+ f.write(b"\x00" * 1024)++ conv_result = MagicMock()+ conv_result.was_converted = False++ with patch("src.services.transcription.get_connector", return_value=connector), \+ patch.object(proc, "is_video_file", return_value=False), \+ patch.object(proc, "convert_if_needed", return_value=conv_result), \+ patch.object(proc, "client", MagicMock()), \+ patch.object(proc, "ENABLE_INQUIRE_MODE", False), \+ patch.object(proc, "generate_title_task") as mock_title, \+ patch.object(proc, "generate_summary_only_task") as mock_summary, \+ patch.object(proc, "chunking_service") as mock_chunk_svc:+ mock_chunk_svc.needs_chunking.return_value = False+ mock_chunk_svc.get_audio_duration.return_value = 120.0+ proc.transcribe_with_connector(+ app.app_context(), rid, tmp_path, "cov.mp3", _time.time(),+ mime_type="audio/mpeg",+ )++ db.session.expire_all()+ out = db.session.get(Recording, rid)+ assert out.transcription == "A transcribed body of text here."+ assert out.transcription_duration_seconds is not None+ # Title + summary tasks were invoked downstream.+ mock_title.assert_called_once()+ mock_summary.assert_called_once()+++def test_transcribe_with_connector_budget_exceeded_sets_failed():+ import time as _time+ with app.app_context():+ user = _make_user("trans_budget")+ rec = _make_recording(user.id, transcription=None, status="PENDING")+ rid = rec.id+ connector = _make_connector()++ with patch("src.services.transcription.get_connector", return_value=connector), \+ patch.object(proc.transcription_tracker, "check_budget",+ return_value=(False, 100, "Budget exceeded for month")):+ proc.transcribe_with_connector(+ app.app_context(), rid, "/nonexistent.mp3", "cov.mp3", _time.time(),+ mime_type="audio/mpeg",+ )++ db.session.expire_all()+ out = db.session.get(Recording, rid)+ assert out.status == "FAILED"+ assert "Budget exceeded" in out.error_message+++def test_transcribe_with_connector_missing_recording():+ import time as _time+ with app.app_context():+ # Must return quietly, not raise.+ proc.transcribe_with_connector(+ app.app_context(), 99999999, "/x.mp3", "x.mp3", _time.time(),+ )+++def test_transcribe_with_connector_error_reraises():+ import time as _time+ with app.app_context():+ user = _make_user("trans_err")+ rec = _make_recording(user.id, transcription=None, status="PENDING")+ rid = rec.id+ connector = _make_connector()+ connector.transcribe.side_effect = RuntimeError("connector blew up")++ tmp_path = os.path.join(app.config["UPLOAD_FOLDER"], f"err_{rid}.mp3")+ with open(tmp_path, "wb") as f:+ f.write(b"\x00" * 512)++ conv_result = MagicMock()+ conv_result.was_converted = False++ with patch("src.services.transcription.get_connector", return_value=connector), \+ patch.object(proc, "is_video_file", return_value=False), \+ patch.object(proc, "convert_if_needed", return_value=conv_result), \+ patch.object(proc, "chunking_service") as mock_chunk_svc:+ mock_chunk_svc.needs_chunking.return_value = False+ mock_chunk_svc.get_audio_duration.return_value = 60.0+ with pytest.raises(RuntimeError):+ proc.transcribe_with_connector(+ app.app_context(), rid, tmp_path, "cov.mp3", _time.time(),+ mime_type="audio/mpeg",+ )+++# ---------------------------------------------------------------------------+# transcribe_audio_task — thin wrapper that sets processing_time+# ---------------------------------------------------------------------------++def test_transcribe_audio_task_sets_processing_time():+ from datetime import datetime, timedelta+ with app.app_context():+ user = _make_user("task_wrap")+ rec = _make_recording(user.id, transcription="word " * 30, status="PENDING")+ rid = rec.id+ start = datetime.utcnow() - timedelta(seconds=5)++ def _fake_transcribe(app_context, recording_id, *a, **k):+ with app_context:+ r = db.session.get(Recording, recording_id)+ r.status = "COMPLETED"+ db.session.commit()++ with patch.object(proc, "transcribe_with_connector", side_effect=_fake_transcribe):+ proc.transcribe_audio_task(+ app.app_context(), rid, "/x.mp3", "x.mp3", start,+ )++ db.session.expire_all()+ out = db.session.get(Recording, rid)+ assert out.processing_time_seconds is not None+ assert out.processing_time_seconds >= 0+++if __name__ == "__main__":+ sys.exit(pytest.main([__file__, "-q"]))diff --git a/tests/test_cov_recordings_read.py b/tests/test_cov_recordings_read.pynew file mode 100644index 00000000..6d613080--- /dev/null+++ b/tests/test_cov_recordings_read.py@@ -0,0 +1,1124 @@+"""Coverage / behaviour tests for the READ / DELETE / REPROCESS / AUDIO-DELIVERY+and AUTHORIZATION paths in src/api/recordings.py.++Scope (this file owns the read/delete/authz side; the upload/write path lives+elsewhere):+ - list recordings (simple + paginated, owner-scoped, filters, pagination)+ - get single recording detail / status (owner OK, non-owner denied)+ - audio delivery (local send_file path, s3 redirect, deleted/missing audio,+ secure download filename)+ - reprocess transcription / summary / generate_summary (enqueue params,+ ownership, guard conditions)+ - delete recording (row removed + storage.delete called, ownership) and bulk+ delete / bulk reprocess+ - permission/authorization checks on each surface++Harness notes+-------------+1. Flask-Login caches the resolved ``current_user`` on the application context.+ So all DB setup happens inside a short-lived ``_db()`` context that is EXITED+ before any ``client.*`` request; each request then pushes its own context and+ resolves the user from its own session cookie. (Same lesson as+ tests/test_shares_authz.py.)++2. External effects are mocked at the recordings.py import site:+ - ``src.api.recordings.get_storage_service`` -> a fake storage service whose+ ``delete`` is observable and whose ``get_audio_delivery`` returns a+ configurable local-file or redirect result.+ - ``job_queue.enqueue`` is patched on the shared job_queue singleton to+ capture params without spawning workers (workers are already 0 via+ conftest).++3. Every assertion is scoped to the recording IDs / user this test created — the+ session DB is shared across test files, so we never assert on global counts.++Run:+ docker run --rm -v $PWD:/app:ro -e UPLOAD_FOLDER=/tmp/up \+ -e ASR_BASE_URL=http://x:9999 -e COVERAGE_FILE=/tmp/cov/.coverage \+ -e PYTHONDONTWRITEBYTECODE=1 speakr-test:cov sh -c \+ "mkdir -p /tmp/cov && cd /app && python -m pytest \+ tests/test_cov_recordings_read.py -q -o cache_dir=/tmp/ptcache"+"""++import os+import sys+import json+import uuid+import tempfile+from contextlib import contextmanager+from dataclasses import dataclass+from typing import Optional+from unittest.mock import patch++import pytest++sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))++# Standalone safety net (conftest.py already sets these under pytest).+if "SQLALCHEMY_DATABASE_URI" not in os.environ:+ _D = tempfile.mkdtemp(prefix="speakr_cov_recordings_")+ os.environ["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{os.path.join(_D, 'test.db')}"+ os.environ.setdefault("UPLOAD_FOLDER", os.path.join(_D, "uploads"))+ os.environ.setdefault("SECRET_KEY", "pytest-secret-key")+ os.environ.setdefault("ENABLE_AUTO_PROCESSING", "false")+ os.environ.setdefault("TEXT_MODEL_API_KEY", "test-key")+ os.environ.setdefault("TRANSCRIPTION_API_KEY", "test-key")+ os.environ.setdefault("TRANSCRIPTION_BASE_URL", "https://api.openai.com/v1")++import src.api.recordings as rec_module+from src.app import app, db+from src.models import User, Recording+from src.models.organization import Tag, Folder, RecordingTag++app.config["WTF_CSRF_ENABLED"] = False+++# --------------------------------------------------------------------------- #+# Fake storage service+# --------------------------------------------------------------------------- #++@dataclass+class _Delivery:+ mode: str # 'local_file' or 'redirect_url'+ local_path: Optional[str] = None+ url: Optional[str] = None+++class FakeStorage:+ """Observable stand-in for the storage facade used by recordings.py."""++ def __init__(self, *, delivery=None, exists=True):+ self._delivery = delivery+ self._exists = exists+ self.deleted = [] # list of audio_path strings passed to delete()+ self.exists_calls = []++ def delete(self, audio_path, missing_ok=True):+ self.deleted.append(audio_path)+ return True++ def exists(self, audio_path):+ self.exists_calls.append(audio_path)+ return self._exists++ def get_audio_delivery(self, audio_path, **kwargs):+ # Capture kwargs for download_name assertions.+ self.last_delivery_kwargs = kwargs+ return self._delivery+++@contextmanager+def use_storage(storage):+ with patch.object(rec_module, "get_storage_service", return_value=storage):+ yield storage+++@contextmanager+def capture_enqueue():+ """Patch the shared job_queue.enqueue, capturing every call's kwargs."""+ calls = []++ def fake_enqueue(*args, **kwargs):+ calls.append(kwargs)+ return 12345++ with patch.object(rec_module.job_queue, "enqueue", side_effect=fake_enqueue):+ yield calls+++# --------------------------------------------------------------------------- #+# DB helpers+# --------------------------------------------------------------------------- #++@contextmanager+def _db():+ """Short-lived app context for DB work; exit before any HTTP request."""+ with app.app_context():+ yield+++_PREFIX = "covrecread"+++def make_user():+ suffix = uuid.uuid4().hex[:10]+ user = User(+ username=f"{_PREFIX}_{suffix}",+ email=f"{_PREFIX}_{suffix}@test.local",+ password="x",+ )+ db.session.add(user)+ db.session.commit()+ return user+++def make_recording(user, *, status="COMPLETED", transcription="hello world this is a transcript",+ audio_path="local://covrec/audio.mp3", title="rec",+ summary="a summary", original_filename="audio.mp3",+ mime_type="audio/mpeg", folder_id=None):+ rec = Recording(+ audio_path=audio_path,+ original_filename=original_filename,+ title=title,+ status=status,+ transcription=transcription,+ summary=summary,+ user_id=user.id,+ mime_type=mime_type,+ folder_id=folder_id,+ )+ db.session.add(rec)+ db.session.commit()+ return rec+++def login(client, user_id):+ with client.session_transaction() as sess:+ sess["_user_id"] = str(user_id)+ sess["_fresh"] = True+++def new_client():+ return app.test_client()+++# --------------------------------------------------------------------------- #+# Module-level cleanup+# --------------------------------------------------------------------------- #++def teardown_module(module):+ with app.app_context():+ users = User.query.filter(User.username.like(f"{_PREFIX}_%")).all()+ for u in users:+ for r in Recording.query.filter_by(user_id=u.id).all():+ RecordingTag.query.filter_by(recording_id=r.id).delete()+ db.session.delete(r)+ Tag.query.filter_by(user_id=u.id).delete()+ Folder.query.filter_by(user_id=u.id).delete()+ db.session.delete(u)+ db.session.commit()+++# --------------------------------------------------------------------------- #+# Pytest fixtures+# --------------------------------------------------------------------------- #++def owner():+ with _db():+ u = make_user()+ return u.id+++def other():+ with _db():+ u = make_user()+ return u.id+++# =========================================================================== #+# LIST: /recordings (simple list, owner-scoped)+# =========================================================================== #++def test_get_recordings_lists_only_owner(owner, other):+ with _db():+ u = db.session.get(User, owner)+ o = db.session.get(User, other)+ mine = make_recording(u, title="mine-A")+ theirs = make_recording(o, title="theirs-A")+ mine_id, theirs_id = mine.id, theirs.id++ c = new_client()+ login(c, owner)+ resp = c.get("/recordings")+ assert resp.status_code == 200+ ids = {r["id"] for r in resp.get_json()}+ assert mine_id in ids+ assert theirs_id not in ids+++def test_get_recordings_unauthenticated_returns_empty_array():+ c = new_client()+ resp = c.get("/recordings")+ assert resp.status_code == 200+ assert resp.get_json() == []+++# =========================================================================== #+# LIST: /api/recordings (paginated + filters)+# =========================================================================== #++def test_paginated_list_owner_scoped(owner, other):+ with _db():+ u = db.session.get(User, owner)+ o = db.session.get(User, other)+ mine_id = make_recording(u, title="p-mine").id+ theirs_id = make_recording(o, title="p-theirs").id++ c = new_client()+ login(c, owner)+ resp = c.get("/api/recordings?per_page=100")+ assert resp.status_code == 200+ body = resp.get_json()+ ids = {r["id"] for r in body["recordings"]}+ assert mine_id in ids+ assert theirs_id not in ids+ assert "pagination" in body+++def test_paginated_list_pagination_metadata(owner):+ with _db():+ u = db.session.get(User, owner)+ for i in range(3):+ make_recording(u, title=f"page-{i}")++ c = new_client()+ login(c, owner)+ resp = c.get("/api/recordings?per_page=2&page=1")+ body = resp.get_json()+ assert resp.status_code == 200+ assert body["pagination"]["per_page"] == 2+ assert body["pagination"]["page"] == 1+ # We created >=3, so total >= 3 and there is a next page.+ assert body["pagination"]["total"] >= 3+ assert body["pagination"]["has_next"] is True+ assert body["pagination"]["has_prev"] is False+ assert len(body["recordings"]) <= 2+++def test_paginated_per_page_capped_at_100(owner):+ c = new_client()+ login(c, owner)+ resp = c.get("/api/recordings?per_page=9999")+ assert resp.status_code == 200+ assert resp.get_json()["pagination"]["per_page"] == 100+++def test_paginated_folder_filter(owner):+ with _db():+ u = db.session.get(User, owner)+ folder = Folder(name=f"fold_{uuid.uuid4().hex[:6]}", user_id=u.id)+ db.session.add(folder)+ db.session.commit()+ in_folder = make_recording(u, title="in-folder", folder_id=folder.id).id+ no_folder = make_recording(u, title="no-folder").id+ folder_id = folder.id++ c = new_client()+ login(c, owner)+ resp = c.get(f"/api/recordings?folder={folder_id}&per_page=100")+ ids = {r["id"] for r in resp.get_json()["recordings"]}+ assert in_folder in ids+ assert no_folder not in ids+++def test_paginated_folder_none_filter(owner):+ with _db():+ u = db.session.get(User, owner)+ folder = Folder(name=f"fold_{uuid.uuid4().hex[:6]}", user_id=u.id)+ db.session.add(folder)+ db.session.commit()+ in_folder = make_recording(u, title="nf-in", folder_id=folder.id).id+ no_folder = make_recording(u, title="nf-out").id++ c = new_client()+ login(c, owner)+ resp = c.get("/api/recordings?folder=none&per_page=100")+ ids = {r["id"] for r in resp.get_json()["recordings"]}+ assert no_folder in ids+ assert in_folder not in ids+++def test_paginated_text_search_by_title(owner):+ marker = f"uniqueterm{uuid.uuid4().hex[:8]}"+ with _db():+ u = db.session.get(User, owner)+ match_id = make_recording(u, title=f"meeting {marker} notes").id+ nomatch_id = make_recording(u, title="unrelated recording").id++ c = new_client()+ login(c, owner)+ resp = c.get(f"/api/recordings?q={marker}&per_page=100")+ ids = {r["id"] for r in resp.get_json()["recordings"]}+ assert match_id in ids+ assert nomatch_id not in ids+++def test_paginated_tag_filter(owner):+ tagname = f"covtag{uuid.uuid4().hex[:6]}"+ with _db():+ u = db.session.get(User, owner)+ tag = Tag(name=tagname, user_id=u.id)+ db.session.add(tag)+ db.session.commit()+ tagged = make_recording(u, title="tagged-rec")+ db.session.add(RecordingTag(recording_id=tagged.id, tag_id=tag.id, order=1))+ db.session.commit()+ tagged_id = tagged.id+ untagged_id = make_recording(u, title="untagged-rec").id++ c = new_client()+ login(c, owner)+ resp = c.get(f"/api/recordings?q=tag:{tagname}&per_page=100")+ ids = {r["id"] for r in resp.get_json()["recordings"]}+ assert tagged_id in ids+ assert untagged_id not in ids+++def test_paginated_speaker_filter(owner):+ speaker = f"covspk{uuid.uuid4().hex[:6]}"+ with _db():+ u = db.session.get(User, owner)+ rec = make_recording(u, title="spk-rec")+ rec.participants = speaker+ db.session.commit()+ match_id = rec.id+ nomatch_id = make_recording(u, title="spk-none").id++ c = new_client()+ login(c, owner)+ resp = c.get(f"/api/recordings?q=speaker:{speaker}&per_page=100")+ ids = {r["id"] for r in resp.get_json()["recordings"]}+ assert match_id in ids+ assert nomatch_id not in ids+++def test_paginated_requires_login():+ c = new_client()+ resp = c.get("/api/recordings")+ assert resp.status_code in (302, 401)+++def test_paginated_can_delete_flag_for_owner(owner):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u, title="del-flag").id++ c = new_client()+ login(c, owner)+ resp = c.get("/api/recordings?per_page=100")+ rec = next(r for r in resp.get_json()["recordings"] if r["id"] == rid)+ assert rec["is_owner"] is True+ assert rec["can_delete"] is True+++# =========================================================================== #+# INBOX / ARCHIVED lists+# =========================================================================== #++def test_inbox_recordings_owner_scoped(owner, other):+ with _db():+ u = db.session.get(User, owner)+ o = db.session.get(User, other)+ mine = make_recording(u, status="PROCESSING", title="inbox-mine")+ mine.is_inbox = True+ theirs = make_recording(o, status="PROCESSING", title="inbox-theirs")+ theirs.is_inbox = True+ db.session.commit()+ mine_id, theirs_id = mine.id, theirs.id++ c = new_client()+ login(c, owner)+ resp = c.get("/api/inbox_recordings")+ assert resp.status_code == 200+ ids = {r["id"] for r in resp.get_json()}+ assert mine_id in ids+ assert theirs_id not in ids+++def test_archived_recordings_only_audio_deleted(owner):+ from datetime import datetime+ with _db():+ u = db.session.get(User, owner)+ archived = make_recording(u, title="arch")+ archived.audio_deleted_at = datetime.utcnow()+ db.session.commit()+ archived_id = archived.id+ active_id = make_recording(u, title="active").id++ c = new_client()+ login(c, owner)+ resp = c.get("/api/recordings/archived")+ assert resp.status_code == 200+ ids = {r["id"] for r in resp.get_json()}+ assert archived_id in ids+ assert active_id not in ids+++# =========================================================================== #+# GET single recording detail+# =========================================================================== #++def test_get_detail_owner_ok(owner):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u, title="detail-ok").id++ c = new_client()+ login(c, owner)+ resp = c.get(f"/api/recordings/{rid}")+ assert resp.status_code == 200+ body = resp.get_json()+ assert body["id"] == rid+ assert body["is_owner"] is True+ assert body["is_shared"] is False+++def test_get_detail_non_owner_denied(owner, other):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u, title="detail-denied").id++ c = new_client()+ login(c, other)+ resp = c.get(f"/api/recordings/{rid}")+ assert resp.status_code == 403+++def test_get_detail_not_found(owner):+ c = new_client()+ login(c, owner)+ resp = c.get("/api/recordings/99999999")+ assert resp.status_code == 404+++def test_get_detail_requires_login(owner):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u).id+ c = new_client()+ resp = c.get(f"/api/recordings/{rid}")+ assert resp.status_code in (302, 401)+++# =========================================================================== #+# status endpoints+# =========================================================================== #++def test_status_only_owner(owner):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u, status="COMPLETED").id+ c = new_client()+ login(c, owner)+ resp = c.get(f"/recording/{rid}/status")+ assert resp.status_code == 200+ assert resp.get_json()["status"] == "COMPLETED"+++def test_status_only_non_owner_denied(owner, other):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u).id+ c = new_client()+ login(c, other)+ resp = c.get(f"/recording/{rid}/status")+ assert resp.status_code == 403+++def test_status_only_not_found(owner):+ c = new_client()+ login(c, owner)+ resp = c.get("/recording/99999999/status")+ assert resp.status_code == 404+++def test_full_status_owner(owner):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u, title="full-status").id+ c = new_client()+ login(c, owner)+ resp = c.get(f"/status/{rid}")+ assert resp.status_code == 200+ assert resp.get_json()["id"] == rid+++def test_full_status_non_owner_denied(owner, other):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u).id+ c = new_client()+ login(c, other)+ resp = c.get(f"/status/{rid}")+ assert resp.status_code == 403+++def test_batch_status_only_accessible(owner, other):+ with _db():+ u = db.session.get(User, owner)+ o = db.session.get(User, other)+ mine = make_recording(u, status="COMPLETED").id+ theirs = make_recording(o, status="FAILED").id++ c = new_client()+ login(c, owner)+ resp = c.post("/api/recordings/batch-status",+ json={"recording_ids": [mine, theirs]})+ assert resp.status_code == 200+ statuses = resp.get_json()["statuses"]+ assert statuses.get(str(mine)) == "COMPLETED"+ assert str(theirs) not in statuses+++def test_batch_status_requires_list(owner):+ c = new_client()+ login(c, owner)+ resp = c.post("/api/recordings/batch-status", json={"recording_ids": "nope"})+ assert resp.status_code == 400+++def test_batch_status_too_many(owner):+ c = new_client()+ login(c, owner)+ resp = c.post("/api/recordings/batch-status",+ json={"recording_ids": list(range(51))})+ assert resp.status_code == 400+++# =========================================================================== #+# AUDIO delivery+# =========================================================================== #++def test_audio_local_send_file(owner):+ # Create a real file on disk for send_file to stream.+ fd, path = tempfile.mkstemp(suffix=".mp3")+ os.write(fd, b"ID3fakeaudio")+ os.close(fd)+ try:+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u, title="audio-local").id++ storage = FakeStorage(delivery=_Delivery(mode="local_file", local_path=path))+ c = new_client()+ login(c, owner)+ with use_storage(storage):+ resp = c.get(f"/audio/{rid}")+ assert resp.status_code == 200+ assert resp.data == b"ID3fakeaudio"+ finally:+ os.remove(path)+++def test_audio_s3_redirect(owner):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u, title="audio-s3").id++ storage = FakeStorage(delivery=_Delivery(mode="redirect_url",+ url="https://s3.example/signed"))+ c = new_client()+ login(c, owner)+ with use_storage(storage):+ resp = c.get(f"/audio/{rid}")+ assert resp.status_code == 302+ assert resp.headers["Location"] == "https://s3.example/signed"+++def test_audio_missing_local_file_404(owner):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u, title="audio-missing").id++ storage = FakeStorage(delivery=_Delivery(mode="local_file",+ local_path="/nonexistent/path.mp3"))+ c = new_client()+ login(c, owner)+ with use_storage(storage):+ resp = c.get(f"/audio/{rid}")+ assert resp.status_code == 404+++def test_audio_no_audio_path_404(owner):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u, audio_path=None, title="no-audio").id++ c = new_client()+ login(c, owner)+ resp = c.get(f"/audio/{rid}")+ assert resp.status_code == 404+++def test_audio_non_owner_denied(owner, other):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u, title="audio-denied").id++ storage = FakeStorage(delivery=_Delivery(mode="redirect_url", url="x"))+ c = new_client()+ login(c, other)+ with use_storage(storage):+ resp = c.get(f"/audio/{rid}")+ assert resp.status_code == 403+++def test_audio_download_secure_filename(owner):+ fd, path = tempfile.mkstemp(suffix=".mp3")+ os.write(fd, b"audiobytes")+ os.close(fd)+ try:+ with _db():+ u = db.session.get(User, owner)+ # Title with characters that must be stripped from download_name.+ rid = make_recording(u, title="My/Bad:Title*?",+ original_filename="orig.wav").id++ storage = FakeStorage(delivery=_Delivery(mode="local_file", local_path=path))+ c = new_client()+ login(c, owner)+ with use_storage(storage):+ resp = c.get(f"/audio/{rid}?download=true")+ assert resp.status_code == 200+ # The endpoint computes a sanitized download_name and passes it to+ # storage.get_audio_delivery; verify the dangerous chars were stripped+ # and the original-filename extension (.wav) preserved.+ dn = storage.last_delivery_kwargs["download_name"]+ assert "/" not in dn and ":" not in dn and "*" not in dn and "?" not in dn+ assert dn.endswith(".wav")+ assert storage.last_delivery_kwargs["download"] is True+ # Response is served as an attachment.+ assert "attachment" in resp.headers.get("Content-Disposition", "")+ finally:+ os.remove(path)+++# =========================================================================== #+# REPROCESS transcription+# =========================================================================== #++def test_reprocess_transcription_enqueues(owner):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u, status="COMPLETED").id++ storage = FakeStorage(exists=True)+ c = new_client()+ login(c, owner)+ with use_storage(storage), capture_enqueue() as calls:+ resp = c.post(f"/recording/{rid}/reprocess_transcription",+ json={"language": "es", "min_speakers": "2"})+ assert resp.status_code in (200, 202)+ assert len(calls) == 1+ call = calls[0]+ assert call["job_type"] == "reprocess_transcription"+ assert call["recording_id"] == rid+ assert call["params"]["language"] == "es"+ assert call["params"]["min_speakers"] == 2+++def test_reprocess_transcription_non_owner_denied(owner, other):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u).id++ storage = FakeStorage(exists=True)+ c = new_client()+ login(c, other)+ with use_storage(storage), capture_enqueue() as calls:+ resp = c.post(f"/recording/{rid}/reprocess_transcription", json={})+ assert resp.status_code == 403+ assert calls == []+++def test_reprocess_transcription_audio_missing_404(owner):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u).id++ storage = FakeStorage(exists=False) # storage.exists() returns False+ c = new_client()+ login(c, owner)+ with use_storage(storage), capture_enqueue() as calls:+ resp = c.post(f"/recording/{rid}/reprocess_transcription", json={})+ assert resp.status_code == 404+ assert calls == []+++def test_reprocess_transcription_already_processing(owner):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u, status="PROCESSING").id++ storage = FakeStorage(exists=True)+ c = new_client()+ login(c, owner)+ with use_storage(storage), capture_enqueue() as calls:+ resp = c.post(f"/recording/{rid}/reprocess_transcription", json={})+ assert resp.status_code == 400+ assert calls == []+++def test_reprocess_transcription_not_found(owner):+ c = new_client()+ login(c, owner)+ storage = FakeStorage(exists=True)+ with use_storage(storage):+ resp = c.post("/recording/99999999/reprocess_transcription", json={})+ assert resp.status_code == 404+++# =========================================================================== #+# REPROCESS summary+# =========================================================================== #++def test_reprocess_summary_enqueues(owner):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u, status="COMPLETED",+ transcription="a real transcript with content").id++ c = new_client()+ login(c, owner)+ # client (the OpenRouter client) must not be None for the endpoint to proceed.+ with patch.object(rec_module, "client", object()), capture_enqueue() as calls:+ resp = c.post(f"/recording/{rid}/reprocess_summary", json={})+ assert resp.status_code in (200, 202)+ assert len(calls) == 1+ assert calls[0]["job_type"] == "reprocess_summary"+ assert calls[0]["recording_id"] == rid+++def test_reprocess_summary_custom_prompt_append(owner):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u, transcription="a real transcript here").id++ c = new_client()+ login(c, owner)+ with patch.object(rec_module, "client", object()), capture_enqueue() as calls:+ resp = c.post(f"/recording/{rid}/reprocess_summary",+ json={"custom_prompt": "Be brief", "prompt_mode": "append"})+ assert resp.status_code in (200, 202)+ params = calls[0]["params"]+ assert params["custom_prompt"] == "Be brief"+ assert params["custom_prompt_append"] is True+++def test_reprocess_summary_no_transcription_400(owner):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u, transcription="").id++ c = new_client()+ login(c, owner)+ with patch.object(rec_module, "client", object()), capture_enqueue() as calls:+ resp = c.post(f"/recording/{rid}/reprocess_summary", json={})+ assert resp.status_code == 400+ assert calls == []+++def test_reprocess_summary_transcription_error_400(owner):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u, transcription="Transcription failed: boom").id++ c = new_client()+ login(c, owner)+ with patch.object(rec_module, "client", object()), capture_enqueue() as calls:+ resp = c.post(f"/recording/{rid}/reprocess_summary", json={})+ assert resp.status_code == 400+ assert calls == []+++def test_reprocess_summary_non_owner_denied(owner, other):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u, transcription="a real transcript here").id++ c = new_client()+ login(c, other)+ with patch.object(rec_module, "client", object()), capture_enqueue() as calls:+ resp = c.post(f"/recording/{rid}/reprocess_summary", json={})+ assert resp.status_code == 403+ assert calls == []+++def test_reprocess_summary_client_unavailable_503(owner):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u, transcription="a real transcript here").id++ c = new_client()+ login(c, owner)+ with patch.object(rec_module, "client", None), capture_enqueue() as calls:+ resp = c.post(f"/recording/{rid}/reprocess_summary", json={})+ assert resp.status_code == 503+ assert calls == []+++# =========================================================================== #+# generate_summary+# =========================================================================== #++def test_generate_summary_enqueues(owner):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u, status="COMPLETED",+ transcription="a real transcript here").id++ c = new_client()+ login(c, owner)+ with patch.object(rec_module, "client", object()), capture_enqueue() as calls:+ resp = c.post(f"/recording/{rid}/generate_summary", json={})+ assert resp.status_code in (200, 202)+ assert len(calls) == 1+ assert calls[0]["job_type"] == "summarize"+++def test_generate_summary_non_owner_denied(owner, other):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u, transcription="a real transcript here").id++ c = new_client()+ login(c, other)+ with patch.object(rec_module, "client", object()), capture_enqueue() as calls:+ resp = c.post(f"/recording/{rid}/generate_summary", json={})+ assert resp.status_code == 403+ assert calls == []+++# =========================================================================== #+# DELETE single recording+# =========================================================================== #++def test_delete_recording_removes_row_and_calls_storage(owner):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u, audio_path="local://covrec/del.mp3").id++ storage = FakeStorage()+ c = new_client()+ login(c, owner)+ with use_storage(storage):+ resp = c.delete(f"/recording/{rid}")+ assert resp.status_code == 200+ assert resp.get_json()["success"] is True+ assert "local://covrec/del.mp3" in storage.deleted+ with _db():+ assert db.session.get(Recording, rid) is None+++def test_delete_recording_non_owner_denied(owner, other):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u).id++ storage = FakeStorage()+ c = new_client()+ login(c, other)+ with use_storage(storage):+ resp = c.delete(f"/recording/{rid}")+ assert resp.status_code == 403+ assert storage.deleted == []+ with _db():+ assert db.session.get(Recording, rid) is not None+++def test_delete_recording_not_found(owner):+ c = new_client()+ login(c, owner)+ storage = FakeStorage()+ with use_storage(storage):+ resp = c.delete("/recording/99999999")+ assert resp.status_code == 404+++def test_delete_recording_requires_login(owner):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u).id+ c = new_client()+ resp = c.delete(f"/recording/{rid}")+ assert resp.status_code in (302, 401)+ with _db():+ assert db.session.get(Recording, rid) is not None+++# =========================================================================== #+# BULK delete+# =========================================================================== #++def test_bulk_delete_owner(owner):+ with _db():+ u = db.session.get(User, owner)+ a = make_recording(u, audio_path="local://covrec/a.mp3").id+ b = make_recording(u, audio_path="local://covrec/b.mp3").id++ storage = FakeStorage()+ c = new_client()+ login(c, owner)+ with use_storage(storage):+ resp = c.delete("/api/recordings/bulk", json={"recording_ids": [a, b]})+ assert resp.status_code == 200+ body = resp.get_json()+ assert set(body["deleted_ids"]) == {a, b}+ assert body["deleted_count"] == 2+ with _db():+ assert db.session.get(Recording, a) is None+ assert db.session.get(Recording, b) is None+++def test_bulk_delete_skips_non_owned(owner, other):+ with _db():+ u = db.session.get(User, owner)+ o = db.session.get(User, other)+ mine = make_recording(u, audio_path="local://covrec/m.mp3").id+ theirs = make_recording(o, audio_path="local://covrec/t.mp3").id++ storage = FakeStorage()+ c = new_client()+ login(c, owner)+ with use_storage(storage):+ resp = c.delete("/api/recordings/bulk",+ json={"recording_ids": [mine, theirs]})+ assert resp.status_code == 200+ body = resp.get_json()+ assert mine in body["deleted_ids"]+ assert theirs not in body["deleted_ids"]+ assert body["errors"] # the non-owned one reports an error+ with _db():+ assert db.session.get(Recording, mine) is None+ assert db.session.get(Recording, theirs) is not None # untouched+++def test_bulk_delete_empty_list_400(owner):+ c = new_client()+ login(c, owner)+ resp = c.delete("/api/recordings/bulk", json={"recording_ids": []})+ assert resp.status_code == 400+++def test_bulk_delete_missing_key_400(owner):+ c = new_client()+ login(c, owner)+ resp = c.delete("/api/recordings/bulk", json={})+ assert resp.status_code == 400+++def test_bulk_delete_too_many_400(owner):+ c = new_client()+ login(c, owner)+ resp = c.delete("/api/recordings/bulk",+ json={"recording_ids": list(range(101))})+ assert resp.status_code == 400+++# =========================================================================== #+# BULK reprocess+# =========================================================================== #++def test_bulk_reprocess_summary_enqueues(owner):+ with _db():+ u = db.session.get(User, owner)+ a = make_recording(u, status="COMPLETED", transcription="content a").id+ b = make_recording(u, status="COMPLETED", transcription="content b").id++ c = new_client()+ login(c, owner)+ with capture_enqueue() as calls:+ resp = c.post("/api/recordings/bulk-reprocess",+ json={"recording_ids": [a, b], "type": "summary"})+ assert resp.status_code == 200+ body = resp.get_json()+ assert set(body["queued_ids"]) == {a, b}+ assert all(call["job_type"] == "reprocess_summary" for call in calls)+ assert len(calls) == 2+++def test_bulk_reprocess_transcription_checks_audio(owner):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u, status="COMPLETED").id++ storage = FakeStorage(exists=True)+ c = new_client()+ login(c, owner)+ with use_storage(storage), capture_enqueue() as calls:+ resp = c.post("/api/recordings/bulk-reprocess",+ json={"recording_ids": [rid], "type": "transcription"})+ assert resp.status_code == 200+ assert rid in resp.get_json()["queued_ids"]+ assert calls[0]["job_type"] == "reprocess_transcription"+++def test_bulk_reprocess_skips_non_owned(owner, other):+ with _db():+ u = db.session.get(User, owner)+ o = db.session.get(User, other)+ mine = make_recording(u, status="COMPLETED", transcription="x").id+ theirs = make_recording(o, status="COMPLETED", transcription="y").id++ c = new_client()+ login(c, owner)+ with capture_enqueue() as calls:+ resp = c.post("/api/recordings/bulk-reprocess",+ json={"recording_ids": [mine, theirs], "type": "summary"})+ assert resp.status_code == 200+ qids = resp.get_json()["queued_ids"]+ assert mine in qids+ assert theirs not in qids+++def test_bulk_reprocess_skips_non_completed(owner):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u, status="PROCESSING", transcription="x").id++ c = new_client()+ login(c, owner)+ with capture_enqueue() as calls:+ resp = c.post("/api/recordings/bulk-reprocess",+ json={"recording_ids": [rid], "type": "summary"})+ assert resp.status_code == 200+ assert rid not in resp.get_json()["queued_ids"]+ assert calls == []+++def test_bulk_reprocess_bad_type_400(owner):+ c = new_client()+ login(c, owner)+ resp = c.post("/api/recordings/bulk-reprocess",+ json={"recording_ids": [1], "type": "nope"})+ assert resp.status_code == 400+++# =========================================================================== #+# delete-via-job + toggle_deletion_exempt authz+# =========================================================================== #++def test_toggle_deletion_exempt_owner(owner):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u).id++ c = new_client()+ login(c, owner)+ resp = c.post(f"/api/recordings/{rid}/toggle_deletion_exempt")+ assert resp.status_code == 200+ assert resp.get_json()["deletion_exempt"] is True+++def test_toggle_deletion_exempt_non_owner_denied(owner, other):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u).id++ c = new_client()+ login(c, other)+ resp = c.post(f"/api/recordings/{rid}/toggle_deletion_exempt")+ assert resp.status_code == 403+++def test_process_chunks_non_owner_denied(owner, other):+ with _db():+ u = db.session.get(User, owner)+ rid = make_recording(u).id++ c = new_client()+ login(c, other)+ resp = c.post(f"/api/recording/{rid}/process_chunks")+ assert resp.status_code == 403diff --git a/tests/test_cov_recordings_write.py b/tests/test_cov_recordings_write.pynew file mode 100644index 00000000..8cd74e12--- /dev/null+++ b/tests/test_cov_recordings_write.py@@ -0,0 +1,1013 @@+"""Coverage tests for the WRITE paths in src/api/recordings.py.++Focus: the /upload handler end-to-end (Recording row creation, audio_path+locator, job enqueue, keep_audio_only, tag/folder association, per-upload+transcription params, duplicate detection, size/type validation rejects,+missing-file 400), plus metadata edit endpoints (/save, update_transcription,+toggle_inbox/highlight, tag add/remove/reorder, bulk-tags) and the+_resolve_transcription_model / resolve_upload_title helpers.++External effects (codec probe, conversion, storage, job queue, ffprobe) are+mocked at the recordings.py import site so nothing touches real storage or+shells out. Storage.upload_local_file is faked to return a StoredObject with a+.locator without moving any file.++SHARED-DB: every assertion is scoped to the specific user/recording the test+created. No assertions on global counts of all recordings.+"""++import io+import os+import sys+import uuid+from contextlib import contextmanager+from unittest.mock import patch, MagicMock++sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))++from src.app import app, db+from src.models import User, Recording, Tag, Folder, RecordingTag, SystemSetting++app.config["WTF_CSRF_ENABLED"] = False+++# --------------------------------------------------------------------------- #+# Helpers+# --------------------------------------------------------------------------- #++def _mk_user(prefix="up"):+ suffix = uuid.uuid4().hex[:8]+ user = User(+ username=f"{prefix}_{suffix}",+ email=f"{prefix}_{suffix}@local.test",+ password="x",+ )+ db.session.add(user)+ db.session.commit()+ return user+++def _login(client, user):+ with client.session_transaction() as sess:+ sess["_user_id"] = str(user.id)+ sess["_fresh"] = True+++def _mk_tag(user, **kwargs):+ tag = Tag(+ name=f"tag_{uuid.uuid4().hex[:8]}",+ user_id=user.id,+ **kwargs,+ )+ db.session.add(tag)+ db.session.commit()+ return tag+++def _mk_folder(user, **kwargs):+ folder = Folder(+ name=f"folder_{uuid.uuid4().hex[:8]}",+ user_id=user.id,+ **kwargs,+ )+ db.session.add(folder)+ db.session.commit()+ return folder+++def _mk_recording(user, **kwargs):+ rec = Recording(+ user_id=user.id,+ title=kwargs.pop("title", f"rec_{uuid.uuid4().hex[:8]}"),+ status=kwargs.pop("status", "COMPLETED"),+ audio_path=kwargs.pop("audio_path", "local://recordings/x.mp3"),+ original_filename=kwargs.pop("original_filename", "x.mp3"),+ **kwargs,+ )+ db.session.add(rec)+ db.session.commit()+ return rec+++class _FakeStoredObject:+ def __init__(self, locator, key):+ self.locator = locator+ self.key = key+ self.size = 1024+ self.content_type = None+ self.etag = None+++class _FakeStorage:+ """Drop-in fake for StorageService used by the upload route.++ upload_local_file does NOT move any file (delete_source ignored) and just+ returns a StoredObject with a deterministic locator so audio_path gets set.+ """++ def __init__(self, staging_dir):+ self._staging = staging_dir+ self.uploaded = []++ def get_staging_dir(self):+ os.makedirs(self._staging, exist_ok=True)+ return self._staging++ def build_recording_key(self, original_filename, recording_id=None, *, now=None):+ return f"recordings/test/{recording_id}/{original_filename}"++ def upload_local_file(self, local_path, key, *, content_type=None, delete_source=False):+ self.uploaded.append((local_path, key))+ return _FakeStoredObject(locator=f"local://{key}", key=key)+++@contextmanager+def _upload_mocks(staging_dir, has_video=False, audio_codec="mp3"):+ """Patch all external upload effects. convert_if_needed returns the input+ path untouched (no conversion); storage is faked so no file moves."""+ fake_storage = _FakeStorage(staging_dir)++ def _convert(filepath, **kwargs):+ result = MagicMock()+ result.output_path = filepath+ result.was_converted = False+ result.was_compressed = False+ result.original_codec = audio_codec+ result.final_codec = audio_codec+ result.size_reduction_percent = 0.0+ return result++ with patch("src.api.recordings.get_storage_service", return_value=fake_storage), \+ patch("src.api.recordings.get_codec_info",+ return_value={"has_video": has_video, "audio_codec": audio_codec,+ "video_codec": "h264" if has_video else None, "duration": 12.0}), \+ patch("src.api.recordings.convert_if_needed", side_effect=_convert), \+ patch("src.api.recordings.get_duration", return_value=12.0), \+ patch("src.api.recordings.get_creation_date", return_value=None), \+ patch("src.services.job_queue.job_queue.enqueue", return_value=1) as enqueue_mock:+ yield fake_storage, enqueue_mock+++def _do_upload(client, filename="sample.mp3", payload=b"\x00" * 4096, **form):+ data = dict(form)+ data["file"] = (io.BytesIO(payload), filename)+ return client.post("/upload", data=data, content_type="multipart/form-data")+++def _latest_rec(user):+ return (Recording.query.filter_by(user_id=user.id)+ .order_by(Recording.id.desc()).first())+++def _cleanup(*objs):+ for o in objs:+ try:+ db.session.delete(o)+ except Exception:+ pass+ db.session.commit()+++# --------------------------------------------------------------------------- #+# Upload — success / row creation+# --------------------------------------------------------------------------- #++def test_upload_creates_recording_row_with_locator_and_enqueues():+ with app.app_context():+ user = _mk_user("up_ok")+ client = app.test_client()+ _login(client, user)+ staging = os.path.join(app.config["UPLOAD_FOLDER"], f"stg_{uuid.uuid4().hex[:6]}")+ with _upload_mocks(staging) as (storage, enqueue):+ resp = _do_upload(client, title="Hello", notes="my note")+ assert resp.status_code == 202, resp.data+ rec = _latest_rec(user)+ assert rec is not None+ assert rec.title == "Hello"+ assert rec.notes == "my note"+ assert rec.status == "PENDING"+ assert rec.processing_source == "upload"+ assert rec.original_filename == "sample.mp3"+ # audio_path is the storage locator returned by upload_local_file+ assert rec.audio_path is not None and rec.audio_path.startswith("local://")+ assert storage.uploaded # upload_local_file was invoked+ assert rec.file_hash is not None+ assert rec.audio_duration_seconds == 12.0+ # Enqueued a transcribe job for this recording+ assert enqueue.called+ kwargs = enqueue.call_args.kwargs+ assert kwargs["recording_id"] == rec.id+ assert kwargs["job_type"] == "transcribe"+ assert kwargs["is_new_upload"] is True+ _cleanup(rec, user)+++def test_upload_default_title_is_placeholder():+ with app.app_context():+ user = _mk_user("up_title")+ client = app.test_client()+ _login(client, user)+ staging = os.path.join(app.config["UPLOAD_FOLDER"], f"stg_{uuid.uuid4().hex[:6]}")+ with _upload_mocks(staging):+ resp = _do_upload(client, filename="interview.mp3")+ assert resp.status_code == 202+ rec = _latest_rec(user)+ assert rec.title == "Recording - interview.mp3"+ _cleanup(rec, user)+++def test_upload_user_meeting_date_parsed():+ with app.app_context():+ user = _mk_user("up_date")+ client = app.test_client()+ _login(client, user)+ staging = os.path.join(app.config["UPLOAD_FOLDER"], f"stg_{uuid.uuid4().hex[:6]}")+ with _upload_mocks(staging):+ resp = _do_upload(client, meeting_date="2022-03-04T09:15:00Z")+ assert resp.status_code == 202+ rec = _latest_rec(user)+ assert rec.meeting_date.year == 2022+ assert rec.meeting_date.month == 3+ assert rec.meeting_date.day == 4+ _cleanup(rec, user)+++def test_upload_file_last_modified_used_for_meeting_date():+ with app.app_context():+ user = _mk_user("up_lm")+ client = app.test_client()+ _login(client, user)+ staging = os.path.join(app.config["UPLOAD_FOLDER"], f"stg_{uuid.uuid4().hex[:6]}")+ # 1577836800000 ms = 2020-01-01 UTC+ with _upload_mocks(staging):+ resp = _do_upload(client, file_last_modified="1577836800000")+ assert resp.status_code == 202+ rec = _latest_rec(user)+ assert rec.meeting_date.year == 2020+ _cleanup(rec, user)+++# --------------------------------------------------------------------------- #+# Upload — keep_audio_only+# --------------------------------------------------------------------------- #++def test_upload_keep_audio_only_flag_persisted():+ with app.app_context():+ user = _mk_user("up_kao")+ client = app.test_client()+ _login(client, user)+ staging = os.path.join(app.config["UPLOAD_FOLDER"], f"stg_{uuid.uuid4().hex[:6]}")+ with _upload_mocks(staging, has_video=True, audio_codec="aac"):+ resp = _do_upload(client, filename="movie.mp4", keep_audio_only="true")+ assert resp.status_code == 202+ rec = _latest_rec(user)+ assert rec.keep_audio_only is True+ _cleanup(rec, user)+++def test_upload_keep_audio_only_default_when_video_retention_off():+ with app.app_context(), patch("src.api.recordings.VIDEO_RETENTION", False):+ user = _mk_user("up_vroff")+ client = app.test_client()+ _login(client, user)+ staging = os.path.join(app.config["UPLOAD_FOLDER"], f"stg_{uuid.uuid4().hex[:6]}")+ with _upload_mocks(staging, has_video=True, audio_codec="aac"):+ resp = _do_upload(client, filename="movie.mp4")+ assert resp.status_code == 202+ rec = _latest_rec(user)+ # VIDEO_RETENTION off => effective_audio_only True even without flag+ assert rec.keep_audio_only is True+ _cleanup(rec, user)+++# --------------------------------------------------------------------------- #+# Upload — tag / folder association + per-upload transcription params+# --------------------------------------------------------------------------- #++def test_upload_with_tags_creates_associations_in_order():+ with app.app_context():+ user = _mk_user("up_tags")+ client = app.test_client()+ _login(client, user)+ tag1 = _mk_tag(user)+ tag2 = _mk_tag(user)+ staging = os.path.join(app.config["UPLOAD_FOLDER"], f"stg_{uuid.uuid4().hex[:6]}")+ with _upload_mocks(staging) as (storage, enqueue):+ resp = _do_upload(client, **{"tag_ids[0]": str(tag1.id),+ "tag_ids[1]": str(tag2.id)})+ assert resp.status_code == 202+ rec = _latest_rec(user)+ assocs = (RecordingTag.query.filter_by(recording_id=rec.id)+ .order_by(RecordingTag.order).all())+ assert [a.tag_id for a in assocs] == [tag1.id, tag2.id]+ assert [a.order for a in assocs] == [1, 2]+ # First tag id is passed to the job+ assert enqueue.call_args.kwargs["params"]["tag_id"] == tag1.id+ for a in assocs:+ db.session.delete(a)+ db.session.commit()+ _cleanup(rec, tag1, tag2, user)+++def test_upload_single_tag_id_backward_compat():+ with app.app_context():+ user = _mk_user("up_stag")+ client = app.test_client()+ _login(client, user)+ tag = _mk_tag(user)+ staging = os.path.join(app.config["UPLOAD_FOLDER"], f"stg_{uuid.uuid4().hex[:6]}")+ with _upload_mocks(staging):+ resp = _do_upload(client, tag_id=str(tag.id))+ assert resp.status_code == 202+ rec = _latest_rec(user)+ assocs = RecordingTag.query.filter_by(recording_id=rec.id).all()+ assert len(assocs) == 1 and assocs[0].tag_id == tag.id+ for a in assocs:+ db.session.delete(a)+ db.session.commit()+ _cleanup(rec, tag, user)+++def test_upload_other_users_tag_is_ignored():+ with app.app_context():+ owner = _mk_user("up_own")+ other = _mk_user("up_oth")+ foreign_tag = _mk_tag(other)+ client = app.test_client()+ _login(client, owner)+ staging = os.path.join(app.config["UPLOAD_FOLDER"], f"stg_{uuid.uuid4().hex[:6]}")+ with _upload_mocks(staging):+ resp = _do_upload(client, **{"tag_ids[0]": str(foreign_tag.id)})+ assert resp.status_code == 202+ rec = _latest_rec(owner)+ assert RecordingTag.query.filter_by(recording_id=rec.id).count() == 0+ _cleanup(rec, foreign_tag, owner, other)+++def test_upload_with_folder_sets_folder_id():+ with app.app_context():+ user = _mk_user("up_fld")+ client = app.test_client()+ _login(client, user)+ folder = _mk_folder(user)+ staging = os.path.join(app.config["UPLOAD_FOLDER"], f"stg_{uuid.uuid4().hex[:6]}")+ with _upload_mocks(staging):+ resp = _do_upload(client, folder_id=str(folder.id))+ assert resp.status_code == 202+ rec = _latest_rec(user)+ assert rec.folder_id == folder.id+ _cleanup(rec, folder, user)+++def test_upload_other_users_folder_ignored():+ with app.app_context():+ owner = _mk_user("up_fown")+ other = _mk_user("up_foth")+ foreign_folder = _mk_folder(other)+ client = app.test_client()+ _login(client, owner)+ staging = os.path.join(app.config["UPLOAD_FOLDER"], f"stg_{uuid.uuid4().hex[:6]}")+ with _upload_mocks(staging):+ resp = _do_upload(client, folder_id=str(foreign_folder.id))+ assert resp.status_code == 202+ rec = _latest_rec(owner)+ assert rec.folder_id is None+ _cleanup(rec, foreign_folder, owner, other)+++def test_upload_per_request_transcription_params_passed_to_job():+ with app.app_context():+ user = _mk_user("up_params")+ client = app.test_client()+ _login(client, user)+ staging = os.path.join(app.config["UPLOAD_FOLDER"], f"stg_{uuid.uuid4().hex[:6]}")+ with _upload_mocks(staging) as (storage, enqueue):+ resp = _do_upload(+ client,+ language="en",+ min_speakers="2",+ max_speakers="4",+ hotwords="acme, foo",+ initial_prompt="prior context",+ )+ assert resp.status_code == 202+ params = enqueue.call_args.kwargs["params"]+ assert params["language"] == "en"+ assert params["min_speakers"] == 2+ assert params["max_speakers"] == 4+ assert params["hotwords"] == "acme, foo"+ assert params["initial_prompt"] == "prior context"+ rec = _latest_rec(user)+ _cleanup(rec, user)+++def test_upload_invalid_speaker_counts_become_none():+ with app.app_context():+ user = _mk_user("up_badspk")+ client = app.test_client()+ _login(client, user)+ staging = os.path.join(app.config["UPLOAD_FOLDER"], f"stg_{uuid.uuid4().hex[:6]}")+ with _upload_mocks(staging) as (storage, enqueue):+ resp = _do_upload(client, min_speakers="abc", max_speakers="")+ assert resp.status_code == 202+ params = enqueue.call_args.kwargs["params"]+ assert params["min_speakers"] is None+ assert params["max_speakers"] is None+ rec = _latest_rec(user)+ _cleanup(rec, user)+++def test_upload_tag_defaults_applied_when_not_user_provided():+ with app.app_context():+ user = _mk_user("up_tagdef")+ client = app.test_client()+ _login(client, user)+ tag = _mk_tag(user, default_language="fr", default_hotwords="bonjour")+ staging = os.path.join(app.config["UPLOAD_FOLDER"], f"stg_{uuid.uuid4().hex[:6]}")+ with _upload_mocks(staging) as (storage, enqueue):+ resp = _do_upload(client, **{"tag_ids[0]": str(tag.id)})+ assert resp.status_code == 202+ params = enqueue.call_args.kwargs["params"]+ assert params["language"] == "fr"+ assert params["hotwords"] == "bonjour"+ rec = _latest_rec(user)+ for a in RecordingTag.query.filter_by(recording_id=rec.id).all():+ db.session.delete(a)+ db.session.commit()+ _cleanup(rec, tag, user)+++def test_upload_folder_defaults_applied_when_no_tags():+ with app.app_context():+ user = _mk_user("up_flddef")+ client = app.test_client()+ _login(client, user)+ folder = _mk_folder(user, default_language="de")+ staging = os.path.join(app.config["UPLOAD_FOLDER"], f"stg_{uuid.uuid4().hex[:6]}")+ with _upload_mocks(staging) as (storage, enqueue):+ resp = _do_upload(client, folder_id=str(folder.id))+ assert resp.status_code == 202+ params = enqueue.call_args.kwargs["params"]+ assert params["language"] == "de"+ rec = _latest_rec(user)+ _cleanup(rec, folder, user)+++def test_upload_prompt_variables_sanitized_and_stored():+ with app.app_context():+ user = _mk_user("up_pvar")+ client = app.test_client()+ _login(client, user)+ staging = os.path.join(app.config["UPLOAD_FOLDER"], f"stg_{uuid.uuid4().hex[:6]}")+ with _upload_mocks(staging):+ resp = _do_upload(client, prompt_variables='{"client_name": "Acme"}')+ assert resp.status_code == 202+ rec = _latest_rec(user)+ assert rec.prompt_variables == {"client_name": "Acme"}+ _cleanup(rec, user)+++def test_upload_invalid_prompt_variables_json_ignored():+ with app.app_context():+ user = _mk_user("up_pvbad")+ client = app.test_client()+ _login(client, user)+ staging = os.path.join(app.config["UPLOAD_FOLDER"], f"stg_{uuid.uuid4().hex[:6]}")+ with _upload_mocks(staging):+ resp = _do_upload(client, prompt_variables="{not json")+ assert resp.status_code == 202+ rec = _latest_rec(user)+ assert rec.prompt_variables is None+ _cleanup(rec, user)+++# --------------------------------------------------------------------------- #+# Upload — duplicate detection+# --------------------------------------------------------------------------- #++def test_upload_duplicate_hash_returns_warning():+ with app.app_context():+ user = _mk_user("up_dup")+ client = app.test_client()+ _login(client, user)+ staging = os.path.join(app.config["UPLOAD_FOLDER"], f"stg_{uuid.uuid4().hex[:6]}")+ payload = b"duplicate-bytes" * 500+ with _upload_mocks(staging):+ r1 = _do_upload(client, payload=payload, title="First")+ assert r1.status_code == 202+ first = _latest_rec(user)+ with _upload_mocks(staging):+ r2 = _do_upload(client, payload=payload, title="Second")+ assert r2.status_code == 202+ body = r2.get_json()+ assert "duplicate_warning" in body+ assert body["duplicate_warning"]["existing_recording_id"] == first.id+ second = _latest_rec(user)+ assert second.id != first.id+ _cleanup(second, first, user)+++# --------------------------------------------------------------------------- #+# Upload — validation rejects+# --------------------------------------------------------------------------- #++def test_upload_missing_file_returns_400():+ with app.app_context():+ user = _mk_user("up_nofile")+ client = app.test_client()+ _login(client, user)+ resp = client.post("/upload", data={"title": "x"},+ content_type="multipart/form-data")+ assert resp.status_code == 400+ assert "No file provided" in resp.get_json()["error"]+ _cleanup(user)+++def test_upload_empty_filename_returns_400():+ with app.app_context():+ user = _mk_user("up_emptyfn")+ client = app.test_client()+ _login(client, user)+ data = {"file": (io.BytesIO(b"abc"), "")}+ resp = client.post("/upload", data=data, content_type="multipart/form-data")+ assert resp.status_code == 400+ assert "No file selected" in resp.get_json()["error"]+ _cleanup(user)+++def test_upload_too_large_returns_413():+ with app.app_context():+ user = _mk_user("up_big")+ client = app.test_client()+ _login(client, user)+ staging = os.path.join(app.config["UPLOAD_FOLDER"], f"stg_{uuid.uuid4().hex[:6]}")+ # Force a tiny per-request limit so a small payload trips it.+ with _upload_mocks(staging), \+ patch.object(SystemSetting, "get_setting",+ side_effect=lambda k, d=None: 0 if k == "max_file_size_mb" else d), \+ patch("src.api.recordings.chunking_service", None):+ resp = _do_upload(client, payload=b"x" * 8192)+ assert resp.status_code == 413+ body = resp.get_json()+ assert "max_size_mb" in body+ # No recording row created for the rejected upload+ assert Recording.query.filter_by(user_id=user.id).count() == 0+ _cleanup(user)+++def test_upload_requires_login():+ with app.app_context():+ client = app.test_client() # not logged in+ resp = _do_upload(client)+ # @login_required redirects (302) or 401+ assert resp.status_code in (302, 401)+++def test_upload_ffmpeg_error_returns_500():+ with app.app_context():+ user = _mk_user("up_ffmpeg")+ client = app.test_client()+ _login(client, user)+ staging = os.path.join(app.config["UPLOAD_FOLDER"], f"stg_{uuid.uuid4().hex[:6]}")+ from src.utils.ffmpeg_utils import FFmpegError+ fake_storage = _FakeStorage(staging)+ with patch("src.api.recordings.get_storage_service", return_value=fake_storage), \+ patch("src.api.recordings.get_codec_info",+ return_value={"has_video": False, "audio_codec": "flac"}), \+ patch("src.api.recordings.convert_if_needed",+ side_effect=FFmpegError("boom")), \+ patch("src.services.job_queue.job_queue.enqueue", return_value=1):+ resp = _do_upload(client, filename="a.flac")+ assert resp.status_code == 500+ # An error payload is returned; the exact wording (convert-specific vs+ # the generic upload handler) is an implementation detail.+ assert resp.get_json().get("error")+ _cleanup(user)+++# --------------------------------------------------------------------------- #+# /save metadata edit+# --------------------------------------------------------------------------- #++def test_save_updates_title_notes_participants():+ with app.app_context():+ user = _mk_user("sv_ok")+ client = app.test_client()+ _login(client, user)+ rec = _mk_recording(user, title="old")+ resp = client.post("/save", json={+ "id": rec.id, "title": "new title",+ "participants": "Alice, Bob", "notes": "edited notes",+ })+ assert resp.status_code == 200+ db.session.refresh(rec)+ assert rec.title == "new title"+ assert rec.participants == "Alice, Bob"+ assert "edited notes" in (rec.notes or "")+ _cleanup(rec, user)+++def test_save_meeting_date_date_only():+ with app.app_context():+ user = _mk_user("sv_date")+ client = app.test_client()+ _login(client, user)+ rec = _mk_recording(user)+ resp = client.post("/save", json={"id": rec.id, "meeting_date": "2021-07-08"})+ assert resp.status_code == 200+ db.session.refresh(rec)+ assert rec.meeting_date.year == 2021+ assert rec.meeting_date.month == 7+ assert rec.meeting_date.day == 8+ _cleanup(rec, user)+++def test_save_no_data_returns_400():+ with app.app_context():+ user = _mk_user("sv_nd")+ client = app.test_client()+ _login(client, user)+ resp = client.post("/save", json={})+ assert resp.status_code == 400+ _cleanup(user)+++def test_save_missing_id_returns_400():+ with app.app_context():+ user = _mk_user("sv_nid")+ client = app.test_client()+ _login(client, user)+ resp = client.post("/save", json={"title": "x"})+ assert resp.status_code == 400+ _cleanup(user)+++def test_save_nonexistent_returns_404():+ with app.app_context():+ user = _mk_user("sv_404")+ client = app.test_client()+ _login(client, user)+ resp = client.post("/save", json={"id": 99999999, "title": "x"})+ assert resp.status_code == 404+ _cleanup(user)+++def test_save_other_users_recording_forbidden():+ with app.app_context():+ owner = _mk_user("sv_own")+ other = _mk_user("sv_oth")+ rec = _mk_recording(owner, title="keep")+ client = app.test_client()+ _login(client, other)+ resp = client.post("/save", json={"id": rec.id, "title": "hacked"})+ assert resp.status_code == 403+ db.session.refresh(rec)+ assert rec.title == "keep"+ _cleanup(rec, owner, other)+++# --------------------------------------------------------------------------- #+# update_transcription+# --------------------------------------------------------------------------- #++def test_update_transcription_sets_field():+ with app.app_context():+ user = _mk_user("ut_ok")+ client = app.test_client()+ _login(client, user)+ rec = _mk_recording(user)+ resp = client.post(f"/recording/{rec.id}/update_transcription",+ json={"transcription": "new transcript text"})+ assert resp.status_code == 200+ db.session.refresh(rec)+ assert rec.transcription == "new transcript text"+ _cleanup(rec, user)+++def test_update_transcription_missing_data_400():+ with app.app_context():+ user = _mk_user("ut_nd")+ client = app.test_client()+ _login(client, user)+ rec = _mk_recording(user)+ resp = client.post(f"/recording/{rec.id}/update_transcription", json={})+ assert resp.status_code == 400+ _cleanup(rec, user)+++def test_update_transcription_nonexistent_404():+ with app.app_context():+ user = _mk_user("ut_404")+ client = app.test_client()+ _login(client, user)+ resp = client.post("/recording/99999999/update_transcription",+ json={"transcription": "x"})+ assert resp.status_code == 404+ _cleanup(user)+++def test_update_transcription_forbidden_for_other_user():+ with app.app_context():+ owner = _mk_user("ut_own")+ other = _mk_user("ut_oth")+ rec = _mk_recording(owner, transcription="orig")+ client = app.test_client()+ _login(client, other)+ resp = client.post(f"/recording/{rec.id}/update_transcription",+ json={"transcription": "tamper"})+ assert resp.status_code == 403+ db.session.refresh(rec)+ assert rec.transcription == "orig"+ _cleanup(rec, owner, other)+++# --------------------------------------------------------------------------- #+# toggle inbox / highlight+# --------------------------------------------------------------------------- #++def test_toggle_inbox_flips_status():+ with app.app_context():+ user = _mk_user("tib")+ client = app.test_client()+ _login(client, user)+ rec = _mk_recording(user)+ r1 = client.post(f"/recording/{rec.id}/toggle_inbox")+ assert r1.status_code == 200+ first = r1.get_json()["is_inbox"]+ r2 = client.post(f"/recording/{rec.id}/toggle_inbox")+ assert r2.status_code == 200+ assert r2.get_json()["is_inbox"] != first+ _cleanup(rec, user)+++def test_toggle_highlight_flips_status():+ with app.app_context():+ user = _mk_user("thl")+ client = app.test_client()+ _login(client, user)+ rec = _mk_recording(user)+ r1 = client.post(f"/recording/{rec.id}/toggle_highlight")+ assert r1.status_code == 200+ first = r1.get_json()["is_highlighted"]+ r2 = client.post(f"/recording/{rec.id}/toggle_highlight")+ assert r2.status_code == 200+ assert r2.get_json()["is_highlighted"] != first+ _cleanup(rec, user)+++def test_toggle_inbox_nonexistent_404():+ with app.app_context():+ user = _mk_user("tib404")+ client = app.test_client()+ _login(client, user)+ resp = client.post("/recording/99999999/toggle_inbox")+ assert resp.status_code == 404+ _cleanup(user)+++# --------------------------------------------------------------------------- #+# Tag add / remove / reorder+# --------------------------------------------------------------------------- #++def test_add_tag_to_recording():+ with app.app_context():+ user = _mk_user("at_ok")+ client = app.test_client()+ _login(client, user)+ rec = _mk_recording(user)+ tag = _mk_tag(user)+ resp = client.post(f"/api/recordings/{rec.id}/tags", json={"tag_id": tag.id})+ assert resp.status_code == 200+ assert RecordingTag.query.filter_by(recording_id=rec.id, tag_id=tag.id).count() == 1+ for a in RecordingTag.query.filter_by(recording_id=rec.id).all():+ db.session.delete(a)+ db.session.commit()+ _cleanup(rec, tag, user)+++def test_add_tag_missing_tag_id_400():+ with app.app_context():+ user = _mk_user("at_nid")+ client = app.test_client()+ _login(client, user)+ rec = _mk_recording(user)+ resp = client.post(f"/api/recordings/{rec.id}/tags", json={})+ assert resp.status_code == 400+ _cleanup(rec, user)+++def test_add_tag_duplicate_400():+ with app.app_context():+ user = _mk_user("at_dup")+ client = app.test_client()+ _login(client, user)+ rec = _mk_recording(user)+ tag = _mk_tag(user)+ db.session.add(RecordingTag(recording_id=rec.id, tag_id=tag.id, order=1))+ db.session.commit()+ resp = client.post(f"/api/recordings/{rec.id}/tags", json={"tag_id": tag.id})+ assert resp.status_code == 400+ for a in RecordingTag.query.filter_by(recording_id=rec.id).all():+ db.session.delete(a)+ db.session.commit()+ _cleanup(rec, tag, user)+++def test_add_foreign_personal_tag_forbidden():+ with app.app_context():+ owner = _mk_user("at_own")+ other = _mk_user("at_oth")+ rec = _mk_recording(owner)+ foreign_tag = _mk_tag(other)+ client = app.test_client()+ _login(client, owner)+ resp = client.post(f"/api/recordings/{rec.id}/tags",+ json={"tag_id": foreign_tag.id})+ assert resp.status_code == 403+ _cleanup(rec, foreign_tag, owner, other)+++def test_remove_tag_from_recording():+ with app.app_context():+ user = _mk_user("rt_ok")+ client = app.test_client()+ _login(client, user)+ rec = _mk_recording(user)+ tag = _mk_tag(user)+ db.session.add(RecordingTag(recording_id=rec.id, tag_id=tag.id, order=1))+ db.session.commit()+ resp = client.delete(f"/api/recordings/{rec.id}/tags/{tag.id}")+ assert resp.status_code == 200+ assert RecordingTag.query.filter_by(recording_id=rec.id, tag_id=tag.id).count() == 0+ _cleanup(rec, tag, user)+++def test_remove_tag_not_on_recording_404():+ with app.app_context():+ user = _mk_user("rt_404")+ client = app.test_client()+ _login(client, user)+ rec = _mk_recording(user)+ tag = _mk_tag(user)+ resp = client.delete(f"/api/recordings/{rec.id}/tags/{tag.id}")+ assert resp.status_code == 404+ _cleanup(rec, tag, user)+++def test_reorder_recording_tags():+ with app.app_context():+ user = _mk_user("ro_ok")+ client = app.test_client()+ _login(client, user)+ rec = _mk_recording(user)+ t1 = _mk_tag(user)+ t2 = _mk_tag(user)+ db.session.add(RecordingTag(recording_id=rec.id, tag_id=t1.id, order=1))+ db.session.add(RecordingTag(recording_id=rec.id, tag_id=t2.id, order=2))+ db.session.commit()+ resp = client.put(f"/api/recordings/{rec.id}/tags/reorder",+ json={"tag_ids": [t2.id, t1.id]})+ assert resp.status_code == 200+ a1 = RecordingTag.query.filter_by(recording_id=rec.id, tag_id=t1.id).first()+ a2 = RecordingTag.query.filter_by(recording_id=rec.id, tag_id=t2.id).first()+ assert a2.order == 1 and a1.order == 2+ for a in RecordingTag.query.filter_by(recording_id=rec.id).all():+ db.session.delete(a)+ db.session.commit()+ _cleanup(rec, t1, t2, user)+++def test_reorder_missing_tag_ids_400():+ with app.app_context():+ user = _mk_user("ro_400")+ client = app.test_client()+ _login(client, user)+ rec = _mk_recording(user)+ resp = client.put(f"/api/recordings/{rec.id}/tags/reorder", json={})+ assert resp.status_code == 400+ _cleanup(rec, user)+++def test_reorder_forbidden_for_other_user():+ with app.app_context():+ owner = _mk_user("ro_own")+ other = _mk_user("ro_oth")+ rec = _mk_recording(owner)+ client = app.test_client()+ _login(client, other)+ resp = client.put(f"/api/recordings/{rec.id}/tags/reorder",+ json={"tag_ids": []})+ assert resp.status_code == 403+ _cleanup(rec, owner, other)+++# --------------------------------------------------------------------------- #+# bulk-tags+# --------------------------------------------------------------------------- #++def test_bulk_tags_add_and_remove():+ with app.app_context():+ user = _mk_user("bt_ok")+ client = app.test_client()+ _login(client, user)+ rec1 = _mk_recording(user)+ rec2 = _mk_recording(user)+ tag = _mk_tag(user)+ # Add+ resp = client.post("/api/recordings/bulk-tags", json={+ "recording_ids": [rec1.id, rec2.id], "tag_id": tag.id, "action": "add"})+ assert resp.status_code == 200+ assert set(resp.get_json()["affected_ids"]) == {rec1.id, rec2.id}+ assert RecordingTag.query.filter_by(tag_id=tag.id, recording_id=rec1.id).count() == 1+ # Remove+ resp2 = client.post("/api/recordings/bulk-tags", json={+ "recording_ids": [rec1.id], "tag_id": tag.id, "action": "remove"})+ assert resp2.status_code == 200+ assert RecordingTag.query.filter_by(tag_id=tag.id, recording_id=rec1.id).count() == 0+ for a in RecordingTag.query.filter(+ RecordingTag.recording_id.in_([rec1.id, rec2.id])).all():+ db.session.delete(a)+ db.session.commit()+ _cleanup(rec1, rec2, tag, user)+++def test_bulk_tags_missing_args_400():+ with app.app_context():+ user = _mk_user("bt_400")+ client = app.test_client()+ _login(client, user)+ resp = client.post("/api/recordings/bulk-tags", json={"recording_ids": []})+ assert resp.status_code == 400+ _cleanup(user)+++def test_bulk_tags_invalid_action_400():+ with app.app_context():+ user = _mk_user("bt_act")+ client = app.test_client()+ _login(client, user)+ rec = _mk_recording(user)+ tag = _mk_tag(user)+ resp = client.post("/api/recordings/bulk-tags", json={+ "recording_ids": [rec.id], "tag_id": tag.id, "action": "frobnicate"})+ assert resp.status_code == 400+ _cleanup(rec, tag, user)+++def test_bulk_tags_nonexistent_tag_404():+ with app.app_context():+ user = _mk_user("bt_404t")+ client = app.test_client()+ _login(client, user)+ rec = _mk_recording(user)+ resp = client.post("/api/recordings/bulk-tags", json={+ "recording_ids": [rec.id], "tag_id": 99999999, "action": "add"})+ assert resp.status_code == 404+ _cleanup(rec, user)+++# --------------------------------------------------------------------------- #+# _resolve_transcription_model helper+# --------------------------------------------------------------------------- #++def test_resolve_transcription_model_no_allowlist_accepts():+ from src.api.recordings import _resolve_transcription_model+ with app.app_context(), \+ patch("src.config.app_config.TRANSCRIPTION_MODELS_AVAILABLE", []), \+ patch.object(SystemSetting, "get_setting", return_value=None):+ assert _resolve_transcription_model("whisper-large") == "whisper-large"+++def test_resolve_transcription_model_falls_back_to_admin_default():+ from src.api.recordings import _resolve_transcription_model+ with app.app_context(), \+ patch.object(SystemSetting, "get_setting",+ side_effect=lambda k, d=None:+ "admin-default" if k == "transcription_default_model" else None):+ assert _resolve_transcription_model("") == "admin-default"+ assert _resolve_transcription_model(None) == "admin-default"+++def test_resolve_transcription_model_drops_value_not_in_allowlist():+ from src.api.recordings import _resolve_transcription_model+ with app.app_context(), \+ patch("src.config.app_config.TRANSCRIPTION_MODELS_AVAILABLE", ["model-a"]), \+ patch.object(SystemSetting, "get_setting", return_value=None):+ assert _resolve_transcription_model("model-b") is None+ assert _resolve_transcription_model("model-a") == "model-a"+++def test_resolve_transcription_model_accepts_from_db_visible_list():+ from src.api.recordings import _resolve_transcription_model+ import json as _json++ def _get(k, d=None):+ if k == "transcription_models_visible_json":+ return _json.dumps([{"value": "vis-model"}, "plain-model"])+ return None+ with app.app_context(), \+ patch("src.config.app_config.TRANSCRIPTION_MODELS_AVAILABLE", []), \+ patch.object(SystemSetting, "get_setting", side_effect=_get):+ assert _resolve_transcription_model("vis-model") == "vis-model"+ assert _resolve_transcription_model("plain-model") == "plain-model"+ assert _resolve_transcription_model("nope") is Nonediff --git a/tests/test_cov_retention.py b/tests/test_cov_retention.pynew file mode 100644index 00000000..35ff0a79--- /dev/null+++ b/tests/test_cov_retention.py@@ -0,0 +1,317 @@+#!/usr/bin/env python3+"""+Coverage tests for src/services/retention.py.++This module performs data-destructive auto-deletion of recordings/audio by+retention policy, so correctness matters. All external side effects (storage+byte deletion, speaker cleanup) are mocked so tests are hermetic and offline.++Strategy:+- Create User/Recording/Tag rows directly via models.+- Patch module-level policy constants (ENABLE_AUTO_DELETION, GLOBAL_RETENTION_DAYS,+ DELETION_MODE) since retention.py reads them as module globals.+- Patch retention.get_storage_service so no real disk/storage I/O happens, and+ assert the deleter is called with the correct locator.+"""++import os+import sys+import uuid+import unittest+from datetime import datetime, timedelta+from unittest.mock import patch, MagicMock++sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))++from src.app import app, db+from src.models import User, Recording, Tag, RecordingTag+from src.services import retention+++def _make_storage_mock(exists=True):+ """Build a fake storage service whose .exists()/.delete() are observable."""+ storage = MagicMock()+ storage.exists.return_value = exists+ return storage+++class RetentionTestBase(unittest.TestCase):+ def setUp(self):+ self.ctx = app.app_context()+ self.ctx.push()+ suffix = uuid.uuid4().hex[:8]+ self.user = User(+ username=f"ret_{suffix}",+ email=f"ret_{suffix}@local.test",+ password="fakehash",+ )+ db.session.add(self.user)+ db.session.commit()+ # process_auto_deletion() operates on ALL recordings globally, and the+ # pytest session DB is shared across test files. Wipe any recordings+ # left behind by other suites so this class's global stat-counter+ # assertions are deterministic regardless of run order.+ RecordingTag.query.delete()+ Recording.query.delete()+ db.session.commit()+ self._created_recordings = []+ self._created_tags = []++ def tearDown(self):+ db.session.rollback()+ # Remove any recordings still present (full-delete tests may already remove them)+ for rec in self._created_recordings:+ obj = db.session.get(Recording, rec) if isinstance(rec, int) else rec+ if obj is not None and db.session.get(Recording, obj.id) is not None:+ RecordingTag.query.filter_by(recording_id=obj.id).delete()+ db.session.delete(obj)+ for tag in self._created_tags:+ obj = db.session.get(Tag, tag.id)+ if obj is not None:+ db.session.delete(obj)+ db.session.commit()+ db.session.delete(self.user)+ db.session.commit()+ self.ctx.pop()++ def make_recording(self, age_days=100, status='COMPLETED', audio_path='user/a.mp3',+ deletion_exempt=False, audio_deleted_at=None):+ rec = Recording(+ user_id=self.user.id,+ title="rec",+ status=status,+ audio_path=audio_path,+ deletion_exempt=deletion_exempt,+ audio_deleted_at=audio_deleted_at,+ )+ db.session.add(rec)+ db.session.flush()+ # created_at has a default, so override after flush to set the age+ rec.created_at = datetime.utcnow() - timedelta(days=age_days)+ db.session.commit()+ self._created_recordings.append(rec)+ return rec++ def make_tag(self, retention_days=None, protect_from_deletion=False):+ tag = Tag(+ name=f"tag_{uuid.uuid4().hex[:8]}",+ user_id=self.user.id,+ retention_days=retention_days,+ protect_from_deletion=protect_from_deletion,+ )+ db.session.add(tag)+ db.session.flush()+ self._created_tags.append(tag)+ return tag++ def tag_recording(self, recording, tag):+ assoc = RecordingTag(recording_id=recording.id, tag_id=tag.id, order=0)+ db.session.add(assoc)+ db.session.commit()+++# ---------------------------------------------------------------------------+# is_recording_exempt_from_deletion+# ---------------------------------------------------------------------------++class TestExemption(RetentionTestBase):+ def test_manual_deletion_exempt_flag(self):+ rec = self.make_recording(deletion_exempt=True)+ self.assertTrue(retention.is_recording_exempt_from_deletion(rec))++ def test_protect_from_deletion_tag(self):+ rec = self.make_recording()+ tag = self.make_tag(protect_from_deletion=True)+ self.tag_recording(rec, tag)+ self.assertTrue(retention.is_recording_exempt_from_deletion(rec))++ def test_retention_days_minus_one_is_protected(self):+ rec = self.make_recording()+ tag = self.make_tag(retention_days=-1)+ self.tag_recording(rec, tag)+ self.assertTrue(retention.is_recording_exempt_from_deletion(rec))++ def test_not_exempt_by_default(self):+ rec = self.make_recording()+ self.assertFalse(retention.is_recording_exempt_from_deletion(rec))+++# ---------------------------------------------------------------------------+# get_retention_days_for_recording+# ---------------------------------------------------------------------------++class TestRetentionDays(RetentionTestBase):+ def test_no_tags_no_global_returns_none(self):+ rec = self.make_recording()+ with patch.object(retention, 'GLOBAL_RETENTION_DAYS', 0):+ self.assertIsNone(retention.get_retention_days_for_recording(rec))++ def test_global_retention_used_when_no_tags(self):+ rec = self.make_recording()+ with patch.object(retention, 'GLOBAL_RETENTION_DAYS', 30):+ self.assertEqual(retention.get_retention_days_for_recording(rec), 30)++ def test_tag_retention_overrides_global(self):+ rec = self.make_recording()+ tag = self.make_tag(retention_days=7)+ self.tag_recording(rec, tag)+ with patch.object(retention, 'GLOBAL_RETENTION_DAYS', 30):+ self.assertEqual(retention.get_retention_days_for_recording(rec), 7)++ def test_shortest_tag_retention_wins(self):+ rec = self.make_recording()+ t1 = self.make_tag(retention_days=20)+ t2 = self.make_tag(retention_days=5)+ self.tag_recording(rec, t1)+ self.tag_recording(rec, t2)+ with patch.object(retention, 'GLOBAL_RETENTION_DAYS', 30):+ self.assertEqual(retention.get_retention_days_for_recording(rec), 5)++ def test_minus_one_tag_ignored_falls_to_global(self):+ # retention_days == -1 is skipped here (handled by exemption), so global applies+ rec = self.make_recording()+ tag = self.make_tag(retention_days=-1)+ self.tag_recording(rec, tag)+ with patch.object(retention, 'GLOBAL_RETENTION_DAYS', 30):+ self.assertEqual(retention.get_retention_days_for_recording(rec), 30)+++# ---------------------------------------------------------------------------+# process_auto_deletion+# ---------------------------------------------------------------------------++class TestProcessAutoDeletion(RetentionTestBase):+ def _run(self, enabled=True, global_days=30, mode='full_recording', storage=None):+ if storage is None:+ storage = _make_storage_mock(exists=True)+ with patch.object(retention, 'ENABLE_AUTO_DELETION', enabled), \+ patch.object(retention, 'GLOBAL_RETENTION_DAYS', global_days), \+ patch.object(retention, 'DELETION_MODE', mode), \+ patch.object(retention, 'get_storage_service', return_value=storage), \+ patch('src.services.speaker_cleanup.cleanup_orphaned_speakers',+ return_value={'speakers_deleted': 0, 'embeddings_removed': 0,+ 'speakers_evaluated': 0}):+ stats = retention.process_auto_deletion()+ return stats, storage++ def test_disabled_returns_error(self):+ stats, _ = self._run(enabled=False)+ self.assertEqual(stats, {'error': 'Auto-deletion is not enabled'})++ def test_full_delete_removes_old_recording_and_audio(self):+ rec = self.make_recording(age_days=100, audio_path='user/old.mp3')+ rec_id = rec.id+ stats, storage = self._run(mode='full_recording', global_days=30)+ self.assertEqual(stats['deleted_full'], 1)+ # Row is gone+ self.assertIsNone(db.session.get(Recording, rec_id))+ # Audio bytes deletion invoked with the right locator+ storage.delete.assert_called_with('user/old.mp3', missing_ok=True)++ def test_recording_within_retention_kept(self):+ rec = self.make_recording(age_days=5, audio_path='user/new.mp3')+ rec_id = rec.id+ stats, storage = self._run(mode='full_recording', global_days=30)+ self.assertEqual(stats['deleted_full'], 0)+ self.assertIsNotNone(db.session.get(Recording, rec_id))+ storage.delete.assert_not_called()++ def test_exempt_recording_skipped(self):+ rec = self.make_recording(age_days=100, deletion_exempt=True)+ rec_id = rec.id+ stats, storage = self._run(mode='full_recording', global_days=30)+ self.assertEqual(stats['exempted'], 1)+ self.assertEqual(stats['deleted_full'], 0)+ self.assertIsNotNone(db.session.get(Recording, rec_id))+ storage.delete.assert_not_called()++ def test_no_retention_policy_skips_recording(self):+ rec = self.make_recording(age_days=100)+ rec_id = rec.id+ # global_days=0 and no tag retention => no policy applies+ stats, storage = self._run(mode='full_recording', global_days=0)+ self.assertEqual(stats['skipped_no_retention'], 1)+ self.assertEqual(stats['deleted_full'], 0)+ self.assertIsNotNone(db.session.get(Recording, rec_id))++ def test_audio_only_mode_deletes_audio_keeps_row(self):+ rec = self.make_recording(age_days=100, audio_path='user/keep.mp3')+ rec_id = rec.id+ stats, storage = self._run(mode='audio_only', global_days=30)+ self.assertEqual(stats['deleted_audio_only'], 1)+ # Row preserved+ row = db.session.get(Recording, rec_id)+ self.assertIsNotNone(row)+ # Audio marked deleted and bytes removed+ self.assertIsNotNone(row.audio_deleted_at)+ storage.delete.assert_called_with('user/keep.mp3', missing_ok=True)++ def test_audio_only_skips_already_deleted_audio(self):+ # In audio_only mode the query filters out audio_deleted_at != None,+ # so this recording is not even checked.+ rec = self.make_recording(+ age_days=100, audio_path='user/gone.mp3',+ audio_deleted_at=datetime.utcnow(),+ )+ stats, storage = self._run(mode='audio_only', global_days=30)+ self.assertEqual(stats['checked'], 0)+ self.assertEqual(stats['deleted_audio_only'], 0)+ storage.delete.assert_not_called()++ def test_audio_only_missing_file_marks_timestamp_without_delete(self):+ rec = self.make_recording(age_days=100, audio_path='user/missing.mp3')+ rec_id = rec.id+ storage = _make_storage_mock(exists=False) # storage.exists -> False+ stats, storage = self._run(mode='audio_only', global_days=30, storage=storage)+ # Falls into the "already deleted or doesn't exist" branch+ self.assertEqual(stats['deleted_audio_only'], 0)+ row = db.session.get(Recording, rec_id)+ self.assertIsNotNone(row.audio_deleted_at)+ storage.delete.assert_not_called()++ def test_full_mode_completes_prior_audio_only_deletion(self):+ # Recording whose audio was already deleted (audio_only earlier);+ # full mode should still remove the row.+ rec = self.make_recording(+ age_days=100, audio_path='user/x.mp3',+ audio_deleted_at=datetime.utcnow(),+ )+ rec_id = rec.id+ stats, storage = self._run(mode='full_recording', global_days=30)+ self.assertEqual(stats['deleted_full'], 1)+ self.assertIsNone(db.session.get(Recording, rec_id))++ def test_no_eligible_recordings_is_noop(self):+ # Only a young recording exists; nothing deleted.+ rec = self.make_recording(age_days=1, audio_path='user/young.mp3')+ rec_id = rec.id+ stats, storage = self._run(mode='full_recording', global_days=30)+ self.assertEqual(stats['deleted_full'], 0)+ self.assertEqual(stats['deleted_audio_only'], 0)+ self.assertIsNotNone(db.session.get(Recording, rec_id))+ storage.delete.assert_not_called()++ def test_tag_retention_drives_deletion_over_global(self):+ # Global says keep 365 days, but a 1-day tag forces deletion of a 100-day recording.+ rec = self.make_recording(age_days=100, audio_path='user/tagged.mp3')+ rec_id = rec.id+ tag = self.make_tag(retention_days=1)+ self.tag_recording(rec, tag)+ stats, storage = self._run(mode='full_recording', global_days=365)+ self.assertEqual(stats['deleted_full'], 1)+ self.assertIsNone(db.session.get(Recording, rec_id))++ def test_error_during_deletion_increments_errors_and_rolls_back(self):+ rec = self.make_recording(age_days=100, audio_path='user/boom.mp3')+ rec_id = rec.id+ storage = _make_storage_mock(exists=True)+ storage.delete.side_effect = RuntimeError("storage down")+ stats, storage = self._run(mode='full_recording', global_days=30, storage=storage)+ self.assertEqual(stats['errors'], 1)+ self.assertEqual(stats['deleted_full'], 0)+ # Recording survives because the transaction was rolled back+ self.assertIsNotNone(db.session.get(Recording, rec_id))+++if __name__ == "__main__":+ unittest.main(verbosity=2)diff --git a/tests/test_cov_storage_s3.py b/tests/test_cov_storage_s3.pynew file mode 100644index 00000000..5d01ae91--- /dev/null+++ b/tests/test_cov_storage_s3.py@@ -0,0 +1,416 @@+"""Unit tests for the S3/MinIO storage backend (src/services/storage/s3.py).++All tests run fully offline using a mocked boto3 client. No real network/AWS.+"""++import os+import tempfile+from datetime import datetime+from unittest.mock import MagicMock, patch++import pytest+from botocore.exceptions import ClientError++from src.services.storage.s3 import S3StorageBackend+from src.services.storage.interfaces import (+ MaterializedFile,+ ObjectStat,+ StorageLocator,+ StoredObject,+)+++# ---------------------------------------------------------------------------+# Helpers+# ---------------------------------------------------------------------------++def make_backend(**overrides):+ kwargs = dict(bucket='mybucket', region='us-east-1')+ kwargs.update(overrides)+ return S3StorageBackend(**kwargs)+++def patch_client(backend, fake_client):+ """Patch _get_client so it returns the provided fake client."""+ return patch.object(+ S3StorageBackend, '_get_client', return_value=fake_client+ )+++def make_head_response(size=123, etag='"abc123"', content_type='audio/mpeg',+ last_modified=None):+ return {+ 'ContentLength': size,+ 'ETag': etag,+ 'ContentType': content_type,+ 'LastModified': last_modified or datetime(2024, 1, 1, 12, 0, 0),+ }+++def make_client_error(code='404', http_status=404, operation='HeadObject'):+ response = {+ 'Error': {'Code': str(code), 'Message': 'boom'},+ 'ResponseMetadata': {'HTTPStatusCode': http_status},+ }+ return ClientError(response, operation)+++def s3_locator(bucket='mybucket', key='audio/file.mp3'):+ return StorageLocator(+ scheme='s3',+ raw=f's3://{bucket}/{key}',+ bucket=bucket,+ key=key,+ )+++# ---------------------------------------------------------------------------+# build_locator+# ---------------------------------------------------------------------------++def test_build_locator():+ backend = make_backend()+ assert backend.build_locator('audio/file.mp3') == 's3://mybucket/audio/file.mp3'+++def test_build_locator_normalizes_key():+ backend = make_backend()+ # leading slashes + doubled slashes get normalized away+ assert backend.build_locator('/audio//file.mp3') == 's3://mybucket/audio/file.mp3'+++# ---------------------------------------------------------------------------+# _bucket_key+# ---------------------------------------------------------------------------++def test_bucket_key_uses_locator_bucket():+ backend = make_backend()+ bucket, key = backend._bucket_key(s3_locator(bucket='otherbucket', key='k/v.mp3'))+ assert bucket == 'otherbucket'+ assert key == 'k/v.mp3'+++def test_bucket_key_falls_back_to_backend_bucket():+ backend = make_backend()+ loc = StorageLocator(scheme='s3', raw='s3:///k', bucket=None, key='k')+ bucket, key = backend._bucket_key(loc)+ assert bucket == 'mybucket'+ assert key == 'k'+++def test_bucket_key_bad_scheme_raises():+ backend = make_backend()+ loc = StorageLocator(scheme='local', raw='local://k', key='k')+ with pytest.raises(ValueError, match='Unsupported locator'):+ backend._bucket_key(loc)+++def test_bucket_key_missing_key_raises():+ backend = make_backend()+ loc = StorageLocator(scheme='s3', raw='s3://mybucket/', bucket='mybucket', key=None)+ with pytest.raises(ValueError, match='missing bucket or key'):+ backend._bucket_key(loc)+++def test_bucket_key_missing_bucket_raises():+ backend = make_backend(bucket='')+ loc = StorageLocator(scheme='s3', raw='s3:///k', bucket=None, key='k')+ with pytest.raises(ValueError, match='missing bucket or key'):+ backend._bucket_key(loc)+++# ---------------------------------------------------------------------------+# save_fileobj+# ---------------------------------------------------------------------------++def test_save_fileobj_with_content_type_and_metadata():+ backend = make_backend()+ fake = MagicMock()+ fake.head_object.return_value = make_head_response(size=42, etag='"deadbeef"',+ content_type='audio/wav')+ fileobj = MagicMock()+ with patch_client(backend, fake):+ result = backend.save_fileobj(+ fileobj, 'audio/x.wav', content_type='audio/wav',+ metadata={'owner': 'u1'},+ )+ fake.upload_fileobj.assert_called_once_with(+ fileobj, 'mybucket', 'audio/x.wav',+ ExtraArgs={'ContentType': 'audio/wav', 'Metadata': {'owner': 'u1'}},+ )+ assert isinstance(result, StoredObject)+ assert result.locator == 's3://mybucket/audio/x.wav'+ assert result.key == 'audio/x.wav'+ assert result.size == 42+ assert result.etag == 'deadbeef'+ assert result.content_type == 'audio/wav'+++def test_save_fileobj_without_extra_args():+ backend = make_backend()+ fake = MagicMock()+ fake.head_object.return_value = make_head_response()+ fileobj = MagicMock()+ with patch_client(backend, fake):+ backend.save_fileobj(fileobj, 'audio/y.mp3')+ fake.upload_fileobj.assert_called_once_with(fileobj, 'mybucket', 'audio/y.mp3')+++# ---------------------------------------------------------------------------+# upload_local_file+# ---------------------------------------------------------------------------++def test_upload_local_file_with_content_type():+ backend = make_backend()+ fake = MagicMock()+ fake.head_object.return_value = make_head_response(size=10)+ with patch_client(backend, fake):+ result = backend.upload_local_file(+ '/tmp/src.mp3', 'audio/z.mp3', content_type='audio/mpeg',+ metadata={'a': 'b'},+ )+ fake.upload_file.assert_called_once_with(+ '/tmp/src.mp3', 'mybucket', 'audio/z.mp3',+ ExtraArgs={'ContentType': 'audio/mpeg', 'Metadata': {'a': 'b'}},+ )+ assert result.key == 'audio/z.mp3'+ assert result.size == 10+++def test_upload_local_file_without_extra_args():+ backend = make_backend()+ fake = MagicMock()+ fake.head_object.return_value = make_head_response()+ with patch_client(backend, fake):+ backend.upload_local_file('/tmp/src.mp3', 'audio/z.mp3')+ fake.upload_file.assert_called_once_with('/tmp/src.mp3', 'mybucket', 'audio/z.mp3')+++def test_upload_local_file_delete_source_removes_file():+ backend = make_backend()+ fake = MagicMock()+ fake.head_object.return_value = make_head_response()+ fd, tmp_path = tempfile.mkstemp(prefix='speakr_test_src_')+ os.close(fd)+ assert os.path.exists(tmp_path)+ with patch_client(backend, fake):+ backend.upload_local_file(tmp_path, 'audio/del.mp3', delete_source=True)+ assert not os.path.exists(tmp_path)+++def test_upload_local_file_delete_source_missing_file_ignored():+ backend = make_backend()+ fake = MagicMock()+ fake.head_object.return_value = make_head_response()+ missing = '/tmp/definitely_not_here_speakr_xyz.mp3'+ with patch_client(backend, fake):+ # should not raise even though the file doesn't exist+ backend.upload_local_file(missing, 'audio/del.mp3', delete_source=True)+++# ---------------------------------------------------------------------------+# exists+# ---------------------------------------------------------------------------++def test_exists_true_when_head_succeeds():+ backend = make_backend()+ fake = MagicMock()+ fake.head_object.return_value = make_head_response()+ with patch_client(backend, fake):+ assert backend.exists(s3_locator()) is True++[email protected]('code,http_status', [+ ('404', 404),+ ('NoSuchKey', 200),+ ('NotFound', 200),+ ('SomethingElse', 404), # 404 status code path+])+def test_exists_false_on_not_found(code, http_status):+ backend = make_backend()+ fake = MagicMock()+ fake.head_object.side_effect = make_client_error(code=code, http_status=http_status)+ with patch_client(backend, fake):+ assert backend.exists(s3_locator()) is False+++def test_exists_reraises_other_client_errors():+ backend = make_backend()+ fake = MagicMock()+ fake.head_object.side_effect = make_client_error(code='AccessDenied', http_status=403)+ with patch_client(backend, fake):+ with pytest.raises(ClientError):+ backend.exists(s3_locator())+++# ---------------------------------------------------------------------------+# delete+# ---------------------------------------------------------------------------++def test_delete_calls_delete_object():+ backend = make_backend()+ fake = MagicMock()+ with patch_client(backend, fake):+ result = backend.delete(s3_locator(key='audio/del.mp3'))+ fake.delete_object.assert_called_once_with(Bucket='mybucket', Key='audio/del.mp3')+ assert result is True+++# ---------------------------------------------------------------------------+# stat+# ---------------------------------------------------------------------------++def test_stat_maps_fields():+ backend = make_backend()+ fake = MagicMock()+ lm = datetime(2023, 5, 6, 7, 8, 9)+ fake.head_object.return_value = make_head_response(+ size=999, etag='"quoted-etag"', content_type='audio/ogg', last_modified=lm,+ )+ with patch_client(backend, fake):+ stat = backend.stat(s3_locator(key='audio/s.ogg'))+ fake.head_object.assert_called_once_with(Bucket='mybucket', Key='audio/s.ogg')+ assert isinstance(stat, ObjectStat)+ assert stat.size == 999+ assert stat.etag == 'quoted-etag' # surrounding quotes stripped+ assert stat.content_type == 'audio/ogg'+ assert stat.last_modified == lm+++def test_stat_handles_missing_etag():+ backend = make_backend()+ fake = MagicMock()+ fake.head_object.return_value = {+ 'ContentLength': 5,+ 'ContentType': 'application/octet-stream',+ 'LastModified': datetime(2024, 1, 1),+ }+ with patch_client(backend, fake):+ stat = backend.stat(s3_locator())+ assert stat.etag is None+ assert stat.size == 5+++# ---------------------------------------------------------------------------+# materialize+# ---------------------------------------------------------------------------++def test_materialize_downloads_to_temp_and_flags_cleanup():+ backend = make_backend()+ fake = MagicMock()+ with patch_client(backend, fake):+ result = backend.materialize(s3_locator(key='audio/m.flac'))+ assert isinstance(result, MaterializedFile)+ assert result.cleanup_required is True+ assert result.local_path.endswith('.flac')+ fake.download_file.assert_called_once()+ args, kwargs = fake.download_file.call_args+ assert args[0] == 'mybucket'+ assert args[1] == 'audio/m.flac'+ assert args[2] == result.local_path+ # the temp file was created by mkstemp; clean up after the test+ if os.path.exists(result.local_path):+ os.remove(result.local_path)+++# ---------------------------------------------------------------------------+# presign_get_url+# ---------------------------------------------------------------------------++def test_presign_get_url_basic():+ backend = make_backend()+ fake = MagicMock()+ fake.generate_presigned_url.return_value = 'https://signed.example/url'+ with patch_client(backend, fake):+ url = backend.presign_get_url(s3_locator(key='audio/p.mp3'), 600)+ assert url == 'https://signed.example/url'+ fake.generate_presigned_url.assert_called_once_with(+ 'get_object',+ Params={'Bucket': 'mybucket', 'Key': 'audio/p.mp3'},+ ExpiresIn=600,+ )+++def test_presign_get_url_with_response_overrides():+ backend = make_backend()+ fake = MagicMock()+ fake.generate_presigned_url.return_value = 'https://signed.example/url2'+ with patch_client(backend, fake):+ backend.presign_get_url(+ s3_locator(key='audio/p.mp3'),+ expires_seconds=120,+ response_content_type='audio/mpeg',+ response_content_disposition='attachment; filename="p.mp3"',+ )+ fake.generate_presigned_url.assert_called_once_with(+ 'get_object',+ Params={+ 'Bucket': 'mybucket',+ 'Key': 'audio/p.mp3',+ 'ResponseContentType': 'audio/mpeg',+ 'ResponseContentDisposition': 'attachment; filename="p.mp3"',+ },+ ExpiresIn=120,+ )+++# ---------------------------------------------------------------------------+# _get_client config wiring (patch boto3.client and inspect kwargs)+# ---------------------------------------------------------------------------++def test_get_client_path_style_addressing():+ backend = make_backend(+ endpoint_url='http://minio:9000',+ access_key_id='AK',+ secret_access_key='SK',+ session_token='ST',+ use_path_style=True,+ verify_ssl=False,+ )+ fake_boto_client = MagicMock()+ fake_config_instance = MagicMock()+ with patch('boto3.client', return_value=fake_boto_client) as mock_client, \+ patch('botocore.config.Config', return_value=fake_config_instance) as mock_config:+ client = backend._get_client()++ assert client is fake_boto_client+ kwargs = mock_client.call_args.kwargs+ assert kwargs['service_name'] == 's3'+ assert kwargs['verify'] is False+ assert kwargs['region_name'] == 'us-east-1'+ assert kwargs['endpoint_url'] == 'http://minio:9000'+ assert kwargs['aws_access_key_id'] == 'AK'+ assert kwargs['aws_secret_access_key'] == 'SK'+ assert kwargs['aws_session_token'] == 'ST'+ assert kwargs['config'] is fake_config_instance+ config_kwargs = mock_config.call_args.kwargs+ assert config_kwargs['signature_version'] == 's3v4'+ assert config_kwargs['s3'] == {'addressing_style': 'path'}+++def test_get_client_virtual_addressing_and_minimal_kwargs():+ backend = S3StorageBackend(bucket='b') # no region/endpoint/creds+ fake_boto_client = MagicMock()+ fake_config_instance = MagicMock()+ with patch('boto3.client', return_value=fake_boto_client) as mock_client, \+ patch('botocore.config.Config', return_value=fake_config_instance) as mock_config:+ backend._get_client()++ kwargs = mock_client.call_args.kwargs+ assert kwargs['verify'] is True+ assert 'region_name' not in kwargs+ assert 'endpoint_url' not in kwargs+ assert 'aws_access_key_id' not in kwargs+ config_kwargs = mock_config.call_args.kwargs+ assert config_kwargs['s3'] == {'addressing_style': 'auto'}+++def test_get_client_is_cached():+ backend = make_backend()+ fake_boto_client = MagicMock()+ with patch('boto3.client', return_value=fake_boto_client) as mock_client, \+ patch('botocore.config.Config', return_value=MagicMock()):+ first = backend._get_client()+ second = backend._get_client()+ assert first is second+ mock_client.assert_called_once()