feat(CORE-001D1): harden unit-aware workspace persistence

This commit is contained in:
Bu5hm4nn
2026-03-25 13:19:33 +01:00
parent cfb6abd842
commit 132aaed512
13 changed files with 464 additions and 87 deletions

View File

@@ -1,5 +1,6 @@
from __future__ import annotations
import json
import re
from uuid import uuid4
@@ -37,6 +38,28 @@ def test_workspace_repository_persists_workspace_specific_portfolio_config(tmp_p
assert reloaded.gold_value == created.gold_value
def test_workspace_repository_writes_explicit_portfolio_schema(tmp_path) -> None:
repo = WorkspaceRepository(base_path=tmp_path / "workspaces")
workspace_id = str(uuid4())
config = repo.create_workspace(workspace_id)
config.entry_basis_mode = "weight"
config.entry_price = 4400.0
config.gold_ounces = 220.0
config.gold_value = 968000.0
repo.save_portfolio_config(workspace_id, config)
payload = json.loads((tmp_path / "workspaces" / workspace_id / "portfolio_config.json").read_text())
assert payload["schema_version"] == 2
assert payload["portfolio"]["gold_ounces"] == {"value": "220.0", "unit": "ozt"}
assert payload["portfolio"]["entry_price"] == {
"value": "4400.0",
"currency": "USD",
"per_weight_unit": "ozt",
}
def test_root_without_workspace_cookie_shows_welcome_page(tmp_path, monkeypatch) -> None:
_install_workspace_repo(tmp_path, monkeypatch)
@@ -93,6 +116,30 @@ def test_unknown_workspace_route_redirects_to_welcome_page(tmp_path, monkeypatch
assert "Create a private workspace URL" in response.text
def test_invalid_workspace_config_is_not_treated_as_existing(tmp_path) -> None:
repo = WorkspaceRepository(base_path=tmp_path / "workspaces")
workspace_id = str(uuid4())
workspace_path = tmp_path / "workspaces" / workspace_id
workspace_path.mkdir(parents=True, exist_ok=True)
(workspace_path / "portfolio_config.json").write_text(json.dumps({"gold_value": 215000.0}))
assert repo.workspace_exists(workspace_id) is False
def test_invalid_workspace_config_route_redirects_to_welcome_page(tmp_path, monkeypatch) -> None:
_install_workspace_repo(tmp_path, monkeypatch)
workspace_id = str(uuid4())
workspace_path = tmp_path / "workspaces" / workspace_id
workspace_path.mkdir(parents=True, exist_ok=True)
(workspace_path / "portfolio_config.json").write_text(json.dumps({"gold_value": 215000.0}))
with TestClient(app) as client:
response = client.get(f"/{workspace_id}")
assert response.status_code == 200
assert "Create a private workspace URL" in response.text
def test_arbitrary_fake_workspace_like_path_redirects_to_welcome_page(tmp_path, monkeypatch) -> None:
_install_workspace_repo(tmp_path, monkeypatch)
@@ -152,7 +199,6 @@ def test_workspace_routes_seed_page_defaults_from_workspace_portfolio_config(tmp
hedge_response = client.get(f"/{workspace_id}/hedge")
backtests_response = client.get(f"/{workspace_id}/backtests")
event_response = client.get(f"/{workspace_id}/event-comparison")
redirect_response = client.get("/backtests", cookies={"workspace_id": workspace_id}, follow_redirects=False)
assert hedge_response.status_code == 200
assert "Monthly hedge budget" in hedge_response.text
@@ -176,6 +222,3 @@ def test_workspace_routes_seed_page_defaults_from_workspace_portfolio_config(tmp
assert "222,000" in event_response.text or "222000" in event_response.text
assert "9,680" in event_response.text or "9680" in event_response.text
assert "80.0%" in event_response.text
assert redirect_response.status_code in {302, 303, 307}
assert redirect_response.headers["location"] == f"/{workspace_id}/backtests"