feat(lifecycle): alive doar la deschidere/închidere, /window persistent, /status cu fereastra
Heartbeat-ul periodic (activ/IDLE la 30 min) e oprit implicit (heartbeat_min = 0). Semnalul „alive" devin alertele zilnice „🟢 Piața deschisă" / „🔴 Piața închisă", emise pe tranziția ferestrei (operating_hours AND /window) independent de pauză — body-ul spune dacă monitorizarea e pauzată manual sau pe drift. /window era doar în memorie și se pierdea la repornire (09-09), așa că programul revenea la 16:30–23:00 și trimitea mesaje înainte/după fereastra 19:30–22:00. Acum e persistat în logs/session_window.json și restaurat la pornire; /window off șterge fișierul. /status afișează blocul de fereastră: deschisă/închisă + următoarea deschidere/închidere (ora locală), /window, orele bursei cu echivalentul local, și config | heartbeat. Config activ: heartbeat_min = 0; include și baseline_phash rescris de auto-rebase-ul canary din 09-09. DOX pass: notifier + configs. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Em4jtmh4pRpYMdyd9ueHYF
This commit is contained in:
@@ -870,6 +870,63 @@ async def test_window_command_sets_and_clears_session_window():
|
||||
assert any(e.get("event") == "session_window_cleared" for e in ctx.audit.events)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_window_command_persists_across_restart(tmp_path):
|
||||
"""/window is saved to disk and restored on the next start; /window off deletes it."""
|
||||
import atm.main as _main
|
||||
from atm.commands import Command
|
||||
|
||||
sw_path = tmp_path / "logs" / "session_window.json"
|
||||
ctx = _dispatch_ctx(lifecycle=_main.LifecycleState(session_window_path=sw_path))
|
||||
await _main._dispatch_command(ctx, Command(action="window", window=("19:30", "22:00")))
|
||||
assert _main._load_session_window(sw_path) == ("19:30", "22:00")
|
||||
|
||||
await _main._dispatch_command(ctx, Command(action="window", window=None))
|
||||
assert not sw_path.exists()
|
||||
assert _main._load_session_window(sw_path) is None
|
||||
|
||||
|
||||
def test_load_session_window_corrupt_file_is_none(tmp_path):
|
||||
import atm.main as _main
|
||||
|
||||
p = tmp_path / "session_window.json"
|
||||
p.write_text("{not json", encoding="utf-8")
|
||||
assert _main._load_session_window(p) is None
|
||||
p.write_text('{"start": "19:30"}', encoding="utf-8")
|
||||
assert _main._load_session_window(p) is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_open_close_alerts_sent_while_user_paused():
|
||||
"""Daily open/close alerts are the alive signal → they fire even when paused."""
|
||||
import atm.main as _main
|
||||
|
||||
cfg = _oh_cfg()
|
||||
cfg.lockout_s = 240
|
||||
tz = cfg.operating_hours._tz_cache
|
||||
lifecycle = _main.LifecycleState(user_paused=True, last_window_state="closed")
|
||||
ctx = _dispatch_ctx(lifecycle=lifecycle, cfg=cfg)
|
||||
ctx.charts = []
|
||||
|
||||
mid = _dt.datetime(2026, 4, 20, 12, 0, tzinfo=tz).timestamp()
|
||||
assert await _main._lifecycle_gate(ctx, mid) == "user_paused"
|
||||
assert lifecycle.last_window_state == "open"
|
||||
opened = [a for a in ctx.notifier.alerts if "deschisă" in a.title]
|
||||
assert len(opened) == 1 and "oprită manual" in opened[0].body
|
||||
# Paused → TradeStation isn't pulled to the front on open.
|
||||
assert not any(e.get("event") == "window_focused" for e in ctx.audit.events)
|
||||
|
||||
close = _dt.datetime(2026, 4, 20, 16, 5, tzinfo=tz).timestamp()
|
||||
assert await _main._lifecycle_gate(ctx, close) == "user_paused"
|
||||
assert lifecycle.last_window_state == "closed"
|
||||
assert sum("închisă" in a.title for a in ctx.notifier.alerts) == 1
|
||||
|
||||
# Same side of the boundary again → no duplicate.
|
||||
n = len(ctx.notifier.alerts)
|
||||
await _main._lifecycle_gate(ctx, close + 60)
|
||||
assert len(ctx.notifier.alerts) == n
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Commit 5: /pause /resume dispatch (plan tests #11-15, #16, R2 #21)
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -1741,7 +1798,60 @@ async def test_status_window_line_when_oh_enabled():
|
||||
|
||||
status = [a for a in ctx.notifier.alerts if a.kind == "status"]
|
||||
body = status[0].body
|
||||
assert "fereastră: deschisă" in body
|
||||
# Open/closed depends on the real clock here; exact content is covered by
|
||||
# the _window_status_lines tests below with fixed timestamps.
|
||||
assert "fereastră: " in body
|
||||
assert "/window: —" in body
|
||||
assert "bursă: 09:30–16:00 America/New_York MON–FRI" in body
|
||||
|
||||
|
||||
def test_window_status_lines_session_window_only():
|
||||
"""Local /window only (oh off) → open/closed + next change in local time."""
|
||||
import atm.main as _main
|
||||
|
||||
cfg = _oh_cfg(enabled=False)
|
||||
lifecycle = _main.LifecycleState(session_window=("19:30", "22:00"))
|
||||
inside = _dt.datetime(2026, 4, 20, 20, 0).timestamp()
|
||||
assert _main._window_status_lines(inside, lifecycle, cfg) == [
|
||||
"fereastră: deschisă → închidere azi 22:00",
|
||||
"/window: 19:30–22:00 (local)",
|
||||
]
|
||||
after = _dt.datetime(2026, 4, 20, 22, 30).timestamp()
|
||||
assert _main._window_status_lines(after, lifecycle, cfg)[0] == (
|
||||
"fereastră: închisă → deschidere mâine 19:30"
|
||||
)
|
||||
|
||||
|
||||
def test_window_status_lines_nothing_configured_is_empty():
|
||||
import atm.main as _main
|
||||
lines = _main._window_status_lines(0.0, _main.LifecycleState(), _oh_cfg(enabled=False))
|
||||
assert lines == []
|
||||
|
||||
|
||||
def test_window_status_lines_exchange_hours_local_conversion():
|
||||
import atm.main as _main
|
||||
|
||||
cfg = _oh_cfg()
|
||||
tz = cfg.operating_hours._tz_cache
|
||||
mid = _dt.datetime(2026, 4, 20, 12, 0, tzinfo=tz).timestamp()
|
||||
loc = lambda h, m: _dt.datetime(2026, 4, 20, h, m, tzinfo=tz).astimezone().strftime("%H:%M") # noqa: E731
|
||||
lines = _main._window_status_lines(mid, _main.LifecycleState(), cfg)
|
||||
assert lines[0].startswith("fereastră: deschisă → închidere ")
|
||||
assert lines[1] == "/window: —"
|
||||
assert lines[2] == (
|
||||
f"bursă: 09:30–16:00 America/New_York MON–FRI (local {loc(9, 30)}–{loc(16, 0)})"
|
||||
)
|
||||
|
||||
|
||||
def test_next_window_change_skips_weekend():
|
||||
"""Friday after close → next flip is Monday's open, not Saturday."""
|
||||
import atm.main as _main
|
||||
|
||||
cfg = _oh_cfg()
|
||||
tz = cfg.operating_hours._tz_cache
|
||||
fri_evening = _dt.datetime(2026, 4, 24, 17, 0, tzinfo=tz).timestamp()
|
||||
mon_open = _dt.datetime(2026, 4, 27, 9, 30, tzinfo=tz).timestamp()
|
||||
assert _main._next_window_change(fri_evening, _main.LifecycleState(), cfg) == mon_open
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user