import { afterEach, describe, expect, it } from 'vitest';
import { notify, useNotificationStore } from './NotificationStore';

afterEach(() => useNotificationStore.getState().clear());

describe('NotificationStore', () => {
  it('appends a notification carrying its options and returns the id', () => {
    const id = notify({ kind: 'error', title: 'Storage full', message: 'Free space.' });
    const list = useNotificationStore.getState().notifications;
    expect(list).toHaveLength(1);
    expect(list[0]).toMatchObject({
      id,
      kind: 'error',
      title: 'Storage full',
      message: 'Free space.',
    });
  });

  it('coalesces an identical notification instead of stacking duplicates', () => {
    const first = notify({ kind: 'error', title: 'Couldn’t save', message: 'A storage error.' });
    const second = notify({ kind: 'error', title: 'Couldn’t save', message: 'A storage error.' });
    expect(second).toBe(first);
    expect(useNotificationStore.getState().notifications).toHaveLength(1);
  });

  it('does not coalesce when the content differs', () => {
    notify({ kind: 'error', title: 'A', message: 'one' });
    notify({ kind: 'error', title: 'A', message: 'two' });
    notify({ kind: 'warning', title: 'A', message: 'one' });
    expect(useNotificationStore.getState().notifications).toHaveLength(3);
  });

  it('dismiss removes only the matching notification', () => {
    const a = notify({ kind: 'info', title: 'A', message: 'a' });
    notify({ kind: 'info', title: 'B', message: 'b' });
    useNotificationStore.getState().dismiss(a);
    const list = useNotificationStore.getState().notifications;
    expect(list).toHaveLength(1);
    expect(list[0].title).toBe('B');
  });

  it('clear empties the list', () => {
    notify({ kind: 'success', title: 'Saved', message: 'Done.' });
    useNotificationStore.getState().clear();
    expect(useNotificationStore.getState().notifications).toHaveLength(0);
  });
});
