REQ-048/010 change-req. `--config` 를 이름으로 해석 + `--config-path <dir>` (기본 ./config) + resolve_config_toml(슬래시 포함 값은 하위호환 명시 경로). `.env` 를 config-dir 결정적 해석(--env-file 우선, 없으면 <config_dir>/.env, CWD 무관) + config/.env.example + .gitignore config/* 허용목록. 운영자 CLI 도 --config-path/--env-file 동일 적용. from_env() legacy 루트 .env 폴백 유지로 완전 하위호환. 단위 23 + 전체 375 passed, ruff clean, 라이브 스모크(name·path 동일). REQ-048/010 → Implemented. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
92 lines
3.5 KiB
Python
92 lines
3.5 KiB
Python
"""Per-instance TOML config loader tests (REQ-048 / BAT-013)."""
|
|
from __future__ import annotations
|
|
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from trex_core.runtime.config import DaemonConfig, DaemonInstanceConfig
|
|
|
|
_REPO = Path(__file__).resolve().parents[2]
|
|
|
|
|
|
def _write_toml(tmp_path, body: str) -> str:
|
|
p = tmp_path / "inst.toml"
|
|
p.write_text(body)
|
|
return str(p)
|
|
|
|
|
|
def test_from_toml_parses(tmp_path):
|
|
"""AC-1: from_toml reads instance_key + portfolio_hub_token."""
|
|
path = _write_toml(tmp_path, 'instance_key = "inst-1"\nportfolio_hub_token = "tok"\n')
|
|
cfg = DaemonInstanceConfig.from_toml(path)
|
|
assert cfg.instance_key == "inst-1"
|
|
assert cfg.portfolio_hub_token == "tok"
|
|
|
|
|
|
def test_from_toml_missing_instance_key_raises(tmp_path):
|
|
"""AC-4: missing required instance_key → error (no silent default)."""
|
|
path = _write_toml(tmp_path, 'portfolio_hub_token = "tok"\n')
|
|
with pytest.raises(RuntimeError):
|
|
DaemonInstanceConfig.from_toml(path)
|
|
|
|
|
|
def test_from_config_injects_token(tmp_path, monkeypatch):
|
|
"""AC-3 / S-5: TOML token is injected into PortfolioHubConfig when base_url is set,
|
|
even with TREX_PORTFOLIO_HUB_TOKEN absent from the environment."""
|
|
monkeypatch.setenv("TREX_POSTGRESQL_DSN", "postgresql://x/y")
|
|
monkeypatch.setenv("TREX_PORTFOLIO_HUB_BASE_URL", "http://hub:18000")
|
|
monkeypatch.delenv("TREX_PORTFOLIO_HUB_TOKEN", raising=False)
|
|
path = _write_toml(tmp_path, 'instance_key = "inst-9"\nportfolio_hub_token = "toml-tok"\n')
|
|
cfg = DaemonConfig.from_config(path)
|
|
assert cfg.instance_key == "inst-9"
|
|
assert cfg.portfolio_hub is not None
|
|
assert cfg.portfolio_hub.token == "toml-tok"
|
|
|
|
|
|
def test_default_instance_key_when_from_env(monkeypatch):
|
|
monkeypatch.setenv("TREX_POSTGRESQL_DSN", "postgresql://x/y")
|
|
assert DaemonConfig.from_env().instance_key == "default"
|
|
|
|
|
|
def test_trading_timezone_defaults_to_kst(tmp_path, monkeypatch):
|
|
"""dcy-014 / REQ-048: trading TZ + daily trigger default to KST / midnight."""
|
|
monkeypatch.setenv("TREX_POSTGRESQL_DSN", "postgresql://x/y")
|
|
inst = DaemonInstanceConfig.from_toml(_write_toml(tmp_path, 'instance_key = "i1"\n'))
|
|
assert inst.trading_timezone == "Asia/Seoul"
|
|
assert inst.daily_preallocation_time == "00:00"
|
|
assert DaemonConfig.from_env().trading_timezone == "Asia/Seoul"
|
|
|
|
|
|
def test_trading_timezone_from_toml_propagates(tmp_path, monkeypatch):
|
|
"""dcy-014 / REQ-048: TOML trading_timezone + trigger flow into DaemonConfig."""
|
|
monkeypatch.setenv("TREX_POSTGRESQL_DSN", "postgresql://x/y")
|
|
path = _write_toml(
|
|
tmp_path,
|
|
'instance_key = "i2"\ntrading_timezone = "UTC"\ndaily_preallocation_time = "07:30"\n',
|
|
)
|
|
cfg = DaemonConfig.from_config(path)
|
|
assert cfg.trading_timezone == "UTC"
|
|
assert cfg.daily_preallocation_time == "07:30"
|
|
|
|
|
|
def test_config_required_cli_exits_nonzero():
|
|
"""AC-2: daemon entry without --config exits non-zero (argparse required)."""
|
|
proc = subprocess.run(
|
|
[sys.executable, "-m", "trex_core.runtime.daemon"],
|
|
capture_output=True, text=True, timeout=30, cwd=str(_REPO),
|
|
)
|
|
assert proc.returncode != 0
|
|
assert "config" in (proc.stderr + proc.stdout).lower()
|
|
|
|
|
|
def test_gitignore_excludes_instance_toml():
|
|
"""AC-5 (CHG-20260609-01): .gitignore ignores config/* but keeps example
|
|
templates (*.example.toml covers instance.example.toml) and .env.example."""
|
|
gi = (_REPO / ".gitignore").read_text()
|
|
assert "/config/*" in gi
|
|
assert "!/config/*.example.toml" in gi
|
|
assert "!/config/.env.example" in gi
|