smartpdf/test/test.ts

85 lines
3.3 KiB
TypeScript
Raw Permalink Normal View History

2023-07-26 14:17:11 +02:00
import { expect, tap } from '@push.rocks/tapbundle';
2022-03-24 14:32:49 +01:00
import * as smartpdf from '../ts/index.js';
import * as fs from 'fs';
import * as path from 'path';
2018-10-06 13:25:45 +00:00
let testSmartPdf: smartpdf.SmartPdf;
/**
* Ensures that a directory exists.
* @param dirPath - The directory path to ensure.
*/
function ensureDir(dirPath: string): void {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
}
tap.test('should create a valid instance of SmartPdf', async () => {
2018-10-06 13:25:45 +00:00
testSmartPdf = new smartpdf.SmartPdf();
2022-03-24 14:32:49 +01:00
expect(testSmartPdf).toBeInstanceOf(smartpdf.SmartPdf);
2018-10-06 17:35:26 +02:00
});
2018-10-06 13:25:45 +00:00
tap.test('should start the SmartPdf instance', async () => {
2019-05-29 14:14:02 +02:00
await testSmartPdf.start();
});
tap.test('should create PDFs from HTML string', async () => {
const pdf1 = await testSmartPdf.getA4PdfResultForHtmlString('hi');
const pdf2 = await testSmartPdf.getA4PdfResultForHtmlString('hello');
expect(pdf1.buffer).toBeInstanceOf(Buffer);
expect(pdf2.buffer).toBeInstanceOf(Buffer);
2022-01-05 17:20:28 +01:00
});
tap.test('should create PDFs from websites', async () => {
const pdfA4 = await testSmartPdf.getPdfResultForWebsite('https://www.wikipedia.org');
const pdfSingle = await testSmartPdf.getFullWebsiteAsSinglePdf('https://www.wikipedia.org');
expect(pdfA4.buffer).toBeInstanceOf(Buffer);
expect(pdfSingle.buffer).toBeInstanceOf(Buffer);
2018-10-06 13:25:45 +00:00
});
tap.test('should create valid PDF results and write them to disk', async () => {
const writePdfToDisk = async (urlArg: string, fileName: string) => {
2021-10-14 10:59:45 +02:00
const pdfResult = await testSmartPdf.getFullWebsiteAsSinglePdf(urlArg);
2022-03-24 14:32:49 +01:00
expect(pdfResult.buffer).toBeInstanceOf(Buffer);
ensureDir('.nogit');
fs.writeFileSync(path.join('.nogit', fileName), pdfResult.buffer as Buffer);
2021-10-14 10:59:45 +02:00
};
await writePdfToDisk('https://lossless.com/', '1.pdf');
await writePdfToDisk('https://layer.io', '2.pdf');
2021-10-14 10:59:45 +02:00
});
tap.test('should merge PDFs into a combined PDF', async () => {
2022-06-15 22:14:55 +02:00
const pdf1 = await testSmartPdf.readFileToPdfObject('.nogit/1.pdf');
const pdf2 = await testSmartPdf.readFileToPdfObject('.nogit/2.pdf');
const mergedBuffer = await testSmartPdf.mergePdfs([pdf1.buffer, pdf2.buffer]);
ensureDir('.nogit');
fs.writeFileSync(path.join('.nogit', 'combined.pdf'), mergedBuffer);
2022-10-26 23:04:59 +02:00
});
2019-05-29 19:49:23 +02:00
tap.test('should create PNG images from combined PDF using Puppeteer conversion', async () => {
2024-04-25 18:48:08 +02:00
const pdfObject = await testSmartPdf.readFileToPdfObject('.nogit/combined.pdf');
2024-04-27 12:07:16 +02:00
const images = await testSmartPdf.convertPDFToPngBytes(pdfObject.buffer);
expect(images.length).toBeGreaterThan(0);
console.log('Puppeteer-based conversion image sizes:', images.map(img => img.length));
});
tap.test('should store PNG results from both conversion functions in .nogit/testresults', async () => {
const testResultsDir = path.join('.nogit', 'testresults');
ensureDir(testResultsDir);
const pdfObject = await testSmartPdf.readFileToPdfObject('.nogit/combined.pdf');
// Convert using Puppeteer-based function and store images
const imagesPuppeteer = await testSmartPdf.convertPDFToPngBytes(pdfObject.buffer);
imagesPuppeteer.forEach((img, index) => {
const filePath = path.join(testResultsDir, `puppeteer_method_page_${index + 1}.png`);
fs.writeFileSync(filePath, Buffer.from(img));
});
2024-04-25 18:48:08 +02:00
});
tap.test('should close the SmartPdf instance properly', async () => {
2019-05-29 14:14:02 +02:00
await testSmartPdf.stop();
2018-10-06 17:35:26 +02:00
});
tap.start();