@eeacms/volto-eea-chatbot 1.0.9 → 1.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/CHANGELOG.md +15 -752
  2. package/package.json +1 -1
  3. package/razzle.extend.js +8 -4
  4. package/src/ChatBlock/ChatBlockView.jsx +26 -2
  5. package/src/ChatBlock/chat/AIMessage.tsx +5 -1
  6. package/src/ChatBlock/chat/ChatWindow.tsx +12 -3
  7. package/src/ChatBlock/components/AutoResizeTextarea.jsx +2 -1
  8. package/src/ChatBlock/components/QualityCheckToggle.jsx +1 -1
  9. package/src/ChatBlock/hooks/useChatController.ts +10 -2
  10. package/src/ChatBlock/index.js +1 -1
  11. package/src/ChatBlock/packets/renderers/MessageTextRenderer.tsx +8 -0
  12. package/src/ChatBlock/services/streamingService.ts +30 -26
  13. package/src/ChatBlock/style.less +3 -1
  14. package/src/ChatBlock/tests/ChatMessage.test.jsx +75 -0
  15. package/src/ChatBlock/tests/ClaimModal.test.jsx +136 -0
  16. package/src/ChatBlock/tests/ClaimSegments.test.jsx +206 -0
  17. package/src/ChatBlock/tests/CustomToolRenderer.test.jsx +241 -0
  18. package/src/ChatBlock/tests/FetchToolRenderer.test.jsx +161 -0
  19. package/src/ChatBlock/tests/ImageToolRenderer.test.jsx +178 -0
  20. package/src/ChatBlock/tests/MessageTextRenderer.test.jsx +227 -0
  21. package/src/ChatBlock/tests/MultiToolRenderer.test.jsx +134 -0
  22. package/src/ChatBlock/tests/ReasoningRenderer.test.jsx +163 -0
  23. package/src/ChatBlock/tests/RenderClaimView.test.jsx +191 -0
  24. package/src/ChatBlock/tests/RendererComponent.test.jsx +139 -0
  25. package/src/ChatBlock/tests/SearchToolRenderer.test.jsx +295 -0
  26. package/src/ChatBlock/tests/SourceChip.test.jsx +108 -0
  27. package/src/ChatBlock/tests/UserActionsToolbar.test.jsx +135 -0
  28. package/src/ChatBlock/tests/UserMessage.test.jsx +83 -0
  29. package/src/ChatBlock/tests/WebResultIcon.test.jsx +61 -0
  30. package/src/ChatBlock/tests/citations.test.js +114 -0
  31. package/src/ChatBlock/tests/messageProcessor.test.jsx +285 -1
  32. package/src/ChatBlock/tests/packetUtils.test.js +158 -0
  33. package/src/ChatBlock/tests/streamingService.test.js +467 -0
  34. package/src/ChatBlock/tests/useChatController.test.jsx +268 -0
  35. package/src/ChatBlock/tests/useChatStreaming.test.jsx +163 -0
  36. package/src/ChatBlock/tests/useMarked.test.jsx +107 -0
  37. package/src/ChatBlock/tests/useQualityMarkers.test.jsx +150 -0
  38. package/src/ChatBlock/tests/useScrollonStream.test.jsx +121 -0
  39. package/src/ChatBlock/tests/utils.test.jsx +241 -0
  40. package/src/ChatBlock/tests/withOnyxData.test.jsx +81 -0
  41. package/src/ChatBlock/utils/citations.ts +1 -1
  42. package/src/halloumi/generative.js +1 -0
  43. package/src/halloumi/generative.test.js +278 -0
  44. package/src/halloumi/middleware.test.js +69 -0
  45. package/src/index.js +1 -0
  46. package/src/middleware.js +21 -13
  47. package/src/middleware.test.js +221 -0
@@ -0,0 +1,108 @@
1
+ import React from 'react';
2
+ import renderer, { act } from 'react-test-renderer';
3
+ import { render, fireEvent, screen } from '@testing-library/react';
4
+ import '@testing-library/jest-dom/extend-expect';
5
+ import { SourceChip } from '../components/SourceChip';
6
+
7
+ describe('SourceChip', () => {
8
+ beforeEach(() => {
9
+ jest.useFakeTimers();
10
+ });
11
+
12
+ afterEach(() => {
13
+ jest.useRealTimers();
14
+ });
15
+
16
+ it('renders with title only', () => {
17
+ const component = renderer.create(<SourceChip title="Test Source" />);
18
+ const json = component.toJSON();
19
+ expect(json).toMatchSnapshot();
20
+ });
21
+
22
+ it('renders with icon', () => {
23
+ const MockIcon = () => <span data-testid="mock-icon">Icon</span>;
24
+ const component = renderer.create(
25
+ <SourceChip title="Test Source" icon={<MockIcon />} />,
26
+ );
27
+ const json = component.toJSON();
28
+ expect(json).toMatchSnapshot();
29
+ });
30
+
31
+ it('renders with remove button when onRemove is provided', () => {
32
+ const onRemove = jest.fn();
33
+ const component = renderer.create(
34
+ <SourceChip title="Test Source" onRemove={onRemove} />,
35
+ );
36
+ const json = component.toJSON();
37
+ expect(json).toMatchSnapshot();
38
+ });
39
+
40
+ it('calls onClick when clicked', () => {
41
+ const onClick = jest.fn();
42
+ render(<SourceChip title="Test Source" onClick={onClick} />);
43
+
44
+ fireEvent.click(screen.getByRole('button'));
45
+ expect(onClick).toHaveBeenCalledTimes(1);
46
+ });
47
+
48
+ it('calls onRemove when remove button is clicked', () => {
49
+ const onRemove = jest.fn();
50
+ const onClick = jest.fn();
51
+ render(
52
+ <SourceChip title="Test Source" onRemove={onRemove} onClick={onClick} />,
53
+ );
54
+
55
+ const removeButton = screen.getByRole('button', { name: /Remove/i });
56
+ fireEvent.click(removeButton);
57
+
58
+ expect(onRemove).toHaveBeenCalledTimes(1);
59
+ expect(onClick).not.toHaveBeenCalled(); // stopPropagation should prevent this
60
+ });
61
+
62
+ it('calls onRemove when Enter key is pressed on remove button', () => {
63
+ const onRemove = jest.fn();
64
+ render(<SourceChip title="Test Source" onRemove={onRemove} />);
65
+
66
+ const removeButton = screen.getByRole('button', { name: /Remove/i });
67
+ fireEvent.keyDown(removeButton, { key: 'Enter' });
68
+
69
+ expect(onRemove).toHaveBeenCalledTimes(1);
70
+ });
71
+
72
+ it('does not call onRemove when other keys are pressed', () => {
73
+ const onRemove = jest.fn();
74
+ render(<SourceChip title="Test Source" onRemove={onRemove} />);
75
+
76
+ const removeButton = screen.getByRole('button', { name: /Remove/i });
77
+ fireEvent.keyDown(removeButton, { key: 'Escape' });
78
+
79
+ expect(onRemove).not.toHaveBeenCalled();
80
+ });
81
+
82
+ it('applies animation class when includeAnimation is true', () => {
83
+ render(<SourceChip title="Test Source" includeAnimation={true} />);
84
+
85
+ const button = screen.getByRole('button');
86
+ expect(button).toHaveClass('animate-in');
87
+ });
88
+
89
+ it('removes animation class after timeout', () => {
90
+ render(<SourceChip title="Test Source" includeAnimation={true} />);
91
+
92
+ const button = screen.getByRole('button');
93
+ expect(button).toHaveClass('animate-in');
94
+
95
+ act(() => {
96
+ jest.advanceTimersByTime(300);
97
+ });
98
+
99
+ expect(button).not.toHaveClass('animate-in');
100
+ });
101
+
102
+ it('does not apply animation class when includeAnimation is false', () => {
103
+ render(<SourceChip title="Test Source" includeAnimation={false} />);
104
+
105
+ const button = screen.getByRole('button');
106
+ expect(button).not.toHaveClass('animate-in');
107
+ });
108
+ });
@@ -0,0 +1,135 @@
1
+ import React from 'react';
2
+ import { MemoryRouter } from 'react-router-dom';
3
+ import configureStore from 'redux-mock-store';
4
+ import renderer from 'react-test-renderer';
5
+ import { render, fireEvent, screen } from '@testing-library/react';
6
+ import '@testing-library/jest-dom/extend-expect';
7
+ import { Provider } from 'react-intl-redux';
8
+ import UserActionsToolbar from '../components/UserActionsToolbar';
9
+
10
+ const mockStore = configureStore();
11
+
12
+ // Mock clipboard API
13
+ Object.assign(navigator, {
14
+ clipboard: {
15
+ writeText: jest.fn().mockResolvedValue(undefined),
16
+ },
17
+ });
18
+
19
+ describe('UserActionsToolbar', () => {
20
+ let store;
21
+
22
+ beforeEach(() => {
23
+ store = mockStore({
24
+ userSession: { token: '1234' },
25
+ intl: { locale: 'en', messages: {} },
26
+ });
27
+ jest.clearAllMocks();
28
+ });
29
+
30
+ const renderComponent = (props) =>
31
+ renderer.create(
32
+ <Provider store={store}>
33
+ <MemoryRouter>
34
+ <UserActionsToolbar {...props} />
35
+ </MemoryRouter>
36
+ </Provider>,
37
+ );
38
+
39
+ const renderWithRTL = (props) =>
40
+ render(
41
+ <Provider store={store}>
42
+ <MemoryRouter>
43
+ <UserActionsToolbar {...props} />
44
+ </MemoryRouter>
45
+ </Provider>,
46
+ );
47
+
48
+ it('renders toolbar with copy button', () => {
49
+ const props = {
50
+ message: {
51
+ messageId: 1,
52
+ message: 'Test message content',
53
+ },
54
+ enableFeedback: false,
55
+ };
56
+
57
+ const component = renderComponent(props);
58
+ const json = component.toJSON();
59
+ expect(json).toMatchSnapshot();
60
+ });
61
+
62
+ it('renders toolbar with feedback when enabled', () => {
63
+ const props = {
64
+ message: {
65
+ messageId: 1,
66
+ message: 'Test message content',
67
+ },
68
+ enableFeedback: true,
69
+ feedbackReasons: ['Incorrect', 'Unhelpful'],
70
+ };
71
+
72
+ const component = renderComponent(props);
73
+ const json = component.toJSON();
74
+ expect(json).toMatchSnapshot();
75
+ });
76
+
77
+ it('renders with custom className', () => {
78
+ const props = {
79
+ message: {
80
+ messageId: 1,
81
+ message: 'Test message content',
82
+ },
83
+ className: 'custom-toolbar',
84
+ enableFeedback: false,
85
+ };
86
+
87
+ const component = renderComponent(props);
88
+ const json = component.toJSON();
89
+ expect(json).toMatchSnapshot();
90
+ });
91
+
92
+ it('copies message to clipboard when copy button is clicked', async () => {
93
+ const props = {
94
+ message: {
95
+ messageId: 1,
96
+ message: 'Test message to copy',
97
+ },
98
+ enableFeedback: false,
99
+ };
100
+
101
+ renderWithRTL(props);
102
+
103
+ const copyButton = screen.getByRole('button', { name: /copy/i });
104
+ fireEvent.click(copyButton);
105
+
106
+ expect(navigator.clipboard.writeText).toHaveBeenCalledWith(
107
+ 'Test message to copy',
108
+ );
109
+ });
110
+
111
+ it('handles empty message gracefully', () => {
112
+ const props = {
113
+ message: {
114
+ messageId: 1,
115
+ message: '',
116
+ },
117
+ enableFeedback: false,
118
+ };
119
+
120
+ const component = renderComponent(props);
121
+ const json = component.toJSON();
122
+ expect(json).toMatchSnapshot();
123
+ });
124
+
125
+ it('handles undefined message gracefully', () => {
126
+ const props = {
127
+ message: null,
128
+ enableFeedback: false,
129
+ };
130
+
131
+ const component = renderComponent(props);
132
+ const json = component.toJSON();
133
+ expect(json).toMatchSnapshot();
134
+ });
135
+ });
@@ -0,0 +1,83 @@
1
+ import { MemoryRouter } from 'react-router-dom';
2
+ import configureStore from 'redux-mock-store';
3
+ import renderer from 'react-test-renderer';
4
+
5
+ import '@testing-library/jest-dom/extend-expect';
6
+ import { Provider } from 'react-intl-redux';
7
+ import { UserMessage } from '../chat/UserMessage';
8
+
9
+ const mockStore = configureStore();
10
+
11
+ // Mock loadable components
12
+ jest.mock('@loadable/component', () => {
13
+ return () => {
14
+ const MockComponent = ({ children }) => <div>{children}</div>;
15
+ return MockComponent;
16
+ };
17
+ });
18
+
19
+ describe('UserMessage', () => {
20
+ let store;
21
+
22
+ beforeEach(() => {
23
+ store = mockStore({
24
+ userSession: { token: '1234' },
25
+ intl: { locale: 'en', messages: {} },
26
+ });
27
+ });
28
+
29
+ const renderComponent = (props) =>
30
+ renderer.create(
31
+ <Provider store={store}>
32
+ <MemoryRouter>
33
+ <UserMessage {...props} />
34
+ </MemoryRouter>
35
+ </Provider>,
36
+ );
37
+
38
+ it('renders user message with basic text', () => {
39
+ const props = {
40
+ message: {
41
+ messageId: 1,
42
+ message: 'Hello, this is my question',
43
+ type: 'user',
44
+ },
45
+ libs: { remarkGfm: { default: [] } },
46
+ };
47
+
48
+ const component = renderComponent(props);
49
+ const json = component.toJSON();
50
+ expect(json).toMatchSnapshot();
51
+ });
52
+
53
+ it('renders user message with custom className', () => {
54
+ const props = {
55
+ message: {
56
+ messageId: 2,
57
+ message: 'Another question',
58
+ type: 'user',
59
+ },
60
+ libs: { remarkGfm: { default: [] } },
61
+ className: 'most-recent',
62
+ };
63
+
64
+ const component = renderComponent(props);
65
+ const json = component.toJSON();
66
+ expect(json).toMatchSnapshot();
67
+ });
68
+
69
+ it('renders user message without className', () => {
70
+ const props = {
71
+ message: {
72
+ messageId: 4,
73
+ message: 'Test message',
74
+ type: 'user',
75
+ },
76
+ libs: { remarkGfm: { default: [] } },
77
+ };
78
+
79
+ const component = renderComponent(props);
80
+ const json = component.toJSON();
81
+ expect(json).toMatchSnapshot();
82
+ });
83
+ });
@@ -0,0 +1,61 @@
1
+ import React from 'react';
2
+ import renderer from 'react-test-renderer';
3
+ import { render, fireEvent, screen } from '@testing-library/react';
4
+ import '@testing-library/jest-dom/extend-expect';
5
+ import { WebResultIcon } from '../components/WebResultIcon';
6
+
7
+ describe('WebResultIcon', () => {
8
+ it('renders favicon for valid URL', () => {
9
+ const component = renderer.create(
10
+ <WebResultIcon url="https://example.com" size={16} />,
11
+ );
12
+ const json = component.toJSON();
13
+ expect(json).toMatchSnapshot();
14
+ });
15
+
16
+ it('renders with default size', () => {
17
+ const component = renderer.create(
18
+ <WebResultIcon url="https://example.com" />,
19
+ );
20
+ const json = component.toJSON();
21
+ expect(json).toMatchSnapshot();
22
+ });
23
+
24
+ it('renders FileIcon for invalid URL', () => {
25
+ const component = renderer.create(
26
+ <WebResultIcon url="invalid-url" size={16} />,
27
+ );
28
+ const json = component.toJSON();
29
+ expect(json).toMatchSnapshot();
30
+ });
31
+
32
+ it('renders GlobeIcon when favicon fails to load', () => {
33
+ render(<WebResultIcon url="https://example.com" size={16} />);
34
+
35
+ const img = screen.getByRole('img');
36
+ fireEvent.error(img);
37
+
38
+ // After error, the component should re-render with GlobeIcon
39
+ // The img should no longer be present
40
+ expect(screen.queryByRole('img')).not.toBeInTheDocument();
41
+ });
42
+
43
+ it('generates correct favicon URL', () => {
44
+ render(<WebResultIcon url="https://www.google.com/search" size={20} />);
45
+
46
+ const img = screen.getByRole('img');
47
+ expect(img).toHaveAttribute(
48
+ 'src',
49
+ 'https://t3.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=https://www.google.com&size=128',
50
+ );
51
+ });
52
+
53
+ it('applies correct size styles', () => {
54
+ render(<WebResultIcon url="https://example.com" size={24} />);
55
+
56
+ const img = screen.getByRole('img');
57
+ expect(img).toHaveAttribute('height', '24');
58
+ expect(img).toHaveAttribute('width', '24');
59
+ expect(img).toHaveStyle({ height: '24px', width: '24px' });
60
+ });
61
+ });
@@ -0,0 +1,114 @@
1
+ import { addCitations } from '../utils/citations';
2
+
3
+ describe('addCitations', () => {
4
+ it('transforms single citation marker into markdown link', () => {
5
+ const message = {
6
+ citations: {
7
+ 1: 'https://example.com/doc1',
8
+ },
9
+ };
10
+ const text = 'This is a fact [1].';
11
+ const result = addCitations(text, message);
12
+ expect(result).toBe('This is a fact [[1]](https://example.com/doc1).');
13
+ });
14
+
15
+ it('transforms multiple citation markers', () => {
16
+ const message = {
17
+ citations: {
18
+ 1: 'https://example.com/doc1',
19
+ 2: 'https://example.com/doc2',
20
+ 3: 'https://example.com/doc3',
21
+ },
22
+ };
23
+ const text = 'Fact one [1], fact two [2], and fact three [3].';
24
+ const result = addCitations(text, message);
25
+ expect(result).toBe(
26
+ 'Fact one [[1]](https://example.com/doc1), fact two [[2]](https://example.com/doc2), and fact three [[3]](https://example.com/doc3).',
27
+ );
28
+ });
29
+
30
+ it('does not transform already formatted citations [1](url)', () => {
31
+ const message = {
32
+ citations: {
33
+ 1: 'https://example.com/doc1',
34
+ },
35
+ };
36
+ const text = 'Already formatted [[1]](https://example.com/doc1).';
37
+ const result = addCitations(text, message);
38
+ expect(result).toBe('Already formatted [[1]](https://example.com/doc1).');
39
+ });
40
+
41
+ it('does not transform citations in brackets like [1][', () => {
42
+ const message = {
43
+ citations: {
44
+ 1: 'https://example.com/doc1',
45
+ },
46
+ };
47
+ const text = 'Text with [1][';
48
+ const result = addCitations(text, message);
49
+ expect(result).toBe('Text with [1][');
50
+ });
51
+
52
+ it('does not transform citations followed by ]', () => {
53
+ const message = {
54
+ citations: {
55
+ 1: 'https://example.com/doc1',
56
+ },
57
+ };
58
+ const text = 'Text with [1]]';
59
+ const result = addCitations(text, message);
60
+ expect(result).toBe('Text with [1]]');
61
+ });
62
+
63
+ it('returns original text when no citations in message', () => {
64
+ const message = {};
65
+ const text = 'This is a fact [1].';
66
+ const result = addCitations(text, message);
67
+ expect(result).toBe('This is a fact [1].');
68
+ });
69
+
70
+ it('returns original text when citations is undefined', () => {
71
+ const message = { citations: undefined };
72
+ const text = 'This is a fact [1].';
73
+ const result = addCitations(text, message);
74
+ expect(result).toBe('This is a fact [1].');
75
+ });
76
+
77
+ it('handles text with no citation markers', () => {
78
+ const message = {
79
+ citations: {
80
+ 1: 'https://example.com/doc1',
81
+ },
82
+ };
83
+ const text = 'This is plain text without citations.';
84
+ const result = addCitations(text, message);
85
+ expect(result).toBe('This is plain text without citations.');
86
+ });
87
+
88
+ it('handles double-digit citation numbers', () => {
89
+ const message = {
90
+ citations: {
91
+ 10: 'https://example.com/doc10',
92
+ 11: 'https://example.com/doc11',
93
+ },
94
+ };
95
+ const text = 'References [10] and [11].';
96
+ const result = addCitations(text, message);
97
+ expect(result).toBe(
98
+ 'References [[10]](https://example.com/doc10) and [[11]](https://example.com/doc11).',
99
+ );
100
+ });
101
+
102
+ it('handles citation number not in citations object', () => {
103
+ const message = {
104
+ citations: {
105
+ 1: 'https://example.com/doc1',
106
+ },
107
+ };
108
+ const text = 'Citation [2] not found.';
109
+ const result = addCitations(text, message);
110
+ // When citation number exists but is not in citations object,
111
+ // the function returns the citation with undefined URL
112
+ expect(result).toBe('Citation [[2]](undefined) not found.');
113
+ });
114
+ });