"use strict";
var import_vitest = require("vitest");
var import_dexto_auth_refresh = require("./dexto-auth-refresh.js");
function createConfig(provider) {
  return {
    provider,
    model: provider === "dexto-nova" ? "openai/gpt-5" : "gpt-5",
    apiKey: "existing-key",
    maxIterations: 50,
    maxInputTokens: 128e3
  };
}
(0, import_vitest.describe)("refreshDextoNovaAuthAfterLogin", () => {
  (0, import_vitest.it)("does nothing when neither the global nor session config uses dexto-nova", async () => {
    const getCurrentLLMConfig = import_vitest.vi.fn().mockImplementation(
      (sessionId) => sessionId ? createConfig("openai") : createConfig("openai")
    );
    const hasSessionLLMOverride = import_vitest.vi.fn().mockReturnValue(false);
    const switchLLM = import_vitest.vi.fn().mockResolvedValue(
      createConfig("openai")
    );
    const refreshed = await (0, import_dexto_auth_refresh.refreshDextoNovaAuthAfterLogin)(
      { getCurrentLLMConfig, hasSessionLLMOverride, switchLLM },
      "session-1"
    );
    (0, import_vitest.expect)(refreshed).toBe(false);
    (0, import_vitest.expect)(switchLLM).not.toHaveBeenCalled();
  });
  (0, import_vitest.it)("refreshes the global and active session config when both use dexto-nova", async () => {
    const getCurrentLLMConfig = import_vitest.vi.fn().mockImplementation(
      (sessionId) => sessionId ? createConfig("dexto-nova") : createConfig("dexto-nova")
    );
    const hasSessionLLMOverride = import_vitest.vi.fn().mockReturnValue(true);
    const switchLLM = import_vitest.vi.fn().mockResolvedValue(
      createConfig("dexto-nova")
    );
    const refreshed = await (0, import_dexto_auth_refresh.refreshDextoNovaAuthAfterLogin)(
      { getCurrentLLMConfig, hasSessionLLMOverride, switchLLM },
      "session-1"
    );
    (0, import_vitest.expect)(refreshed).toBe(true);
    (0, import_vitest.expect)(switchLLM).toHaveBeenCalledTimes(2);
    (0, import_vitest.expect)(switchLLM).toHaveBeenNthCalledWith(1, {
      provider: "dexto-nova",
      model: "openai/gpt-5",
      apiKey: "$DEXTO_API_KEY"
    });
    (0, import_vitest.expect)(switchLLM).toHaveBeenNthCalledWith(
      2,
      {
        provider: "dexto-nova",
        model: "openai/gpt-5",
        apiKey: "$DEXTO_API_KEY"
      },
      "session-1"
    );
  });
  (0, import_vitest.it)("refreshes only the global config when the session inherits dexto-nova without an override", async () => {
    const getCurrentLLMConfig = import_vitest.vi.fn().mockImplementation(
      (sessionId) => sessionId ? createConfig("dexto-nova") : createConfig("dexto-nova")
    );
    const hasSessionLLMOverride = import_vitest.vi.fn().mockReturnValue(false);
    const switchLLM = import_vitest.vi.fn().mockResolvedValue(
      createConfig("dexto-nova")
    );
    const refreshed = await (0, import_dexto_auth_refresh.refreshDextoNovaAuthAfterLogin)(
      { getCurrentLLMConfig, hasSessionLLMOverride, switchLLM },
      "session-1"
    );
    (0, import_vitest.expect)(refreshed).toBe(true);
    (0, import_vitest.expect)(hasSessionLLMOverride).toHaveBeenCalledWith("session-1");
    (0, import_vitest.expect)(switchLLM).toHaveBeenCalledTimes(1);
    (0, import_vitest.expect)(switchLLM).toHaveBeenCalledWith({
      provider: "dexto-nova",
      model: "openai/gpt-5",
      apiKey: "$DEXTO_API_KEY"
    });
  });
  (0, import_vitest.it)("refreshes only the active session when the global config uses another provider", async () => {
    const getCurrentLLMConfig = import_vitest.vi.fn().mockImplementation(
      (sessionId) => sessionId ? createConfig("dexto-nova") : createConfig("openai")
    );
    const hasSessionLLMOverride = import_vitest.vi.fn().mockReturnValue(true);
    const switchLLM = import_vitest.vi.fn().mockResolvedValue(
      createConfig("dexto-nova")
    );
    const refreshed = await (0, import_dexto_auth_refresh.refreshDextoNovaAuthAfterLogin)(
      { getCurrentLLMConfig, hasSessionLLMOverride, switchLLM },
      "session-1"
    );
    (0, import_vitest.expect)(refreshed).toBe(true);
    (0, import_vitest.expect)(switchLLM).toHaveBeenCalledTimes(1);
    (0, import_vitest.expect)(switchLLM).toHaveBeenCalledWith(
      {
        provider: "dexto-nova",
        model: "openai/gpt-5",
        apiKey: "$DEXTO_API_KEY"
      },
      "session-1"
    );
  });
  (0, import_vitest.it)("refreshes only the global config when there is no active session id", async () => {
    const getCurrentLLMConfig = import_vitest.vi.fn().mockImplementation(() => createConfig("dexto-nova"));
    const hasSessionLLMOverride = import_vitest.vi.fn();
    const switchLLM = import_vitest.vi.fn().mockResolvedValue(
      createConfig("dexto-nova")
    );
    const refreshed = await (0, import_dexto_auth_refresh.refreshDextoNovaAuthAfterLogin)({
      getCurrentLLMConfig,
      hasSessionLLMOverride,
      switchLLM
    });
    (0, import_vitest.expect)(refreshed).toBe(true);
    (0, import_vitest.expect)(switchLLM).toHaveBeenCalledTimes(1);
    (0, import_vitest.expect)(switchLLM).toHaveBeenCalledWith({
      provider: "dexto-nova",
      model: "openai/gpt-5",
      apiKey: "$DEXTO_API_KEY"
    });
    (0, import_vitest.expect)(hasSessionLLMOverride).not.toHaveBeenCalled();
  });
});
