Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion packages/components/src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { removeInvalidImageMarkdown, convertRequireToImport, COMMONJS_REQUIRE_REGEX, IMPORT_EXTRACTION_REGEX } from './utils'
import {
removeInvalidImageMarkdown,
convertRequireToImport,
COMMONJS_REQUIRE_REGEX,
IMPORT_EXTRACTION_REGEX,
mapExtToInputField
} from './utils'

describe('removeInvalidImageMarkdown', () => {
describe('strips non-http/https image markdown', () => {
Expand Down Expand Up @@ -229,3 +235,35 @@ describe('Import extraction regex (utils.ts line 1596 pattern)', () => {
expect(extractModules('console.log("hello")')).toEqual([])
})
})

describe('mapExtToInputField', () => {
it('maps known lowercase extensions to their input field', () => {
expect(mapExtToInputField('.pdf')).toBe('pdfFile')
expect(mapExtToInputField('.txt')).toBe('txtFile')
expect(mapExtToInputField('.json')).toBe('jsonFile')
expect(mapExtToInputField('.csv')).toBe('csvFile')
expect(mapExtToInputField('.docx')).toBe('docxFile')
})

it('maps uppercase extensions to the same input field (case-insensitive)', () => {
expect(mapExtToInputField('.PDF')).toBe('pdfFile')
expect(mapExtToInputField('.TXT')).toBe('txtFile')
expect(mapExtToInputField('.JSON')).toBe('jsonFile')
expect(mapExtToInputField('.DOCX')).toBe('docxFile')
})

it('maps mixed-case extensions to the same input field', () => {
expect(mapExtToInputField('.Pdf')).toBe('pdfFile')
expect(mapExtToInputField('.Csv')).toBe('csvFile')
})

it('falls back to txtFile for unknown extensions', () => {
expect(mapExtToInputField('.unknown')).toBe('txtFile')
})

it('falls back to txtFile for empty or nullish extensions', () => {
expect(mapExtToInputField('')).toBe('txtFile')
expect(mapExtToInputField(undefined)).toBe('txtFile')
expect(mapExtToInputField(null as unknown as string)).toBe('txtFile')
})
})
6 changes: 3 additions & 3 deletions packages/components/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1068,11 +1068,11 @@ export const getVersion: () => Promise<{ version: string }> = async () => {

/**
* Map Ext to InputField
* @param {string} ext
* @param {string} [ext] File extension (case-insensitive), e.g. ".pdf"
* @returns {string}
*/
export const mapExtToInputField = (ext: string) => {
switch (ext) {
export const mapExtToInputField = (ext?: string) => {
switch (ext?.toLowerCase()) {
case '.txt':
return 'txtFile'
case '.pdf':
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/utils/createAttachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export const createFileAttachment = async (req: Request) => {
if (fileInputFieldFromExt !== 'txtFile') {
fileInputField = fileInputFieldFromExt
} else if (fileInputFieldFromMimeType !== 'txtFile') {
fileInputField = fileInputFieldFromExt
fileInputField = fileInputFieldFromMimeType
}

await removeSpecificFileFromUpload(file.path ?? file.key)
Expand Down