File size: 3,622 Bytes
3a1a713
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from __future__ import annotations

from pathlib import Path

import pytest

from polyhedra.agents.base import Mention
from polyhedra.agents.quant.market_watchers import OptionsFlowWatcher
from polyhedra.services.file_writer import FileWriter
from polyhedra.services.options_data import OptionContract, OptionsDataSnapshot


class _FakeOptionsProvider:
    def __init__(self, snapshot: OptionsDataSnapshot) -> None:
        self.snapshot = snapshot

    def fetch(self, symbol: str) -> OptionsDataSnapshot:
        assert symbol == self.snapshot.symbol
        return self.snapshot


def _snapshot(status: str = "ok") -> OptionsDataSnapshot:
    contracts = []
    if status == "ok":
        contracts = [
            OptionContract(
                provider="unit-test",
                retrieved_at="2026-08-24T00:00:00+00:00",
                symbol="SPY",
                expiration="2026-08-28",
                strike=650.0,
                option_type="call",
                bid=1.2,
                ask=1.3,
                last=1.25,
                volume=9000,
                open_interest=2000,
                implied_volatility=0.31,
                source_delay_or_entitlement_note="test provider",
            ),
            OptionContract(
                provider="unit-test",
                retrieved_at="2026-08-24T00:00:00+00:00",
                symbol="SPY",
                expiration="2026-08-28",
                strike=640.0,
                option_type="put",
                bid=1.0,
                ask=1.1,
                last=1.05,
                volume=200,
                open_interest=4000,
                implied_volatility=0.28,
                source_delay_or_entitlement_note="test provider",
            ),
        ]
    return OptionsDataSnapshot(
        symbol="SPY",
        provider="unit-test",
        retrieved_at="2026-08-24T00:00:00+00:00",
        status=status,
        note="available" if status == "ok" else "provider unavailable",
        expirations_checked=["2026-08-28"] if status == "ok" else [],
        contracts=contracts,
    )


@pytest.mark.asyncio
async def test_options_flow_watcher_writes_chain_context(
    tmp_path: Path,
) -> None:
    watcher = OptionsFlowWatcher(options_provider=_FakeOptionsProvider(_snapshot()))
    watcher.files = FileWriter(tmp_path)

    response = await watcher.handle(
        Mention("optionsflowwatcher", "marketeventwatcher", "continue SPY"),
        [],
        {"quant_state": {"watchlist": ["SPY"]}},
    )

    artifact = (tmp_path / response.artifact_path).read_text(encoding="utf-8")
    assert response.mentions == ["macrocontextwatcher"]
    assert "OPTIONS_MARKET_CONTEXT" in artifact
    assert "FLOW_CANDIDATE" in artifact
    assert "UNUSUAL_OPTION_FLOW" in artifact
    assert "not directional by itself" in artifact
    assert "CALL" in artifact
    assert "bullish" not in artifact.lower()
    assert "bearish" not in artifact.lower()


@pytest.mark.asyncio
async def test_options_flow_watcher_records_data_gap_without_na_signal(tmp_path: Path) -> None:
    watcher = OptionsFlowWatcher(
        options_provider=_FakeOptionsProvider(_snapshot(status="unavailable"))
    )
    watcher.files = FileWriter(tmp_path)

    response = await watcher.handle(
        Mention("optionsflowwatcher", "marketeventwatcher", "continue SPY"),
        [],
        {"quant_state": {"watchlist": ["SPY"]}},
    )

    artifact = (tmp_path / response.artifact_path).read_text(encoding="utf-8")
    assert "DATA_GAP" in artifact
    assert "excluded from signal weighting" in artifact
    assert "N/A" not in artifact