import { screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { renderWithProviders } from "../utils/testHelpers";
import ChatProvider from "./ChatProvider";

// Mock Stream Chat
vi.mock("stream-chat", () => ({
  StreamChat: {
    getInstance: vi.fn(() => ({
      connectUser: vi.fn(),
      disconnectUser: vi.fn(),
    })),
  },
  StateStore: vi.fn(),
  FixedSizeQueueCache: vi.fn(),
}));

vi.mock("stream-chat-react", () => ({
  Chat: ({ children }: { children: React.ReactNode }) => (
    <div data-testid="stream-chat">{children}</div>
  ),
  useCreateChatClient: () => ({ id: "test-client" }),
}));

vi.mock("@xtablo/shared/contexts/SessionContext", async () => {
  const actual = await vi.importActual("@xtablo/shared/contexts/SessionContext");
  return {
    ...actual,
    useSession: () => ({
      session: {
        access_token: "test-token",
      },
    }),
  };
});

describe("ChatProvider", () => {
  it("renders children", () => {
    renderWithProviders(
      <ChatProvider>
        <div>Test Child</div>
      </ChatProvider>
    );
    expect(screen.getByText("Test Child")).toBeInTheDocument();
  });

  it("renders without crashing", () => {
    const { container } = renderWithProviders(
      <ChatProvider>
        <div>Content</div>
      </ChatProvider>
    );
    expect(container).toBeInTheDocument();
  });
});
