import { describe, expect, it } from "vitest";
import { ChatContext } from "../llm/chat_context.js";
import { AgentSessionEventTypes, createSessionUsageUpdatedEvent } from "./events.js";
import { createSessionReport, sessionReportToJSON } from "./report.js";
function baseOptions() {
  return {
    maxToolSteps: 3,
    userAwayTimeout: 15,
    useTtsAlignedTranscript: true,
    turnHandling: {
      preemptiveGeneration: { enabled: false }
    }
  };
}
function serializeOptions(options) {
  const report = createSessionReport({
    jobId: "job",
    roomId: "room-id",
    room: "room",
    options,
    events: [],
    chatHistory: ChatContext.empty(),
    enableRecording: false,
    timestamp: 0,
    startedAt: 0
  });
  const payload = sessionReportToJSON(report);
  return payload.options;
}
describe("sessionReportToJSON", () => {
  it("serializes interruption and endpointing values from turnHandling", () => {
    const options = baseOptions();
    options.turnHandling = {
      interruption: {
        mode: "adaptive",
        discardAudioIfUninterruptible: false,
        minDuration: 1200,
        minWords: 2
      },
      endpointing: {
        minDelay: 900,
        maxDelay: 4500
      }
    };
    const serialized = serializeOptions(options);
    expect(serialized).toMatchObject({
      allow_interruptions: true,
      discard_audio_if_uninterruptible: false,
      min_interruption_duration: 1200,
      min_interruption_words: 2,
      min_endpointing_delay: 900,
      max_endpointing_delay: 4500,
      max_tool_steps: 3
    });
  });
  it("prefers turnHandling values over deprecated flat fields", () => {
    const options = baseOptions();
    options.allowInterruptions = false;
    options.discardAudioIfUninterruptible = true;
    options.minInterruptionDuration = 400;
    options.minInterruptionWords = 1;
    options.minEndpointingDelay = 500;
    options.maxEndpointingDelay = 2500;
    options.turnHandling = {
      interruption: {
        mode: "vad",
        discardAudioIfUninterruptible: false,
        minDuration: 1400,
        minWords: 4
      },
      endpointing: {
        minDelay: 700,
        maxDelay: 3900
      }
    };
    const serialized = serializeOptions(options);
    expect(serialized).toMatchObject({
      allow_interruptions: true,
      discard_audio_if_uninterruptible: false,
      min_interruption_duration: 1400,
      min_interruption_words: 4,
      min_endpointing_delay: 700,
      max_endpointing_delay: 3900,
      max_tool_steps: 3
    });
  });
  it("serializes allow_interruptions from interruption.enabled when present", () => {
    const options = baseOptions();
    options.allowInterruptions = true;
    options.turnHandling = {
      interruption: {
        enabled: false,
        mode: "adaptive"
      }
    };
    const serialized = serializeOptions(options);
    expect(serialized).toMatchObject({
      allow_interruptions: false,
      max_tool_steps: 3
    });
  });
  it("falls back to deprecated flat fields when turnHandling values are absent", () => {
    const options = baseOptions();
    options.allowInterruptions = false;
    options.discardAudioIfUninterruptible = false;
    options.minInterruptionDuration = 600;
    options.minInterruptionWords = 3;
    options.minEndpointingDelay = 1e3;
    options.maxEndpointingDelay = 5e3;
    const serialized = serializeOptions(options);
    expect(serialized).toMatchObject({
      allow_interruptions: false,
      discard_audio_if_uninterruptible: false,
      min_interruption_duration: 600,
      min_interruption_words: 3,
      min_endpointing_delay: 1e3,
      max_endpointing_delay: 5e3,
      max_tool_steps: 3
    });
  });
  it("serializes model usage as usage", () => {
    const usage = [
      {
        type: "tts_usage",
        provider: "elevenlabs",
        model: "eleven_flash_v2_5",
        inputTokens: 0,
        outputTokens: 0,
        charactersCount: 42,
        audioDurationMs: 1200
      }
    ];
    const report = createSessionReport({
      jobId: "job",
      roomId: "room-id",
      room: "room",
      options: baseOptions(),
      events: [],
      chatHistory: ChatContext.empty(),
      enableRecording: false,
      timestamp: 0,
      startedAt: 0,
      modelUsage: usage
    });
    const payload = sessionReportToJSON(report);
    expect(payload.usage).toEqual([
      {
        type: "tts_usage",
        provider: "elevenlabs",
        model: "eleven_flash_v2_5",
        charactersCount: 42,
        audioDurationMs: 1200
      }
    ]);
  });
  it("omits session usage update events from serialized events", () => {
    const report = createSessionReport({
      jobId: "job",
      roomId: "room-id",
      room: "room",
      options: baseOptions(),
      events: [
        createSessionUsageUpdatedEvent({
          usage: {
            modelUsage: [
              {
                type: "tts_usage",
                provider: "elevenlabs",
                model: "eleven_flash_v2_5"
              }
            ]
          },
          createdAt: 123
        })
      ],
      chatHistory: ChatContext.empty(),
      enableRecording: false,
      timestamp: 0,
      startedAt: 0
    });
    const payload = sessionReportToJSON(report);
    expect(payload.events).toEqual([]);
  });
  it("exports AgentSessionUsage from the voice barrel", () => {
    const usage = { modelUsage: [] };
    const eventType = AgentSessionEventTypes.SessionUsageUpdated;
    expect(usage.modelUsage).toEqual([]);
    expect(eventType).toBe("session_usage_updated");
  });
});
describe("createSessionReport recordingOptions", () => {
  function makeReport(recordingOptions) {
    return createSessionReport({
      jobId: "job",
      roomId: "room-id",
      room: "room",
      options: baseOptions(),
      events: [],
      chatHistory: ChatContext.empty(),
      recordingOptions
    });
  }
  it("defaults every recording category to off when omitted", () => {
    expect(makeReport().recordingOptions).toEqual({
      audio: false,
      traces: false,
      logs: false,
      transcript: false
    });
  });
  it("passes provided recording options through", () => {
    const recordingOptions = {
      audio: true,
      traces: false,
      logs: true,
      transcript: false
    };
    expect(makeReport(recordingOptions).recordingOptions).toEqual(recordingOptions);
  });
});
//# sourceMappingURL=report.test.js.map