import { describe, it, expect } from "vitest";

import { VolatilityEstimator } from "../packages/utils/src/volatilityEstimator";

describe("VolatilityEstimator", () => {
  it("tracks hourly volatility in bps", () => {
    const estimator = new VolatilityEstimator({ windowMinutes: 60, minSamples: 2 });
    const start = Date.now();
    estimator.update(100, start - 60 * 60 * 1000);
    estimator.update(110, start);

    const hourlyBps = estimator.getHourlyVolatilityBps();
    expect(hourlyBps).toBeDefined();
    expect(hourlyBps).toBeCloseTo(1000);
  });

  it("computes annualized volatility from returns", () => {
    const estimator = new VolatilityEstimator({ windowMinutes: 30, minSamples: 2 });
    const base = Date.now();
    estimator.update(100, base - 3 * 60 * 1000);
    estimator.update(102, base - 2 * 60 * 1000);
    estimator.update(98, base - 60 * 1000);
    estimator.update(101, base);

    const annualized = estimator.getAnnualizedVolatility();
    expect(annualized).toBeDefined();
    expect(annualized).toBeGreaterThan(0);
  });

  it("exposes status snapshot", () => {
    const estimator = new VolatilityEstimator({ windowMinutes: 5, minSamples: 2 });
    const now = Date.now();
    estimator.update(50_000, now - 10_000);
    estimator.update(50_500, now);

    const status = estimator.getStatus();
    expect(status.historySize).toBeGreaterThanOrEqual(2);
    expect(status.latestPrice).toBeCloseTo(50_500);
    expect(status.hourlyVolBps).toBeGreaterThan(0);
  });
});
