feat: auto npm install in dev mode to sync dependencies (#94)

This commit is contained in:
Zihao Xu
2026-01-24 17:19:44 -08:00
committed by GitHub
parent 8af763531b
commit a2f2010ee5
2 changed files with 104 additions and 0 deletions

76
tests/test_dev_mode.py Normal file
View File

@@ -0,0 +1,76 @@
"""Tests for dev mode functionality in the server."""
import subprocess
from unittest.mock import patch, MagicMock
import pytest
class TestEnsureNpmDependencies:
"""Tests for ensure_npm_dependencies function."""
def test_npm_install_success_unix(self):
"""Test npm install succeeds on Unix-like systems."""
from src.server.main import ensure_npm_dependencies
with patch("platform.system", return_value="Darwin"), \
patch("subprocess.run") as mock_run:
mock_run.return_value = MagicMock(returncode=0)
result = ensure_npm_dependencies("/fake/web/dir")
assert result is True
mock_run.assert_called_once_with(
["npm", "install"],
cwd="/fake/web/dir",
shell=False,
check=True
)
def test_npm_install_success_windows(self):
"""Test npm install succeeds on Windows."""
from src.server.main import ensure_npm_dependencies
with patch("platform.system", return_value="Windows"), \
patch("subprocess.run") as mock_run:
mock_run.return_value = MagicMock(returncode=0)
result = ensure_npm_dependencies("/fake/web/dir")
assert result is True
mock_run.assert_called_once_with(
"npm install",
cwd="/fake/web/dir",
shell=True,
check=True
)
def test_npm_install_failure_returns_false(self):
"""Test npm install failure returns False but doesn't raise."""
from src.server.main import ensure_npm_dependencies
with patch("platform.system", return_value="Darwin"), \
patch("subprocess.run") as mock_run:
mock_run.side_effect = subprocess.CalledProcessError(1, "npm install")
result = ensure_npm_dependencies("/fake/web/dir")
assert result is False
def test_npm_install_linux(self):
"""Test npm install on Linux uses same path as macOS."""
from src.server.main import ensure_npm_dependencies
with patch("platform.system", return_value="Linux"), \
patch("subprocess.run") as mock_run:
mock_run.return_value = MagicMock(returncode=0)
result = ensure_npm_dependencies("/fake/web/dir")
assert result is True
# Linux should use the same non-shell approach as macOS.
mock_run.assert_called_once_with(
["npm", "install"],
cwd="/fake/web/dir",
shell=False,
check=True
)