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";

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");
    }
  });
});