import { describe, it, expect } from 'vitest';
import { render, screen } from '@testing-library/react';
import { MessagingProvider, useAdapter } from './provider';
import { MockAdapter } from './adapters/mock';

function ShowActor() {
  const adapter = useAdapter();
  return <span>{adapter.currentActorId()}</span>;
}

describe('MessagingProvider', () => {
  it('supplies the injected adapter to descendants', () => {
    render(
      <MessagingProvider adapter={new MockAdapter()}>
        <ShowActor />
      </MessagingProvider>,
    );
    expect(screen.getByText('me')).toBeDefined();
  });

  it('throws a helpful error when a hook is used outside the provider', () => {
    // React logs the error boundary trace; that noise is expected.
    expect(() => render(<ShowActor />)).toThrow(/useAdapter must be used within a <MessagingProvider>/);
  });
});
