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

import {
  checkAlive,
  checkAvgLatency,
  checkMaxLatency,
  checkPacketLoss,
} from "../../../../../src/server/checker/runner/icmp/expect";

describe("ping expect", () => {
  test("alive 通过和失败", () => {
    expect(checkAlive(true, true).matched).toBe(true);
    const result = checkAlive(false, true);
    expect(result.matched).toBe(false);
    expect(result.failure?.phase).toBe("alive");
  });

  test("packetLoss 通过和失败", () => {
    expect(checkPacketLoss(0, { lte: 10 }).matched).toBe(true);
    const result = checkPacketLoss(33, { lte: 10 });
    expect(result.matched).toBe(false);
    expect(result.failure?.phase).toBe("packetLoss");
  });

  test("avgLatency 通过和失败", () => {
    expect(checkAvgLatency(12, { lte: 200 }).matched).toBe(true);
    const result = checkAvgLatency(156, { lte: 100 });
    expect(result.matched).toBe(false);
    expect(result.failure?.phase).toBe("avgLatency");
  });

  test("maxLatency 通过和失败", () => {
    expect(checkMaxLatency(340, { lte: 500 }).matched).toBe(true);
    const result = checkMaxLatency(340, { lte: 200 });
    expect(result.matched).toBe(false);
    expect(result.failure?.phase).toBe("maxLatency");
  });

  test("未配置阈值默认通过", () => {
    expect(checkPacketLoss(100, undefined).matched).toBe(true);
    expect(checkAvgLatency(null, undefined).matched).toBe(true);
    expect(checkMaxLatency(null, undefined).matched).toBe(true);
  });
});
