import test from "node:test";
import assert from "node:assert/strict";
import {
  readAccountMosaicPreference,
  readConversationAccountVisibilityPreference,
  writeAccountMosaicPreference,
  writeConversationAccountVisibilityPreference
} from "./account-mosaic-preference.js";

test("account mosaic preference is disabled by default and persists explicit choices", () => {
  const values = new Map();
  const storage = { getItem: (key) => values.get(key) ?? null, setItem: (key, value) => values.set(key, value) };

  assert.equal(readAccountMosaicPreference(storage), false);
  writeAccountMosaicPreference(storage, true);
  assert.equal(readAccountMosaicPreference(storage), true);
  writeAccountMosaicPreference(storage, false);
  assert.equal(readAccountMosaicPreference(storage), false);
});

test("account mosaic preference tolerates unavailable storage", () => {
  const storage = { getItem: () => { throw new Error("blocked"); }, setItem: () => { throw new Error("blocked"); } };

  assert.equal(readAccountMosaicPreference(storage), false);
  assert.doesNotThrow(() => writeAccountMosaicPreference(storage, true));
});

test("conversation account visibility defaults on and persists explicit choices", () => {
  const values = new Map();
  const storage = { getItem: (key) => values.get(key) ?? null, setItem: (key, value) => values.set(key, value) };

  assert.equal(readConversationAccountVisibilityPreference(storage), true);
  writeConversationAccountVisibilityPreference(storage, false);
  assert.equal(readConversationAccountVisibilityPreference(storage), false);
  writeConversationAccountVisibilityPreference(storage, true);
  assert.equal(readConversationAccountVisibilityPreference(storage), true);
});

test("conversation account visibility remains on when storage is unavailable", () => {
  const storage = { getItem: () => { throw new Error("blocked"); }, setItem: () => { throw new Error("blocked"); } };

  assert.equal(readConversationAccountVisibilityPreference(storage), true);
  assert.doesNotThrow(() => writeConversationAccountVisibilityPreference(storage, false));
});
