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

import { checkContentExpectations } from "../../expect/content";
import { mismatchFailure } from "../../expect/failure";
import { checkHeaderExpectations } from "../../expect/headers";

export function checkConnected(connected: boolean, expected: boolean): ExpectationResult {
  if (connected === expected) return { failure: null, matched: true };
  if (!connected && expected) {
    return {
      failure: mismatchFailure("connect", "connected", true, false, "期望 WebSocket 连接成功但连接失败"),
      matched: false,
    };
  }
  return {
    failure: mismatchFailure("connect", "connected", false, true, "期望 WebSocket 连接失败但连接成功"),
    matched: false,
  };
}

export function checkHandshakeHeaders(
  headers: Record<string, unknown>,
  expectations: KeyedExpectations | undefined,
): ExpectationResult {
  return checkHeaderExpectations(headers, expectations);
}

export function checkMessage(message: string, expectations: ContentExpectations): ExpectationResult {
  return checkContentExpectations(message, expectations, { path: "message", phase: "message" });
}
