Commit 841bed29 authored by Aryan Noshirvani's avatar Aryan Noshirvani

Add remix engine tests, fix stale mock test, run CI on dev pushes

- tests/test_remix.py: exercises the /remix API contract (validation,
  job bookkeeping, 404 for unknown jobs) with identify_audio and the
  actual render pipeline mocked out - deliberately not running real
  Demucs/Gemini in CI, that's proven manually above.
- Removed test_music.py's test for the old /music/remix/batch mock
  stub, which no longer exists.
- test_social.py used a hardcoded "valid_token_xyz" cookie that the
  previous commit's auth rework removed; switched it to HTTP Basic
  using the configured DOCS_USERNAME/DOCS_PASSWORD.
- conftest.py sets safe default DOCS_USERNAME/DOCS_PASSWORD for the
  test run specifically, so tests don't depend on CI having real
  secrets configured.
- .gitlab-ci.yml: install CPU-only torch before requirements.txt
  (otherwise pip pulls a multi-GB CUDA build for nothing), and run the
  test stage on pushes to dev too, not just main/merge_requests.
- Adds the dev-instance systemd unit (deploy/systemd/, mirrors how
  stax_bot.service is tracked) for reference.
Co-Authored-By: 's avatarClaude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BCxXmJVvDFJVHTUi4QgNzi
parent f02cc53e
...@@ -16,11 +16,15 @@ test_api: ...@@ -16,11 +16,15 @@ test_api:
image: python:3.10-slim image: python:3.10-slim
before_script: before_script:
- apt-get update && apt-get install -y --no-install-recommends ffmpeg git - apt-get update && apt-get install -y --no-install-recommends ffmpeg git
# CPU-only torch first - this repo has no GPU anywhere, and the default PyPI
# wheel is a multi-GB CUDA build that would blow out CI time/disk for nothing.
- pip install --index-url https://download.pytorch.org/whl/cpu torch
- pip install --prefer-binary -r requirements.txt pytest - pip install --prefer-binary -r requirements.txt pytest
script: script:
- PYTHONPATH=. pytest tests/ - PYTHONPATH=. pytest tests/
only: only:
- main - main
- dev
- merge_requests - merge_requests
deploy_prod: deploy_prod:
......
[Unit]
Description=Stax Backend API Service (dev branch)
After=network.target
[Service]
User=root
WorkingDirectory=/root/stax_backend_api-dev
ExecStart=/root/stax_backend_api-dev/venv/bin/uvicorn src.api.main:app --host 0.0.0.0 --port 8001
Restart=always
RestartSec=3
Environment=PYTHONPATH=/root/stax_backend_api-dev
Environment=HTTP_PROXY=http://127.0.0.1:8118
Environment=HTTPS_PROXY=http://127.0.0.1:8118
Environment=http_proxy=http://127.0.0.1:8118
Environment=https_proxy=http://127.0.0.1:8118
Environment=NO_PROXY=127.0.0.1,localhost
Environment=no_proxy=127.0.0.1,localhost
[Install]
WantedBy=multi-user.target
import os
os.environ.setdefault("DOCS_USERNAME", "test-docs-user")
os.environ.setdefault("DOCS_PASSWORD", "test-docs-password")
import pytest import pytest
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from src.api.main import app from src.api.main import app
......
...@@ -7,17 +7,6 @@ def test_music_tag_missing_fields(client): ...@@ -7,17 +7,6 @@ def test_music_tag_missing_fields(client):
response = client.post("/music/tag", json={"chat_id": 1}) response = client.post("/music/tag", json={"chat_id": 1})
assert response.status_code == 422 assert response.status_code == 422
def test_batch_remix_endpoint(client):
response = client.post("/music/remix/batch", json={"files": [{"chat_id": 1, "message_id": 2}], "prompt": "slowed"})
assert response.status_code == 200
assert response.json()["status"] == "accepted"
job_id = response.json()["job_id"]
# check status
status_res = client.get(f"/music/remix/status/{job_id}")
assert status_res.status_code == 200
assert status_res.json()["status"] == "processing"
def test_music_upload_no_file(client): def test_music_upload_no_file(client):
response = client.post("/music/upload") response = client.post("/music/upload")
assert response.status_code == 422 assert response.status_code == 422
import io
from src.api.routers import remix as remix_router
def _wav_bytes():
# A tiny valid WAV file is enough - the engine itself is mocked out below,
# these tests only exercise the API contract (validation, job bookkeeping).
import struct
sample_rate = 8000
samples = b"\x00\x00" * sample_rate # 1 second of silence, 16-bit mono
data_size = len(samples)
header = struct.pack(
"<4sI4s4sIHHIIHH4sI",
b"RIFF", 36 + data_size, b"WAVE", b"fmt ", 16, 1, 1,
sample_rate, sample_rate * 2, 2, 16, b"data", data_size,
)
return header + samples
def test_remix_requires_at_least_one_file(client):
response = client.post("/remix", data={"prompt": "make it slower"}, files=[])
assert response.status_code == 422
async def _fake_identify_audio(path):
return []
def test_remix_rejects_more_than_five_songs(client, monkeypatch):
monkeypatch.setattr(remix_router, "render_remix_sync", lambda *a, **kw: None)
monkeypatch.setattr(remix_router, "identify_audio", _fake_identify_audio)
files = [("files", (f"song{i}.wav", io.BytesIO(_wav_bytes()), "audio/wav")) for i in range(6)]
response = client.post("/remix", data={"prompt": "mashup"}, files=files)
assert response.status_code == 400
def test_remix_accepts_job_and_reports_status(client, monkeypatch):
captured = {}
def fake_render(job_id, input_paths, prompt, songs_meta):
captured["job_id"] = job_id
captured["input_paths"] = input_paths
captured["prompt"] = prompt
captured["songs_meta"] = songs_meta
monkeypatch.setattr(remix_router, "render_remix_sync", fake_render)
monkeypatch.setattr(remix_router, "identify_audio", _fake_identify_audio)
files = [
("files", ("song_a.wav", io.BytesIO(_wav_bytes()), "audio/wav")),
("files", ("song_b.wav", io.BytesIO(_wav_bytes()), "audio/wav")),
]
response = client.post("/remix", data={"prompt": "blend the vocals of the first over the beat of the second"}, files=files)
assert response.status_code == 200
body = response.json()
assert body["status"] == "queued"
job_id = body["job_id"]
status_response = client.get(f"/remix/{job_id}")
assert status_response.status_code == 200
assert status_response.json()["job_id"] == job_id
assert captured["job_id"] == job_id
assert len(captured["input_paths"]) == 2
assert len(captured["songs_meta"]) == 2
def test_remix_status_404_for_unknown_job(client):
response = client.get("/remix/999999")
assert response.status_code == 404
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from src.api.main import app from src.api.main import app
from src.core.config import DOCS_PASSWORD, DOCS_USERNAME
client = TestClient(app) client = TestClient(app)
def test_social_router_exists(): def test_social_router_exists():
response = client.get("/openapi.json", cookies={"session_token": "valid_token_xyz"}) response = client.get("/openapi.json", auth=(DOCS_USERNAME, DOCS_PASSWORD))
assert response.status_code == 200 assert response.status_code == 200
assert "/social/posts" in response.json()["paths"] assert "/social/posts" in response.json()["paths"]
assert "/social/geo-drop" in response.json()["paths"] assert "/social/geo-drop" in response.json()["paths"]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment