chore: simplify dev port lookup and isolate settings tests

Resolve the shared backend port once for make dev and reuse it in the child make. Keep pure configuration tests independent of local credentials and URLs without altering integration database settings.
This commit is contained in:
hungtran24 2026-10-02 11:58:19 +07:00
parent 8082a9f968
commit d5e096b952
5 changed files with 18 additions and 2 deletions

View File

@ -14,7 +14,7 @@ install: ## Sync project-local dependencies from both lockfiles
cd apps/web && bun install --frozen-lockfile cd apps/web && bun install --frozen-lockfile
dev: ## Start PostgreSQL and run the native backend and web together dev: ## Start PostgreSQL and run the native backend and web together
$(MAKE) --jobs=2 backend-dev web-dev $(MAKE) --jobs=2 backend-dev web-dev BACKEND_PORT=$(BACKEND_PORT)
db-up: ## Start local PostgreSQL and wait until ready db-up: ## Start local PostgreSQL and wait until ready
docker compose up -d --wait postgres docker compose up -d --wait postgres

View File

@ -1,4 +1,5 @@
import asyncio import asyncio
import os
import time import time
from collections.abc import Callable, Iterator from collections.abc import Callable, Iterator
from typing import Any from typing import Any
@ -19,6 +20,15 @@ from ai_platform.infrastructure.persistence.models import AIProfile, AppCredenti
from ai_platform.platform.security import digest from ai_platform.platform.security import digest
@pytest.fixture
def isolated_settings(monkeypatch: pytest.MonkeyPatch) -> None:
"""Keep pure configuration tests independent of operator credentials and local URLs."""
monkeypatch.setitem(Settings.model_config, "env_file", None)
for name in tuple(os.environ):
if name.startswith("AI_PLATFORM_"):
monkeypatch.delenv(name)
@pytest.fixture @pytest.fixture
def valid_run_payload(): def valid_run_payload():
return { return {

View File

@ -4,6 +4,7 @@ from pydantic import SecretStr, ValidationError
from ai_platform.infrastructure.config.settings import Settings from ai_platform.infrastructure.config.settings import Settings
_TEST_DATABASE_URL = "postgresql+asyncpg://test:test@localhost/ai_platform_test" _TEST_DATABASE_URL = "postgresql+asyncpg://test:test@localhost/ai_platform_test"
pytestmark = pytest.mark.usefixtures("isolated_settings")
@pytest.mark.parametrize( @pytest.mark.parametrize(

View File

@ -35,7 +35,9 @@ def test_dev_uses_the_same_backend_port_for_both_servers(
(tmp_path / ".env").write_text(config) (tmp_path / ".env").write_text(config)
# Only the port lookup executes during dry-run; bypass uv project discovery in this fixture. # Only the port lookup executes during dry-run; bypass uv project discovery in this fixture.
uv = tmp_path / "uv" uv = tmp_path / "uv"
uv.write_text(f'#!/bin/sh\nshift 3\nexec {shlex.quote(sys.executable)} "$@"\n') uv.write_text(
f'#!/bin/sh\necho port-lookup >&2\nshift 3\nexec {shlex.quote(sys.executable)} "$@"\n'
)
uv.chmod(0o700) uv.chmod(0o700)
environment = os.environ.copy() environment = os.environ.copy()
for name in ("BACKEND_PORT", "MAKEFLAGS", "MFLAGS", "MAKEOVERRIDES"): for name in ("BACKEND_PORT", "MAKEFLAGS", "MFLAGS", "MAKEOVERRIDES"):
@ -62,3 +64,5 @@ def test_dev_uses_the_same_backend_port_for_both_servers(
assert "alembic" not in result.stdout assert "alembic" not in result.stdout
assert secret not in result.stdout + result.stderr assert secret not in result.stdout + result.stderr
assert "AI_PLATFORM_OPENAI_API_KEY" not in result.stdout + result.stderr assert "AI_PLATFORM_OPENAI_API_KEY" not in result.stdout + result.stderr
expected_lookups = 0 if environment_port is not None or argument_port is not None else 1
assert result.stderr.count("port-lookup") == expected_lookups

View File

@ -27,6 +27,7 @@ NOW = datetime(2026, 9, 28, tzinfo=UTC)
SECRET = "test-only-webhook-secret-32-characters-long" SECRET = "test-only-webhook-secret-32-characters-long"
@pytest.mark.usefixtures("isolated_settings")
def test_webhook_configuration_requires_pair_and_safe_url(): def test_webhook_configuration_requires_pair_and_safe_url():
with pytest.raises(ValidationError): with pytest.raises(ValidationError):
AppWebhook.model_validate({"url": "https://omni.example.invalid/events"}) AppWebhook.model_validate({"url": "https://omni.example.invalid/events"})