From 9312e6196b63a82b6152a0ea6a51b081789004bb Mon Sep 17 00:00:00 2001 From: Yuki Hattori Date: Fri, 10 Jan 2025 23:56:17 +0900 Subject: [PATCH] Add basic test for pptxEditable converter option --- .circleci/config.yml | 4 +++ .github/workflows/test-win.yml | 3 +++ test/converter.ts | 48 ++++++++++++++++++++++++++++++++++ test/marp-cli.ts | 14 ++++++++++ 4 files changed, 69 insertions(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index 52244e08..b28f2d4a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -24,6 +24,10 @@ commands: type: steps default: [] steps: + - run: + name: Set up LibreOffice + command: sudo apt-get install -y libreoffice-common + - restore_cache: keys: - v3-dependencies-{{ .Environment.CIRCLE_JOB }}-{{ checksum "package-lock.json" }}-{{ .Branch }} diff --git a/.github/workflows/test-win.yml b/.github/workflows/test-win.yml index b0b53ec9..bded1a02 100644 --- a/.github/workflows/test-win.yml +++ b/.github/workflows/test-win.yml @@ -37,6 +37,9 @@ jobs: npm ci npx patch-package + - name: Set up LibreOffice + run: sudo apt-get install -y libreoffice-common + # Retry tests up to 3 times due to flaky tests on Windows CI # https://stackoverflow.com/a/59365905 - name: Jest diff --git a/test/converter.ts b/test/converter.ts index bb9067e0..ecc07597 100644 --- a/test/converter.ts +++ b/test/converter.ts @@ -1060,6 +1060,54 @@ describe('Converter', () => { timeout ) }) + + describe('with pptxEditable option', () => { + beforeEach(() => { + // Don't mock writeFile to use actually saved tmp file for conversion + writeFileSpy.mockRestore() + }) + + it( + 'converts markdown file into PDF -> PPTX through soffice', + async () => { + const cvt = converter({ pptxEditable: true }) + const editablePptxSpy = jest.spyOn( + cvt as any, + 'convertFileToEditablePPTX' + ) + const unlink = jest.spyOn(fs.promises, 'unlink') + const fileSave = jest + .spyOn(File.prototype, 'save') + .mockImplementation() + const fileTmp = jest.spyOn(File.prototype, 'saveTmpFile') + const warn = jest.spyOn(console, 'warn').mockImplementation() + + await cvt.convertFile(new File(onePath)) + expect(editablePptxSpy).toHaveBeenCalled() + expect(warn).toHaveBeenCalledWith( + expect.stringContaining( + 'Converting to editable PPTX is experimental feature' + ) + ) + expect(fileTmp).toHaveBeenCalledWith( + expect.objectContaining({ extension: '.pdf' }) + ) + expect(unlink).toHaveBeenCalledWith( + expect.stringContaining(os.tmpdir()) + ) + expect(fileSave).toHaveBeenCalled() + + const savedFile = fileSave.mock.instances[0] as unknown as File + expect(savedFile).toBeInstanceOf(File) + expect(savedFile.path).toBe('test.pptx') + + // ZIP PK header for Office Open XML + expect(savedFile.buffer?.toString('ascii', 0, 2)).toBe('PK') + expect(savedFile.buffer?.toString('hex', 2, 4)).toBe('0304') + }, + timeoutLarge + ) + }) }) describe('when convert type is PNG', () => { diff --git a/test/marp-cli.ts b/test/marp-cli.ts index 94f7ec97..a9a8e96c 100644 --- a/test/marp-cli.ts +++ b/test/marp-cli.ts @@ -1501,6 +1501,20 @@ describe('Marp CLI', () => { }) }) + describe('with --pptx-editable options', () => { + it('prefers PPTX than HTML if not specified conversion type', async () => { + const cmd = [onePath, '--pptx-editable'] + expect((await conversion(...cmd)).options.type).toBe(ConvertType.pptx) + + // This option is actually not for defining conversion type so other + // options to set conversion type are always prioritized. + const cmdPptx = [onePath, '--pptx-editable', '--pdf'] + expect((await conversion(...cmdPptx)).options.type).toBe( + ConvertType.pdf + ) + }) + }) + describe('with PUPPETEER_TIMEOUT env', () => { beforeEach(() => { process.env.PUPPETEER_TIMEOUT = '12345'