Skip to content
Snippets Groups Projects
index.test.ts 2.24 KiB
import fs from "fs";
import path from "path";
import { describe, it, expect } from "@jest/globals";
import PDFparser from "../src/pdfParser";
import { AppError } from "../src/lib/errors";
import { saveAs } from "file-saver";

describe("PDF parser", () => {
  it("should return pdf document metadata including signatures", async () => {
    const file = fs.readFileSync(
      path.resolve(__dirname, "./abacus-two-signatures.pdf")
    );

    const parser = new PDFparser(file);
    const actual = await parser.getPDFMeta();

    expect(actual.pages).toEqual(2);
  });

  it("should return pdf document metadata without signatures", async () => {
    const file = fs.readFileSync(path.resolve(__dirname, "./example.pdf"));

    const parser = new PDFparser(file);

    const actual = await parser.getPDFMeta();
    expect(actual.pages).toEqual(1);
    expect(actual.title).toEqual("PDF Digital Signatures");
    expect(actual.author).toEqual("Tecxoft");
  });

  it("should throw error without file", async () => {
    try {
      const parser = new PDFparser(null);
      const actual = await parser.getPDFMeta();
    } catch (error) {
      expect(error).toBeInstanceOf(AppError);
    }
  });

  it("should throw error if file type is different then pdf", async () => {
    const file = fs.readFileSync(path.resolve(__dirname, "./test.txt"));

    try {
      const parser = new PDFparser(file);
      const actual = await parser.getPDFMeta();
    } catch (error) {
      expect(error).toBeInstanceOf(AppError);
      expect(error.message).toEqual("Only pdf file type is supported");
    }
  });
});

type SealCoords = {
  [key: string]: { x: string; y: string };
};

describe("PDF insert", () => {
  it.only("should insert qrcode into the pdf without breaking the signature", async () => {
    const file = fs.readFileSync(
      path.resolve(__dirname, "./abacus-two-signatures.pdf")
    );

    const qrcode = fs.readFileSync(path.resolve(__dirname, "./qrcode.png"));

    const parser = new PDFparser(file);

    const sealCoords: SealCoords = { 1: { x: "261.6", y: "384.84" } };

    const resultPDF = await parser.insertQrCode(
      qrcode.buffer,
      "vereign.com",
      sealCoords,
      0.25
    );

    fs.writeFileSync(`${__dirname}/test.pdf`, Buffer.from(resultPDF));
  });
});