import assert from "node:assert/strict";
import test from "node:test";
import {
  getChatModelSelection,
  getChatModelSelectionSyncKey,
} from "../src/lib/chat-model-selection.ts";

test("chat model selections are normalized from persisted metadata", () => {
  assert.deepEqual(
    getChatModelSelection({
      lastUsedProvider: "anthropic",
      lastUsedModel: "  claude-sonnet-4-5  ",
    }),
    {
      provider: "anthropic",
      model: "claude-sonnet-4-5",
    }
  );
});

test("unrelated chat updates do not change the model synchronization key", () => {
  const beforeSettingsSave = getChatModelSelection({
    lastUsedProvider: "openai",
    lastUsedModel: "gpt-4.1-mini",
  });
  const afterSettingsSave = getChatModelSelection({
    lastUsedProvider: "openai",
    lastUsedModel: "gpt-4.1-mini",
  });

  assert.equal(
    getChatModelSelectionSyncKey("chat-1", beforeSettingsSave),
    getChatModelSelectionSyncKey("chat-1", afterSettingsSave)
  );
});

test("switching chats or persisted models changes the synchronization key", () => {
  const original = { provider: "openai", model: "gpt-4.1-mini" };
  const updated = { provider: "gemini", model: "gemini-3.5-flash" };

  assert.notEqual(
    getChatModelSelectionSyncKey("chat-1", original),
    getChatModelSelectionSyncKey("chat-2", original)
  );
  assert.notEqual(
    getChatModelSelectionSyncKey("chat-1", original),
    getChatModelSelectionSyncKey("chat-1", updated)
  );
});
