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

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

export function checkResponded(responded: boolean, expected: boolean): ExpectationResult {
  if (responded === expected) return { failure: null, matched: true };
  if (!responded && expected) {
    return {
      failure: mismatchFailure("responded", "responded", true, false, "期望收到响应但未收到"),
      matched: false,
    };
  }
  return {
    failure: mismatchFailure("responded", "responded", false, true, "期望无响应但收到响应"),
    matched: false,
  };
}

export function checkResponseSize(size: number, matcher: ValueExpectation): ExpectationResult {
  return checkValueExpectation(size, matcher, {
    message: "响应大小不满足条件",
    path: "responseSize",
    phase: "responseSize",
  });
}

export function checkResponseText(text: string, expectations: ContentExpectations): ExpectationResult {
  return checkContentExpectations(text, expectations, { path: "response", phase: "response" });
}

export function checkSourceHost(actual: string, matcher: ValueExpectation): ExpectationResult {
  return checkValueExpectation(actual, matcher, {
    message: "响应来源地址不满足条件",
    path: "sourceHost",
    phase: "sourceHost",
  });
}

export function checkSourcePort(actual: number, matcher: ValueExpectation): ExpectationResult {
  return checkValueExpectation(actual, matcher, {
    message: "响应来源端口不满足条件",
    path: "sourcePort",
    phase: "sourcePort",
  });
}
