File size: 13,030 Bytes
f0743f4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 | import { renderHook, act } from '@testing-library/react';
import copy from 'copy-to-clipboard';
import { ContentTypes } from 'librechat-data-provider';
import type {
SearchResultData,
ProcessedOrganic,
TMessageContentParts,
} from 'librechat-data-provider';
import useCopyToClipboard from '~/hooks/Messages/useCopyToClipboard';
// Mock the copy-to-clipboard module
jest.mock('copy-to-clipboard');
describe('useCopyToClipboard', () => {
const mockSetIsCopied = jest.fn();
const mockCopy = copy as jest.MockedFunction<typeof copy>;
beforeEach(() => {
jest.clearAllMocks();
jest.useFakeTimers();
});
afterEach(() => {
jest.runOnlyPendingTimers();
jest.useRealTimers();
});
describe('Basic functionality', () => {
it('should copy plain text without citations', () => {
const { result } = renderHook(() =>
useCopyToClipboard({
text: 'Simple text without citations',
}),
);
act(() => {
result.current(mockSetIsCopied);
});
expect(mockCopy).toHaveBeenCalledWith('Simple text without citations', {
format: 'text/plain',
});
expect(mockSetIsCopied).toHaveBeenCalledWith(true);
});
it('should handle content array with text types', () => {
const content = [
{ type: ContentTypes.TEXT, text: 'First line' },
{ type: ContentTypes.TEXT, text: 'Second line' },
];
const { result } = renderHook(() =>
useCopyToClipboard({
content: content as TMessageContentParts[],
}),
);
act(() => {
result.current(mockSetIsCopied);
});
expect(mockCopy).toHaveBeenCalledWith('First line\nSecond line', {
format: 'text/plain',
});
});
it('should reset isCopied after timeout', () => {
const { result } = renderHook(() =>
useCopyToClipboard({
text: 'Test text',
}),
);
act(() => {
result.current(mockSetIsCopied);
});
expect(mockSetIsCopied).toHaveBeenCalledWith(true);
act(() => {
jest.advanceTimersByTime(3000);
});
expect(mockSetIsCopied).toHaveBeenCalledWith(false);
});
});
describe('Citation formatting', () => {
const mockSearchResults: { [key: string]: SearchResultData } = {
'0': {
organic: [
{
link: 'https://example.com/search1',
title: 'Search Result 1',
snippet: 'This is a search result',
},
],
topStories: [
{
link: 'https://example.com/news1',
title: 'News Story 1',
},
{
link: 'https://example.com/news2',
title: 'News Story 2',
},
],
images: [
{
link: 'https://example.com/image1',
title: 'Image 1',
},
],
videos: [
{
link: 'https://example.com/video1',
title: 'Video 1',
},
],
},
};
it('should format standalone search citations', () => {
const text = 'This is a fact \\ue202turn0search0 from search.';
const { result } = renderHook(() =>
useCopyToClipboard({
text,
searchResults: mockSearchResults,
}),
);
act(() => {
result.current(mockSetIsCopied);
});
const expectedText = `This is a fact [1] from search.
Citations:
[1] https://example.com/search1
`;
expect(mockCopy).toHaveBeenCalledWith(expectedText, { format: 'text/plain' });
});
it('should format news citations with correct mapping', () => {
const text = 'Breaking news \\ue202turn0news0 and more news \\ue202turn0news1.';
const { result } = renderHook(() =>
useCopyToClipboard({
text,
searchResults: mockSearchResults,
}),
);
act(() => {
result.current(mockSetIsCopied);
});
const expectedText = `Breaking news [1] and more news [2].
Citations:
[1] https://example.com/news1
[2] https://example.com/news2
`;
expect(mockCopy).toHaveBeenCalledWith(expectedText, { format: 'text/plain' });
});
it('should handle highlighted text with citations', () => {
const text = '\\ue203This is highlighted text\\ue204 \\ue202turn0search0 with citation.';
const { result } = renderHook(() =>
useCopyToClipboard({
text,
searchResults: mockSearchResults,
}),
);
act(() => {
result.current(mockSetIsCopied);
});
const expectedText = `**This is highlighted text** [1] with citation.
Citations:
[1] https://example.com/search1
`;
expect(mockCopy).toHaveBeenCalledWith(expectedText, { format: 'text/plain' });
});
it('should handle composite citations', () => {
const text =
'Multiple sources \\ue200\\ue202turn0search0\\ue202turn0news0\\ue202turn0news1\\ue201.';
const { result } = renderHook(() =>
useCopyToClipboard({
text,
searchResults: mockSearchResults,
}),
);
act(() => {
result.current(mockSetIsCopied);
});
const expectedText = `Multiple sources [1][2][3].
Citations:
[1] https://example.com/search1
[2] https://example.com/news1
[3] https://example.com/news2
`;
expect(mockCopy).toHaveBeenCalledWith(expectedText, { format: 'text/plain' });
});
});
describe('Citation deduplication', () => {
it('should use same number for duplicate URLs', () => {
const mockSearchResultsWithDupes: { [key: string]: SearchResultData } = {
'0': {
organic: [
{
link: 'https://example.com/article',
title: 'Article from search',
},
],
topStories: [
{
link: 'https://example.com/article', // Same URL
title: 'Article from news',
},
],
},
};
const text = 'First citation \\ue202turn0search0 and second \\ue202turn0news0.';
const { result } = renderHook(() =>
useCopyToClipboard({
text,
searchResults: mockSearchResultsWithDupes,
}),
);
act(() => {
result.current(mockSetIsCopied);
});
const expectedText = `First citation [1] and second [1].
Citations:
[1] https://example.com/article
`;
expect(mockCopy).toHaveBeenCalledWith(expectedText, { format: 'text/plain' });
});
it('should handle multiple citations of the same source', () => {
const mockSearchResults: { [key: string]: SearchResultData } = {
'0': {
organic: [
{
link: 'https://example.com/source1',
title: 'Source 1',
},
],
},
};
const text =
'First mention \\ue202turn0search0. Second mention \\ue202turn0search0. Third \\ue202turn0search0.';
const { result } = renderHook(() =>
useCopyToClipboard({
text,
searchResults: mockSearchResults,
}),
);
act(() => {
result.current(mockSetIsCopied);
});
const expectedText = `First mention [1]. Second mention [1]. Third [1].
Citations:
[1] https://example.com/source1
`;
expect(mockCopy).toHaveBeenCalledWith(expectedText, { format: 'text/plain' });
});
});
describe('Edge cases', () => {
it('should handle missing search results gracefully', () => {
const text = 'Text with citation \\ue202turn0search0 but no data.';
const { result } = renderHook(() =>
useCopyToClipboard({
text,
searchResults: {},
}),
);
act(() => {
result.current(mockSetIsCopied);
});
// Updated expectation: Citation marker should be removed
expect(mockCopy).toHaveBeenCalledWith('Text with citation but no data.', {
format: 'text/plain',
});
});
it('should handle invalid citation indices', () => {
const mockSearchResults: { [key: string]: SearchResultData } = {
'0': {
organic: [
{
link: 'https://example.com/search1',
title: 'Search Result 1',
},
],
},
};
const text = 'Valid citation \\ue202turn0search0 and invalid \\ue202turn0search5.';
const { result } = renderHook(() =>
useCopyToClipboard({
text,
searchResults: mockSearchResults,
}),
);
act(() => {
result.current(mockSetIsCopied);
});
// Updated expectation: Invalid citation marker should be removed
const expectedText = `Valid citation [1] and invalid.
Citations:
[1] https://example.com/search1
`;
expect(mockCopy).toHaveBeenCalledWith(expectedText, { format: 'text/plain' });
});
it('should handle citations without links', () => {
const mockSearchResults: { [key: string]: SearchResultData } = {
'0': {
organic: [
{
title: 'No link source',
// No link property
} as ProcessedOrganic,
],
},
};
const text = 'Citation without link \\ue202turn0search0.';
const { result } = renderHook(() =>
useCopyToClipboard({
text,
searchResults: mockSearchResults,
}),
);
act(() => {
result.current(mockSetIsCopied);
});
// Updated expectation: Citation marker without link should be removed
expect(mockCopy).toHaveBeenCalledWith('Citation without link.', {
format: 'text/plain',
});
});
it('should clean up orphaned citation lists at the end', () => {
const mockSearchResults: { [key: string]: SearchResultData } = {
'0': {
organic: [
{ link: 'https://example.com/1', title: 'Source 1' },
{ link: 'https://example.com/2', title: 'Source 2' },
],
},
};
const text = 'Text with citations \\ue202turn0search0.\n\n[1][2]';
const { result } = renderHook(() =>
useCopyToClipboard({
text,
searchResults: mockSearchResults,
}),
);
act(() => {
result.current(mockSetIsCopied);
});
const expectedText = `Text with citations [1].
Citations:
[1] https://example.com/1
`;
expect(mockCopy).toHaveBeenCalledWith(expectedText, { format: 'text/plain' });
});
});
describe('All citation types', () => {
const mockSearchResults: { [key: string]: SearchResultData } = {
'0': {
organic: [{ link: 'https://example.com/search', title: 'Search' }],
topStories: [{ link: 'https://example.com/news', title: 'News' }],
images: [{ link: 'https://example.com/image', title: 'Image' }],
videos: [{ link: 'https://example.com/video', title: 'Video' }],
references: [{ link: 'https://example.com/ref', title: 'Reference', type: 'link' }],
},
};
it('should handle all citation types correctly', () => {
const text =
'Search \\ue202turn0search0, news \\ue202turn0news0, image \\ue202turn0image0, video \\ue202turn0video0, ref \\ue202turn0ref0.';
const { result } = renderHook(() =>
useCopyToClipboard({
text,
searchResults: mockSearchResults,
}),
);
act(() => {
result.current(mockSetIsCopied);
});
const expectedText = `Search [1], news [2], image [3], video [4], ref [5].
Citations:
[1] https://example.com/search
[2] https://example.com/news
[3] https://example.com/image
[4] https://example.com/video
[5] https://example.com/ref
`;
expect(mockCopy).toHaveBeenCalledWith(expectedText, { format: 'text/plain' });
});
});
describe('Complex scenarios', () => {
it('should handle mixed highlighted text and composite citations', () => {
const mockSearchResults: { [key: string]: SearchResultData } = {
'0': {
organic: [
{ link: 'https://example.com/1', title: 'Source 1' },
{ link: 'https://example.com/2', title: 'Source 2' },
],
topStories: [{ link: 'https://example.com/3', title: 'News 1' }],
},
};
const text =
'\\ue203Highlighted text with citation\\ue204 \\ue202turn0search0 and composite \\ue200\\ue202turn0search1\\ue202turn0news0\\ue201.';
const { result } = renderHook(() =>
useCopyToClipboard({
text,
searchResults: mockSearchResults,
}),
);
act(() => {
result.current(mockSetIsCopied);
});
const expectedText = `**Highlighted text with citation** [1] and composite [2][3].
Citations:
[1] https://example.com/1
[2] https://example.com/2
[3] https://example.com/3
`;
expect(mockCopy).toHaveBeenCalledWith(expectedText, { format: 'text/plain' });
});
});
});
|