import test from "node:test";
import assert from "node:assert/strict";
import { readChatViewPreference, writeChatViewPreference } from "./chat-view-preference.js";

test("chat view defaults to the classic main screen", () => {
  assert.equal(readChatViewPreference({ getItem: () => null }), "classic");
  assert.equal(readChatViewPreference({ getItem: () => "unexpected" }), "classic");
  assert.equal(readChatViewPreference({ getItem: () => "grouped" }), "grouped");
});

test("chat view preference persists only supported values and tolerates storage failures", () => {
  const values = new Map();
  const storage = {
    getItem: (key) => values.get(key) ?? null,
    setItem: (key, value) => values.set(key, value)
  };

  writeChatViewPreference(storage, "grouped");
  assert.equal(readChatViewPreference(storage), "grouped");
  writeChatViewPreference(storage, "unknown");
  assert.equal(readChatViewPreference(storage), "classic");
  assert.equal(readChatViewPreference({ getItem: () => { throw new Error("blocked"); } }), "classic");
  assert.doesNotThrow(() => writeChatViewPreference({ setItem: () => { throw new Error("blocked"); } }, "grouped"));
});
