pdf-visual-diff
Version:
Visual Regression Testing for PDFs in JavaScript
97 lines • 4.7 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const node_test_1 = require("node:test");
const assert = __importStar(require("node:assert/strict"));
const path_1 = require("path");
const node_os_1 = require("node:os");
const pdf2png_1 = require("./pdf2png");
const compare_images_1 = require("../compare-images");
const types_1 = require("../types");
// Tolerance for cross-platform font rendering differences
// Snapshots are generated on Linux, so use strict tolerance there
const isLinux = (0, node_os_1.platform)() === 'linux';
const crossPlatformTolerance = isLinux ? 0 : 0.05;
const testDataDir = (0, path_1.join)(__dirname, '../test-data');
const pdfs = (0, path_1.join)(testDataDir, 'pdfs');
const singlePage = (0, path_1.join)(pdfs, 'single-page.pdf');
const twoPage = (0, path_1.join)(pdfs, 'two-page.pdf');
const cmaps = (0, path_1.join)(pdfs, 'cmaps.pdf');
const expectedDir = (0, path_1.join)(testDataDir, 'pdf2png-expected');
(0, node_test_1.describe)('pdf2png()', () => {
(0, node_test_1.it)('two-page.pdf png per page with scaling', () => {
const expectedImage1Path = (0, path_1.join)(expectedDir, 'two-page_png_per_page_scaled_1.png');
const expectedImage2Path = (0, path_1.join)(expectedDir, 'two-page_png_per_page_scaled_2.png');
return (0, pdf2png_1.pdf2png)(twoPage, { dpi: types_1.Dpi.High })
.then((imgs) => {
return Promise.all([
(0, compare_images_1.compareImages)(expectedImage1Path, [imgs[0]]),
(0, compare_images_1.compareImages)(expectedImage2Path, [imgs[1]]),
]);
})
.then((results) => {
results.forEach((x) => assert.strictEqual(x.equal, true));
});
});
(0, node_test_1.it)('two-page.pdf png per page and without scaling', () => {
const expectedImage1Path = (0, path_1.join)(expectedDir, 'two-page_png_per_page_1.png');
const expectedImage2Path = (0, path_1.join)(expectedDir, 'two-page_png_per_page_2.png');
return (0, pdf2png_1.pdf2png)(twoPage, { dpi: types_1.Dpi.Low })
.then((imgs) => {
return Promise.all([
(0, compare_images_1.compareImages)(expectedImage1Path, [imgs[0]]),
(0, compare_images_1.compareImages)(expectedImage2Path, [imgs[1]]),
]);
})
.then((results) => {
results.forEach((x) => assert.strictEqual(x.equal, true));
});
});
(0, node_test_1.it)('should scale using custom DPI', () => {
const expectedImagePath = (0, path_1.join)(expectedDir, 'should_scale_using_custom_DPI.png');
return (0, pdf2png_1.pdf2png)(singlePage, { dpi: 200 })
.then((imgs) => (0, compare_images_1.compareImages)(expectedImagePath, imgs))
.then((result) => assert.strictEqual(result.equal, true));
});
(0, node_test_1.it)('pdf that requires cmaps', async () => {
const expectedImagePath = (0, path_1.join)(expectedDir, 'cmaps.png');
const imgs = await (0, pdf2png_1.pdf2png)(cmaps);
const result = await (0, compare_images_1.compareImages)(expectedImagePath, imgs, {
tolerance: crossPlatformTolerance,
});
assert.strictEqual(result.equal, true);
});
});
//# sourceMappingURL=pdf2png.test.js.map