"use strict";
var import_vitest = require("vitest");
var import_general_commands = require("./general-commands.js");
var import_exit_stats = require("./exit-stats.js");
const { mockTriggerExit } = import_vitest.vi.hoisted(() => ({
  mockTriggerExit: import_vitest.vi.fn()
}));
import_vitest.vi.mock("./exit-handler.js", () => ({
  triggerExit: mockTriggerExit
}));
function getExitCommand() {
  const exitCommand = import_general_commands.generalCommands.find((command) => command.name === "exit");
  if (!exitCommand) {
    throw new Error("Exit command not found");
  }
  return exitCommand;
}
(0, import_vitest.describe)("generalCommands /exit", () => {
  (0, import_vitest.beforeEach)(() => {
    (0, import_exit_stats.clearExitStats)();
    mockTriggerExit.mockReset();
  });
  (0, import_vitest.it)("shows a ChatGPT usage note instead of zero token totals", async () => {
    const exitCommand = getExitCommand();
    const agent = {
      sessionManager: {
        getSessionMetadata: import_vitest.vi.fn().mockResolvedValue({
          createdAt: Date.now() - 65e3,
          tokenUsage: {
            inputTokens: 0,
            outputTokens: 0,
            reasoningTokens: 0,
            cacheReadTokens: 0,
            cacheWriteTokens: 0,
            totalTokens: 0
          },
          usageTracking: {
            hasUntrackedChatGPTLoginUsage: true
          }
        })
      },
      getSessionHistory: import_vitest.vi.fn().mockResolvedValue([
        { role: "user", content: "hi" },
        { role: "assistant", content: "hello" }
      ]),
      getCurrentLLMConfig: import_vitest.vi.fn().mockReturnValue({
        provider: "openai-compatible",
        model: "gpt-5.4",
        baseURL: "codex://chatgpt"
      }),
      logger: {
        debug: import_vitest.vi.fn(),
        error: import_vitest.vi.fn()
      }
    };
    const result = await exitCommand.handler([], agent, {
      sessionId: "session-1",
      configFilePath: null
    });
    (0, import_vitest.expect)(result).toBe(true);
    (0, import_vitest.expect)(mockTriggerExit).toHaveBeenCalledTimes(1);
    (0, import_vitest.expect)((0, import_exit_stats.getExitStats)()).toMatchObject({
      sessionId: "session-1",
      messageCount: {
        total: 2,
        user: 1,
        assistant: 1
      },
      usageNote: "Tracked via ChatGPT Login. Token counts are not available in Dexto for this session."
    });
    (0, import_vitest.expect)((0, import_exit_stats.getExitStats)()?.tokenUsage).toBeUndefined();
  });
  (0, import_vitest.it)("shows partial usage note for mixed ChatGPT and tracked models", async () => {
    const exitCommand = getExitCommand();
    const tokenUsage = {
      inputTokens: 120,
      outputTokens: 45,
      reasoningTokens: 10,
      cacheReadTokens: 5,
      cacheWriteTokens: 2,
      totalTokens: 175
    };
    const agent = {
      sessionManager: {
        getSessionMetadata: import_vitest.vi.fn().mockResolvedValue({
          createdAt: Date.now() - 3e4,
          tokenUsage,
          usageTracking: {
            hasUntrackedChatGPTLoginUsage: true
          }
        })
      },
      getSessionHistory: import_vitest.vi.fn().mockResolvedValue([
        { role: "user", content: "hi" },
        { role: "assistant", content: "hello" }
      ]),
      getCurrentLLMConfig: import_vitest.vi.fn().mockReturnValue({
        provider: "openai",
        model: "gpt-5"
      }),
      logger: {
        debug: import_vitest.vi.fn(),
        error: import_vitest.vi.fn()
      }
    };
    await exitCommand.handler([], agent, {
      sessionId: "session-mixed",
      configFilePath: null
    });
    (0, import_vitest.expect)((0, import_exit_stats.getExitStats)()?.tokenUsage).toEqual(tokenUsage);
    (0, import_vitest.expect)((0, import_exit_stats.getExitStats)()?.usageNote).toBe(
      "Partial totals only. This session used ChatGPT Login and other tracked models; ChatGPT token counts are not available in Dexto."
    );
  });
  (0, import_vitest.it)("preserves token totals for standard providers", async () => {
    const exitCommand = getExitCommand();
    const tokenUsage = {
      inputTokens: 120,
      outputTokens: 45,
      reasoningTokens: 10,
      cacheReadTokens: 5,
      cacheWriteTokens: 2,
      totalTokens: 175
    };
    const agent = {
      sessionManager: {
        getSessionMetadata: import_vitest.vi.fn().mockResolvedValue({
          createdAt: Date.now() - 3e4,
          tokenUsage
        })
      },
      getSessionHistory: import_vitest.vi.fn().mockResolvedValue([
        { role: "user", content: "hi" },
        { role: "assistant", content: "hello" }
      ]),
      getCurrentLLMConfig: import_vitest.vi.fn().mockReturnValue({
        provider: "openai",
        model: "gpt-5"
      }),
      logger: {
        debug: import_vitest.vi.fn(),
        error: import_vitest.vi.fn()
      }
    };
    await exitCommand.handler([], agent, {
      sessionId: "session-2",
      configFilePath: null
    });
    (0, import_vitest.expect)((0, import_exit_stats.getExitStats)()?.tokenUsage).toEqual(tokenUsage);
    (0, import_vitest.expect)((0, import_exit_stats.getExitStats)()?.usageNote).toBeUndefined();
  });
});
