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