Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

5.8 KB
Raw Blame
import { describe, expect, test, vi } from 'vitest';
import {
  CONTACT_EMAIL_MAX_LENGTH,
  CONTACT_EMAIL_STORAGE_KEY,
  type ContactEmailStorage,
  commitContactEmail,
  createContactEmailStore,
  DEFAULT_CONTACT_EMAIL_STATE,
  readPersistedState,
  writePersistedState,
} from './contact-email-store.ts';

function memoryStorage(initial: Record<string, string> = {}): ContactEmailStorage & {
  raw(): string | null;
} {
  const data = new Map(Object.entries(initial));
  return {
    getItem(key) {
      return data.get(key) ?? null;
    },
    setItem(key, value) {
      data.set(key, value);
    },
    raw() {
      return data.get(CONTACT_EMAIL_STORAGE_KEY) ?? null;
    },
  };
}

function refusingStorage(): ContactEmailStorage {
  return {
    getItem() {
      return null;
    },
    setItem() {
      throw new Error('QuotaExceededError');
    },
  };
}

describe('readPersistedState', () => {
  test('absent key returns default', () => {
    expect(readPersistedState(memoryStorage())).toEqual(DEFAULT_CONTACT_EMAIL_STATE);
  });

  test('round-trips a stored address', () => {
    const s = memoryStorage({
      [CONTACT_EMAIL_STORAGE_KEY]: JSON.stringify({ email: '[email protected]' }),
    });
    expect(readPersistedState(s)).toEqual({ email: '[email protected]' });
  });

  test('non-string, empty, and over-long values coerce to null', () => {
    for (const email of [42, {}, null, '', '   ', 'x'.repeat(CONTACT_EMAIL_MAX_LENGTH + 1)]) {
      const s = memoryStorage({ [CONTACT_EMAIL_STORAGE_KEY]: JSON.stringify({ email }) });
      expect(readPersistedState(s)).toEqual(DEFAULT_CONTACT_EMAIL_STATE);
    }
  });

  test('corrupt JSON returns default without throwing', () => {
    const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
    const s = memoryStorage({ [CONTACT_EMAIL_STORAGE_KEY]: '{not json' });
    expect(readPersistedState(s)).toEqual(DEFAULT_CONTACT_EMAIL_STATE);
    warn.mockRestore();
  });
});

describe('writePersistedState', () => {
  test('writes the state verbatim', () => {
    const s = memoryStorage();
    writePersistedState({ email: '[email protected]' }, s);
    expect(s.raw()).toBe(JSON.stringify({ email: '[email protected]' }));
  });
});

describe('storage that refuses to write', () => {
  test('a write failure does not propagate and the store keeps the value in memory', () => {
    const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
    const store = createContactEmailStore(refusingStorage());
    expect(() => store.remember('[email protected]')).not.toThrow();
    expect(store.getSnapshot().email).toBe('[email protected]');
    warn.mockRestore();
  });
});

describe('createContactEmailStore', () => {
  test('remember trims and persists; a fresh store over the same storage sees it', () => {
    const shared = memoryStorage();
    createContactEmailStore(shared).remember('  [email protected]  ');
    expect(createContactEmailStore(shared).getSnapshot().email).toBe('[email protected]');
  });

  test('remember ignores blank and over-long values', () => {
    const store = createContactEmailStore(memoryStorage());
    store.remember('   ');
    expect(store.getSnapshot().email).toBeNull();
    store.remember('x'.repeat(CONTACT_EMAIL_MAX_LENGTH + 1));
    expect(store.getSnapshot().email).toBeNull();
  });

  test('forget clears the address, and nothing survives a fresh store', () => {
    const shared = memoryStorage();
    const store = createContactEmailStore(shared);
    store.remember('[email protected]');
    store.forget();
    expect(store.getSnapshot().email).toBeNull();
    expect(createContactEmailStore(shared).getSnapshot().email).toBeNull();
  });

  test('subscribe notifies on real transitions only', () => {
    const store = createContactEmailStore(memoryStorage());
    let calls = 0;
    const unsub = store.subscribe(() => {
      calls++;
    });
    store.remember('[email protected]');
    store.remember('[email protected]');
    store.forget();
    store.forget();
    unsub();
    store.remember('[email protected]');
    expect(calls).toBe(2);
  });

  test('install re-reads storage written after construction', () => {
    const s = memoryStorage();
    const store = createContactEmailStore(s);
    expect(store.getSnapshot().email).toBeNull();
    writePersistedState({ email: '[email protected]' }, s);
    store.install();
    expect(store.getSnapshot().email).toBe('[email protected]');
  });

  test('syncFromStorage adopts another window write and notifies', () => {
    const shared = memoryStorage();
    const thisWindow = createContactEmailStore(shared);
    let notified = 0;
    thisWindow.subscribe(() => {
      notified++;
    });

    const otherWindow = createContactEmailStore(shared);
    otherWindow.remember('[email protected]');

    thisWindow.syncFromStorage();
    expect(thisWindow.getSnapshot().email).toBe('[email protected]');
    expect(notified).toBeGreaterThan(0);
  });

  test('syncFromStorage adopts another window forget', () => {
    const shared = memoryStorage();
    const thisWindow = createContactEmailStore(shared);
    thisWindow.remember('[email protected]');

    const otherWindow = createContactEmailStore(shared);
    otherWindow.forget();

    thisWindow.syncFromStorage();
    expect(thisWindow.getSnapshot().email).toBeNull();
  });
});

describe('commitContactEmail', () => {
  test('a checked box remembers the address', () => {
    const shared = memoryStorage();
    const store = createContactEmailStore(shared);
    commitContactEmail(true, '[email protected]', store);
    expect(createContactEmailStore(shared).getSnapshot().email).toBe('[email protected]');
  });

  test('an unchecked box forgets a previously stored address', () => {
    const shared = memoryStorage();
    const store = createContactEmailStore(shared);
    commitContactEmail(true, '[email protected]', store);
    commitContactEmail(false, '[email protected]', store);
    expect(createContactEmailStore(shared).getSnapshot().email).toBeNull();
  });
});