"use strict";
var import_vitest = require("vitest");
var import_detector = require("../inference/eot/detector.cjs");
var import_vad = require("../vad.cjs");
var import_agent = require("./agent.cjs");
var import_agent_activity = require("./agent_activity.cjs");
var import_agent_session = require("./agent_session.cjs");
class FakeVAD extends import_vad.VAD {
  label = "FakeVAD";
  constructor() {
    super({ updateInterval: 32 });
  }
  stream() {
    throw new Error("not used in this test");
  }
}
(0, import_vitest.describe)("AgentSession endpointing defaults", () => {
  (0, import_vitest.it)("default session (streaming detector) uses the tighter streaming defaults", async () => {
    const session = new import_agent_session.AgentSession();
    try {
      (0, import_vitest.expect)(session.sessionOptions.turnHandling.endpointing.minDelay).toBe(300);
      (0, import_vitest.expect)(session.sessionOptions.turnHandling.endpointing.maxDelay).toBe(2500);
      (0, import_vitest.expect)(session.sessionOptions.turnHandling.endpointingOverrides).toEqual({});
    } finally {
      await session.close().catch(() => {
      });
    }
  });
  (0, import_vitest.it)("a non-streaming mode keeps the legacy defaults", async () => {
    const session = new import_agent_session.AgentSession({ turnHandling: { turnDetection: "vad" } });
    try {
      (0, import_vitest.expect)(session.sessionOptions.turnHandling.endpointing.minDelay).toBe(500);
      (0, import_vitest.expect)(session.sessionOptions.turnHandling.endpointing.maxDelay).toBe(3e3);
    } finally {
      await session.close().catch(() => {
      });
    }
  });
  (0, import_vitest.it)("an explicit delay is honored; the unset one still gets the streaming default", async () => {
    const session = new import_agent_session.AgentSession({ turnHandling: { endpointing: { minDelay: 400 } } });
    try {
      (0, import_vitest.expect)(session.sessionOptions.turnHandling.endpointing.minDelay).toBe(400);
      (0, import_vitest.expect)(session.sessionOptions.turnHandling.endpointing.maxDelay).toBe(2500);
      (0, import_vitest.expect)(session.sessionOptions.turnHandling.endpointingOverrides).toEqual({ minDelay: 400 });
    } finally {
      await session.close().catch(() => {
      });
    }
  });
  (0, import_vitest.it)("a user-constructed streaming detector also triggers the streaming defaults", async () => {
    const session = new import_agent_session.AgentSession({
      turnHandling: { turnDetection: new import_detector.TurnDetector({ version: "v1-mini" }) }
    });
    try {
      (0, import_vitest.expect)(session.sessionOptions.turnHandling.endpointing.minDelay).toBe(300);
      (0, import_vitest.expect)(session.sessionOptions.turnHandling.endpointing.maxDelay).toBe(2500);
    } finally {
      await session.close().catch(() => {
      });
    }
  });
  (0, import_vitest.it)("deprecated top-level turnDetection + no delays \u2192 legacy defaults", async () => {
    const session = new import_agent_session.AgentSession({ turnDetection: "vad" });
    try {
      (0, import_vitest.expect)(session.sessionOptions.turnHandling.endpointing.minDelay).toBe(500);
      (0, import_vitest.expect)(session.sessionOptions.turnHandling.endpointing.maxDelay).toBe(3e3);
    } finally {
      await session.close().catch(() => {
      });
    }
  });
  (0, import_vitest.it)("endpointingOpts uses the activity-resolved detector, not just the session", async () => {
    const session = new import_agent_session.AgentSession({ vad: new FakeVAD() });
    try {
      const streamingActivity = new import_agent_activity.AgentActivity(new import_agent.Agent({ instructions: "test" }), session);
      (0, import_vitest.expect)(streamingActivity.endpointingOpts.minDelay).toBe(300);
      (0, import_vitest.expect)(streamingActivity.endpointingOpts.maxDelay).toBe(2500);
      const vadActivity = new import_agent_activity.AgentActivity(
        new import_agent.Agent({ instructions: "test", turnDetection: "vad" }),
        session
      );
      (0, import_vitest.expect)(vadActivity.endpointingOpts.minDelay).toBe(500);
      (0, import_vitest.expect)(vadActivity.endpointingOpts.maxDelay).toBe(3e3);
    } finally {
      await session.close().catch(() => {
      });
    }
  });
  (0, import_vitest.it)("runtime updateOptions changes survive a handoff via overrides", async () => {
    const session = new import_agent_session.AgentSession({ vad: new FakeVAD() });
    try {
      session.updateOptions({
        turnHandling: { endpointing: { mode: "dynamic", alpha: 0.5, minDelay: 400 } }
      });
      const activity = new import_agent_activity.AgentActivity(new import_agent.Agent({ instructions: "test" }), session);
      (0, import_vitest.expect)(activity.endpointingOpts.mode).toBe("dynamic");
      (0, import_vitest.expect)(activity.endpointingOpts.alpha).toBe(0.5);
      (0, import_vitest.expect)(activity.endpointingOpts.minDelay).toBe(400);
      (0, import_vitest.expect)(activity.endpointingOpts.maxDelay).toBe(2500);
    } finally {
      await session.close().catch(() => {
      });
    }
  });
});
//# sourceMappingURL=agent_session_endpointing.test.cjs.map