"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var import_vitest = require("vitest");
var import_log = require("../../log.cjs");
var import_mock_ws = require("./_mock_ws.cjs");
var import_interruption_detector = require("./interruption_detector.cjs");
var import_interruption_stream = require("./interruption_stream.cjs");
var import_ws_transport = require("./ws_transport.cjs");
import_vitest.vi.mock("ws", async () => {
  const { MockWebSocket: MockWebSocket2 } = await import("./_mock_ws.cjs");
  return { default: MockWebSocket2, WebSocket: MockWebSocket2 };
});
(0, import_log.initializeLogger)({ pretty: false, level: "silent" });
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function waitFor(predicate, timeoutMs = 2e3) {
  const start = performance.now();
  while (!predicate()) {
    if (performance.now() - start > timeoutMs) {
      throw new Error("condition not met within timeout");
    }
    await sleep(5);
  }
}
function createDetector(opts = {}) {
  return new import_interruption_detector.AdaptiveInterruptionDetector({
    baseUrl: "http://localhost:9999",
    apiKey: "test-key",
    apiSecret: "test-secret",
    ...opts
  });
}
async function captureSessionCreate(detector) {
  const stream = new import_interruption_stream.InterruptionStreamBase(detector, {});
  try {
    await waitFor(() => import_mock_ws.MockWebSocket.instances.length > 0);
    const ws = import_mock_ws.MockWebSocket.instances[import_mock_ws.MockWebSocket.instances.length - 1];
    ws.simulateOpen();
    await waitFor(() => ws.sent.length > 0);
    return JSON.parse(String(ws.sent[0]));
  } finally {
    await stream.close();
  }
}
(0, import_vitest.beforeEach)(() => {
  import_mock_ws.MockWebSocket.instances.length = 0;
});
(0, import_vitest.describe)("session.create threshold", () => {
  (0, import_vitest.it)("omits threshold when not given", async () => {
    const payload = await captureSessionCreate(createDetector());
    (0, import_vitest.expect)("threshold" in payload.settings).toBe(false);
  });
  (0, import_vitest.it)("includes threshold when overridden", async () => {
    const payload = await captureSessionCreate(createDetector({ threshold: 0.7 }));
    (0, import_vitest.expect)(payload.settings.threshold).toBe(0.7);
  });
});
(0, import_vitest.describe)("session.created default_threshold parsing", () => {
  (0, import_vitest.it)("parses default_threshold", () => {
    const msg = import_ws_transport.wsMessageSchema.parse({ type: "session.created", default_threshold: 0.42 });
    (0, import_vitest.expect)(msg).toMatchObject({ type: "session.created", default_threshold: 0.42 });
  });
  (0, import_vitest.it)("treats default_threshold as optional", () => {
    const msg = import_ws_transport.wsMessageSchema.parse({ type: "session.created" });
    (0, import_vitest.expect)(msg.default_threshold ?? null).toBeNull();
  });
});
(0, import_vitest.describe)("resolveEffectiveThreshold", () => {
  (0, import_vitest.it)("prefers the user override", () => {
    (0, import_vitest.expect)((0, import_ws_transport.resolveEffectiveThreshold)(0.7, 0.3)).toBe(0.7);
  });
  (0, import_vitest.it)("falls back to the server default", () => {
    (0, import_vitest.expect)((0, import_ws_transport.resolveEffectiveThreshold)(void 0, 0.3)).toBe(0.3);
  });
  (0, import_vitest.it)("returns null when neither the user nor the server provides a value", () => {
    (0, import_vitest.expect)((0, import_ws_transport.resolveEffectiveThreshold)(void 0, null)).toBeNull();
  });
});
//# sourceMappingURL=interruption_session_create.test.cjs.map