Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
CI: run full pytest + vitest suite on push/PR with isolated test DB
Murtaza Nasir385b4ca05c56
diff --git a/.github/workflows/test.yml b/.github/workflows/test.ymlnew file mode 100644index 00000000..fa1a0781--- /dev/null+++ b/.github/workflows/test.yml@@ -0,0 +1,77 @@+name: Tests++# Runs the full automated suite on every push and pull request. The existing+# docker-publish.yml only ran two migration tests on release, so nothing+# guarded day-to-day changes; this workflow gives the ~378 Python tests + the+# Vitest JS tests real per-commit regression protection.+#+# Syntax is standard GitHub Actions and also runs on Forgejo Actions+# (Forgejo reads .github/workflows by default). The Python suite isolates+# itself to a throwaway SQLite DB via conftest.py, so it needs no app config.++on:+ push:+ pull_request:+ workflow_dispatch:++jobs:+ python-tests:+ runs-on: ubuntu-latest++ services:+ postgres:+ image: postgres:16+ env:+ POSTGRES_USER: speakr+ POSTGRES_PASSWORD: speakr+ POSTGRES_DB: speakr_test+ ports:+ - 5432:5432+ options: >-+ --health-cmd "pg_isready -U speakr"+ --health-interval 10s+ --health-timeout 5s+ --health-retries 5++ steps:+ - name: Checkout repository+ uses: actions/checkout@v5++ - name: Set up Python+ uses: actions/setup-python@v6+ with:+ python-version: '3.11'+ cache: pip++ - name: Install ffmpeg (needed by stitch / ffprobe tests)+ run: sudo apt-get update && sudo apt-get install -y ffmpeg++ - name: Install dependencies+ run: pip install -r requirements.txt pytest++ - name: Run test suite+ env:+ # The app tests run against an isolated SQLite DB (see conftest.py).+ # 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++ js-tests:+ runs-on: ubuntu-latest++ steps:+ - name: Checkout repository+ uses: actions/checkout@v5++ - name: Set up Node+ uses: actions/setup-node@v4+ with:+ node-version: '20'+ cache: npm++ - name: Install dependencies+ run: npm ci++ - name: Run Vitest+ run: npm testdiff --git a/conftest.py b/conftest.pynew file mode 100644index 00000000..7bcd9159--- /dev/null+++ b/conftest.py@@ -0,0 +1,58 @@+"""Pytest configuration for the Speakr test suite.++This file is imported by pytest BEFORE it collects any test module, so the+environment it sets here is in place before any `from src.app import app`+binds the Flask app to a database.++Goal: isolation. Point the app at a throwaway temporary SQLite database so+the test suite never reads or mutates the developer's real+`instance/transcriptions.db`. Running the suite with `pytest` therefore+needs no services and is safe on any machine / in CI.++Note: the legacy standalone invocation (`python tests/test_foo.py`) does NOT+load conftest.py, so this changes nothing for that workflow — it only affects+`pytest` runs.+"""++import os+import tempfile++# --- Isolated database (set BEFORE src.app is imported) ---------------------+# Use a real file (not :memory:) because the app and tests open multiple+# connections / app contexts, which an in-memory SQLite DB would not share.+_TEST_DIR = tempfile.mkdtemp(prefix="speakr_pytest_")+_TEST_DB_PATH = os.path.join(_TEST_DIR, "test.db")+os.environ["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{_TEST_DB_PATH}"++# The app os.makedirs(UPLOAD_FOLDER) at import time; default is /data/uploads+# which isn't writable in CI / outside Docker. Point it at the temp dir.+os.environ["UPLOAD_FOLDER"] = os.path.join(_TEST_DIR, "uploads")++# --- Quiet, deterministic defaults for tests --------------------------------+# Only set if the caller hasn't already chosen a value.+os.environ.setdefault("SECRET_KEY", "pytest-secret-key")+os.environ.setdefault("ENABLE_AUTO_PROCESSING", "false") # no black-hole file monitor+os.environ.setdefault("TEXT_MODEL_API_KEY", "test-key") # avoid config hard-fails+# NB: do NOT force WEBHOOK_GLOBAL_ENABLED=false here — the webhook suite needs+# delivery enabled (it mocks the actual HTTP POST), so leave the default (true).++import pytest # noqa: E402++[email protected](scope="session", autouse=True)+def _initialize_test_database():+ """Create the full schema in the throwaway DB once per session."""+ from src.app import app+ from src.init_db import initialize_database++ with app.app_context():+ initialize_database(app)++ yield++ # Best-effort cleanup of the temp DB file after the session.+ for path in (_TEST_DB_PATH, _TEST_DB_PATH + "-wal", _TEST_DB_PATH + "-shm"):+ try:+ os.remove(path)+ except OSError:+ passdiff --git a/tests/test_csrf_token_bypass_fix.py b/tests/test_csrf_token_bypass_fix.pyindex b8df47de..45364593 100644--- a/tests/test_csrf_token_bypass_fix.py+++ b/tests/test_csrf_token_bypass_fix.py@@ -424,9 +424,13 @@ def test_api_v1_post_with_query_string_token_still_works():db.session.add(rec)db.session.commit()- # Pure-query-string authentication on a v1 POST endpoint+ # Pure-query-string authentication on a v1 POST endpoint.+ # The v1 route delegates to src.api.recordings.regenerate_title,+ # which calls _generate_ai_title (a real LLM call). Mock that so+ # the test exercises the auth/CSRF path without needing a live LLM+ # — otherwise it returns 500 in CI and masks what we're asserting.from unittest.mock import patch- with patch('src.api.api_v1._enqueue_title_regeneration', create=True), \+ with patch('src.tasks.processing._generate_ai_title', return_value='Mock Title'), \patch('src.services.job_queue.job_queue.enqueue', return_value=999):resp = client.post(f'/api/v1/recordings/{rec.id}/regenerate_title?token={plaintext}',