import { afterEach, describe, expect, it } from 'vitest';
import { usePreviewStore } from './PreviewStore';

const store = () => usePreviewStore.getState();

afterEach(() => {
  // Reset to a known clean state between tests so store leaks don't affect order.
  store().setError(null);
  store().setBusy(false);
});

describe('PreviewStore — error slice', () => {
  it('starts with null error', () => {
    expect(store().error).toBeNull();
  });

  it('setError stores the provided message', () => {
    store().setError('Rendering error: something went wrong.');
    expect(store().error).toBe('Rendering error: something went wrong.');
  });

  it('setError(null) clears the message', () => {
    store().setError('an error');
    store().setError(null);
    expect(store().error).toBeNull();
  });
});

describe('PreviewStore — busy slice', () => {
  it('starts with busy=false', () => {
    expect(store().busy).toBe(false);
  });

  it('setBusy(true) sets busy to true', () => {
    store().setBusy(true);
    expect(store().busy).toBe(true);
  });

  it('setBusy(false) clears busy', () => {
    store().setBusy(true);
    store().setBusy(false);
    expect(store().busy).toBe(false);
  });

  it('busy and error are independent — setting one does not affect the other', () => {
    store().setBusy(true);
    store().setError('some error');
    expect(store().busy).toBe(true);
    expect(store().error).toBe('some error');

    store().setBusy(false);
    expect(store().error).toBe('some error'); // error unchanged by clearing busy
  });
});
