from datetime import datetime import io import json import sys import time from unittest.mock import MagicMock, patch import zipfile import numpy as np import pandas as pd import pytest import joblib import fo import fo_predictor from research import train_fo_predictor @pytest.fixture(autouse=True) def isolate_ai_cache(): fo_predictor._reviews.clear() yield fo_predictor._reviews.clear() class ConstantQuantile: def __init__(self, value): self.value = value def predict(self, frame): return np.full(len(frame), self.value) def fit(self, frame, targets): return self class ConstantDirection: classes_ = np.array(["BEARISH", "BULLISH"]) def predict_proba(self, frame): return np.tile([0.10, 0.90], (len(frame), 1)) def fit(self, frame, targets): return self class FixtureDirection(ConstantDirection): def predict_proba(self, frame): put = frame["is_put"].to_numpy() == 1 return np.column_stack([np.where(put, 0.9, 0.1), np.where(put, 0.1, 0.9)]) @pytest.fixture def approved_model(tmp_path): models = {"lo": ConstantQuantile(-0.2), "mid": ConstantQuantile(0.1), "hi": ConstantQuantile(0.3), "direction": ConstantDirection()} artifact = tmp_path / "INTRADAY.joblib" joblib.dump(models, artifact) metadata = {"approved": True, "artifact": artifact.name, "artifact_sha256": train_fo_predictor.archive_digest(artifact), "metrics": {"selected_samples": 1000, "test_expiries": 10, "forecast_coverage": 0.3, "direction_accuracy": 0.80, "range_coverage": 0.85, "majority_baseline": 0.60}, "min_probability": 0.8, "conformal_adjustment": 0.02, "feature_bounds": {column: [-1e9, 1e9] for column in fo_predictor.FEATURE_COLUMNS}} manifest = {"schema_version": 1, "feature_columns": fo_predictor.FEATURE_COLUMNS, "supported_underlyings": ["NIFTY"], "instrument_type": "OPTION", "data_end": "2026-10-15", "timeframes": {"INTRADAY": metadata}} (tmp_path / "manifest.json").write_text(json.dumps(manifest)) return fo_predictor.FoPremiumPredictor(str(tmp_path)), manifest def reference(**updates): raw = {"underlying": "NIFTY", "underlying_type": "INDEX", "expiry": "2026-10-19", "strike": 22900, "option_type": "PE", "lot_size": 65, "tick_size": 0.05, **updates} return fo.validate_contract(raw, "OPTION", today=datetime(2026, 10, 16).date()) def observation(**updates): quoted = datetime.fromisoformat("2026-10-16T10:00:00+05:30").timestamp() return {"price": 100, "underlying_price": 22950, "iv": 20, "volume": 10000, "open_interest": 20000, "quote_time": quoted, "premium_history": [{"time": quoted - 60 * offset, "close": 100} for offset in range(30, -1, -1)], **updates} def test_pooled_model_accepts_new_expiry_with_only_recent_contract_history(approved_model): model, manifest = approved_model now = datetime.fromisoformat("2026-10-23T11:00:00+05:30") contract = reference(expiry="2026-10-27") quote = observation(quote_time=now.timestamp(), premium_history=[ {"time": now.timestamp() - 60 * offset, "close": 100} for offset in range(31)]) result = model.predict(contract, quote, fo.forecast_windows(contract, now=now), now=now) assert result["timeframes"]["INTRADAY"]["available"] assert manifest["data_end"] < contract["expiry"] def test_model_features_use_verified_quote_time_without_relaxing_input_age(approved_model): model, manifest = approved_model now = datetime.fromisoformat("2026-10-16T10:01:59+05:30") with patch("fo_predictor.build_features", wraps=fo_predictor.build_features) as features: result = model.predict(reference(), observation(), fo.forecast_windows(reference(), now=now), now=now) assert result["timeframes"]["INTRADAY"]["available"] assert features.call_args.kwargs["as_of"].timestamp() == observation()["quote_time"] now = datetime.fromisoformat("2026-10-16T10:02:01+05:30") stale = model.predict(reference(), observation(), fo.forecast_windows(reference(), now=now), now=now) assert not stale["timeframes"]["INTRADAY"]["available"] def test_model_reports_invalid_intrinsic_inputs_instead_of_history_failure(approved_model): model, manifest = approved_model now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") result = model.predict(reference(), observation(price=613.5, underlying_price=22200), fo.forecast_windows(reference(), now=now), now=now) prediction = result["timeframes"]["INTRADAY"] assert not prediction["available"] assert prediction["input_status"] == "invalid_iv_inputs" @pytest.mark.parametrize("gap", [60, 121, 180]) def test_one_minute_history_allows_independent_ai_without_invented_long_lookbacks(monkeypatch, gap): monkeypatch.setenv("FO_AI_ENABLED", "1") now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") quote = observation(premium_history=[{"time": now.timestamp() - gap, "close": 99}, {"time": now.timestamp(), "close": 100}]) raw = {"direction": "BULLISH", "expected_change_pct": 8, "predicted_return_lo": 2, "predicted_return_hi": 15, "confidence": "HIGH", "summary": "Limited observed premium history."} with patch("fo_predictor.datetime", wraps=datetime) as clock, \ patch("llm_client.make_chat_call", return_value=(json.dumps({"forecasts": {"INTRADAY": raw}}), "fake", "test")) as call: clock.now.return_value = now result = fo_predictor.ai_forecasts(reference(), quote, fo.forecast_windows(reference(), now=now)) prediction = result["INTRADAY"] assert prediction["available"] assert prediction["limited_history"] is True assert prediction["history_minutes"] == round(gap / 60, 2) assert prediction["confidence"] == "LOW" context = json.loads(call.call_args.kwargs["messages"][1]["content"]) assert context["premium_history_features"]["premium_return_15m"] is None assert context["premium_history_features"]["premium_return_30m"] is None assert context["premium_history_features"]["premium_volatility_15m"] is None expected_return = pytest.approx(100 / 99 - 1) if gap == 60 else None assert context["history_context"]["premium_return_1m"] == expected_return @pytest.mark.parametrize("gap", [60, 121, 180]) def test_one_minute_ml_is_explicitly_experimental_and_keeps_missing_features(approved_model, gap): model, manifest = approved_model now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") quote = observation(premium_history=[{"time": now.timestamp() - gap, "close": 99}, {"time": now.timestamp(), "close": 100}]) windows = fo.forecast_windows(reference(), now=now) assert not model.predict(reference(), quote, windows, now=now)["timeframes"]["INTRADAY"]["available"] manifest["serving_mode"] = "experimental" manifest["timeframes"]["INTRADAY"]["approved"] = False with open(model.model_dir + "/manifest.json", "w") as stream: json.dump(manifest, stream) with patch("fo_predictor.build_features", wraps=fo_predictor.build_features) as build: prediction = model.predict(reference(), quote, windows, now=now)["timeframes"]["INTRADAY"] assert prediction["available"] assert prediction["limited_history"] is True assert prediction["history_minutes"] == round(gap / 60, 2) assert prediction["confidence"] == "LOW" and prediction["confidence_prob"] is None features = fo_predictor.build_features(*build.call_args.args, **build.call_args.kwargs) assert all(np.isnan(features[column]) for column in fo_predictor.HISTORY_COLUMNS) @pytest.mark.parametrize("offsets", [[0], [59, 0], [60, -1], [301, 241], [1921, 0]]) def test_short_history_rejects_subminute_future_only_or_stale_observations(offsets): now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") points = [{"time": now.timestamp() - offset, "close": 100} for offset in offsets] with pytest.raises(fo_predictor.ForecastInputError): fo_predictor.premium_history_context(points, as_of=now) @pytest.mark.parametrize("gap", [121, 180, 300]) def test_short_history_accepts_irregular_real_ticks_without_claiming_a_one_minute_return(gap): now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") points = [{"time": now.timestamp() - gap, "close": 99}, {"time": now.timestamp(), "close": 100}] context = fo_predictor.premium_history_context(points, as_of=now) assert context["limited_history"] is True assert context["history_samples"] == 2 assert context["history_minutes"] == round(gap / 60, 2) assert context["premium_return_1m"] is None assert all(value is None for value in context["features"].values()) def test_short_history_rejects_conflicting_prices_and_ignores_future_prices(): now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") points = [{"time": now.timestamp() - 60, "close": 99}, {"time": now.timestamp(), "close": 100}] context = fo_predictor.premium_history_context(points, as_of=now) assert context == fo_predictor.premium_history_context(points + [{"time": now.timestamp() + 1, "close": 999}], as_of=now) with pytest.raises(fo_predictor.ForecastInputError): fo_predictor.premium_history_context(points + [{"time": now.timestamp(), "close": 101}], as_of=now) def test_contract_service_allows_one_minute_and_upgrades_ai_cache_when_full_history_arrives(approved_model, monkeypatch): model, manifest = approved_model monkeypatch.setenv("FO_AI_ENABLED", "1") manifest["serving_mode"] = "experimental" manifest["timeframes"]["INTRADAY"]["approved"] = False with open(model.model_dir + "/manifest.json", "w") as stream: json.dump(manifest, stream) now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") quote = observation() service = MagicMock() service.chart.return_value = {"candles": quote["premium_history"][-2:]} raw = {"direction": "BULLISH", "expected_change_pct": 8, "predicted_return_lo": 2, "predicted_return_hi": 15, "confidence": "HIGH", "summary": "Observed premium levels."} content = json.dumps({"forecasts": {timeframe: raw for timeframe in fo.forecast_windows(reference(), now=now)}}) with patch("fo_predictor.get_predictor", return_value=model), \ patch("fo_predictor.datetime", wraps=datetime) as clock, \ patch("llm_client.make_chat_call", return_value=(content, "fake", "test")) as call: clock.now.return_value = now windows = fo.forecast_windows(reference(), now=now) result = fo_predictor.contract_forecasts(reference(), quote, windows, service=service) short = result["timeframes"]["INTRADAY"] assert short["available"] and short["limited_history"] assert short["ai_forecast"]["available"] and short["ai_forecast"]["limited_history"] service.chart.return_value = {"candles": quote["premium_history"]} full = fo_predictor.contract_forecasts(reference(), quote, windows, service=service)["timeframes"]["INTRADAY"] assert full["available"] and not full["limited_history"] assert not full["ai_forecast"]["limited_history"] assert full["ai_forecast"]["confidence"] == "HIGH" assert call.call_count == 2 def test_premium_features_are_expiry_and_option_side_aware(): current = datetime.fromisoformat("2026-10-16T10:00:00+05:30") put = fo_predictor.build_features(reference(), observation(), as_of=current) call = fo_predictor.build_features(reference(option_type="CE"), observation(), as_of=current) assert list(put) == fo_predictor.FEATURE_COLUMNS assert np.isfinite(list(put.values())).all() assert put["is_put"] == 1 and call["is_put"] == 0 assert put["minutes_to_expiry"] == 3 * 1440 + 330 assert put["delta"] < 0 < call["delta"] assert put["gamma"] > 0 and put["vega"] > 0 assert "volume_log" not in put @pytest.mark.parametrize("updates", [ {"price": 0}, {"price": float("nan")}, {"underlying_price": 0}, {"iv": float("inf")}, {"iv": -1}, {"quote_time": 0}, ]) def test_premium_features_reject_invalid_or_stale_observations(updates): with pytest.raises(ValueError): fo_predictor.build_features(reference(), observation(**updates), as_of=datetime.fromisoformat("2026-10-16T10:00:00+05:30")) def test_premium_features_reject_observations_after_contract_expiry(): with pytest.raises(ValueError, match="expir"): fo_predictor.build_features(reference(), observation(), as_of=datetime.fromisoformat("2026-10-19T15:30:00+05:30")) def test_premium_history_accepts_nearest_observed_tick_around_lookback(): now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") points = [{"time": now.timestamp() - 60 * offset, "close": 100 - offset} for offset in range(31) if offset not in {15, 16, 17}] points.append({"time": now.timestamp() - 15 * 60 + 3, "close": 85}) result = fo_predictor.premium_history_features(points, as_of=now) assert result["premium_return_15m"] == pytest.approx(100 / 85 - 1) assert result["premium_return_30m"] == pytest.approx(100 / 70 - 1) def test_premium_history_features_never_read_future_prices(): now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") points = [{"time": now.timestamp() - 60 * offset, "close": 100 - offset * 0.1} for offset in range(31)] result = fo_predictor.premium_history_features(points, as_of=now) assert result["premium_return_15m"] == pytest.approx(100 / 98.5 - 1) assert result["premium_return_30m"] == pytest.approx(100 / 97 - 1) assert result["premium_volatility_15m"] >= 0 assert fo_predictor.premium_history_features(points + [{"time": now.timestamp() + 60, "close": 999}], as_of=now) == result with pytest.raises(ValueError, match="history"): fo_predictor.premium_history_features(points[:5], as_of=now) def test_historical_iv_can_be_derived_from_observed_premium_and_spot(): features = fo_predictor.build_features(reference(), observation(iv=None), as_of=datetime.fromisoformat("2026-10-16T10:00:00+05:30")) assert 0 < features["iv"] < 500 assert features["delta"] < 0 def test_exact_contract_target_is_strictly_after_the_feature_timestamp(): frame = pd.DataFrame([ {"contract_id": "NIFTY:2026-10-19:22900:PE", "timestamp": "2026-10-16T10:00:00+05:30", "price": 100}, {"contract_id": "NIFTY:2026-10-19:22900:PE", "timestamp": "2026-10-16T15:00:00+05:30", "price": 80}, ]) labels = fo_predictor.attach_targets(frame, "INTRADAY") assert len(labels) == 1 assert labels.iloc[0]["target_price"] == 80 assert labels.iloc[0]["target_at"] > labels.iloc[0]["timestamp"] def test_next_session_target_does_not_roll_past_expiry(): frame = pd.DataFrame([ {"contract_id": "NIFTY:2026-10-19:22900:PE", "timestamp": "2026-10-19T10:00:00+05:30", "price": 100}, {"contract_id": "NIFTY:2026-10-19:22900:PE", "timestamp": "2026-10-20T15:30:00+05:30", "price": 80}, ]) assert fo_predictor.attach_targets(frame, "1D").empty def test_reachable_labels_use_future_high_low_not_the_closing_direction(): identity = "NSE:OPTION:INDEX:NIFTY:2026-10-19:22900:PE" frame = pd.DataFrame([ {"contract_id": identity, "timestamp": "2026-10-16T10:00:00+05:30", "price": 100, "high": 102, "low": 98}, {"contract_id": identity, "timestamp": "2026-10-16T10:01:00+05:30", "price": 110, "high": 120, "low": 95}, {"contract_id": identity, "timestamp": "2026-10-16T15:00:00+05:30", "price": 90, "high": 110, "low": 80}, {"contract_id": identity, "timestamp": "2026-10-16T15:01:00+05:30", "price": 90, "high": 999, "low": 1}, {"contract_id": identity.replace(":22900:", ":22850:"), "timestamp": "2026-10-16T14:00:00+05:30", "price": 800, "high": 900, "low": 1}, ]) labels = fo_predictor.attach_excursion_targets(frame, "INTRADAY") row = labels[labels["timestamp"] == pd.Timestamp("2026-10-16T04:30:00Z")].iloc[0] assert row["future_high"] == 120 assert row["future_low"] == 80 assert row["target_price"] == 90 assert row["up_log_move"] == pytest.approx(np.log1p(120) - np.log1p(100)) assert row["down_log_move"] == pytest.approx(np.log1p(100) - np.log1p(80)) def test_recent_minute_source_uses_exact_expiry_and_matching_spot_bar(): from research.fo_recent_data import normalize_options raw = pd.DataFrame([ {"date": pd.Timestamp("2026-04-09T09:59:00+05:30"), "open": 100, "high": 110, "low": 90, "close": 100, "volume": 1000, "oi": 5000, "name": "NIFTY", "symbol": "NIFTY2641322900PE", "expiry": "2026-04-13", "strike": 22900, "instrument_type": "PE"}, {"date": pd.Timestamp("2026-04-09T09:59:00+05:30"), "open": 23000, "high": 23020, "low": 22990, "close": 23000, "volume": 1000, "oi": 5000, "name": "NIFTY", "symbol": "NIFTY26APRFUT", "expiry": "2026-04-28", "strike": 0, "instrument_type": "FUT"}, ]) spots = pd.DataFrame({"timestamp": [pd.Timestamp("2026-04-09T10:00:00+05:30")], "underlying_price": [23000]}) result = normalize_options(raw, spots) assert len(result) == 1 assert result.iloc[0]["contract_id"] == "NSE:OPTION:INDEX:NIFTY:2026-04-13:22900:PE" assert result.iloc[0]["timestamp"] == spots.iloc[0]["timestamp"] assert result.iloc[0]["underlying_price"] == 23000 assert normalize_options(raw, spots.assign(timestamp=pd.Timestamp("2026-04-09T10:01:00+05:30"))).empty def test_sampled_reach_targets_keep_full_forward_extremes_for_next_session(): identity = "NSE:OPTION:INDEX:NIFTY:2026-10-19:22900:PE" frame = pd.DataFrame([ {"contract_id": identity, "timestamp": "2026-10-16T10:00:00+05:30", "price": 100, "high": 102, "low": 98, "intraday_high": 120, "intraday_low": 80, "remaining_high": 125, "remaining_low": 75, "session_high": 130, "session_low": 70}, {"contract_id": identity, "timestamp": "2026-10-16T15:00:00+05:30", "price": 90, "high": 95, "low": 85, "intraday_high": 90, "intraday_low": 90, "remaining_high": 95, "remaining_low": 85, "session_high": 130, "session_low": 70}, {"contract_id": identity, "timestamp": "2026-10-19T15:30:00+05:30", "price": 110, "high": 115, "low": 100, "intraday_high": 110, "intraday_low": 110, "remaining_high": 110, "remaining_low": 110, "session_high": 150, "session_low": 60}, ]) intraday = fo_predictor.attach_excursion_targets(frame, "INTRADAY").iloc[0] next_day = fo_predictor.attach_excursion_targets(frame, "1D").iloc[0] assert intraday["future_high"] == 120 and intraday["future_low"] == 80 assert next_day["future_high"] == 150 and next_day["future_low"] == 60 def test_training_labels_never_join_another_strike_or_expiry(): frame = pd.DataFrame([ {"contract_id": "NIFTY:2026-10-19:22900:PE", "timestamp": "2026-10-16T10:00:00+05:30", "price": 100}, {"contract_id": "NIFTY:2026-10-19:22850:PE", "timestamp": "2026-10-16T15:00:00+05:30", "price": 999}, {"contract_id": "NIFTY:2026-10-26:22900:PE", "timestamp": "2026-10-16T15:00:00+05:30", "price": 888}, ]) labels = fo_predictor.attach_targets(frame, "INTRADAY") assert labels.empty def test_rolling_data_without_exact_contract_identity_is_rejected(): with pytest.raises(ValueError, match="contract_id"): fo_predictor.attach_targets(pd.DataFrame({"strike": [22900], "price": [100]}), "INTRADAY") def test_quality_gate_rejects_apparent_accuracy_without_baseline_or_range_improvement(): metrics = {"selected_samples": 1000, "test_expiries": 10, "forecast_coverage": 0.3, "direction_accuracy": 0.80, "range_coverage": 0.85, "majority_baseline": 0.60} assert fo_predictor.promotion_passes(metrics) for updates in ({"direction_accuracy": 0.75}, {"range_coverage": 0.74}, {"majority_baseline": 0.80}, {"forecast_coverage": 0.01}, {"selected_samples": 5}, {"test_expiries": 1}, {"direction_accuracy": 1.5}, {"direction_accuracy": float("nan")}): assert not fo_predictor.promotion_passes({**metrics, **updates}) def test_reachable_gate_requires_nontrivial_targets_and_honest_sample_coverage(): metrics = {"selected_samples": 1000, "test_expiries": 8, "forecast_coverage": 0.4, "target_reach_rate": 0.81, "far_bound_coverage": 0.89, "minimum_target_move_pct": 5.0, "direction_accuracy_all": 0.52} assert fo_predictor.reachable_quality_passes(metrics) for updates in ({"target_reach_rate": 0.75}, {"far_bound_coverage": 0.70}, {"minimum_target_move_pct": 0.01}, {"forecast_coverage": 0.01}, {"selected_samples": 5}, {"test_expiries": 1}): assert not fo_predictor.reachable_quality_passes({**metrics, **updates}) def test_reachable_model_returns_reach_prices_without_claiming_direction_probability(approved_model): model, manifest = approved_model metadata = manifest["timeframes"]["INTRADAY"] metadata.update({"target_kind": "reachable", "reach_adjustments": {"up_target": 0, "down_target": 0, "up_far": 0, "down_far": 0}, "minimum_target_move_pct": 5, "metrics": {"selected_samples": 1000, "test_expiries": 8, "forecast_coverage": 0.4, "target_reach_rate": 0.81, "far_bound_coverage": 0.89, "minimum_target_move_pct": 5.0, "direction_accuracy_all": 0.52}}) models = {"up_near": ConstantQuantile(0.05), "up_target": ConstantQuantile(0.10), "up_far": ConstantQuantile(0.30), "down_near": ConstantQuantile(0.03), "down_target": ConstantQuantile(0.08), "down_far": ConstantQuantile(0.20)} path = model.model_dir + "/INTRADAY.joblib" joblib.dump(models, path) metadata["artifact_sha256"] = train_fo_predictor.archive_digest(path) manifest["range_semantics"] = "reachable_premium_excursions" with open(model.model_dir + "/manifest.json", "w") as stream: json.dump(manifest, stream) now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") result = model.predict(reference(), observation(), fo.forecast_windows(reference(), now=now), now=now) prediction = result["timeframes"]["INTRADAY"] assert prediction["available"] assert prediction["direction"] == "BULLISH" assert prediction["target_price_lo"] <= prediction["expected_target_price"] <= prediction["target_price_hi"] assert prediction["expected_target_price"] > 105 assert prediction["estimated_high"] >= prediction["expected_target_price"] assert prediction["estimated_low"] < 100 assert prediction["confidence_prob"] is None assert prediction["range_semantics"] == "reachable_premium_excursions" assert prediction["target_reach_rate"] == 0.81 def test_experimental_reach_estimates_remain_visible_without_becoming_actionable(): models = {side + role: ConstantQuantile(value) for side in ("up", "down") for role, value in (("_near", 0.01), ("_target", 0.02), ("_far", 0.10))} metadata = {"reach_adjustments": dict.fromkeys(("up_target", "down_target", "up_far", "down_far"), 0), "minimum_target_move_pct": 5, "metrics": {"target_reach_rate": 0.63, "far_bound_coverage": 0.89}} vector = pd.DataFrame([dict.fromkeys(fo_predictor.FEATURE_COLUMNS, 1.0)]) blocked = fo_predictor.reachable_prediction(models, vector, metadata, observation()) assert not blocked["available"] and blocked["no_trade_reason"] == "insufficient_move" experimental = fo_predictor.reachable_prediction(models, vector, metadata, observation(), allow_non_actionable=True) assert experimental["available"] and experimental["actionable"] is False assert 100 < experimental["expected_target_price"] < 105 assert experimental["minimum_target_move_pct"] == 5 def test_expiry_partitions_have_no_contract_or_time_overlap(): rows = [] for expiry in pd.date_range("2024-01-04", periods=20, freq="7D", tz=fo.IST): for sample in range(250): rows.append({"expiry": expiry.date().isoformat(), "contract_id": str(expiry.date()) + str(sample), "timestamp": expiry - pd.Timedelta(days=1), "target_at": expiry}) train, calibration, test = train_fo_predictor.chronological_partitions(pd.DataFrame(rows)) assert train["target_at"].max() < calibration["timestamp"].min() assert calibration["target_at"].max() < test["timestamp"].min() assert not set(train["expiry"]) & set(calibration["expiry"]) assert not set(calibration["expiry"]) & set(test["expiry"]) def test_absent_and_failed_models_never_generate_premiums(tmp_path): model = fo_predictor.FoPremiumPredictor(str(tmp_path)) windows = fo.forecast_windows(reference(), now=datetime.fromisoformat("2026-10-16T10:00:00+05:30")) result = model.predict(reference(), observation(), windows) assert result["forecast_status"] == "not_trained" (tmp_path / "manifest.json").write_text(json.dumps({"schema_version": 1, "feature_columns": fo_predictor.FEATURE_COLUMNS, "supported_underlyings": ["NIFTY"], "instrument_type": "OPTION", "timeframes": {"INTRADAY": { "approved": False, "status": "quality_gate_failed", "metrics": {"direction_accuracy_all": 0.58}}}})) with patch("fo_predictor.joblib.load") as load: result = model.predict(reference(), observation(), windows) assert result["timeframes"]["INTRADAY"]["no_trade_reason"] == "quality_gate_failed" assert "target_price_lo" not in result["timeframes"]["INTRADAY"] load.assert_not_called() def test_approved_model_outputs_ordered_premium_quantiles(approved_model): model, manifest = approved_model now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") result = model.predict(reference(), observation(), fo.forecast_windows(reference(), now=now), now=now) prediction = result["timeframes"]["INTRADAY"] assert result["forecast_status"] == "ready" assert prediction["available"] assert 0 <= prediction["target_price_lo"] <= prediction["expected_target_price"] <= prediction["target_price_hi"] assert prediction["direction"] == "BULLISH" assert prediction["confidence_prob"] == 0.90 assert prediction["holdout_direction_accuracy"] == 0.80 assert prediction["range_semantics"] == "terminal_premium_q10_q90" def test_artifact_tampering_stale_data_and_unsupported_contracts_abstain(approved_model): model, manifest = approved_model now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") windows = fo.forecast_windows(reference(), now=now) stale = model.predict(reference(), observation(stale=True), windows, now=now) assert stale["timeframes"]["INTRADAY"]["no_trade_reason"] == "model_input_unavailable" unsupported = model.predict(reference(underlying="RELIANCE", underlying_type="STOCK"), observation(), windows, now=now) assert unsupported["forecast_status"] == "unsupported_contract" with open(model.model_dir + "/INTRADAY.joblib", "ab") as stream: stream.write(b"tampered") result = model.predict(reference(), observation(), windows, now=now) assert result["timeframes"]["INTRADAY"]["no_trade_reason"] == "model_input_unavailable" def test_high_approved_flag_cannot_bypass_bad_validation_metrics(approved_model): model, manifest = approved_model manifest["timeframes"]["INTRADAY"]["metrics"]["direction_accuracy"] = 0.50 with open(model.model_dir + "/manifest.json", "w") as stream: json.dump(manifest, stream) now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") with patch("fo_predictor.joblib.load") as load: result = model.predict(reference(), observation(), fo.forecast_windows(reference(), now=now), now=now) assert result["forecast_status"] == "quality_gate_failed" load.assert_not_called() def test_stale_training_data_cannot_be_served_as_a_current_model(approved_model): model, manifest = approved_model manifest["data_end"] = "2024-12-31" with open(model.model_dir + "/manifest.json", "w") as stream: json.dump(manifest, stream) now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") with patch("fo_predictor.joblib.load") as load: result = model.predict(reference(), observation(), fo.forecast_windows(reference(), now=now), now=now) assert result["forecast_status"] == "model_stale" load.assert_not_called() @pytest.mark.parametrize("experimental", [False, True]) def test_explicit_experimental_publication_keeps_failed_metrics_and_age_visible(approved_model, experimental): model, manifest = approved_model manifest["data_end"] = "2026-04-13" manifest["timeframes"]["INTRADAY"]["approved"] = False manifest["timeframes"]["INTRADAY"]["metrics"]["direction_accuracy"] = 0.60 if experimental: manifest["serving_mode"] = "experimental" with open(model.model_dir + "/manifest.json", "w") as stream: json.dump(manifest, stream) now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") windows = fo.forecast_windows(reference(), now=now) result = model.predict(reference(), observation(), windows, now=now) prediction = result["timeframes"]["INTRADAY"] assert prediction["available"] is experimental if experimental: assert result["forecast_status"] == "experimental" assert prediction["experimental"] is True assert prediction["validation_passed"] is False assert prediction["model_data_stale"] is True assert prediction["accuracy_validated"] is False assert prediction["confidence"] == "LOW" and prediction["confidence_prob"] is None stale = model.predict(reference(), observation(stale=True), windows, now=now) assert not stale["timeframes"]["INTRADAY"]["available"] assert stale["forecast_status"] == "model_input_unavailable" else: assert result["forecast_status"] == "quality_gate_failed" def test_ai_review_cannot_replace_validated_model_numbers(monkeypatch): monkeypatch.setenv("FO_AI_ENABLED", "1") prediction = {"available": True, "direction": "BULLISH", "target_price_lo": 90, "expected_target_price": 110, "target_price_hi": 130, "end_at": "2026-10-16T15:00:00+05:30"} with patch("llm_client.make_chat_call", return_value=(json.dumps({"summary": "Time decay remains a risk.", "target_price_hi": 999999}), "fake", "test")) as call: review = fo_predictor.review_forecast(reference(), observation(), prediction) assert review["available"] assert review["target_price_hi"] == 130 assert not review["independent_range"] assert call.call_args.kwargs["call_timeout"] == 6 with patch("llm_client.make_chat_call") as call: review = fo_predictor.review_forecast(reference(), observation(), {"available": False}) assert not review["available"] call.assert_not_called() def test_ai_determines_premium_change_when_ml_is_unavailable(monkeypatch): monkeypatch.setenv("FO_AI_ENABLED", "1") now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") windows = fo.forecast_windows(reference(), now=now) failed = {"forecast_status": "quality_gate_failed", "forecast_metrics": {}, "timeframes": { timeframe: {"available": False, "no_trade_reason": "quality_gate_failed", "end_at": window["end_at"]} for timeframe, window in windows.items()}} predictor = MagicMock() predictor.has_approved_models.return_value = False predictor.predict.return_value = failed service = MagicMock() service.chart.return_value = {"candles": observation()["premium_history"]} content = json.dumps({"forecasts": { timeframe: {"direction": "BEARISH", "expected_change_pct": -8, "predicted_return_lo": -15, "predicted_return_hi": -2, "confidence": "MEDIUM", "summary": "The put premium is weakening."} for timeframe in windows}}) with patch("fo_predictor.get_predictor", return_value=predictor), \ patch("fo_predictor.datetime", wraps=datetime) as clock, \ patch("llm_client.make_chat_call", return_value=(content, "fake", "test")) as call: clock.now.return_value = now result = fo_predictor.contract_forecasts(reference(), observation(), windows, service=service) prediction = result["timeframes"]["INTRADAY"] assert not prediction["available"] assert result["forecast_status"] == "quality_gate_failed" ai = prediction["ai_forecast"] assert ai["available"] and ai["independent_range"] assert ai["direction"] == "BEARISH" assert ai["expected_change_pct"] == -8 assert ai["expected_target_price"] == 92 assert ai["target_price_lo"] == 85 and ai["target_price_hi"] == 98 assert ai["accuracy_validated"] is False assert "confidence_prob" not in ai call.assert_called_once() assert call.call_args.kwargs["call_timeout"] == 20 @pytest.mark.parametrize("direction,change,price,calls", [("BULLISH", 8, 109, 2), ("BEARISH", -8, 91, 2), ("NEUTRAL", 0, 106, 2), ("BULLISH", 8, 103, 1)]) def test_ai_cache_rechecks_targets_against_new_contract_price(monkeypatch, direction, change, price, calls): monkeypatch.setenv("FO_AI_ENABLED", "1") now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") windows = fo.forecast_windows(reference(), now=now) raw = {"direction": direction, "expected_change_pct": change, "predicted_return_lo": change - 5, "predicted_return_hi": change + 5, "confidence": "LOW", "summary": "Observed premium momentum."} content = json.dumps({"forecasts": {timeframe: raw for timeframe in windows}}) with patch("fo_predictor.datetime", wraps=datetime) as clock, \ patch("llm_client.make_chat_call", return_value=(content, "fake", "test")) as call: clock.now.return_value = now fo_predictor.ai_forecasts(reference(), observation(), windows) changed = observation(price=price, quote_time=now.timestamp() + 60) clock.now.return_value = datetime.fromtimestamp(changed["quote_time"], fo.IST) result = fo_predictor.ai_forecasts(reference(), changed, windows) assert call.call_count == calls assert result["INTRADAY"]["current_price"] == (price if calls == 2 else 100) def test_explicit_refresh_always_calls_ai_again(monkeypatch): monkeypatch.setenv("FO_AI_ENABLED", "1") now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") windows = fo.forecast_windows(reference(), now=now) raw = {"direction": "BEARISH", "expected_change_pct": -8, "predicted_return_lo": -15, "predicted_return_hi": -2, "confidence": "LOW", "summary": "Observed premium weakness."} content = json.dumps({"forecasts": {timeframe: raw for timeframe in windows}}) with patch("fo_predictor.datetime", wraps=datetime) as clock, \ patch("llm_client.make_chat_call", return_value=(content, "fake", "test")) as call: clock.now.return_value = now first = fo_predictor.ai_forecasts(reference(), observation(), windows) cached = fo_predictor.ai_forecasts(reference(), observation(), windows) assert first["INTRADAY"]["cached"] is False assert first["INTRADAY"]["generated_at"] == now.isoformat() assert cached["INTRADAY"]["cached"] is True assert call.call_count == 1 fresh = fo_predictor.ai_forecasts(reference(), observation(), windows, refresh=True) assert fresh["INTRADAY"]["cached"] is False assert call.call_count == 2 def test_invalid_today_response_is_retried_without_replacing_valid_1d(monkeypatch): monkeypatch.setenv("FO_AI_ENABLED", "1") now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") windows = fo.forecast_windows(reference(), now=now) valid = {"direction": "BEARISH", "expected_change_pct": -8, "predicted_return_lo": -15, "predicted_return_hi": -2, "confidence": "LOW", "summary": "Observed premium weakness."} partial = json.dumps({"forecasts": {"INTRADAY": {**valid, "direction": "BULLISH"}, "1D": valid}}) repaired = json.dumps({"forecasts": {"INTRADAY": {**valid, "expected_change_pct": -6}}}) with patch("fo_predictor.datetime", wraps=datetime) as clock, \ patch("llm_client.make_chat_call", side_effect=[(partial, "fake", "first"), (repaired, "fake", "repair")]) as call: clock.now.return_value = now result = fo_predictor.ai_forecasts(reference(), observation(), windows) assert result["INTRADAY"]["available"] and result["1D"]["available"] assert result["INTRADAY"]["expected_change_pct"] == -6 assert result["1D"]["expected_change_pct"] == -8 assert result["1D"]["source"] == "fake:first" retry_context = json.loads(call.call_args.kwargs["messages"][1]["content"]) assert set(retry_context["horizons"]) == {"INTRADAY"} assert "INTRADAY" in retry_context["validation_errors"] assert 0 < call.call_args.kwargs["call_timeout"] <= 20 @pytest.mark.parametrize("exhaust_budget", [False, True]) def test_invalid_forecast_is_not_cached_and_repairs_share_the_call_budget(monkeypatch, exhaust_budget): monkeypatch.setenv("FO_AI_ENABLED", "1") now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") windows = fo.forecast_windows(reference(), now=now) valid = {"direction": "BEARISH", "expected_change_pct": -8, "predicted_return_lo": -15, "predicted_return_hi": -2, "confidence": "LOW", "summary": "Observed premium weakness."} bad = {**valid, "direction": "BULLISH"} partial = json.dumps({"forecasts": {"INTRADAY": bad, "1D": valid}}) outputs = [(partial, "fake", "first")] if not exhaust_budget: outputs += [(json.dumps({"forecasts": {"INTRADAY": bad}}), "fake", "repair"), (json.dumps({"forecasts": {timeframe: valid for timeframe in windows}}), "fake", "next")] elapsed = iter([0.0, 20.0]) if exhaust_budget else None with patch("fo_predictor.datetime", wraps=datetime) as clock, \ patch("fo_predictor.time.monotonic", side_effect=(lambda: next(elapsed)) if elapsed else None, return_value=0), \ patch("llm_client.make_chat_call", side_effect=outputs) as call: clock.now.return_value = now first = fo_predictor.ai_forecasts(reference(), observation(), windows) assert not first["INTRADAY"]["available"] and first["1D"]["available"] if exhaust_budget: assert call.call_count == 1 else: second = fo_predictor.ai_forecasts(reference(), observation(), windows) assert second["INTRADAY"]["available"] assert call.call_count == 3 def test_ai_uses_separate_today_and_future_session_horizons(monkeypatch): monkeypatch.setenv("FO_AI_ENABLED", "1") now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") windows = fo.forecast_windows(reference(), now=now) forecasts = {} for timeframe, change in (("INTRADAY", -8), ("1D", -16)): forecasts[timeframe] = {"direction": "BEARISH", "expected_change_pct": change, "predicted_return_lo": change - 5, "predicted_return_hi": change + 3, "confidence": "LOW", "summary": timeframe + " premium estimate."} with patch("fo_predictor.datetime", wraps=datetime) as clock, \ patch("llm_client.make_chat_call", return_value=(json.dumps({"forecasts": forecasts}), "fake", "test")) as call: clock.now.return_value = now result = fo_predictor.ai_forecasts(reference(), observation(), windows) assert result["INTRADAY"]["expected_target_price"] == 92 assert result["1D"]["expected_target_price"] == 84 assert result["INTRADAY"]["end_at"][:10] == "2026-10-16" assert result["1D"]["end_at"][:10] == "2026-10-19" context = json.loads(call.call_args.kwargs["messages"][1]["content"]) assert call.call_args.kwargs["json_mode"] is True assert context["forecast_at"] == now.isoformat() assert context["option_context"]["moneyness"] == "OTM" assert context["horizons"]["INTRADAY"]["calendar_minutes"] == 300 assert context["horizons"]["1D"]["calendar_minutes"] > 300 def test_independent_ai_can_reach_healthy_provider_after_one_provider_times_out(monkeypatch): from concurrent.futures import Future import llm_client monkeypatch.setenv("FO_AI_ENABLED", "1") monkeypatch.setenv("GEMINI_API_KEY", "test-gemini") monkeypatch.setenv("GROQ_API_KEY", "test-groq") monkeypatch.setenv("LLM_PROVIDER_TIMEOUT_SECS", "6") now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") elapsed = [0.0] raw = {"direction": "BULLISH", "expected_change_pct": 8, "predicted_return_lo": 2, "predicted_return_hi": 15, "confidence": "MEDIUM", "summary": "Premium momentum is positive."} response = MagicMock(status_code=200) response.json.return_value = {"choices": [{"message": {"content": json.dumps({"forecasts": {"INTRADAY": raw}})}}]} attempted = [] formats = [] def post(url, **kwargs): attempted.append(url) formats.append(kwargs["json"].get("response_format")) if "googleapis" in url: elapsed[0] += kwargs["timeout"] raise llm_client.requests.exceptions.Timeout("First provider timed out") return response def submit(operation, *args, **kwargs): future = Future() try: future.set_result(operation(*args, **kwargs)) except Exception as error: future.set_exception(error) return future with patch("fo_predictor.datetime", wraps=datetime) as clock, \ patch("llm_client.time.monotonic", side_effect=lambda: elapsed[0]), \ patch("llm_client._get_cloud_order", return_value=["gemini", "groq"]), \ patch("llm_client.provider_key_status", return_value={"gemini": True, "groq": True}), \ patch("llm_client._is_provider_available", return_value=True), \ patch("llm_client._all_cloud_daily_exhausted", return_value=False), \ patch("llm_client._LLM_DISABLED_UNTIL", 0), \ patch("llm_client._mark_rate_limited"), patch("llm_client._mark_ok"), \ patch("llm_client._TRANSPORT_POOL") as pool, \ patch("llm_client.requests.post", side_effect=post): clock.now.return_value = now pool.submit.side_effect = submit result = fo_predictor.ai_forecasts(reference(), observation(), { "INTRADAY": fo.forecast_windows(reference(), now=now)["INTRADAY"]}) assert result["INTRADAY"]["available"] assert result["INTRADAY"]["source"].startswith("groq:") assert len(attempted) == 2 assert formats == [{"type": "json_object"}, {"type": "json_object"}] @pytest.mark.parametrize("updates", [ {"expected_change_pct": True}, {"expected_change_pct": float("nan")}, {"predicted_return_lo": -101}, {"predicted_return_hi": 501}, {"predicted_return_lo": 20}, {"direction": "BEARISH"}, {"confidence": "90%"}, {"summary": ""}, ]) def test_independent_ai_rejects_invalid_change_estimates(updates, monkeypatch): monkeypatch.setenv("FO_AI_ENABLED", "1") now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") raw = {"direction": "BULLISH", "expected_change_pct": 8, "predicted_return_lo": 2, "predicted_return_hi": 15, "confidence": "MEDIUM", "summary": "Premium momentum is positive.", **updates} with patch("fo_predictor.datetime", wraps=datetime) as clock, patch("llm_client.make_chat_call", return_value=(json.dumps({"forecasts": {"INTRADAY": raw}}), "fake", "test")): clock.now.return_value = now result = fo_predictor.ai_forecasts(reference(), observation(), fo.forecast_windows(reference(), now=now)) assert not result["INTRADAY"]["available"] assert "target_price_lo" not in result["INTRADAY"] @pytest.mark.parametrize("updates", [{"stale": True}, {"quote_time": 0}, {"premium_history": []}]) def test_independent_ai_does_not_call_provider_for_stale_or_missing_data(updates, monkeypatch): monkeypatch.setenv("FO_AI_ENABLED", "1") now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") with patch("fo_predictor.datetime", wraps=datetime) as clock, patch("llm_client.make_chat_call") as call: clock.now.return_value = now result = fo_predictor.ai_forecasts(reference(), observation(**updates), fo.forecast_windows(reference(), now=now)) assert result["INTRADAY"]["no_trade_reason"] == "ai_input_unavailable" call.assert_not_called() def test_independent_ai_discards_response_finished_after_window_close(monkeypatch): monkeypatch.setenv("FO_AI_ENABLED", "1") now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") after = datetime.fromisoformat("2026-10-16T15:00:01+05:30") raw = {"direction": "BULLISH", "expected_change_pct": 8, "predicted_return_lo": 2, "predicted_return_hi": 15, "confidence": "MEDIUM", "summary": "Premium momentum is positive."} with patch("fo_predictor.datetime", wraps=datetime) as clock, patch("llm_client.make_chat_call", return_value=(json.dumps({"forecasts": {"INTRADAY": raw}}), "fake", "test")): clock.now.side_effect = [now, after] result = fo_predictor.ai_forecasts(reference(), observation(), {"INTRADAY": fo.forecast_windows(reference(), now=now)["INTRADAY"]}) assert not result["INTRADAY"]["available"] assert result["INTRADAY"]["no_trade_reason"] == "session_closed" def test_independent_ai_accepts_verified_delayed_snapshot_without_faking_freshness(monkeypatch): monkeypatch.setenv("FO_AI_ENABLED", "1") now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") raw = {"direction": "BULLISH", "expected_change_pct": 8, "predicted_return_lo": 2, "predicted_return_hi": 15, "confidence": "MEDIUM", "summary": "Premium momentum is positive."} delayed = observation(stale=True, ai_snapshot_usable=True, quote_time=now.timestamp() - 180, premium_history=[{"time": point["time"] - 180, "close": point["close"]} for point in observation()["premium_history"]]) with patch("fo_predictor.datetime", wraps=datetime) as clock, patch("llm_client.make_chat_call", return_value=(json.dumps({"forecasts": {"INTRADAY": raw}}), "fake", "test")): clock.now.return_value = now result = fo_predictor.ai_forecasts(reference(), delayed, fo.forecast_windows(reference(), now=now)) ai = result["INTRADAY"] assert ai["available"] and ai["input_delayed"] assert ai["as_of"] == "2026-10-16T09:57:00+05:30" assert ai["data_age_seconds"] == 180 @pytest.fixture(autouse=True) def no_provider_retry_pause(monkeypatch): monkeypatch.setattr(fo_predictor, "AI_RETRY_PAUSE_SECS", 0) monkeypatch.setattr(fo_predictor, "AI_SLOT_WAIT_SECS", 0.05) @pytest.fixture(autouse=True) def no_provider_cooldown(monkeypatch): monkeypatch.setattr("llm_client.seconds_until_provider_available", lambda: 0.0) def test_provider_cooldown_is_waited_out_instead_of_failing_at_once(monkeypatch): waits = iter([2.0, 0.0]) monkeypatch.setattr("llm_client.seconds_until_provider_available", lambda: next(waits)) sleeps = [] monkeypatch.setattr(fo_predictor.time, "sleep", sleeps.append) call = lambda **kwargs: ("ok", "fake", "test") assert fo_predictor._chat_with_retry(call, [], 20) == ("ok", "fake", "test") assert [value for value in sleeps if value] == [pytest.approx(2.2)] def test_cooldown_longer_than_the_wait_budget_fails_fast(monkeypatch): monkeypatch.setattr("llm_client.seconds_until_provider_available", lambda: fo_predictor.AI_COOLDOWN_WAIT_SECS + 5) sleeps = [] monkeypatch.setattr(fo_predictor.time, "sleep", sleeps.append) with pytest.raises(RuntimeError): fo_predictor._chat_with_retry(MagicMock(side_effect=RuntimeError("cooling down")), [], 20) assert not any(sleeps) # only the (zeroed) retry pause, never a cooldown wait def test_total_cooldown_wait_is_bounded_across_attempts(monkeypatch): monkeypatch.setattr("llm_client.seconds_until_provider_available", lambda: 6.0) sleeps = [] monkeypatch.setattr(fo_predictor.time, "sleep", sleeps.append) with pytest.raises(RuntimeError): fo_predictor._chat_with_retry(MagicMock(side_effect=RuntimeError("down")), [], 20) # 6.2s on the first attempt leaves 3.8s, too little for the second 6s cooldown. waited = [value for value in sleeps if value] # drop the (zeroed) retry pause between attempts assert waited == [pytest.approx(6.2)] assert sum(waited) <= fo_predictor.AI_COOLDOWN_WAIT_SECS + 0.2 def _valid_ai_content(windows): raw = {"direction": "BEARISH", "expected_change_pct": -8, "predicted_return_lo": -15, "predicted_return_hi": -2, "confidence": "LOW", "summary": "Observed premium weakness."} return json.dumps({"forecasts": {timeframe: raw for timeframe in windows}}) def test_transient_provider_failure_is_retried_with_a_rotated_provider(monkeypatch): monkeypatch.setenv("FO_AI_ENABLED", "1") now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") windows = fo.forecast_windows(reference(), now=now) outputs = [TimeoutError("Cloud queue deadline exceeded"), (_valid_ai_content(windows), "fake", "retry")] with patch("fo_predictor.datetime", wraps=datetime) as clock, \ patch("llm_client.make_chat_call", side_effect=outputs) as call: clock.now.return_value = now result = fo_predictor.ai_forecasts(reference(), observation(), windows) assert result["INTRADAY"]["available"] and result["INTRADAY"]["source"] == "fake:retry" assert [item.kwargs["task_offset"] for item in call.call_args_list] == [0, 1] def test_persistent_provider_failure_stops_after_bounded_attempts(monkeypatch): monkeypatch.setenv("FO_AI_ENABLED", "1") now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") windows = fo.forecast_windows(reference(), now=now) with patch("fo_predictor.datetime", wraps=datetime) as clock, \ patch("llm_client.make_chat_call", side_effect=RuntimeError("down")) as call: clock.now.return_value = now result = fo_predictor.ai_forecasts(reference(), observation(), windows) assert call.call_count == fo_predictor.AI_PROVIDER_ATTEMPTS assert result["INTRADAY"]["no_trade_reason"] == "ai_provider_unavailable" def test_forecast_waits_for_a_free_ai_slot_and_reports_busy_only_after_the_wait(monkeypatch): monkeypatch.setenv("FO_AI_ENABLED", "1") now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") windows = fo.forecast_windows(reference(), now=now) held = [fo_predictor._review_slots.acquire() for _ in range(3)] try: with patch("fo_predictor.datetime", wraps=datetime) as clock, patch("llm_client.make_chat_call") as call: clock.now.return_value = now result = fo_predictor.ai_forecasts(reference(), observation(), windows) call.assert_not_called() assert result["INTRADAY"]["no_trade_reason"] == "ai_busy" finally: for _ in held: fo_predictor._review_slots.release() def test_independent_ai_accepts_fenced_json_and_reports_provider_failure(monkeypatch): monkeypatch.setenv("FO_AI_ENABLED", "1") now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") raw = {"direction": "BULLISH", "expected_change_pct": 8, "predicted_return_lo": 2, "predicted_return_hi": 15, "confidence": "MEDIUM", "summary": "Premium momentum is positive."} content = "```json\n" + json.dumps({"forecasts": {"INTRADAY": raw}}) + "\n```" with patch("fo_predictor.datetime", wraps=datetime) as clock, patch("llm_client.make_chat_call", return_value=(content, "fake", "test")): clock.now.return_value = now result = fo_predictor.ai_forecasts(reference(), observation(), fo.forecast_windows(reference(), now=now)) assert result["INTRADAY"]["available"] fo_predictor._reviews.clear() with patch("fo_predictor.datetime", wraps=datetime) as clock, patch("llm_client.make_chat_call", side_effect=RuntimeError("Unavailable")): clock.now.return_value = now result = fo_predictor.ai_forecasts(reference(), observation(), fo.forecast_windows(reference(), now=now)) assert result["INTRADAY"]["no_trade_reason"] == "ai_provider_unavailable" assert result["INTRADAY"]["independent_range"] def test_archive_import_matches_minute_closes_and_excludes_conflicting_rows(tmp_path): archive = tmp_path / "history.zip" spot = "datetime,close\n2024-04-01 09:59,22000\n" options = "datetime,strike_price,right,open,high,low,close,open_interest,volume\n" options += "09:59,22000,PE,100,110,90,100,1000,50\n" options += "09:59,22000,PE,100,110,90,100,1000,100\n" options += "09:59,21950,CE,100,110,90,100,1000,50\n" with zipfile.ZipFile(archive, "w") as source: source.writestr("2024/2024Nifty/Nifty-2024APR.csv", spot) source.writestr("2024/2024APR/NIFTY-04APR24-01APR24.csv", options) history = train_fo_predictor.prepare_history(archive) assert len(history) == 1 assert history.iloc[0]["timestamp"] == pd.Timestamp("2024-04-01T10:00:00+05:30") assert history.iloc[0]["underlying_price"] == 22000 assert history.iloc[0]["strike"] == 21950 assert history.iloc[0]["contract_id"] == "NSE:OPTION:INDEX:NIFTY:2024-04-04:21950:CE" def test_training_rows_reuse_point_in_time_features_for_both_horizons(): timestamps = list(pd.date_range("2024-04-01T09:30:00+05:30", periods=31, freq="min")) timestamps += [pd.Timestamp("2024-04-01T15:00:00+05:30"), pd.Timestamp("2024-04-02T15:30:00+05:30")] history = pd.DataFrame([{"contract_id": "NSE:OPTION:INDEX:NIFTY:2024-04-04:22000:CE", "timestamp": pd.Timestamp(timestamp), "expiry": "2024-04-04", "price": 100, "strike": 22000, "option_type": "CE", "underlying_price": 22000, "volume": 100, "open_interest": 1000} for timestamp in timestamps]) history = history.assign(**dict.fromkeys(fo_predictor.HISTORY_COLUMNS, 0.0)) rows = train_fo_predictor.training_rows(history) assert rows.columns.is_unique assert set(rows["timeframe"]) == {"INTRADAY", "1D"} assert len(rows) == 2 assert rows.iloc[0]["iv"] == rows.iloc[1]["iv"] assert (rows["target_at"] > rows["timestamp"]).all() def test_reachable_training_rows_keep_forward_extremes_out_of_feature_vector(): identity = "NSE:OPTION:INDEX:NIFTY:2026-10-19:22900:PE" rows = [] for timestamp, price in (("2026-10-16T10:00:00+05:30", 100), ("2026-10-16T15:00:00+05:30", 90), ("2026-10-19T15:30:00+05:30", 80)): rows.append({"contract_id": identity, "timestamp": pd.Timestamp(timestamp), "expiry": "2026-10-19", "price": price, "high": price + 2, "low": price - 2, "strike": 22900, "option_type": "PE", "underlying_price": 22950, "volume": 100, "open_interest": 1000, "intraday_high": 120, "intraday_low": 80, "remaining_high": 125, "remaining_low": 75, "session_high": 140, "session_low": 70, **dict.fromkeys(fo_predictor.HISTORY_COLUMNS, 0.0)}) result = train_fo_predictor.training_rows(pd.DataFrame(rows), target_kind="reachable") assert set(result["timeframe"]) == {"INTRADAY", "1D"} assert "up_log_move" in result and "down_log_move" in result assert not set(fo_predictor.FEATURE_COLUMNS) & {"future_high", "future_low", "up_log_move", "down_log_move"} assert result[result["timeframe"] == "1D"].iloc[0]["future_high"] == 140 @pytest.mark.parametrize("invalid", [False, True]) def test_archive_download_is_bounded_and_atomic(tmp_path, invalid): buffer = io.BytesIO() with zipfile.ZipFile(buffer, "w") as archive: archive.writestr("schema.csv", "field\nvalue\n") payload = b"not an archive" if invalid else buffer.getvalue() response = MagicMock() response.__enter__.return_value = response response.headers = {"Content-Length": str(len(payload))} response.iter_content.return_value = [payload] destination = str(tmp_path / "data.zip") with patch.object(train_fo_predictor.requests, "get", return_value=response) as get: if invalid: with pytest.raises(ValueError, match="archive"): train_fo_predictor.download_archive(destination) assert not list(tmp_path.iterdir()) else: assert train_fo_predictor.download_archive(destination) == destination assert zipfile.is_zipfile(destination) assert train_fo_predictor.download_archive(destination) == destination get.assert_called_once() @pytest.mark.parametrize("target_kind,experimental", [("terminal", False), ("reachable", False), ("reachable", True)]) def test_training_orchestration_saves_only_quality_approved_test_fixtures(tmp_path, target_kind, experimental): rows = [] for expiry in pd.date_range("2024-01-04", periods=25, freq="7D", tz=fo.IST): for index in range(300): put = index % 2 rows.append({**dict.fromkeys(fo_predictor.FEATURE_COLUMNS, 1.0), "is_put": put, "expiry": expiry.date().isoformat(), "timestamp": expiry - pd.Timedelta(days=1), "target_at": expiry, "price": 100, "target_price": 90 if put else 110, "target_log_return": np.log1p(90 if put else 110) - np.log1p(100), "timeframe": "INTRADAY"}) data = pd.DataFrame(rows) data = pd.concat([data, data.assign(timeframe="1D")], ignore_index=True) if target_kind == "reachable": data = data.assign(future_high=125, future_low=80, up_log_move=np.log1p(125) - np.log1p(100), down_log_move=np.log1p(100) - np.log1p(80)) def quantile(**parameters): return ConstantQuantile({0.1: -0.2, 0.2: 0.1, 0.5: 0, 0.9: 0.3}[parameters["quantile"]]) with patch.object(train_fo_predictor, "HistGradientBoostingRegressor", side_effect=quantile), \ patch.object(train_fo_predictor, "HistGradientBoostingClassifier", return_value=FixtureDirection()), \ patch.object(train_fo_predictor, "CalibratedClassifierCV", return_value=FixtureDirection()), \ patch.object(train_fo_predictor, "reachable_quality_passes", wraps=fo_predictor.reachable_quality_passes) as gate: if experimental: gate.return_value = False train = train_fo_predictor.train_reachable_models if target_kind == "reachable" else train_fo_predictor.train_models options = {"publish_experimental": True} if experimental else {} manifest = train(data, str(tmp_path), "test-fixture-only", **options) assert (tmp_path / "manifest.json").exists() assert (manifest.get("serving_mode") == "experimental") is experimental for metadata in manifest["timeframes"].values(): assert metadata["approved"] is not experimental if target_kind == "terminal": assert metadata["metrics"]["majority_baseline"] == 0.5 else: assert metadata["target_kind"] == "reachable" assert metadata["metrics"]["target_reach_rate"] > 0.75 assert metadata["metrics"]["minimum_target_move_pct"] >= 5 assert (tmp_path / metadata["artifact"]).exists() assert metadata["train_expiry_end"] < metadata["test_expiry_start"] def test_contract_service_uses_fresh_chart_and_abstains_on_stale_chart(approved_model, monkeypatch): model, manifest = approved_model now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") windows = fo.forecast_windows(reference(), now=now) service = MagicMock() service.chart.return_value = {"candles": observation()["premium_history"]} monkeypatch.setenv("FO_AI_ENABLED", "0") with patch("fo_predictor.get_predictor", return_value=model), patch("fo_predictor.datetime", wraps=datetime) as clock: clock.now.return_value = now result = fo_predictor.contract_forecasts(reference(), observation(), windows, service=service) assert result["forecast_status"] == "ready" assert not result["timeframes"]["INTRADAY"]["ai_forecast"]["available"] service.chart.return_value = {"candles": [{"time": now.timestamp() - 300, "close": 100}]} result = fo_predictor.contract_forecasts(reference(), observation(), windows, service=service) assert result["forecast_status"] == "model_input_unavailable" result = fo_predictor.contract_forecasts(reference(), observation(), windows) assert result["forecast_status"] == "model_input_unavailable" def test_contract_context_keeps_premium_and_underlying_from_same_quote(monkeypatch, approved_model): model, manifest = approved_model monkeypatch.setenv("FO_AI_ENABLED", "1") now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") service = MagicMock() service.chart.return_value = {"candles": [ {"time": point["time"] - 60, "close": 200} for point in observation()["premium_history"]]} with patch("fo_predictor.get_predictor", return_value=model), \ patch("fo_predictor.datetime", wraps=datetime) as clock, \ patch("fo_predictor.ai_forecasts", return_value={}) as ai: clock.now.return_value = now fo_predictor.contract_forecasts(reference(), observation(), fo.forecast_windows(reference(), now=now), service=service) context = ai.call_args.args[1] assert context["price"] == 100 assert context["underlying_price"] == 22950 assert context["quote_time"] == now.timestamp() assert context["premium_history"][-1] == {"time": now.timestamp(), "close": 100} @pytest.mark.parametrize("failure,reason", [ ("quote", "ai_quote_unavailable"), ("stale", "ai_stale_data"), ]) def test_contract_forecast_reports_data_failure_without_hiding_ml_status(approved_model, monkeypatch, failure, reason): model, manifest = approved_model manifest["timeframes"]["INTRADAY"]["approved"] = False with open(model.model_dir + "/manifest.json", "w") as stream: json.dump(manifest, stream) monkeypatch.setenv("FO_AI_ENABLED", "1") now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") service = MagicMock() service.chart.return_value = {"candles": observation()["premium_history"]} quote = observation() if failure == "quote": quote = None elif failure == "stale": quote["quote_time"] = now.timestamp() - 301 with patch("fo_predictor.get_predictor", return_value=model), \ patch("fo_predictor.datetime", wraps=datetime) as clock, \ patch("llm_client.make_chat_call") as call: clock.now.return_value = now result = fo_predictor.contract_forecasts(reference(), quote, fo.forecast_windows(reference(), now=now), service=service) assert result["forecast_status"] == "quality_gate_failed" assert result["timeframes"]["INTRADAY"]["ai_forecast"]["no_trade_reason"] == reason call.assert_not_called() if failure in {"quote", "stale"}: service.chart.assert_not_called() def _snapshot_only_context(approved_model, monkeypatch, *, chart): model, manifest = approved_model monkeypatch.setenv("FO_AI_ENABLED", "1") manifest["timeframes"]["INTRADAY"]["approved"] = False with open(model.model_dir + "/manifest.json", "w") as stream: json.dump(manifest, stream) now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") service = MagicMock() if isinstance(chart, Exception): service.chart.side_effect = chart else: service.chart.return_value = {"candles": chart(now)} raw = {"direction": "BEARISH", "expected_change_pct": -8, "predicted_return_lo": -15, "predicted_return_hi": -2, "confidence": "HIGH", "summary": "Only a snapshot is available."} content = json.dumps({"forecasts": {tf: raw for tf in fo.forecast_windows(reference(), now=now)}}) with patch("fo_predictor.get_predictor", return_value=model), \ patch("fo_predictor.datetime", wraps=datetime) as clock, \ patch("llm_client.make_chat_call", return_value=(content, "fake", "test")) as call: clock.now.return_value = now result = fo_predictor.contract_forecasts(reference(), observation(), fo.forecast_windows(reference(), now=now), service=service) return result, call def test_newly_added_contract_gets_limited_ai_forecast_from_a_single_observation(approved_model, monkeypatch): result, call = _snapshot_only_context(approved_model, monkeypatch, chart=lambda now: [{"time": now.timestamp(), "close": 100}]) ai = result["timeframes"]["INTRADAY"]["ai_forecast"] assert ai["available"] and ai["limited_history"] and ai["history_samples"] == 1 assert ai["confidence"] == "LOW" call.assert_called_once() @pytest.mark.parametrize("chart", [ fo.FoUnavailable("NSE unavailable"), lambda now: [], lambda now: [{"time": now.timestamp() - 900, "close": 95}], # chart lags the quote by 15 minutes ]) def test_missing_or_lagging_chart_never_blocks_the_ai_forecast(approved_model, monkeypatch, chart): result, call = _snapshot_only_context(approved_model, monkeypatch, chart=chart) ai = result["timeframes"]["INTRADAY"]["ai_forecast"] assert ai["available"], ai assert ai["limited_history"] and ai["confidence"] == "LOW" call.assert_called_once() def test_stale_quote_still_reports_stale_data_even_when_chart_is_fresh(approved_model, monkeypatch): model, _ = approved_model monkeypatch.setenv("FO_AI_ENABLED", "1") now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") service = MagicMock() service.chart.return_value = {"candles": observation()["premium_history"]} quote = observation() quote["quote_time"] = now.timestamp() - 301 with patch("fo_predictor.get_predictor", return_value=model), \ patch("fo_predictor.datetime", wraps=datetime) as clock, patch("llm_client.make_chat_call") as call: clock.now.return_value = now result = fo_predictor.contract_forecasts(reference(), quote, fo.forecast_windows(reference(), now=now), service=service) assert result["timeframes"]["INTRADAY"]["ai_forecast"]["no_trade_reason"] == "ai_stale_data" call.assert_not_called() def test_premarket_outlook_falls_back_to_last_snapshot_when_chart_history_is_missing(): now = datetime.fromisoformat("2026-10-19T08:30:00+05:30") last_close = datetime.fromisoformat("2026-10-16T15:29:00+05:30").timestamp() quote = {"price": 100, "underlying_price": 22950, "quote_time": last_close} for chart in (fo.FoUnavailable("NSE unavailable"), {"candles": []}): service = MagicMock() if isinstance(chart, Exception): service.chart.side_effect = chart else: service.chart.return_value = chart observed = fo_predictor.outlook_observation(reference(), quote, service, now) assert observed["outlook"] and observed["premium_history"] == [{"time": last_close, "close": 100.0}] assert observed["quote_time"] == last_close def test_model_abstains_outside_training_domain_and_below_confidence_threshold(approved_model): model, manifest = approved_model now = datetime.fromisoformat("2026-10-16T10:00:00+05:30") windows = fo.forecast_windows(reference(), now=now) manifest["timeframes"]["INTRADAY"]["min_probability"] = 0.95 with open(model.model_dir + "/manifest.json", "w") as stream: json.dump(manifest, stream) result = model.predict(reference(), observation(), windows, now=now) assert result["forecast_status"] == "low_confidence" manifest["timeframes"]["INTRADAY"]["feature_bounds"]["moneyness"] = [0.5, 1] with open(model.model_dir + "/manifest.json", "w") as stream: json.dump(manifest, stream) result = model.predict(reference(), observation(), windows, now=now) assert result["forecast_status"] == "model_input_unavailable" def test_ai_review_failure_never_creates_a_fallback_forecast(monkeypatch): monkeypatch.setenv("FO_AI_ENABLED", "1") fo_predictor._reviews.clear() prediction = {"available": True, "direction": "BULLISH", "target_price_lo": 90, "expected_target_price": 110, "target_price_hi": 130, "end_at": "2026-10-16T15:00:00+05:30"} with patch("llm_client.make_chat_call", side_effect=RuntimeError("Unavailable")): result = fo_predictor.review_forecast(reference(), observation(), prediction) assert not result["available"] assert "target_price_lo" not in result with patch("fo_predictor._review_slots") as slots, patch("llm_client.make_chat_call") as call: slots.acquire.return_value = False result = fo_predictor.review_forecast(reference(), observation(), prediction) assert result["reason"] == "busy" call.assert_not_called() def test_prepared_cache_is_bound_to_source_and_detects_tampering(tmp_path, monkeypatch): archive = tmp_path / "history.zip" with zipfile.ZipFile(archive, "w") as stream: stream.writestr("placeholder.csv", "column\nvalue\n") rows = pd.DataFrame({"expiry": ["2024-04-04"], "timestamp": [pd.Timestamp("2024-04-01T10:00:00+05:30")], "target_at": [pd.Timestamp("2024-04-01T15:00:00+05:30")]}) rows.attrs["data_quality"] = {"excluded_conflicting_rows": 2} monkeypatch.setattr(sys, "argv", ["train_fo_predictor", "--archive", str(archive), "--prepare-only"]) with patch.object(train_fo_predictor, "prepare_history", return_value=pd.DataFrame()), \ patch.object(train_fo_predictor, "training_rows", return_value=rows) as prepare: train_fo_predictor.main() train_fo_predictor.main() assert prepare.call_count == 1 cache = tmp_path / "fo_training_rows.csv.gz" cache.write_bytes(b"corrupted") train_fo_predictor.main() assert prepare.call_count == 2 metadata = json.loads((tmp_path / "fo_training_rows.meta.json").read_text()) assert metadata["identity"]["source_sha256"] == train_fo_predictor.archive_digest(archive) assert metadata["data_quality"]["excluded_conflicting_rows"] == 2 def test_recent_cli_uses_reachable_targets_without_legacy_archive(tmp_path, monkeypatch): from research import fo_recent_data history = pd.DataFrame({"expiry": ["2026-04-13"], "timestamp": [pd.Timestamp("2026-04-09T10:00:00+05:30")]}) history.attrs["source_provenance"] = {"options": [{"sha256": "fixture-only"}]} monkeypatch.setattr(sys, "argv", ["train_fo_predictor", "--source", "recent", "--out", str(tmp_path)]) with patch.object(fo_recent_data, "prepare_recent", return_value=history), \ patch.object(train_fo_predictor, "training_rows", return_value=history) as prepare, \ patch.object(train_fo_predictor, "train_reachable_models") as train, \ patch.object(train_fo_predictor, "download_archive") as legacy: train_fo_predictor.main() assert prepare.call_args.kwargs["target_kind"] == "reachable" train.assert_called_once() legacy.assert_not_called() def test_recent_source_sample_keeps_complete_causal_windows(): from research import fo_recent_data timestamps = pd.date_range("2026-04-09T09:16:00+05:30", "2026-04-09T15:30:00+05:30", freq="min") frame = pd.DataFrame({"timestamp": timestamps, "contract_id": "NSE:OPTION:INDEX:NIFTY:2026-04-13:22900:PE", "expiry": "2026-04-13", "price": 100.0, "high": 101.0, "low": 99.0, "strike": 22900.0, "option_type": "PE", "volume": 100, "open_interest": 1000, "underlying_price": 23000.0}) frame.loc[frame["timestamp"] == pd.Timestamp("2026-04-09T14:20:00+05:30"), "high"] = 140 sampled = fo_recent_data.sample_day(frame) anchor = sampled[sampled["timestamp"] == pd.Timestamp("2026-04-09T10:00:00+05:30")].iloc[0] assert anchor["intraday_high"] == 140 assert anchor["intraday_complete"] and anchor["remaining_complete"] and anchor["session_complete"] assert anchor["premium_return_30m"] == 0 partial = fo_recent_data.sample_day(frame.iloc[:180]) assert not partial["session_complete"].any() assert not partial["intraday_complete"].any() def test_recent_source_download_budget_and_cached_file(tmp_path): from research import fo_recent_data response = MagicMock() response.__enter__.return_value = response response.headers = {"Content-Length": "4"} response.iter_content.return_value = [b"data"] destination = str(tmp_path / "sample.feather") with patch.object(fo_recent_data.requests, "get", return_value=response) as get: assert fo_recent_data.download_file("fixture/source", "sample.feather", destination, 10) == destination assert fo_recent_data.download_file("fixture/source", "sample.feather", destination, 10) == destination get.assert_called_once() response.headers = {"Content-Length": "100"} with patch.object(fo_recent_data.requests, "get", return_value=response): with pytest.raises(ValueError, match="budget"): fo_recent_data.download_file("fixture/source", "too-big.feather", str(tmp_path / "too-big.feather"), 10) def test_recent_catalog_pagination_uses_verified_prefix(): from research import fo_recent_data first = MagicMock() first.json.return_value = {"datasetFiles": [{"name": "untrusted/2026-04-09-index-nfo-data.feather"}], "nextPageToken": "next"} second = MagicMock() name = fo_recent_data.OPTION_PREFIX + "2026-04-09-index-nfo-data.feather" second.json.return_value = {"datasetFiles": [{"name": name}]} with patch.object(fo_recent_data.requests, "get", side_effect=[first, second]) as get: assert fo_recent_data.list_option_files() == [name] assert get.call_args.kwargs["params"]["pageToken"] == "next" def test_recent_preparation_restores_source_hashed_cache(tmp_path): from research import fo_recent_data timestamps = pd.date_range("2026-04-09T09:15:00+05:30", "2026-04-09T15:29:00+05:30", freq="min") raw = pd.DataFrame({"date": timestamps, "open": 100.0, "high": 101.0, "low": 99.0, "close": 100.0, "volume": 100, "oi": 1000, "name": "NIFTY", "expiry": "2026-04-13", "strike": 22900, "instrument_type": "PE", "symbol": "NIFTY2641322900PE"}) filename = "2026-04-09-index-nfo-data.feather" raw.to_feather(tmp_path / filename) spots = pd.DataFrame({"timestamp": timestamps + pd.Timedelta(minutes=1), "underlying_price": 23000.0}) with patch.object(fo_recent_data, "list_option_files", return_value=[fo_recent_data.OPTION_PREFIX + filename]), \ patch.object(fo_recent_data, "load_spots", return_value=(spots, {"source": "fixture", "license": "CC0-1.0"})), \ patch.object(fo_recent_data, "download_file", return_value=str(tmp_path / filename)) as download: fresh = fo_recent_data.prepare_recent("2026-04-09", "2026-04-13", str(tmp_path)) cached = fo_recent_data.prepare_recent("2026-04-09", "2026-04-13", str(tmp_path)) assert len(fresh) == len(cached) == 5 assert cached.attrs["source"] == fo_recent_data.OPTION_SOURCE assert cached.attrs["source_license"] == "MIT" download.assert_called_once() def _previous_session_inputs(snapshot_price=100): anchor = datetime.fromisoformat("2026-10-26T15:30:00+05:30").timestamp() candles = [{"time": anchor - 60 * offset, "close": 100} for offset in range(40, -1, -1)] quote = {"price": snapshot_price, "underlying_price": 22950, "provider_iv": 20, "open_interest": 20000, "quote_time": anchor + 600, "stale": True, "is_live": False, "source": "jugaad"} return quote, candles def _outlook_forecast(now, quote, candles, direction="BULLISH", confidence="HIGH"): contract = reference(expiry="2026-10-29") windows = fo.forecast_windows(contract, now=now) predictor = MagicMock() predictor.has_approved_models.return_value = False predictor.predict.return_value = {"forecast_status": "model_input_unavailable", "forecast_metrics": {}, "timeframes": { timeframe: {"available": False, "no_trade_reason": "model_input_unavailable", "end_at": window["end_at"]} for timeframe, window in windows.items()}} service = MagicMock() service.chart.return_value = {"candles": candles} raw = {"direction": direction, "expected_change_pct": 6, "predicted_return_lo": -2, "predicted_return_hi": 14, "confidence": confidence, "summary": "Previous-session momentum favours a bounce."} content = json.dumps({"forecasts": {timeframe: raw for timeframe in windows}}) with patch("fo_predictor.get_predictor", return_value=predictor), patch("fo_predictor.datetime", wraps=datetime) as clock, \ patch("llm_client.make_chat_call", return_value=(content, "fake", "test")) as call: clock.now.return_value = now result = fo_predictor.contract_forecasts(contract, quote, windows, service=service) return result, call def test_premarket_shows_ai_outlook_from_previous_session_and_keeps_ml_honest(monkeypatch): monkeypatch.setenv("FO_AI_ENABLED", "1") quote, candles = _previous_session_inputs() result, call = _outlook_forecast(datetime.fromisoformat("2026-10-27T08:50:00+05:30"), quote, candles) ai = result["timeframes"]["INTRADAY"]["ai_forecast"] assert ai["available"] and ai["premarket_outlook"] and ai["input_delayed"] assert ai["confidence"] == "MEDIUM" assert ai["as_of"] == "2026-10-26T15:30:00+05:30" assert ai["expected_target_price"] == 106 assert result["timeframes"]["INTRADAY"]["input_status"] == "market_not_open" context = json.loads(call.call_args.kwargs["messages"][1]["content"]) assert context["market_state"]["next_session_open"] == "2026-10-27T09:15:00+05:30" assert context["horizons"]["INTRADAY"]["start_at"] == "2026-10-27T09:15:00+05:30" assert context["horizons"]["INTRADAY"]["calendar_minutes"] == 345 assert "previous-session outlook" in call.call_args.kwargs["messages"][0]["content"] def test_premarket_outlook_uses_chart_close_when_snapshot_disagrees_and_rejects_old_data(monkeypatch): monkeypatch.setenv("FO_AI_ENABLED", "1") quote, candles = _previous_session_inputs(snapshot_price=150) result, _ = _outlook_forecast(datetime.fromisoformat("2026-10-27T08:50:00+05:30"), quote, candles) assert result["timeframes"]["INTRADAY"]["ai_forecast"]["current_price"] == 100 quote, candles = _previous_session_inputs() result, call = _outlook_forecast(datetime.fromisoformat("2026-11-05T08:50:00+05:30"), quote, candles) assert not result["timeframes"]["INTRADAY"]["ai_forecast"]["available"] call.assert_not_called() def test_live_session_never_uses_previous_session_outlook(monkeypatch): monkeypatch.setenv("FO_AI_ENABLED", "1") quote, candles = _previous_session_inputs() result, call = _outlook_forecast(datetime.fromisoformat("2026-10-27T10:30:00+05:30"), quote, candles) assert not result["timeframes"]["INTRADAY"]["ai_forecast"]["available"] call.assert_not_called() def test_watchlist_quote_falls_back_to_last_known_price_and_ignores_expired_values(tmp_path, monkeypatch): monkeypatch.setenv("FO_LAST_QUOTES_PATH", str(tmp_path / "last.json")) contract = reference() now = time.time() good = {"price": 101.5, "quote_time": now - 3600, "source": "jugaad", "underlying_price": 22950, "stale": False} with patch("fo.fetch_contract_snapshot", return_value=good): assert fo.FoService().watchlist_quote(contract)["price"] == 101.5 with patch("fo.fetch_contract_snapshot", side_effect=fo.FoUnavailable("NSE down")): restarted = fo.FoService() fallback = restarted.watchlist_quote(contract) assert fallback["price"] == 101.5 and fallback["stale"] and not fallback["is_live"] and fallback["last_known"] (tmp_path / "last.json").write_text(json.dumps({contract["contract_id"]: {**good, "quote_time": now - 6 * 86400}})) with patch("fo.fetch_contract_snapshot", side_effect=fo.FoUnavailable("NSE down")): with pytest.raises(fo.FoUnavailable): fo.FoService().watchlist_quote(contract)