mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 09:29:39 +08:00
The PR's relative-path + proxy.ts rewrite design depends on two env vars (DEEPTUTOR_API_BASE_URL, DEEPTUTOR_AUTH_ENABLED) read server-side by the Next.js middleware. As submitted they were only set by the Docker entrypoint, and the auth one read a non-existent auth.json key — so auth enforcement was silently disabled and the PyPI `deeptutor start` path was unwired. - Export both vars from render_environment() (the single JSON-backed source of truth), so the Docker entrypoint AND the `deeptutor start` launcher get them consistently. DEEPTUTOR_API_BASE_URL falls back to localhost:<backend_port>. - launcher.py: set both in common_env using the resolved backend_url, so they track port-conflict reassignment. - Dockerfile: drop the duplicated, buggy per-var heredocs (one read the wrong key `auth_enabled` instead of `enabled`, leaving the proxy auth gate always off); the export_runtime_settings_to_env eval now provides both. Add them to the env-unset list for hygiene. - Update the two placeholder tests to assert the new proxy-based design instead of the removed build-time sed mechanism; add DEEPTUTOR_* coverage in test_runtime_settings. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
105 lines
4.2 KiB
Python
105 lines
4.2 KiB
Python
from __future__ import annotations
|
|
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
|
|
def _load_module():
|
|
module_path = Path(__file__).resolve().parents[2] / "scripts" / "docker_compose.py"
|
|
spec = importlib.util.spec_from_file_location("docker_compose_under_test", module_path)
|
|
assert spec and spec.loader
|
|
module = importlib.util.module_from_spec(spec)
|
|
sys.modules[spec.name] = module
|
|
try:
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
finally:
|
|
sys.modules.pop(spec.name, None)
|
|
|
|
|
|
def test_render_docker_env_reads_json_only(tmp_path: Path) -> None:
|
|
module = _load_module()
|
|
settings_dir = tmp_path / "settings"
|
|
settings_dir.mkdir()
|
|
(settings_dir / "system.json").write_text(
|
|
json.dumps({"backend_port": 9001, "frontend_port": 4000}),
|
|
encoding="utf-8",
|
|
)
|
|
(settings_dir / "integrations.json").write_text(
|
|
json.dumps({"pocketbase_port": 19090}),
|
|
encoding="utf-8",
|
|
)
|
|
output_path = tmp_path / "docker.env"
|
|
|
|
values = module.render_docker_env(settings_dir, output_path)
|
|
|
|
assert values == {
|
|
"DEEPTUTOR_DOCKER_BACKEND_PORT": "9001",
|
|
"DEEPTUTOR_DOCKER_FRONTEND_PORT": "4000",
|
|
"DEEPTUTOR_DOCKER_POCKETBASE_PORT": "19090",
|
|
}
|
|
saved = output_path.read_text(encoding="utf-8")
|
|
assert "\nBACKEND_PORT=" not in saved
|
|
assert "DEEPTUTOR_DOCKER_BACKEND_PORT=9001" in saved
|
|
|
|
|
|
def test_render_docker_env_uses_defaults_for_missing_or_invalid_json(tmp_path: Path) -> None:
|
|
module = _load_module()
|
|
settings_dir = tmp_path / "settings"
|
|
settings_dir.mkdir()
|
|
(settings_dir / "system.json").write_text(
|
|
json.dumps({"backend_port": "bad", "frontend_port": 70000}),
|
|
encoding="utf-8",
|
|
)
|
|
output_path = tmp_path / "docker.env"
|
|
|
|
values = module.render_docker_env(settings_dir, output_path)
|
|
|
|
assert values["DEEPTUTOR_DOCKER_BACKEND_PORT"] == "8001"
|
|
assert values["DEEPTUTOR_DOCKER_FRONTEND_PORT"] == "3782"
|
|
assert values["DEEPTUTOR_DOCKER_POCKETBASE_PORT"] == "8090"
|
|
|
|
|
|
def test_compose_files_do_not_consume_legacy_env_names() -> None:
|
|
root = Path(__file__).resolve().parents[2]
|
|
for name in ("docker-compose.yml", "docker-compose.ghcr.yml"):
|
|
content = (root / name).read_text(encoding="utf-8")
|
|
assert "${BACKEND_PORT" not in content
|
|
assert "${FRONTEND_PORT" not in content
|
|
assert "\n - BACKEND_PORT" not in content
|
|
assert "\n - AUTH_ENABLED" not in content
|
|
assert "DEEPTUTOR_DOCKER_BACKEND_PORT" in content
|
|
|
|
|
|
def test_dockerfile_is_json_driven_without_bundle_sed() -> None:
|
|
"""The image no longer rewrites the built bundle at startup (the runtime
|
|
``sed -i`` broke under a read-only rootfs). URL/auth knowledge is JSON-driven:
|
|
the entrypoint re-exports runtime settings from data/user/settings/*.json
|
|
(including DEEPTUTOR_API_BASE_URL / DEEPTUTOR_AUTH_ENABLED) and web/proxy.ts
|
|
forwards /api/* and /ws/* to the backend at request time."""
|
|
root = Path(__file__).resolve().parents[2]
|
|
content = (root / "Dockerfile").read_text(encoding="utf-8")
|
|
# The build-time placeholder + runtime bundle sed mechanism is gone.
|
|
assert "__NEXT_PUBLIC_API_BASE_PLACEHOLDER__" not in content
|
|
assert "__NEXT_PUBLIC_AUTH_ENABLED_PLACEHOLDER__" not in content
|
|
# Still JSON-driven: stale runtime env names are ignored and re-exported
|
|
# from the settings JSON on every start.
|
|
assert "DEEPTUTOR_IGNORE_PROCESS_ENV_OVERRIDES=1" in content
|
|
assert 'unset "$key"' in content
|
|
assert "export_runtime_settings_to_env" in content
|
|
|
|
|
|
def test_frontend_api_is_url_agnostic_passthrough() -> None:
|
|
"""web/lib/api.ts no longer carries a build-time API base or a placeholder
|
|
token; apiUrl/wsUrl are pass-throughs and the Next.js middleware
|
|
(web/proxy.ts) performs the forwarding at request time."""
|
|
root = Path(__file__).resolve().parents[2]
|
|
api_ts = (root / "web" / "lib" / "api.ts").read_text(encoding="utf-8")
|
|
assert "NEXT_PUBLIC_API_BASE_PLACEHOLDER" not in api_ts
|
|
assert "process.env.NEXT_PUBLIC_API_BASE" not in api_ts
|
|
proxy_ts = (root / "web" / "proxy.ts").read_text(encoding="utf-8")
|
|
assert "DEEPTUTOR_API_BASE_URL" in proxy_ts
|
|
assert "NextResponse.rewrite" in proxy_ts
|