Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

5.7 KB
Raw Blame
/**
 * @vitest-environment happy-dom
 */
import { moment } from '@lobehub/editor';
import { act, cleanup, fireEvent, render, screen } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';

import RichTextMessage from './RichTextMessage';

vi.mock('@/features/MemberProfileCard/MentionProfileTrigger', () => ({
  default: ({ children, id, type }: { children: React.ReactNode; id?: string; type?: string }) => (
    <span data-mention-profile={id} data-mention-type={type}>
      {children}
    </span>
  ),
}));

const mentionEditorState = {
  root: {
    children: [
      {
        children: [
          {
            label: 'Agent A',
            metadata: { id: 'agent-a', type: 'agent' },
            type: 'mention',
            version: 1,
          },
        ],
        direction: null,
        format: '',
        indent: 0,
        type: 'paragraph',
        version: 1,
      },
    ],
    direction: null,
    format: '',
    indent: 0,
    type: 'root',
    version: 1,
  },
};

const localFileEditorState = {
  root: {
    children: [
      {
        children: [
          {
            isDirectory: false,
            name: 'report.md',
            path: '/Users/me/project/report.md',
            type: 'local-file-tag',
            version: 1,
          },
        ],
        direction: null,
        format: '',
        indent: 0,
        type: 'paragraph',
        version: 1,
      },
    ],
    direction: null,
    format: '',
    indent: 0,
    type: 'root',
    version: 1,
  },
};

const blockImageEditorState = {
  root: {
    children: [
      {
        altText: 'usage chart',
        height: 'inherit',
        maxWidth: 904,
        src: 'https://example.com/chart.png',
        type: 'block-image',
        version: 1,
        width: 'inherit',
      },
    ],
    direction: null,
    format: '',
    indent: 0,
    type: 'root',
    version: 1,
  },
};

afterEach(() => {
  cleanup();
});

const imageRefEditorState = {
  root: {
    children: [
      {
        children: [
          {
            detail: 0,
            format: 0,
            mode: 'normal',
            style: '',
            text: 'compare ',
            type: 'text',
            version: 1,
          },
          { number: 2, src: 'blob:gone', type: 'image-ref-tag', version: 1 },
        ],
        direction: null,
        format: '',
        indent: 0,
        type: 'paragraph',
        version: 1,
      },
    ],
    direction: null,
    format: '',
    indent: 0,
    type: 'root',
    version: 1,
  },
};

describe('RichTextMessage', () => {
  it('should allow document comments to use the default 16px renderer scale', async () => {
    const { container } = render(
      <RichTextMessage editorState={mentionEditorState} variant={'default'} />,
    );

    await act(async () => {
      await moment();
    });

    expect(
      (container.firstElementChild as HTMLElement).style.getPropertyValue('--common-font-size'),
    ).toBe('16px');
  });

  it('should render mention nodes from editor state', async () => {
    const { container } = render(<RichTextMessage editorState={mentionEditorState} />);

    await act(async () => {
      await moment();
    });

    expect(container.querySelector('.editor_mention')?.textContent).toBe('@Agent A');
  });

  it('should open the profile card of the mentioned Agent on hover', async () => {
    const { container } = render(<RichTextMessage editorState={mentionEditorState} />);

    await act(async () => {
      await moment();
    });

    const trigger = container.querySelector('[data-mention-profile]');
    expect(trigger?.getAttribute('data-mention-profile')).toBe('agent-a');
    expect(trigger?.getAttribute('data-mention-type')).toBe('agent');
    expect(trigger?.querySelector('.editor_mention')?.textContent).toBe('@Agent A');
  });

  // Regression: a local file dragged from the working sidebar serializes to a
  // `local-file-tag` node. If that node isn't registered on the renderer,
  // LexicalRenderer throws while parsing the state and the whole message crashes.
  it('should render local-file-tag nodes without crashing', async () => {
    const { container } = render(<RichTextMessage editorState={localFileEditorState} />);

    await act(async () => {
      await moment();
    });

    expect(container.textContent).toContain('report.md');
  });

  // Regression: an image pasted into the composer serializes to an `image-ref-tag`
  // node. Unregistered on the renderer, it made the sent message drop all its text.
  it('should render image-ref-tag nodes with the surrounding text', async () => {
    const { container } = render(<RichTextMessage editorState={imageRefEditorState} />);

    await act(async () => {
      await moment();
    });

    expect(container.textContent).toContain('compare');
    expect(container.textContent).toContain('upload.image.label');
  });

  // Regression: the renderer's stock image node is a bare <img>,
  // so an image inside a published comment could not be enlarged.
  it('should open the image viewer when a rendered image is clicked', async () => {
    const { container } = render(
      <RichTextMessage editorState={blockImageEditorState} variant={'default'} />,
    );

    await act(async () => {
      await moment();
    });

    const image = container.querySelector('img');
    expect(image).toHaveAttribute('src', 'https://example.com/chart.png');
    expect(container.querySelector('figure')).not.toBeNull();

    await act(async () => {
      fireEvent.pointerDown(image!);
      fireEvent.click(image!);
    });

    expect(await screen.findByRole('dialog')).toBeInTheDocument();
  });

  it('should render nothing for empty editor state', () => {
    const { container } = render(<RichTextMessage editorState={{}} />);

    expect(container).toBeEmptyDOMElement();
  });
});