|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + * |
| 4 | + * A knowledge base document's `filename` is a display name. For connector |
| 5 | + * documents it deliberately disagrees with the stored bytes — the sync engine |
| 6 | + * records `Report.pdf` while storing the text the connector already extracted |
| 7 | + * under a `.txt` key — so choosing a parser from the display name re-parsed |
| 8 | + * extracted text as the source binary. In production that failed 1,379 |
| 9 | + * SharePoint PDFs with `Invalid PDF structure.` and silently double-wrapped |
| 10 | + * every spreadsheet, which "succeeded" because SheetJS accepts almost anything. |
| 11 | + */ |
| 12 | +import { describe, expect, it } from 'vitest' |
| 13 | +import { resolveStoredArtifactExtension } from '@/lib/knowledge/documents/parser-extension' |
| 14 | + |
| 15 | +const CONNECTOR_PDF_URL = |
| 16 | + '/api/files/serve/s3/kb%2F1786986883507-abc-Report.pdf.txt?context=knowledge-base' |
| 17 | +const UPLOADED_PDF_URL = |
| 18 | + '/api/files/serve/s3/kb%2F1786986883507-abc-Report.pdf?context=knowledge-base' |
| 19 | + |
| 20 | +describe('resolveStoredArtifactExtension', () => { |
| 21 | + it('reports txt for a connector document whose display name is a PDF', () => { |
| 22 | + expect(resolveStoredArtifactExtension(CONNECTOR_PDF_URL)).toBe('txt') |
| 23 | + }) |
| 24 | + |
| 25 | + it('reports txt for a connector spreadsheet, which SheetJS would otherwise re-wrap', () => { |
| 26 | + expect( |
| 27 | + resolveStoredArtifactExtension( |
| 28 | + '/api/files/serve/s3/kb%2F1-abc-Vendor_Spend.xlsx.txt?context=knowledge-base' |
| 29 | + ) |
| 30 | + ).toBe('txt') |
| 31 | + }) |
| 32 | + |
| 33 | + it('leaves an uploaded document on its real extension', () => { |
| 34 | + expect(resolveStoredArtifactExtension(UPLOADED_PDF_URL)).toBe('pdf') |
| 35 | + }) |
| 36 | + |
| 37 | + it('handles the blob and gcs storage prefixes', () => { |
| 38 | + expect(resolveStoredArtifactExtension('/api/files/serve/blob/kb%2F1-a-x.docx')).toBe('docx') |
| 39 | + expect(resolveStoredArtifactExtension('/api/files/serve/gcs/kb%2F1-a-x.csv')).toBe('csv') |
| 40 | + }) |
| 41 | + |
| 42 | + it('ignores URLs that are not served from our own storage', () => { |
| 43 | + expect(resolveStoredArtifactExtension('https://example.com/files/Report.pdf')).toBeUndefined() |
| 44 | + expect(resolveStoredArtifactExtension('data:application/pdf;base64,AAAA')).toBeUndefined() |
| 45 | + }) |
| 46 | + |
| 47 | + /** |
| 48 | + * `fitStorageKeyName` drops the extension when it cannot fit, and a key may |
| 49 | + * carry no extension at all. Returning undefined puts the caller back on the |
| 50 | + * filename/MIME path rather than guessing. |
| 51 | + */ |
| 52 | + it('returns undefined when the key carries no usable extension', () => { |
| 53 | + expect(resolveStoredArtifactExtension('/api/files/serve/s3/kb%2F1-a-Report')).toBeUndefined() |
| 54 | + expect(resolveStoredArtifactExtension('/api/files/serve/s3/kb%2F1-a-Report.')).toBeUndefined() |
| 55 | + }) |
| 56 | + |
| 57 | + /** |
| 58 | + * Only ever redirects to a parser that exists — an unknown suffix falls back |
| 59 | + * instead of routing the document at a parser that cannot handle it. |
| 60 | + */ |
| 61 | + it('returns undefined for an extension no parser claims', () => { |
| 62 | + expect( |
| 63 | + resolveStoredArtifactExtension('/api/files/serve/s3/kb%2F1-a-archive.zip') |
| 64 | + ).toBeUndefined() |
| 65 | + expect( |
| 66 | + resolveStoredArtifactExtension('/api/files/serve/s3/kb%2F1-a-Report.v2.final') |
| 67 | + ).toBeUndefined() |
| 68 | + }) |
| 69 | + |
| 70 | + it('is case-insensitive', () => { |
| 71 | + expect(resolveStoredArtifactExtension('/api/files/serve/s3/kb%2F1-a-Report.PDF')).toBe('pdf') |
| 72 | + }) |
| 73 | +}) |
0 commit comments