90 lines
3.9 KiB
TypeScript
90 lines
3.9 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { parseFileToText } from '../src/index'
|
|
import {
|
|
buildDocxFixture,
|
|
buildPptxFixture,
|
|
buildXlsxFixture,
|
|
writeFixture,
|
|
} from './helpers/fixtures'
|
|
|
|
describe('parseFileToText: docx', () => {
|
|
it('extracts headings, paragraphs and tables', async () => {
|
|
const path = writeFixture('report.docx', await buildDocxFixture())
|
|
const result = await parseFileToText(path)
|
|
expect(result.ok).toBe(true)
|
|
expect(result.kind).toBe('text')
|
|
expect(result.text).toContain('# Annual Report')
|
|
expect(result.text).toContain('First paragraph hello docx')
|
|
expect(result.text).toContain('Metric | Value')
|
|
expect(result.text).toContain('Revenue | 100')
|
|
})
|
|
})
|
|
|
|
describe('parseFileToText: pptx', () => {
|
|
it('extracts one section per slide in numeric order', async () => {
|
|
const path = writeFixture('deck.pptx', await buildPptxFixture())
|
|
const result = await parseFileToText(path)
|
|
expect(result.ok).toBe(true)
|
|
expect(result.text).toContain('## Slide 1\nProductIntro\nFirst slide subtitle')
|
|
expect(result.text).toContain('## Slide 2\nMarket Analysis')
|
|
// slide10 must sort after slide2 (numeric, not lexicographic)
|
|
expect(result.text!.indexOf('## Slide 10')).toBeGreaterThan(result.text!.indexOf('## Slide 2'))
|
|
expect(result.text).toContain('## Slide 10\nSummary Slide')
|
|
})
|
|
|
|
it('keeps run text verbatim: leading zeros and the spaces between runs', async () => {
|
|
const path = writeFixture('deck.pptx', await buildPptxFixture())
|
|
const result = await parseFileToText(path)
|
|
expect(result.text).toContain('Order 0042')
|
|
})
|
|
|
|
it('keeps a:br as a line break and a:fld in document order', async () => {
|
|
const path = writeFixture('deck.pptx', await buildPptxFixture())
|
|
const result = await parseFileToText(path)
|
|
// two breaks, so the run text is not concatenated and the field lands between its runs;
|
|
// the slide also carries a comment naming those tags, which must not reach the walker
|
|
expect(result.text).toContain('## Slide 3\nBefore\n\nAfter\nPage 3 of 10')
|
|
})
|
|
|
|
it('takes text from a:t only, not from whitespace inside sibling elements', async () => {
|
|
const path = writeFixture('deck.pptx', await buildPptxFixture())
|
|
const result = await parseFileToText(path)
|
|
// the a:br elements are written across lines; that layout whitespace is a value too
|
|
expect(result.text).toContain('Before')
|
|
expect(result.text).not.toMatch(/Before\n[^\S\n]/)
|
|
// and the comment the slide carries is markup, not text
|
|
expect(result.text).not.toContain('authoring note')
|
|
})
|
|
})
|
|
|
|
describe('parseFileToText: xlsx', () => {
|
|
it('extracts sheet name, shared/inline strings, numbers, booleans and empty columns', async () => {
|
|
const path = writeFixture('table.xlsx', await buildXlsxFixture())
|
|
const result = await parseFileToText(path)
|
|
expect(result.ok).toBe(true)
|
|
expect(result.text).toContain('# Grades')
|
|
expect(result.text).toContain('Name | Scores')
|
|
// C2 is missing so the boolean in D2 lands in the 4th column
|
|
expect(result.text).toContain('Alice | 95 | | TRUE')
|
|
})
|
|
|
|
it('keeps cell text verbatim: leading zeros and the spaces between rich-text runs', async () => {
|
|
const path = writeFixture('table.xlsx', await buildXlsxFixture())
|
|
const result = await parseFileToText(path)
|
|
expect(result.text).toContain('02139 | Total due')
|
|
})
|
|
|
|
it('keeps the spaces in a <v> value (cached formula string, error literal)', async () => {
|
|
const path = writeFixture('table.xlsx', await buildXlsxFixture())
|
|
const result = await parseFileToText(path)
|
|
expect(result.text).toContain('\n Alice pts \n #N/A ')
|
|
})
|
|
|
|
it('fails gracefully on a corrupt file', async () => {
|
|
const path = writeFixture('broken.xlsx', Buffer.from('not a zip'))
|
|
const result = await parseFileToText(path)
|
|
expect(result.ok).toBe(false)
|
|
expect(result.error).toBeTruthy()
|
|
})
|
|
})
|