import type { ExpectationResult, ValueExpectation } from "../../expect/types";

import { mismatchFailure } from "../../expect/failure";
import { checkValueExpectation } from "../../expect/value";

export function checkAlive(actual: boolean, expected: boolean): ExpectationResult {
  if (actual === expected) return { failure: null, matched: true };
  return {
    failure: mismatchFailure(
      "alive",
      "alive",
      expected,
      actual,
      expected ? "期望主机可达但 icmp 不可达" : "期望主机不可达但 icmp 可达",
    ),
    matched: false,
  };
}

export function checkAvgLatency(actual: null | number, matcher: undefined | ValueExpectation): ExpectationResult {
  return checkValueExpectation(actual, matcher, {
    message: "平均延迟不满足条件",
    path: "avgLatencyMs",
    phase: "avgLatency",
  });
}

export function checkMaxLatency(actual: null | number, matcher: undefined | ValueExpectation): ExpectationResult {
  return checkValueExpectation(actual, matcher, {
    message: "最大延迟不满足条件",
    path: "maxLatencyMs",
    phase: "maxLatency",
  });
}

export function checkPacketLoss(actual: number, matcher: undefined | ValueExpectation): ExpectationResult {
  return checkValueExpectation(actual, matcher, {
    message: "丢包率不满足条件",
    path: "packetLossPercent",
    phase: "packetLoss",
  });
}
