83 lines
3.2 KiB
Python
83 lines
3.2 KiB
Python
|
|
from tht.workspace import WorkspaceError, load_workspace
|
|
|
|
|
|
def test_load_workspace_expands_env_vars(monkeypatch, tmp_path):
|
|
# Isolate profile: load_dotenv (conftest D3) injects THT_PROFILE from the real
|
|
# harness/.env into os.environ; this test asserts the *default* (server), so it
|
|
# must del THT_PROFILE rather than inherit whatever the operator set.
|
|
monkeypatch.delenv("THT_PROFILE", raising=False)
|
|
monkeypatch.setenv("THT_VEC_API_KEY", "secret-reader")
|
|
monkeypatch.setenv("THT_VEC_WRITE_API_KEY", "secret-writer")
|
|
monkeypatch.setenv("THT_VEC_REST_URL", "https://example/vector/v1/")
|
|
monkeypatch.setenv("THT_DWH_REST_URL", "https://example/dwh/")
|
|
monkeypatch.setenv("THT_DWH_API_KEY", "dwh-key")
|
|
monkeypatch.setenv("THT_DB_HOST", "h")
|
|
monkeypatch.setenv("THT_DB_NAME", "db")
|
|
monkeypatch.setenv("THT_DB_USER", "u")
|
|
monkeypatch.setenv("THT_DB_PASSWORD", "p")
|
|
monkeypatch.setenv("THT_VEC_HOST", "vh")
|
|
monkeypatch.setenv("THT_VEC_USER", "vu")
|
|
monkeypatch.setenv("THT_VEC_PASSWORD", "vp")
|
|
monkeypatch.setenv("THT_OLLAMA_URL", "http://embedding:11434")
|
|
monkeypatch.setenv("THT_DOCS_ROOT", str(tmp_path / "docs"))
|
|
yaml = tmp_path / "w.yaml"
|
|
yaml.write_text(
|
|
"database:\n"
|
|
" host: ${THT_DB_HOST}\n"
|
|
" port: 5432\n"
|
|
" database: ${THT_DB_NAME}\n"
|
|
" schema: datawarehouse\n"
|
|
" user: ${THT_DB_USER}\n"
|
|
" password: ${THT_DB_PASSWORD}\n"
|
|
" transport: rest\n"
|
|
"rest:\n"
|
|
" base_url: ${THT_DWH_REST_URL}\n"
|
|
" api_key: ${THT_DWH_API_KEY}\n"
|
|
"vector_db:\n"
|
|
" host: ${THT_VEC_HOST}\n"
|
|
" port: 5438\n"
|
|
" database: postgres\n"
|
|
" schema: vectors\n"
|
|
" user: ${THT_VEC_USER}\n"
|
|
" password: ${THT_VEC_PASSWORD}\n"
|
|
"vector_rest:\n"
|
|
" base_url: ${THT_VEC_REST_URL}\n"
|
|
" api_key: ${THT_VEC_API_KEY}\n"
|
|
"vector_write_rest:\n"
|
|
" base_url: ${THT_VEC_REST_URL}\n"
|
|
" api_key: ${THT_VEC_WRITE_API_KEY}\n"
|
|
"embeddings:\n"
|
|
" base_url: ${THT_OLLAMA_URL}\n"
|
|
" model: qwen3-embedding:0.6b\n"
|
|
" dim: 1024\n"
|
|
"evidence:\n"
|
|
" source_root: ${THT_DOCS_ROOT}\n"
|
|
)
|
|
ws = load_workspace(yaml)
|
|
# Dual vector key (top-level, per Config reale): reader and writer separate
|
|
assert ws.vector_rest.api_key == "secret-reader"
|
|
assert ws.vector_write_rest.api_key == "secret-writer"
|
|
# DWH key distinct from vector keys
|
|
assert ws.rest.api_key == "dwh-key"
|
|
# profile default = server
|
|
assert ws.profile == "server"
|
|
|
|
|
|
def test_load_workspace_missing_env_raises(monkeypatch, tmp_path):
|
|
monkeypatch.delenv("THT_VEC_API_KEY", raising=False)
|
|
yaml = tmp_path / "w.yaml"
|
|
yaml.write_text(
|
|
"database:\n"
|
|
" host: h\n port: 5432\n database: db\n schema: s\n"
|
|
" user: u\n password: p\n transport: direct\n"
|
|
"vector_rest:\n"
|
|
" base_url: 'https://v/'\n"
|
|
" api_key: '${THT_VEC_API_KEY}'\n"
|
|
)
|
|
try:
|
|
load_workspace(yaml)
|
|
assert False, "should have raised"
|
|
except WorkspaceError as e:
|
|
assert "THT_VEC_API_KEY" in str(e)
|