feat: comprehensive test infrastructure improvements
- Fix unit test imports: add env setup in conftest.py before module imports - Add 24 new auth router tests (RBAC, preferences, password validation) - Add 16 new tests for litellm and chat_summary routers - Apply black formatting and ruff linting across codebase - Add pre-commit hooks configuration (black, ruff, file checks) - Increase CI coverage threshold from 40% to 50% Test Results: - 206 tests passing (91 unit + 115 integration) - Coverage: 58.79% on core modules - auth.py: 57% → 85%, litellm.py: 23% → 87%, chat_summary.py: 41% → 100% - auth_service: 96.51%, config: 100%, rbac: 93.48%
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
import asyncio
|
||||
import struct
|
||||
import logging
|
||||
import shlex
|
||||
import jwt
|
||||
import struct
|
||||
from collections import Counter
|
||||
from fastapi import WebSocket, Query
|
||||
|
||||
import asyncssh
|
||||
import config
|
||||
import jwt
|
||||
from fastapi import Query, WebSocket
|
||||
|
||||
import auth_service as auth
|
||||
import config
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -61,16 +63,18 @@ def build_host_command(host_config: dict) -> str | None:
|
||||
f"bash -l -c 'cd /repos/nas-tools && tmux new-session -A -s {quoted_session}'"
|
||||
)
|
||||
|
||||
script = " ".join([
|
||||
"if",
|
||||
f"{docker_bin} exec {quoted_container} tmux has-session -t {quoted_session} >/dev/null 2>&1;",
|
||||
"then",
|
||||
f"printf '%sattached\\n' {quoted_marker};",
|
||||
"else",
|
||||
f"printf '%screated\\n' {quoted_marker};",
|
||||
"fi;",
|
||||
f"exec {attach_cmd}",
|
||||
])
|
||||
script = " ".join(
|
||||
[
|
||||
"if",
|
||||
f"{docker_bin} exec {quoted_container} tmux has-session -t {quoted_session} >/dev/null 2>&1;",
|
||||
"then",
|
||||
f"printf '%sattached\\n' {quoted_marker};",
|
||||
"else",
|
||||
f"printf '%screated\\n' {quoted_marker};",
|
||||
"fi;",
|
||||
f"exec {attach_cmd}",
|
||||
]
|
||||
)
|
||||
return f"bash -lc {shlex.quote(script)}"
|
||||
|
||||
|
||||
@@ -140,15 +144,17 @@ async def ws_endpoint(websocket: WebSocket, host: str = Query("nas")):
|
||||
try:
|
||||
conn = await asyncio.wait_for(
|
||||
asyncssh.connect(
|
||||
h["host"], username=h["user"],
|
||||
client_keys=[h["key"]], known_hosts=_known_hosts,
|
||||
h["host"],
|
||||
username=h["user"],
|
||||
client_keys=[h["key"]],
|
||||
known_hosts=_known_hosts,
|
||||
keepalive_interval=15,
|
||||
keepalive_count_max=8,
|
||||
),
|
||||
timeout=10.0
|
||||
timeout=10.0,
|
||||
)
|
||||
logger.info("SSH connection established to %s", host)
|
||||
except asyncio.TimeoutError:
|
||||
except TimeoutError:
|
||||
logger.error("SSH connection to %s timed out after 10s", host)
|
||||
await _release_session(session_id)
|
||||
await websocket.close(code=1011, reason="SSH connection timeout")
|
||||
@@ -161,7 +167,9 @@ async def ws_endpoint(websocket: WebSocket, host: str = Query("nas")):
|
||||
|
||||
try:
|
||||
process = await conn.create_process(
|
||||
build_host_command(h), term_type="xterm-256color", term_size=(80, 24),
|
||||
build_host_command(h),
|
||||
term_type="xterm-256color",
|
||||
term_size=(80, 24),
|
||||
encoding=None,
|
||||
)
|
||||
|
||||
@@ -212,7 +220,7 @@ async def ws_endpoint(websocket: WebSocket, host: str = Query("nas")):
|
||||
read_task.cancel()
|
||||
try:
|
||||
await asyncio.wait_for(read_task, timeout=2.0)
|
||||
except (asyncio.TimeoutError, asyncio.CancelledError):
|
||||
except (TimeoutError, asyncio.CancelledError):
|
||||
pass
|
||||
process.close()
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user