import { describe, expect, it, vi } from "vitest";
import { refreshDextoNovaAuthAfterLogin } from "./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
  };
}
describe("refreshDextoNovaAuthAfterLogin", () => {
  it("does nothing when neither the global nor session config uses dexto-nova", async () => {
    const getCurrentLLMConfig = vi.fn().mockImplementation(
      (sessionId) => sessionId ? createConfig("openai") : createConfig("openai")
    );
    const hasSessionLLMOverride = vi.fn().mockReturnValue(false);
    const switchLLM = vi.fn().mockResolvedValue(
      createConfig("openai")
    );
    const refreshed = await refreshDextoNovaAuthAfterLogin(
      { getCurrentLLMConfig, hasSessionLLMOverride, switchLLM },
      "session-1"
    );
    expect(refreshed).toBe(false);
    expect(switchLLM).not.toHaveBeenCalled();
  });
  it("refreshes the global and active session config when both use dexto-nova", async () => {
    const getCurrentLLMConfig = vi.fn().mockImplementation(
      (sessionId) => sessionId ? createConfig("dexto-nova") : createConfig("dexto-nova")
    );
    const hasSessionLLMOverride = vi.fn().mockReturnValue(true);
    const switchLLM = vi.fn().mockResolvedValue(
      createConfig("dexto-nova")
    );
    const refreshed = await refreshDextoNovaAuthAfterLogin(
      { getCurrentLLMConfig, hasSessionLLMOverride, switchLLM },
      "session-1"
    );
    expect(refreshed).toBe(true);
    expect(switchLLM).toHaveBeenCalledTimes(2);
    expect(switchLLM).toHaveBeenNthCalledWith(1, {
      provider: "dexto-nova",
      model: "openai/gpt-5",
      apiKey: "$DEXTO_API_KEY"
    });
    expect(switchLLM).toHaveBeenNthCalledWith(
      2,
      {
        provider: "dexto-nova",
        model: "openai/gpt-5",
        apiKey: "$DEXTO_API_KEY"
      },
      "session-1"
    );
  });
  it("refreshes only the global config when the session inherits dexto-nova without an override", async () => {
    const getCurrentLLMConfig = vi.fn().mockImplementation(
      (sessionId) => sessionId ? createConfig("dexto-nova") : createConfig("dexto-nova")
    );
    const hasSessionLLMOverride = vi.fn().mockReturnValue(false);
    const switchLLM = vi.fn().mockResolvedValue(
      createConfig("dexto-nova")
    );
    const refreshed = await refreshDextoNovaAuthAfterLogin(
      { getCurrentLLMConfig, hasSessionLLMOverride, switchLLM },
      "session-1"
    );
    expect(refreshed).toBe(true);
    expect(hasSessionLLMOverride).toHaveBeenCalledWith("session-1");
    expect(switchLLM).toHaveBeenCalledTimes(1);
    expect(switchLLM).toHaveBeenCalledWith({
      provider: "dexto-nova",
      model: "openai/gpt-5",
      apiKey: "$DEXTO_API_KEY"
    });
  });
  it("refreshes only the active session when the global config uses another provider", async () => {
    const getCurrentLLMConfig = vi.fn().mockImplementation(
      (sessionId) => sessionId ? createConfig("dexto-nova") : createConfig("openai")
    );
    const hasSessionLLMOverride = vi.fn().mockReturnValue(true);
    const switchLLM = vi.fn().mockResolvedValue(
      createConfig("dexto-nova")
    );
    const refreshed = await refreshDextoNovaAuthAfterLogin(
      { getCurrentLLMConfig, hasSessionLLMOverride, switchLLM },
      "session-1"
    );
    expect(refreshed).toBe(true);
    expect(switchLLM).toHaveBeenCalledTimes(1);
    expect(switchLLM).toHaveBeenCalledWith(
      {
        provider: "dexto-nova",
        model: "openai/gpt-5",
        apiKey: "$DEXTO_API_KEY"
      },
      "session-1"
    );
  });
  it("refreshes only the global config when there is no active session id", async () => {
    const getCurrentLLMConfig = vi.fn().mockImplementation(() => createConfig("dexto-nova"));
    const hasSessionLLMOverride = vi.fn();
    const switchLLM = vi.fn().mockResolvedValue(
      createConfig("dexto-nova")
    );
    const refreshed = await refreshDextoNovaAuthAfterLogin({
      getCurrentLLMConfig,
      hasSessionLLMOverride,
      switchLLM
    });
    expect(refreshed).toBe(true);
    expect(switchLLM).toHaveBeenCalledTimes(1);
    expect(switchLLM).toHaveBeenCalledWith({
      provider: "dexto-nova",
      model: "openai/gpt-5",
      apiKey: "$DEXTO_API_KEY"
    });
    expect(hasSessionLLMOverride).not.toHaveBeenCalled();
  });
});
