initial commit — cassandra v0.1
Containerised macro-strategy dashboard: 4-panel web UI (indicators, portfolio, flash news, AI strategic log), MariaDB store, hourly ingestion jobs, OpenRouter-backed AI analysis. Ports the four prototype scripts in the parent dir (market_pulse, flash_news, trading212, strategic_log) into async services backed by a persistent DB and served via FastAPI + Jinja2 + HTMX. APScheduler runs as a separate compose service for crash-safety and easier restarts. Portfolio composition + position names come live from Trading 212; news per-ticker headlines reuse those names. Tone (NOVICE/INTERMEDIATE/ PRO) and analysis style (DRY/SPECULATIVE) are env-configurable and stored on each log row so historical entries show what produced them. Default model is deepseek/deepseek-v4-flash (overridable via env). Light/dark theme toggle, sans-serif for prose surfaces, monospace for data. Bearer-token auth, OpenRouter monthly cost cap, RSS feeds auto- disabled on consecutive failures. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
commit
a10409c02b
61 changed files with 4890 additions and 0 deletions
19
tests/conftest.py
Normal file
19
tests/conftest.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
"""Pytest config — no DB / no network. Tests target pure functions only.
|
||||
|
||||
Heavy runtime deps (fastapi, httpx, sqlalchemy, pydantic-settings, tenacity)
|
||||
are installed inside the container but not necessarily on the host. Tests
|
||||
that need them use pytest.importorskip; the full suite runs via
|
||||
`docker compose run --rm app pytest tests/`."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(ROOT))
|
||||
|
||||
# Sentinel env so importing app.config doesn't try to read a missing .env.
|
||||
os.environ.setdefault("DATABASE_URL", "sqlite+aiosqlite:///:memory:")
|
||||
os.environ.setdefault("CASSANDRA_MOCK", "1")
|
||||
21
tests/fixtures/rss_sample.xml
vendored
Normal file
21
tests/fixtures/rss_sample.xml
vendored
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<rss version="2.0">
|
||||
<channel>
|
||||
<title>Sample</title>
|
||||
<item>
|
||||
<title>Brent crude jumps on Hormuz uncertainty</title>
|
||||
<link>https://example.com/a</link>
|
||||
<pubDate>Fri, 15 May 2026 12:00:00 GMT</pubDate>
|
||||
</item>
|
||||
<item>
|
||||
<title>Fed signals caution as inflation re-accelerates</title>
|
||||
<link>https://example.com/b</link>
|
||||
<pubDate>Fri, 15 May 2026 13:30:00 GMT</pubDate>
|
||||
</item>
|
||||
<item>
|
||||
<title></title>
|
||||
<link>https://example.com/empty</link>
|
||||
<pubDate>Fri, 15 May 2026 14:00:00 GMT</pubDate>
|
||||
</item>
|
||||
</channel>
|
||||
</rss>
|
||||
37
tests/test_api_helpers.py
Normal file
37
tests/test_api_helpers.py
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
"""Tests for tiny helpers in app.routers.api."""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
pytest.importorskip("fastapi")
|
||||
pytest.importorskip("sqlalchemy")
|
||||
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from app.routers.api import _fmt_age, _md_to_html
|
||||
|
||||
|
||||
def test_fmt_age_none():
|
||||
assert _fmt_age(datetime.now(timezone.utc), None) == "—"
|
||||
|
||||
|
||||
def test_fmt_age_units():
|
||||
now = datetime(2026, 5, 15, 12, 0, tzinfo=timezone.utc)
|
||||
assert _fmt_age(now, now - timedelta(seconds=30)) == "30s"
|
||||
assert _fmt_age(now, now - timedelta(minutes=5)) == "5m"
|
||||
assert _fmt_age(now, now - timedelta(hours=3)) == "3h"
|
||||
assert _fmt_age(now, now - timedelta(days=2)) == "2d"
|
||||
|
||||
|
||||
def test_md_to_html_headers_and_bold():
|
||||
src = "## Section one\n\nBody text with **bold** word.\n\n## Section two\n\nMore."
|
||||
out = _md_to_html(src)
|
||||
assert "<h3>Section one</h3>" in out
|
||||
assert "<strong>bold</strong>" in out
|
||||
assert "<p>" in out
|
||||
|
||||
|
||||
def test_md_to_html_preserves_line_breaks_inside_block():
|
||||
src = "Line one\nLine two"
|
||||
out = _md_to_html(src)
|
||||
assert "Line one<br>Line two" in out
|
||||
40
tests/test_config_loading.py
Normal file
40
tests/test_config_loading.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
"""default.toml and portfolio.toml must load cleanly into the expected shapes."""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
pytest.importorskip("pydantic_settings")
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from app.config import load_feeds, load_groups, load_presets
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
DEFAULT = ROOT / "config" / "default.toml"
|
||||
PORTFOLIO = ROOT / "config" / "portfolio.toml"
|
||||
|
||||
|
||||
def test_default_groups_present():
|
||||
g = load_groups(DEFAULT, PORTFOLIO)
|
||||
for expected in ("equity", "mag7", "rates", "macro", "commodities", "fx", "pie"):
|
||||
assert expected in g, f"missing group: {expected}"
|
||||
# Every item is a 3-tuple of strings.
|
||||
for items in g.values():
|
||||
for sym, lab, note in items:
|
||||
assert isinstance(sym, str) and sym
|
||||
assert isinstance(lab, str)
|
||||
assert isinstance(note, str)
|
||||
|
||||
|
||||
def test_default_feeds_present():
|
||||
f = load_feeds(DEFAULT, PORTFOLIO)
|
||||
for cat in ("markets", "world", "energy", "asia"):
|
||||
assert cat in f, f"missing feed category: {cat}"
|
||||
assert all(name and url for name, url in f[cat])
|
||||
|
||||
|
||||
def test_presets_thesis_present():
|
||||
p = load_presets(DEFAULT, PORTFOLIO)
|
||||
assert "thesis" in p
|
||||
assert "hormuz" in p["thesis"]
|
||||
47
tests/test_market_parsing.py
Normal file
47
tests/test_market_parsing.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
"""Pure-function tests for app.services.market."""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
pytest.importorskip("httpx")
|
||||
pytest.importorskip("pydantic_settings")
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from app.services.market import _pct, _parse_date, _yahoo_range_covering, parse_symbol
|
||||
|
||||
|
||||
def test_pct_basic():
|
||||
assert _pct(100, 110) == 10.0
|
||||
assert _pct(100, 90) == -10.0
|
||||
|
||||
|
||||
def test_pct_handles_none_and_zero():
|
||||
assert _pct(None, 10) is None
|
||||
assert _pct(10, None) is None
|
||||
assert _pct(0, 5) is None
|
||||
|
||||
|
||||
def test_parse_date():
|
||||
assert _parse_date("2026-03-04") == datetime(2026, 3, 4)
|
||||
|
||||
|
||||
def test_yahoo_range_covering_picks_smallest():
|
||||
today = datetime.now(timezone.utc).date()
|
||||
# anchor 100 days ago → 1y range is enough
|
||||
short = (today.replace(year=today.year)).isoformat()
|
||||
assert _yahoo_range_covering(None) == "1y"
|
||||
assert _yahoo_range_covering("2026-01-01") == "1y"
|
||||
|
||||
|
||||
def test_parse_symbol_routes_by_prefix():
|
||||
fn, ident = parse_symbol("FRED:DFF")
|
||||
assert ident == "DFF"
|
||||
assert fn.__name__ == "fetch_fred"
|
||||
fn2, ident2 = parse_symbol("AAPL")
|
||||
assert ident2 == "AAPL"
|
||||
assert fn2.__name__ == "fetch_yahoo"
|
||||
# Unknown prefix falls through to yahoo.
|
||||
fn3, ident3 = parse_symbol("UNKNOWN:XYZ")
|
||||
assert ident3 == "UNKNOWN:XYZ"
|
||||
assert fn3.__name__ == "fetch_yahoo"
|
||||
53
tests/test_news_parsing.py
Normal file
53
tests/test_news_parsing.py
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
"""Pure-function tests for app.services.news."""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
pytest.importorskip("httpx")
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
|
||||
from app.services.news import Headline, _parse_date, dedupe, parse_feed
|
||||
|
||||
|
||||
FIXTURE = Path(__file__).parent / "fixtures" / "rss_sample.xml"
|
||||
|
||||
|
||||
def test_parse_feed_returns_real_items_only():
|
||||
items = parse_feed("Sample", "world", FIXTURE.read_bytes())
|
||||
titles = [h.title for h in items]
|
||||
assert "Brent crude jumps on Hormuz uncertainty" in titles
|
||||
assert "Fed signals caution as inflation re-accelerates" in titles
|
||||
# Empty-title row is dropped.
|
||||
assert all(t for t in titles)
|
||||
|
||||
|
||||
def test_parse_feed_uses_rfc822_dates():
|
||||
items = parse_feed("Sample", "world", FIXTURE.read_bytes())
|
||||
when = items[0].when
|
||||
assert when.tzinfo is not None
|
||||
assert when.year == 2026
|
||||
|
||||
|
||||
def test_parse_date_atom_iso():
|
||||
d = _parse_date("2026-05-15T12:34:56Z")
|
||||
assert d == datetime(2026, 5, 15, 12, 34, 56, tzinfo=timezone.utc)
|
||||
|
||||
|
||||
def test_headline_fingerprint_is_normalised():
|
||||
h1 = Headline(datetime.now(timezone.utc), "S1", "c", " Hello WORLD ", "u1")
|
||||
h2 = Headline(datetime.now(timezone.utc), "S2", "c", "hello world", "u2")
|
||||
assert h1.fingerprint == h2.fingerprint
|
||||
|
||||
|
||||
def test_dedupe_keeps_first_by_url_or_title():
|
||||
t = datetime.now(timezone.utc)
|
||||
hs = [
|
||||
Headline(t, "A", "c", "Same headline", "https://a.example/1"),
|
||||
Headline(t, "B", "c", "Same headline", "https://b.example/2"), # title dupe
|
||||
Headline(t, "C", "c", "Other", "https://a.example/1"), # url dupe
|
||||
Headline(t, "D", "c", "Fresh", "https://d.example"),
|
||||
]
|
||||
out = dedupe(hs)
|
||||
assert [h.source for h in out] == ["A", "D"]
|
||||
45
tests/test_openrouter_prompt.py
Normal file
45
tests/test_openrouter_prompt.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
"""build_user_prompt is the heart of the AI log — verify shape."""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
pytest.importorskip("httpx")
|
||||
pytest.importorskip("tenacity")
|
||||
pytest.importorskip("pydantic_settings")
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from app.services.openrouter import SYSTEM_PROMPT, build_user_prompt
|
||||
|
||||
|
||||
def test_system_prompt_has_voice_anchors():
|
||||
# Tripwires for prompt regressions.
|
||||
for marker in ["Objective", "Lens", "Discipline", "watch list"]:
|
||||
assert marker in SYSTEM_PROMPT
|
||||
|
||||
|
||||
def test_build_user_prompt_includes_anchor_and_reference():
|
||||
out = build_user_prompt(
|
||||
today=datetime(2026, 5, 15, tzinfo=timezone.utc),
|
||||
anchor="2026-03-04",
|
||||
quotes_by_group={"equity": [{"symbol": "^GSPC", "label": "S&P 500"}]},
|
||||
headlines_by_bucket={"world": [{"when": "2026-05-15T10:00", "source": "BBC", "title": "x"}]},
|
||||
reference_line="S&P 7501 · VIX 18",
|
||||
)
|
||||
assert "2026-05-15" in out
|
||||
assert "Anchor reference date: 2026-03-04" in out
|
||||
assert "S&P 7501" in out
|
||||
assert "WORLD" in out
|
||||
assert "^GSPC" in out
|
||||
|
||||
|
||||
def test_build_user_prompt_omits_empty_buckets():
|
||||
out = build_user_prompt(
|
||||
today=datetime(2026, 5, 15, tzinfo=timezone.utc),
|
||||
anchor=None,
|
||||
quotes_by_group={},
|
||||
headlines_by_bucket={"world": [], "tech": [{"when": "2026-05-15T10:00",
|
||||
"source": "X", "title": "AI thing"}]},
|
||||
)
|
||||
assert "TECH" in out
|
||||
assert "WORLD" not in out
|
||||
Loading…
Add table
Add a link
Reference in a new issue