import { describe, expect, it } from "vitest";
import { extractTextFromPdfBuffer } from "@/lib/pdf/extractText";

function buildSimpleTextPdfBuffer() {
  const pdf = `%PDF-1.4
1 0 obj
<< /Type /Catalog /Pages 2 0 R >>
endobj
2 0 obj
<< /Type /Pages /Kids [3 0 R] /Count 1 >>
endobj
3 0 obj
<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>
endobj
4 0 obj
<< /Length 65 >>
stream
BT
/F1 24 Tf
100 700 Td
(Hello PDF Text Extraction) Tj
ET
endstream
endobj
5 0 obj
<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>
endobj
xref
0 6
0000000000 65535 f 
0000000010 00000 n 
0000000060 00000 n 
0000000117 00000 n 
0000000243 00000 n 
0000000359 00000 n 
trailer
<< /Root 1 0 R /Size 6 >>
startxref
429
%%EOF`;

  return Buffer.from(pdf, "utf8");
}

describe("extractTextFromPdfBuffer", () => {
  it("extracts text and page count from a text-based PDF", async () => {
    const result = await extractTextFromPdfBuffer(buildSimpleTextPdfBuffer());

    expect(result.numPages).toBe(1);
    expect(result.text).toContain("Hello PDF Text Extraction");
  });
});
