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.

6.1 KB
Raw Blame
import { LOADING_FLAT } from '@lobechat/const';
import { renderHook } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';

import { markdownElements } from '../Markdown/plugins';
import { useChatMarkdown } from './useChatMarkdown';

// The hook reads a general-settings slice for the fade-in transition; stub it so
// the hook renders without a full store. We only assert plugin wiring here.
vi.mock('@/store/user', () => ({
  useUserStore: (selector: any) => selector({ settings: {} }),
}));
let mockTransitionMode = 'none';

vi.mock('@/store/user/selectors', () => ({
  userGeneralSettingsSelectors: { config: () => ({ transitionMode: mockTransitionMode }) },
}));

const remarkPluginFor = (tag: string) =>
  markdownElements.find((el) => el.tag === tag)?.remarkPlugin;

describe('useChatMarkdown (assistant / grouped message pipeline)', () => {
  afterEach(() => {
    mockTransitionMode = 'none';
  });

  it('excludes user-scoped plugins so echoed <skill>/<tool> tags never become chips', () => {
    const { result } = renderHook(() => useChatMarkdown({ id: 'a1', isGenerating: false }));

    const remarkPlugins = result.current.markdownProps.remarkPlugins ?? [];

    const skillPlugin = remarkPluginFor('skill');
    const toolPlugin = remarkPluginFor('tool');
    expect(skillPlugin).toBeTruthy();
    expect(toolPlugin).toBeTruthy();

    // The Skill / Tool plugins are `scope: 'user'` — they must not run on the
    // assistant path (the #2 fix). Before the fix, this hook included them.
    expect(remarkPlugins).not.toContain(skillPlugin);
    expect(remarkPlugins).not.toContain(toolPlugin);
  });

  it('still includes assistant-scoped plugins', () => {
    const { result } = renderHook(() => useChatMarkdown({ id: 'a2', isGenerating: false }));
    const remarkPlugins = result.current.markdownProps.remarkPlugins ?? [];

    // Sanity: an assistant-scoped plugin (e.g. LobeThinking) is still wired in, so
    // the filter narrowed the set rather than emptying it.
    const assistantScoped = markdownElements.find(
      (el) => el.scope === 'assistant' && el.remarkPlugin,
    );
    expect(assistantScoped).toBeTruthy();
    expect(remarkPlugins).toContain(assistantScoped!.remarkPlugin);
  });

  it('keeps markdown footnotes visible when structured citations are absent', () => {
    const { result } = renderHook(() => useChatMarkdown({ id: 'a3', isGenerating: false }));

    expect(result.current.markdownProps.showFootnotes).toBe(true);
  });

  it('keeps animated disabled when streaming is disabled even during generation', () => {
    mockTransitionMode = 'fadeIn';

    const { result } = renderHook(() =>
      useChatMarkdown({ enableStream: false, id: 'a4', isGenerating: true }),
    );

    expect(result.current.markdownProps.animated).toBe(false);
    expect(result.current.markdownProps.enableStream).toBe(false);
  });

  it('animates a message that has never been on screen', () => {
    mockTransitionMode = 'fadeIn';

    const { result } = renderHook(() =>
      useChatMarkdown({ content: 'Shown reply', id: 'fresh-1', isGenerating: true }),
    );

    expect(result.current.markdownProps.animated).toBe(true);
  });

  it('does not re-enter streaming after the text settled statically', () => {
    mockTransitionMode = 'fadeIn';

    // A transient block below makes this one non-last, then disappears again.
    const { result, rerender } = renderHook(
      ({ enableStream }) =>
        useChatMarkdown({
          content: 'Shown reply',
          enableStream,
          id: 'settle-1',
          isGenerating: true,
        }),
      { initialProps: { enableStream: false } },
    );
    rerender({ enableStream: true });

    expect(result.current.markdownProps.animated).toBe(false);
    expect(result.current.markdownProps.enableStream).toBe(false);
  });

  it('does not replay the fade when a message whose text was shown remounts', () => {
    mockTransitionMode = 'fadeIn';

    const first = renderHook(() =>
      useChatMarkdown({ content: 'Shown reply', id: 'remount-1', isGenerating: true }),
    );
    expect(first.result.current.markdownProps.animated).toBe(true);
    first.unmount();

    const second = renderHook(() =>
      useChatMarkdown({ content: 'Shown reply', id: 'remount-1', isGenerating: true }),
    );

    expect(second.result.current.markdownProps.animated).toBe(false);
  });

  it('still animates after a remount when no text had been shown yet', () => {
    mockTransitionMode = 'fadeIn';

    renderHook(() =>
      useChatMarkdown({ content: LOADING_FLAT, id: 'empty-1', isGenerating: true }),
    ).unmount();

    const { result } = renderHook(() =>
      useChatMarkdown({ content: 'Shown reply', id: 'empty-1', isGenerating: true }),
    );

    expect(result.current.markdownProps.animated).toBe(true);
  });

  it('keeps streaming a reply that keeps growing after it remounts', () => {
    mockTransitionMode = 'fadeIn';

    // A post-tool reply starts inside the workflow fold as a short status line,
    // then moves out to the answer segment once it grows, which remounts it.
    renderHook(() =>
      useChatMarkdown({ content: 'Let me summarize.', id: 'grow-1', isGenerating: true }),
    ).unmount();

    const { result } = renderHook(() =>
      useChatMarkdown({
        content: 'Let me summarize.\n\n## Findings',
        id: 'grow-1',
        isGenerating: true,
      }),
    );

    expect(result.current.markdownProps.animated).toBe(true);
    expect(result.current.markdownProps.enableStream).toBe(true);
  });

  it('resumes streaming when settled text grows again', () => {
    mockTransitionMode = 'fadeIn';

    const { result, rerender } = renderHook(
      ({ content, enableStream }) =>
        useChatMarkdown({ content, enableStream, id: 'resume-1', isGenerating: true }),
      { initialProps: { content: 'Shown reply', enableStream: false } },
    );
    rerender({ content: 'Shown reply', enableStream: true });
    expect(result.current.markdownProps.animated).toBe(false);

    rerender({ content: 'Shown reply, continued', enableStream: true });
    expect(result.current.markdownProps.animated).toBe(true);
  });
});