ui: Linting & Formatting scripts (#26819)
This commit is contained in:
@@ -1,57 +1,61 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { AttachmentType } from '$lib/enums';
|
||||
import {
|
||||
formatMessageForClipboard,
|
||||
parseClipboardContent,
|
||||
hasClipboardAttachments
|
||||
hasClipboardAttachments,
|
||||
parseClipboardContent
|
||||
} from '$lib/utils/clipboard';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
describe('formatMessageForClipboard', () => {
|
||||
it('returns plain content when no extras', () => {
|
||||
const result = formatMessageForClipboard('Hello world', undefined);
|
||||
|
||||
expect(result).toBe('Hello world');
|
||||
});
|
||||
|
||||
it('returns plain content when extras is empty array', () => {
|
||||
const result = formatMessageForClipboard('Hello world', []);
|
||||
|
||||
expect(result).toBe('Hello world');
|
||||
});
|
||||
|
||||
it('handles empty string content', () => {
|
||||
const result = formatMessageForClipboard('', undefined);
|
||||
|
||||
expect(result).toBe('');
|
||||
});
|
||||
|
||||
it('returns plain content when extras has only non-text attachments', () => {
|
||||
const extras = [
|
||||
{
|
||||
type: AttachmentType.IMAGE as const,
|
||||
base64Url: 'data:image/png;base64,...',
|
||||
name: 'image.png',
|
||||
base64Url: 'data:image/png;base64,...'
|
||||
type: AttachmentType.IMAGE as const
|
||||
}
|
||||
];
|
||||
const result = formatMessageForClipboard('Hello world', extras);
|
||||
|
||||
expect(result).toBe('Hello world');
|
||||
});
|
||||
|
||||
it('filters non-text attachments and keeps only text ones', () => {
|
||||
const extras = [
|
||||
{
|
||||
type: AttachmentType.IMAGE as const,
|
||||
base64Url: 'data:image/png;base64,...',
|
||||
name: 'image.png',
|
||||
base64Url: 'data:image/png;base64,...'
|
||||
type: AttachmentType.IMAGE as const
|
||||
},
|
||||
{
|
||||
type: AttachmentType.TEXT as const,
|
||||
content: 'Text content',
|
||||
name: 'file.txt',
|
||||
content: 'Text content'
|
||||
type: AttachmentType.TEXT as const
|
||||
},
|
||||
{
|
||||
type: AttachmentType.PDF as const,
|
||||
name: 'doc.pdf',
|
||||
base64Data: 'data:application/pdf;base64,...',
|
||||
content: 'PDF content',
|
||||
processedAsImages: false
|
||||
name: 'doc.pdf',
|
||||
processedAsImages: false,
|
||||
type: AttachmentType.PDF as const
|
||||
}
|
||||
];
|
||||
const result = formatMessageForClipboard('Hello', extras);
|
||||
@@ -64,14 +68,14 @@ describe('formatMessageForClipboard', () => {
|
||||
it('formats message with text attachments', () => {
|
||||
const extras = [
|
||||
{
|
||||
type: AttachmentType.TEXT as const,
|
||||
content: 'File 1 content',
|
||||
name: 'file1.txt',
|
||||
content: 'File 1 content'
|
||||
type: AttachmentType.TEXT as const
|
||||
},
|
||||
{
|
||||
type: AttachmentType.TEXT as const,
|
||||
content: 'File 2 content',
|
||||
name: 'file2.txt',
|
||||
content: 'File 2 content'
|
||||
type: AttachmentType.TEXT as const
|
||||
}
|
||||
];
|
||||
const result = formatMessageForClipboard('Hello world', extras);
|
||||
@@ -87,9 +91,9 @@ describe('formatMessageForClipboard', () => {
|
||||
const content = 'Hello "world" with\nnewline';
|
||||
const extras = [
|
||||
{
|
||||
type: AttachmentType.TEXT as const,
|
||||
content: 'Test content',
|
||||
name: 'test.txt',
|
||||
content: 'Test content'
|
||||
type: AttachmentType.TEXT as const
|
||||
}
|
||||
];
|
||||
const result = formatMessageForClipboard(content, extras);
|
||||
@@ -98,15 +102,16 @@ describe('formatMessageForClipboard', () => {
|
||||
expect(result.startsWith('"')).toBe(true);
|
||||
// The content should be properly escaped
|
||||
const parsed = JSON.parse(result.split('\n')[0]);
|
||||
|
||||
expect(parsed).toBe(content);
|
||||
});
|
||||
|
||||
it('converts legacy context type to TEXT type', () => {
|
||||
const extras = [
|
||||
{
|
||||
type: AttachmentType.LEGACY_CONTEXT as const,
|
||||
content: 'Legacy content',
|
||||
name: 'legacy.txt',
|
||||
content: 'Legacy content'
|
||||
type: AttachmentType.LEGACY_CONTEXT as const
|
||||
}
|
||||
];
|
||||
const result = formatMessageForClipboard('Hello', extras);
|
||||
@@ -118,9 +123,9 @@ describe('formatMessageForClipboard', () => {
|
||||
it('handles attachment content with special characters', () => {
|
||||
const extras = [
|
||||
{
|
||||
type: AttachmentType.TEXT as const,
|
||||
content: 'const x = "hello\\nworld";\nconst y = `template ${var}`;',
|
||||
name: 'code.js',
|
||||
content: 'const x = "hello\\nworld";\nconst y = `template ${var}`;'
|
||||
type: AttachmentType.TEXT as const
|
||||
}
|
||||
];
|
||||
const formatted = formatMessageForClipboard('Check this code', extras);
|
||||
@@ -134,9 +139,9 @@ describe('formatMessageForClipboard', () => {
|
||||
it('handles unicode characters in content and attachments', () => {
|
||||
const extras = [
|
||||
{
|
||||
type: AttachmentType.TEXT as const,
|
||||
content: '日本語テスト 🎉 émojis',
|
||||
name: 'unicode.txt',
|
||||
content: '日本語テスト 🎉 émojis'
|
||||
type: AttachmentType.TEXT as const
|
||||
}
|
||||
];
|
||||
const formatted = formatMessageForClipboard('Привет мир 👋', extras);
|
||||
@@ -149,14 +154,14 @@ describe('formatMessageForClipboard', () => {
|
||||
it('formats as plain text when asPlainText is true', () => {
|
||||
const extras = [
|
||||
{
|
||||
type: AttachmentType.TEXT as const,
|
||||
content: 'File 1 content',
|
||||
name: 'file1.txt',
|
||||
content: 'File 1 content'
|
||||
type: AttachmentType.TEXT as const
|
||||
},
|
||||
{
|
||||
type: AttachmentType.TEXT as const,
|
||||
content: 'File 2 content',
|
||||
name: 'file2.txt',
|
||||
content: 'File 2 content'
|
||||
type: AttachmentType.TEXT as const
|
||||
}
|
||||
];
|
||||
const result = formatMessageForClipboard('Hello world', extras, true);
|
||||
@@ -166,15 +171,16 @@ describe('formatMessageForClipboard', () => {
|
||||
|
||||
it('returns plain content when asPlainText is true but no attachments', () => {
|
||||
const result = formatMessageForClipboard('Hello world', [], true);
|
||||
|
||||
expect(result).toBe('Hello world');
|
||||
});
|
||||
|
||||
it('plain text mode does not use JSON format', () => {
|
||||
const extras = [
|
||||
{
|
||||
type: AttachmentType.TEXT as const,
|
||||
content: 'Test content',
|
||||
name: 'test.txt',
|
||||
content: 'Test content'
|
||||
type: AttachmentType.TEXT as const
|
||||
}
|
||||
];
|
||||
const result = formatMessageForClipboard('Hello', extras, true);
|
||||
@@ -216,7 +222,6 @@ describe('parseClipboardContent', () => {
|
||||
|
||||
it('returns original text when JSON array is malformed', () => {
|
||||
const input = '"Hello"\n[invalid json';
|
||||
|
||||
const result = parseClipboardContent(input);
|
||||
|
||||
expect(result.message).toBe('"Hello"\n[invalid json');
|
||||
@@ -229,7 +234,6 @@ describe('parseClipboardContent', () => {
|
||||
{"type":"TEXT","name":"file1.txt","content":"File 1 content"},
|
||||
{"type":"TEXT","name":"file2.txt","content":"File 2 content"}
|
||||
]`;
|
||||
|
||||
const result = parseClipboardContent(input);
|
||||
|
||||
expect(result.message).toBe('Hello world');
|
||||
@@ -245,7 +249,6 @@ describe('parseClipboardContent', () => {
|
||||
[
|
||||
{"type":"TEXT","name":"file.txt","content":"test"}
|
||||
]`;
|
||||
|
||||
const result = parseClipboardContent(input);
|
||||
|
||||
expect(result.message).toBe('Hello "world" with quotes');
|
||||
@@ -257,7 +260,6 @@ describe('parseClipboardContent', () => {
|
||||
[
|
||||
{"type":"TEXT","name":"file.txt","content":"test"}
|
||||
]`;
|
||||
|
||||
const result = parseClipboardContent(input);
|
||||
|
||||
expect(result.message).toBe('Hello\nworld');
|
||||
@@ -266,7 +268,6 @@ describe('parseClipboardContent', () => {
|
||||
|
||||
it('returns message only when no array follows', () => {
|
||||
const input = '"Just a quoted string"';
|
||||
|
||||
const result = parseClipboardContent(input);
|
||||
|
||||
expect(result.message).toBe('Just a quoted string');
|
||||
@@ -281,7 +282,6 @@ describe('parseClipboardContent', () => {
|
||||
{"name":"missing-type.txt","content":"missing"},
|
||||
{"type":"TEXT","content":"missing name"}
|
||||
]`;
|
||||
|
||||
const result = parseClipboardContent(input);
|
||||
|
||||
expect(result.message).toBe('Hello');
|
||||
@@ -291,7 +291,6 @@ describe('parseClipboardContent', () => {
|
||||
|
||||
it('handles empty attachments array', () => {
|
||||
const input = '"Hello"\n[]';
|
||||
|
||||
const result = parseClipboardContent(input);
|
||||
|
||||
expect(result.message).toBe('Hello');
|
||||
@@ -302,17 +301,16 @@ describe('parseClipboardContent', () => {
|
||||
const originalContent = 'Hello "world" with\nspecial characters';
|
||||
const originalExtras = [
|
||||
{
|
||||
type: AttachmentType.TEXT as const,
|
||||
content: 'Content with\nnewlines and "quotes"',
|
||||
name: 'file1.txt',
|
||||
content: 'Content with\nnewlines and "quotes"'
|
||||
type: AttachmentType.TEXT as const
|
||||
},
|
||||
{
|
||||
type: AttachmentType.TEXT as const,
|
||||
content: 'Another file',
|
||||
name: 'file2.txt',
|
||||
content: 'Another file'
|
||||
type: AttachmentType.TEXT as const
|
||||
}
|
||||
];
|
||||
|
||||
const formatted = formatMessageForClipboard(originalContent, originalExtras);
|
||||
const parsed = parseClipboardContent(formatted);
|
||||
|
||||
@@ -360,9 +358,9 @@ describe('roundtrip edge cases', () => {
|
||||
it('preserves empty message with attachments', () => {
|
||||
const extras = [
|
||||
{
|
||||
type: AttachmentType.TEXT as const,
|
||||
content: 'Content only',
|
||||
name: 'file.txt',
|
||||
content: 'Content only'
|
||||
type: AttachmentType.TEXT as const
|
||||
}
|
||||
];
|
||||
const formatted = formatMessageForClipboard('', extras);
|
||||
@@ -376,9 +374,9 @@ describe('roundtrip edge cases', () => {
|
||||
it('preserves attachment with empty content', () => {
|
||||
const extras = [
|
||||
{
|
||||
type: AttachmentType.TEXT as const,
|
||||
content: '',
|
||||
name: 'empty.txt',
|
||||
content: ''
|
||||
type: AttachmentType.TEXT as const
|
||||
}
|
||||
];
|
||||
const formatted = formatMessageForClipboard('Message', extras);
|
||||
@@ -393,9 +391,9 @@ describe('roundtrip edge cases', () => {
|
||||
const content = 'Path: C:\\\\Users\\\\test\\\\file.txt';
|
||||
const extras = [
|
||||
{
|
||||
type: AttachmentType.TEXT as const,
|
||||
content: 'D:\\\\Data\\\\file',
|
||||
name: 'path.txt',
|
||||
content: 'D:\\\\Data\\\\file'
|
||||
type: AttachmentType.TEXT as const
|
||||
}
|
||||
];
|
||||
const formatted = formatMessageForClipboard(content, extras);
|
||||
@@ -409,9 +407,9 @@ describe('roundtrip edge cases', () => {
|
||||
const content = 'Line1\t\tTabbed\n Spaced\r\nCRLF';
|
||||
const extras = [
|
||||
{
|
||||
type: AttachmentType.TEXT as const,
|
||||
content: '\t\t\n\n ',
|
||||
name: 'whitespace.txt',
|
||||
content: '\t\t\n\n '
|
||||
type: AttachmentType.TEXT as const
|
||||
}
|
||||
];
|
||||
const formatted = formatMessageForClipboard(content, extras);
|
||||
|
||||
Reference in New Issue
Block a user