feat(test): add comprehensive test suite and testing infrastructure
- Add vitest configuration with coverage reporting - Create test setup file with DOM mocks - Add tests for state management, helpers, and tool configurations - Update tsconfig and package.json for testing support - Clean up unused comments and improve code style
This commit is contained in:
@@ -7,7 +7,6 @@ import blobStream from 'blob-stream';
|
||||
import * as pdfjsLib from "pdfjs-dist";
|
||||
|
||||
export async function changePermissions() {
|
||||
// --- 1. GATHER INPUTS FROM THE NEW UI ---
|
||||
const currentPassword = (document.getElementById('current-password') as HTMLInputElement).value;
|
||||
const newUserPassword = (document.getElementById('new-user-password') as HTMLInputElement).value;
|
||||
const newOwnerPassword = (document.getElementById('new-owner-password') as HTMLInputElement).value;
|
||||
@@ -24,7 +23,6 @@ export async function changePermissions() {
|
||||
const file = state.files[0];
|
||||
const pdfData = await readFileAsArrayBuffer(file);
|
||||
|
||||
// --- 2. UNLOCK PDF WITH CURRENT PASSWORD ---
|
||||
let pdf;
|
||||
try {
|
||||
pdf = await pdfjsLib.getDocument({
|
||||
@@ -38,10 +36,9 @@ export async function changePermissions() {
|
||||
showAlert('Incorrect Password', 'The current password you entered is incorrect.');
|
||||
return;
|
||||
}
|
||||
throw e; // Re-throw other errors
|
||||
throw e;
|
||||
}
|
||||
|
||||
// --- 3. RASTERIZE PAGES (UNCHANGED LOGIC) ---
|
||||
const numPages = pdf.numPages;
|
||||
const pageImages = [];
|
||||
|
||||
@@ -63,7 +60,6 @@ export async function changePermissions() {
|
||||
|
||||
document.getElementById('loader-text').textContent = 'Applying new permissions...';
|
||||
|
||||
// --- 4. GATHER ALL PERMISSION CHECKBOX VALUES ---
|
||||
const allowPrinting = (document.getElementById('allow-printing') as HTMLInputElement).checked;
|
||||
const allowCopying = (document.getElementById('allow-copying') as HTMLInputElement).checked;
|
||||
const allowModifying = (document.getElementById('allow-modifying') as HTMLInputElement).checked;
|
||||
@@ -72,10 +68,10 @@ export async function changePermissions() {
|
||||
const allowContentAccessibility = (document.getElementById('allow-content-accessibility') as HTMLInputElement).checked;
|
||||
const allowDocumentAssembly = (document.getElementById('allow-document-assembly') as HTMLInputElement).checked;
|
||||
|
||||
// --- 5. CREATE NEW PDF WITH PDFKIT USING ALL NEW SETTINGS ---
|
||||
|
||||
const doc = new PDFDocument({
|
||||
size: [pageImages[0].width, pageImages[0].height],
|
||||
pdfVersion: '1.7ext3', // Use 256-bit AES encryption
|
||||
pdfVersion: '1.7ext3', // Uses 256-bit AES encryption
|
||||
|
||||
// Apply the new, separate user and owner passwords
|
||||
userPassword: newUserPassword,
|
||||
@@ -104,8 +100,6 @@ export async function changePermissions() {
|
||||
}
|
||||
|
||||
doc.end();
|
||||
|
||||
// --- 6. FINALIZE AND DOWNLOAD (UNCHANGED LOGIC) ---
|
||||
stream.on('finish', function () {
|
||||
const blob = stream.toBlob('application/pdf');
|
||||
downloadFile(blob, `permissions-changed-${file.name}`);
|
||||
|
||||
@@ -169,8 +169,6 @@ async function performMetadataCrop(pdfToModify: any, cropData: any) {
|
||||
}
|
||||
}
|
||||
|
||||
// FILE: js/logic/cropper.js
|
||||
|
||||
/**
|
||||
* Performs a destructive crop by flattening the selected area to an image.
|
||||
*/
|
||||
@@ -179,8 +177,6 @@ async function performFlatteningCrop(cropData: any) {
|
||||
|
||||
// Load the original PDF with pdf-lib to copy un-cropped pages from
|
||||
const sourcePdfDocForCopying = await PDFLibDocument.load(cropperState.originalPdfBytes);
|
||||
|
||||
// CORRECTED: Use .numPages from the pdf.js document object
|
||||
const totalPages = cropperState.pdfDoc.numPages;
|
||||
|
||||
for (let i = 0; i < totalPages; i++) {
|
||||
@@ -217,7 +213,6 @@ async function performFlatteningCrop(cropData: any) {
|
||||
const newPage = newPdfDoc.addPage([finalWidth, finalHeight]);
|
||||
newPage.drawImage(embeddedImage, { x: 0, y: 0, width: finalWidth, height: finalHeight });
|
||||
} else {
|
||||
// Correctly copy the page from the source pdf-lib document
|
||||
const [copiedPage] = await newPdfDoc.copyPages(sourcePdfDocForCopying, [i]);
|
||||
newPdfDoc.addPage(copiedPage);
|
||||
}
|
||||
@@ -225,9 +220,7 @@ async function performFlatteningCrop(cropData: any) {
|
||||
return newPdfDoc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main function to set up the Cropper tool.
|
||||
*/
|
||||
|
||||
export async function setupCropperTool() {
|
||||
if (state.files.length === 0) return;
|
||||
|
||||
@@ -237,16 +230,12 @@ export async function setupCropperTool() {
|
||||
const arrayBuffer = await readFileAsArrayBuffer(state.files[0]);
|
||||
cropperState.originalPdfBytes = arrayBuffer;
|
||||
const arrayBufferForPdfJs = (arrayBuffer as ArrayBuffer).slice(0);
|
||||
|
||||
|
||||
// pdfjsLib.GlobalWorkerOptions.workerSrc = `https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.16.105/pdf.worker.min.js`;
|
||||
pdfjsLib.GlobalWorkerOptions.workerSrc = new URL(
|
||||
'pdfjs-dist/build/pdf.worker.min.mjs',
|
||||
import.meta.url
|
||||
).toString();
|
||||
|
||||
try {
|
||||
// const loadingTask = pdfjsLib.getDocument({ data: cropperState.originalPdfBytes });
|
||||
const loadingTask = pdfjsLib.getDocument({ data: arrayBufferForPdfJs });
|
||||
|
||||
cropperState.pdfDoc = await loadingTask.promise;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// FILE: js/logic/invert-colors.js
|
||||
import { showLoader, hideLoader, showAlert } from '../ui.js';
|
||||
import { downloadFile } from '../utils/helpers.js';
|
||||
import { state } from '../state.js';
|
||||
|
||||
Reference in New Issue
Block a user