import { describe, expect, test } from "bun:test";
import { compareBenchmark, percentile, runBenchmark } from "../src/index.ts";

describe("@wrnexus/benchmark", () => {
  test("calculates percentiles and deterministic samples", async () => {
    let now = 0;
    const result = await runBenchmark(
      "clock",
      () => {
        now += 2;
      },
      {
        iterations: 3,
        warmup: 0,
        clock: () => now,
      },
    );
    expect(result.samples).toEqual([2, 2, 2]);
    expect(result.p95Ms).toBe(2);
    expect(percentile([1, 2, 3, 4], 0.5)).toBe(2);
  });

  test("reports regression budget violations", () => {
    const baseline = { meanMs: 10, p95Ms: 20 } as any;
    const current = { meanMs: 12, p95Ms: 30, maxMs: 40, operationsPerSecond: 50 } as any;
    expect(compareBenchmark(current, baseline, { meanPercent: 10, p95Percent: 20 })).toHaveLength(
      2,
    );
  });
});
