import { describe, expect, test } from "bun:test";

import { checkValueExpectation } from "../../../../../src/server/checker/expect/value";

function checkDuration(durationMs: number, maxDurationMs?: number) {
  return checkValueExpectation(durationMs, maxDurationMs === undefined ? undefined : { lte: maxDurationMs }, {
    path: "durationMs",
    phase: "duration",
  });
}

describe("checkDuration", () => {
  test("未配置 maxDurationMs 返回匹配成功", () => {
    const r = checkDuration(100);
    expect(r.matched).toBe(true);
    expect(r.failure).toBeNull();
  });

  test("duration 在限制内匹配成功", () => {
    const r = checkDuration(50, 100);
    expect(r.matched).toBe(true);
    expect(r.failure).toBeNull();
  });

  test("duration 等于限制匹配成功", () => {
    const r = checkDuration(100, 100);
    expect(r.matched).toBe(true);
  });

  test("duration 超过限制匹配失败", () => {
    const r = checkDuration(200, 100);
    expect(r.matched).toBe(false);
    expect(r.failure).not.toBeNull();
    expect(r.failure!.phase).toBe("duration");
    expect(r.failure!.kind).toBe("mismatch");
  });
});
