"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const attachment_upload_js_1 = require("./attachment-upload.js");
(0, vitest_1.describe)('toAttachmentUploadDescriptor', () => {
    (0, vitest_1.test)('preserves explicit names for non-File uploads', () => {
        const descriptor = (0, attachment_upload_js_1.toAttachmentUploadDescriptor)({
            data: 'data:image/webp;base64,Zm9v',
            mimeType: 'image/webp',
            name: 'capture.webp'
        });
        (0, vitest_1.expect)(descriptor).toMatchObject({
            fileName: 'capture.webp',
            mimeType: 'image/webp'
        });
    });
    (0, vitest_1.test)('falls back to mime-derived filenames for unnamed non-File uploads', () => {
        const descriptor = (0, attachment_upload_js_1.toAttachmentUploadDescriptor)({
            data: 'data:application/pdf;base64,Zm9v',
            mimeType: 'application/pdf'
        });
        (0, vitest_1.expect)(descriptor).toMatchObject({
            fileName: 'attachment.pdf',
            mimeType: 'application/pdf'
        });
    });
    (0, vitest_1.test)('normalizes browser ts mime types from filename when they are not text-like', () => {
        const descriptor = (0, attachment_upload_js_1.toAttachmentUploadDescriptor)(new File(['const answer = 42;'], 'main.ts', { type: 'video/mp2t' }));
        (0, vitest_1.expect)(descriptor).toMatchObject({
            fileName: 'main.ts',
            mimeType: 'text/plain'
        });
        (0, vitest_1.expect)(descriptor?.blob.type).toBe('text/plain');
    });
    (0, vitest_1.test)('normalizes blank browser mime types for python files', () => {
        const descriptor = (0, attachment_upload_js_1.toAttachmentUploadDescriptor)(new File(['print("hi")'], 'script.py', { type: '' }));
        (0, vitest_1.expect)(descriptor).toMatchObject({
            fileName: 'script.py',
            mimeType: 'text/plain'
        });
        (0, vitest_1.expect)(descriptor?.blob.type).toBe('text/plain');
    });
});
(0, vitest_1.describe)('buildAttachmentUploadFormData', () => {
    (0, vitest_1.test)('appends the normalized filename to form data', () => {
        const descriptor = (0, attachment_upload_js_1.toAttachmentUploadDescriptor)({
            data: 'data:image/webp;base64,Zm9v',
            mimeType: 'image/webp',
            name: 'capture.webp'
        });
        (0, vitest_1.expect)(descriptor).not.toBeNull();
        const formData = (0, attachment_upload_js_1.buildAttachmentUploadFormData)({
            descriptor: descriptor,
            prefix: 'captured-screenshot'
        });
        const uploadedFile = formData.get('file');
        (0, vitest_1.expect)(uploadedFile).toBeInstanceOf(File);
        (0, vitest_1.expect)(uploadedFile.name).toBe('capture.webp');
        (0, vitest_1.expect)(formData.get('prefix')).toBe('captured-screenshot');
    });
    (0, vitest_1.test)('appends serialized metadata when provided', () => {
        const descriptor = (0, attachment_upload_js_1.toAttachmentUploadDescriptor)({
            data: 'data:text/plain;base64,Zm9v',
            mimeType: 'text/plain',
            name: 'notes.txt'
        });
        (0, vitest_1.expect)(descriptor).not.toBeNull();
        const formData = (0, attachment_upload_js_1.buildAttachmentUploadFormData)({
            descriptor: descriptor,
            prefix: 'message-attachment',
            metadata: (0, attachment_upload_js_1.buildAttachmentUploadMetadata)({
                label: 'Error screenshot',
                insight: 'Shows a missing submit button'
            })
        });
        (0, vitest_1.expect)(formData.get('metadata')).toBe(JSON.stringify({
            label: 'Error screenshot',
            insight: 'Shows a missing submit button'
        }));
    });
});
(0, vitest_1.describe)('createUploadedAttachmentFromServer', () => {
    (0, vitest_1.test)('falls back to the original mime type when the server omits one', () => {
        const uploadedAttachment = (0, attachment_upload_js_1.createUploadedAttachmentFromServer)({
            response: {
                attachmentId: '123',
                fileName: '',
                contentUrl: '/api/v1/files/content/123',
                mimeType: null,
                purpose: 'attachment',
                scopeType: 'app',
                signedUrl: 'https://signed.example/123',
                signedUrlExpiresAt: '2026-03-06T00:00:00.000Z',
                storageKey: 'message-attachment/123'
            },
            fallbackFileName: 'spec.pdf',
            fallbackMimeType: 'application/pdf',
            label: 'Spec',
            insight: 'Reference doc'
        });
        (0, vitest_1.expect)(uploadedAttachment).toEqual({
            type: 'uploaded',
            url: '/api/v1/files/content/123',
            signedUrl: 'https://signed.example/123',
            mediaType: 'application/pdf',
            fileName: 'spec.pdf',
            label: 'Spec',
            insight: 'Reference doc',
            storageKey: 'message-attachment/123',
            signedUrlExpiresAt: '2026-03-06T00:00:00.000Z',
            scopeType: 'app',
            applicationId: undefined
        });
    });
});
(0, vitest_1.describe)('buildAttachmentUploadMetadata', () => {
    (0, vitest_1.test)('normalizes non-empty label and insight fields', () => {
        (0, vitest_1.expect)((0, attachment_upload_js_1.buildAttachmentUploadMetadata)({
            label: 'Error screenshot',
            insight: 'Shows a missing submit button'
        })).toEqual({
            label: 'Error screenshot',
            insight: 'Shows a missing submit button'
        });
    });
});
(0, vitest_1.describe)('normalizeAttachmentMimeType', () => {
    (0, vitest_1.test)('prefers text-safe mime types for text-like filenames', () => {
        (0, vitest_1.expect)((0, attachment_upload_js_1.normalizeAttachmentMimeType)({
            fileName: 'component.tsx',
            mimeType: 'video/mp2t'
        })).toBe('text/plain');
    });
    (0, vitest_1.test)('normalizes browser-reported application/x-sh for shell scripts', () => {
        (0, vitest_1.expect)((0, attachment_upload_js_1.normalizeAttachmentMimeType)({
            fileName: 'deploy.sh',
            mimeType: 'application/x-sh'
        })).toBe('text/plain');
    });
    (0, vitest_1.test)('normalizes application/x-sh with no filename', () => {
        (0, vitest_1.expect)((0, attachment_upload_js_1.normalizeAttachmentMimeType)({ mimeType: 'application/x-sh' })).toBe('text/plain');
    });
    (0, vitest_1.test)('coerces non-shell text-like mime types with no filename', () => {
        (0, vitest_1.expect)((0, attachment_upload_js_1.normalizeAttachmentMimeType)({ mimeType: 'application/javascript' })).toBe('text/plain');
    });
    (0, vitest_1.test)('does not coerce application/x-shockwave-flash', () => {
        (0, vitest_1.expect)((0, attachment_upload_js_1.normalizeAttachmentMimeType)({ fileName: 'flash.swf', mimeType: 'application/x-shockwave-flash' })).toBe('application/x-shockwave-flash');
    });
    (0, vitest_1.test)('does not coerce image/svg+xml to text/plain', () => {
        (0, vitest_1.expect)((0, attachment_upload_js_1.normalizeAttachmentMimeType)({ fileName: 'logo.svg', mimeType: 'image/svg+xml' })).toBe('image/svg+xml');
    });
    (0, vitest_1.test)('does not coerce image/svg+xml even without a filename', () => {
        (0, vitest_1.expect)((0, attachment_upload_js_1.normalizeAttachmentMimeType)({ mimeType: 'image/svg+xml' })).toBe('image/svg+xml');
    });
    (0, vitest_1.test)('does not coerce Office OpenXML documents to text/plain', () => {
        const docxMimeType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
        (0, vitest_1.expect)((0, attachment_upload_js_1.normalizeAttachmentMimeType)({ fileName: 'report.docx', mimeType: docxMimeType })).toBe(docxMimeType);
        (0, vitest_1.expect)((0, attachment_upload_js_1.normalizeAttachmentMimeType)({ mimeType: docxMimeType })).toBe(docxMimeType);
    });
    (0, vitest_1.test)('does not coerce Office OpenXML spreadsheets to text/plain', () => {
        const xlsxMimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
        (0, vitest_1.expect)((0, attachment_upload_js_1.normalizeAttachmentMimeType)({ mimeType: xlsxMimeType })).toBe(xlsxMimeType);
    });
});
(0, vitest_1.describe)('getTextAttachmentType', () => {
    (0, vitest_1.test)('returns null for office/openxml files even when mime is text-like', () => {
        (0, vitest_1.expect)((0, attachment_upload_js_1.getTextAttachmentType)({
            fileName: 'proposal.docx',
            mimeType: 'text/plain'
        })).toBeNull();
    });
    (0, vitest_1.test)('still classifies markdown as text', () => {
        (0, vitest_1.expect)((0, attachment_upload_js_1.getTextAttachmentType)({
            fileName: 'readme.md',
            mimeType: 'text/markdown'
        })).toBe('txt');
    });
});
(0, vitest_1.describe)('isOfficeXml', () => {
    (0, vitest_1.test)('returns true for office file extensions even with text mime type', () => {
        (0, vitest_1.expect)((0, attachment_upload_js_1.isOfficeXml)({
            fileName: 'requirements.docx',
            mimeType: 'text/plain'
        })).toBe(true);
    });
    (0, vitest_1.test)('returns true for office mime types without filename', () => {
        (0, vitest_1.expect)((0, attachment_upload_js_1.isOfficeXml)({
            mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
        })).toBe(true);
    });
    (0, vitest_1.test)('returns false for markdown files', () => {
        (0, vitest_1.expect)((0, attachment_upload_js_1.isOfficeXml)({
            fileName: 'readme.md',
            mimeType: 'text/markdown'
        })).toBe(false);
    });
});
//# sourceMappingURL=attachment-upload.test.js.map