From a2e5c6413cf34ca0923dc384a89965293a4a4598 Mon Sep 17 00:00:00 2001 From: Mahdi Ashrafee Date: Sun, 26 Jul 2026 05:49:05 +0600 Subject: [PATCH 1/3] fix: fix test suite import paths, env loading, and rate limit conflicts --- app/__init__.py | 0 app/api/analyze.py | 10 +++++----- app/config.py | 4 +++- app/main.py | 2 +- app/services/ai.py | 4 ++-- app/services/rate_limit.py | 2 +- app/services/risk.py | 4 ++-- pyproject.toml | 3 +++ tests/test_main.py | 12 ++++-------- 9 files changed, 21 insertions(+), 20 deletions(-) create mode 100644 app/__init__.py diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/api/analyze.py b/app/api/analyze.py index c096493..f9f5e2e 100644 --- a/app/api/analyze.py +++ b/app/api/analyze.py @@ -1,11 +1,11 @@ import os import filetype from fastapi import APIRouter, HTTPException, WebSocketException, UploadFile, WebSocket, WebSocketDisconnect, Request -from services import rate_limit -from models.response import riskAssessment -from models.request import information -from services.risk import get_assessment -from services.transcription import audio_transcript +from app.services import rate_limit +from app.models.response import riskAssessment +from app.models.request import information +from app.services.risk import get_assessment +from app.services.transcription import audio_transcript router = APIRouter() diff --git a/app/config.py b/app/config.py index 2378915..c2e261a 100644 --- a/app/config.py +++ b/app/config.py @@ -1,8 +1,10 @@ import os from openai import OpenAI +from dotenv import load_dotenv -MAX_REQUEST_LIMIT = 2 +load_dotenv() +MAX_REQUEST_LIMIT = 10 RATE_LIMIT_WINDOW = 60 client = OpenAI( diff --git a/app/main.py b/app/main.py index ef9e48f..e73ad84 100644 --- a/app/main.py +++ b/app/main.py @@ -1,5 +1,5 @@ from fastapi import FastAPI -from api import analyze +from app.api import analyze # if item.sender in r.get("numbers"): # return diff --git a/app/services/ai.py b/app/services/ai.py index 1eee44c..eb4f407 100644 --- a/app/services/ai.py +++ b/app/services/ai.py @@ -1,7 +1,7 @@ import json from typing import Any -from services.filter import filter_sensitive -from config import client +from app.services.filter import filter_sensitive +from app.config import client def ask_deepseek(prompt: str) -> dict[str, Any] | None: diff --git a/app/services/rate_limit.py b/app/services/rate_limit.py index ab3a5b6..333f4db 100644 --- a/app/services/rate_limit.py +++ b/app/services/rate_limit.py @@ -1,5 +1,5 @@ import redis -from config import MAX_REQUEST_LIMIT,RATE_LIMIT_WINDOW +from app.config import MAX_REQUEST_LIMIT,RATE_LIMIT_WINDOW import time r = redis.Redis(host='localhost', port=6379, decode_responses=True) diff --git a/app/services/risk.py b/app/services/risk.py index ca7d55d..b188538 100644 --- a/app/services/risk.py +++ b/app/services/risk.py @@ -1,8 +1,8 @@ from pydantic import ValidationError -from models.response import riskAssessment -from services.ai import ask_deepseek +from app.models.response import riskAssessment +from app.services.ai import ask_deepseek def get_assessment(transcription: str) -> riskAssessment | None: diff --git a/pyproject.toml b/pyproject.toml index 095f1de..9d18301 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,3 +13,6 @@ dependencies = [ "openai-whisper>=20250625", "redis>=8.0.1", ] +[tool.pytest.ini_options] +pythonpath = ["."] +testpaths = ["tests"] diff --git a/tests/test_main.py b/tests/test_main.py index b39fa37..40105d6 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -41,14 +41,10 @@ def audio(): yield audio.read() -@pytest.mark.asyncio -async def test1(audio): - url = "ws://localhost:8000/ws" - async with connect(url) as websockets: - await websockets.send(audio) - response = await websockets.recv() - result = json.loads(response) - +def test1(audio): + with client.websocket_connect("/ws") as websocket: + websocket.send_bytes(audio) + result = websocket.receive_json() assert "label" in result assert "score" in result assert "certainty" in result From 986d7a9d33f4bea292985a9ab27b233d376d52ad Mon Sep 17 00:00:00 2001 From: Mahdi Ashrafee Date: Sun, 26 Jul 2026 06:35:19 +0600 Subject: [PATCH 2/3] fix: change fixture name --- tests/test_main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_main.py b/tests/test_main.py index 3b78327..7b82c35 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -33,14 +33,14 @@ def test_audio_check(): @pytest.fixture -def audio(): +def audio_bytes(): with open(r"tests/test_audio.m4a", "rb") as audio: yield audio.read() -def test1(audiofile): +def test1(audio_bytes): with client.websocket_connect("/ws") as websocket: - websocket.send_bytes(audiofile) + websocket.send_bytes(audio_bytes) result = websocket.receive_json() assert "label" in result assert "score" in result From e4b4fd6631084816c50ea918a27c94a137ddc80f Mon Sep 17 00:00:00 2001 From: Mahdi Ashrafee Date: Sun, 26 Jul 2026 06:45:41 +0600 Subject: [PATCH 3/3] chore: add scripts/check.sh to run tests and lint locally --- scripts/check.sh | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100755 scripts/check.sh diff --git a/scripts/check.sh b/scripts/check.sh new file mode 100755 index 0000000..4ba2210 --- /dev/null +++ b/scripts/check.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +echo "Running tests..." +uv run pytest + +echo "Running lint..." +uv run pylint app tests