import { describe, it, expect } from "vitest";
import { screen } from "@testing-library/react";
import { renderWithMantine } from "../../test/utils";
import EventBubble from "../../components/EventBubble";
import type { MatrixEvent } from "../../types";

function event(over: Partial<MatrixEvent> = {}): MatrixEvent {
  return {
    event_id: "$evt-1",
    room_id: "!room1:server",
    sender: "@bob:server",
    type: "m.room.message",
    ts: Date.now(),
    body: "hello",
    encrypted_raw: false,
    ...over,
  } as MatrixEvent;
}

describe("EventBubble", () => {
  it("renders the body of a plain m.room.message", () => {
    renderWithMantine(<EventBubble event={event({ body: "hello there" })} isSelf={false} />);
    expect(screen.getByText("hello there")).toBeInTheDocument();
  });

  it("renders the encrypted placeholder when encrypted_raw is true", () => {
    renderWithMantine(
      <EventBubble
        event={event({
          type: "m.room.encrypted",
          encrypted_raw: true,
          body: "",
        })}
        isSelf={false}
      />,
    );
    expect(screen.getByText(/encrypted message/i)).toBeInTheDocument();
  });

  it("shows the sender short form", () => {
    renderWithMantine(
      <EventBubble
        event={event({ sender: "@alice:matrix.server", body: "yo" })}
        isSelf={false}
      />,
    );
    expect(screen.getByText("@alice")).toBeInTheDocument();
  });
});
