Files
openchamber/packages/ui/src/components/chat/__tests__/attachmentCitations.test.ts
T
Cursor AgentandSerhii Dziupin ef11ab5b14 feat(ui): attach large text pastes as virtual files
Offer to turn sufficiently large plain-text clipboard pastes into
pasted-context-N.txt attachments instead of inserting them into the
composer, with ask/attach/inline composer settings.

Co-authored-by: Serhii Dziupin <makeittech@users.noreply.github.com>
2026-08-04 11:38:02 +00:00

64 lines
2.6 KiB
TypeScript

import { describe, expect, test } from 'bun:test';
import {
assignImageAttachmentFilenames,
buildAttachmentCitationText,
findAttachmentCitationRanges,
isGenericImageFilename,
nextPastedContextFilename,
} from '../attachmentCitations';
describe('attachment citations', () => {
test('keeps meaningful image names', () => {
expect(assignImageAttachmentFilenames([
{ name: 'desktop_without_icons.jpg', type: 'image/jpeg' },
], [])).toEqual(['desktop_without_icons.jpg']);
});
test('renames generic clipboard image names', () => {
expect(assignImageAttachmentFilenames([
{ name: 'image.png', type: 'image/png' },
{ name: 'Screenshot.png', type: 'image/png' },
], [])).toEqual(['image-1.png', 'image-2.png']);
});
test('deduplicates meaningful names inside pending attachments', () => {
expect(assignImageAttachmentFilenames([
{ name: 'desktop.jpg', type: 'image/jpeg' },
{ name: 'desktop.jpg', type: 'image/jpeg' },
], ['desktop-2.jpg'])).toEqual(['desktop.jpg', 'desktop-3.jpg']);
});
test('continues generated image indexes from existing pending attachments', () => {
expect(assignImageAttachmentFilenames([
{ name: 'image.png', type: 'image/png' },
{ name: '', type: 'image/webp' },
], ['image-1.png'])).toEqual(['image-2.png', 'image-3.webp']);
});
test('detects generic names narrowly', () => {
expect(isGenericImageFilename('image.png')).toBe(true);
expect(isGenericImageFilename('Screenshot (1).png')).toBe(true);
expect(isGenericImageFilename('Screen Shot.png')).toBe(true);
expect(isGenericImageFilename('Screenshot 2026-05-24.png')).toBe(false);
expect(isGenericImageFilename('desktop_without_icons.jpg')).toBe(false);
});
test('builds bracket citations', () => {
expect(buildAttachmentCitationText(['desktop.jpg', 'icon.png'])).toBe('[desktop.jpg] [icon.png]');
});
test('finds active attachment citation ranges', () => {
expect(findAttachmentCitationRanges(
'desktop [desktop.jpg] link [desktop.jpg](https://example.com) missing [other.jpg]',
['desktop.jpg'],
)).toEqual([{ start: 8, end: 21 }]);
});
test('assigns sequential pasted-context filenames', () => {
expect(nextPastedContextFilename([])).toBe('pasted-context-1.txt');
expect(nextPastedContextFilename(['pasted-context-1.txt', 'notes.md'])).toBe('pasted-context-2.txt');
expect(nextPastedContextFilename(['PASTED-CONTEXT-2.TXT'])).toBe('pasted-context-1.txt');
});
});