hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { extractImportedPdfText, processImportedText } from "../note-importer"; |
| 3 | |
| 4 | describe("processImportedText (Note Import Assistant)", () => { |
| 5 | it("removes page numbers, watermarks, and noise lines", () => { |
| 6 | const raw = ` |
| 7 | Header Title |
| 8 | Page 1 of 10 |
| 9 | Sayfa 1 |
| 10 | |
| 11 | This is the main body paragraph. |
| 12 | All rights reserved |
| 13 | - 1 - |
| 14 | Another paragraph of actual content. |
| 15 | `; |
| 16 | const result = processImportedText(raw, "lecture-notes.pdf"); |
| 17 | expect(result.markdown).not.toContain("Page 1 of 10"); |
| 18 | expect(result.markdown).not.toContain("Sayfa 1"); |
| 19 | expect(result.markdown).not.toContain("All rights reserved"); |
| 20 | expect(result.markdown).not.toContain("- 1 -"); |
| 21 | expect(result.markdown).toContain("This is the main body paragraph."); |
| 22 | expect(result.markdown).toContain("Another paragraph of actual content."); |
| 23 | }); |
| 24 | |
| 25 | it("joins lines that were split mid-sentence by PDF line wrapping", () => { |
| 26 | const raw = ` |
| 27 | This is a sentence that was broken |
| 28 | across multiple lines due to PDF extraction. |
| 29 | |
| 30 | Here is a hyphen- |
| 31 | ated word test. |
| 32 | `; |
| 33 | const result = processImportedText(raw, "document.pdf"); |
| 34 | expect(result.markdown).toContain("This is a sentence that was broken across multiple lines due to PDF extraction."); |
| 35 | expect(result.markdown).toContain("Here is a hyphenated word test."); |
| 36 | }); |
| 37 | |
| 38 | it("preserves Markdown code blocks, lists, and headers", () => { |
| 39 | const raw = ` |
| 40 | # Math Lecture 1 |
| 41 | |
| 42 | - First point |
| 43 | - Second point |
| 44 | |
| 45 | \`\`\`javascript |
| 46 | const x = 42; |
| 47 | \`\`\` |
| 48 | `; |
| 49 | const result = processImportedText(raw, "notes.md"); |
| 50 | expect(result.title).toBe("Math Lecture 1"); |
| 51 | expect(result.markdown).toContain("- First point"); |
| 52 | expect(result.markdown).toContain("const x = 42;"); |
| 53 | }); |
| 54 | |
| 55 | it("flags garbled / OCR noise content with warning banner", () => { |
| 56 | const garbledRaw = `\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD Scanned document content here`; |
| 57 | const result = processImportedText(garbledRaw, "scan.pdf"); |
| 58 | expect(result.hasGarbledWarning).toBe(true); |
| 59 | expect(result.markdown).toContain("⚠️ Some content may be unreadable due to OCR/extraction issues"); |
| 60 | }); |
| 61 | |
| 62 | it("derives clean title from heading or file name", () => { |
| 63 | const result1 = processImportedText("## Physics Mechanics\n\nContent here", "physics.pdf"); |
| 64 | expect(result1.title).toBe("Physics Mechanics"); |
| 65 | |
| 66 | const result2 = processImportedText("Just plain text without heading", "my_study_notes.txt"); |
| 67 | expect(result2.title).toBe("my study notes"); |
| 68 | }); |
| 69 | |
| 70 | it("extracts text from a valid PDF through PDF.js", async () => { |
| 71 | const text = await extractImportedPdfText(makeTextPdf("Hello PDF")); |
| 72 | expect(text).toContain("Hello PDF"); |
| 73 | }); |
| 74 | }); |
| 75 | |
| 76 | function makeTextPdf(text: string): Blob { |
| 77 | const escaped = text.replace(/\\/g, "\\\\").replace(/\(/g, "\\(").replace(/\)/g, "\\)"); |
| 78 | const stream = `BT /F1 24 Tf 100 700 Td (${escaped}) Tj ET`; |
| 79 | const objects = [ |
| 80 | "<< /Type /Catalog /Pages 2 0 R >>", |
| 81 | "<< /Type /Pages /Kids [3 0 R] /Count 1 >>", |
| 82 | "<< /Type /Page /Parent 2 0 R /Resources << /Font << /F1 4 0 R >> >> /MediaBox [0 0 612 792] /Contents 5 0 R >>", |
| 83 | "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>", |
| 84 | `<< /Length ${stream.length} >>\nstream\n${stream}\nendstream`, |
| 85 | ]; |
| 86 | const offsets: number[] = []; |
| 87 | let pdf = "%PDF-1.4\n"; |
| 88 | |
| 89 | for (let index = 0; index < objects.length; index += 1) { |
| 90 | offsets.push(pdf.length); |
| 91 | pdf += `${index + 1} 0 obj\n${objects[index]}\nendobj\n`; |
| 92 | } |
| 93 | |
| 94 | const xrefOffset = pdf.length; |
| 95 | pdf += `xref\n0 ${objects.length + 1}\n0000000000 65535 f \n`; |
| 96 | pdf += offsets.map((offset) => `${String(offset).padStart(10, "0")} 00000 n \n`).join(""); |
| 97 | pdf += `trailer\n<< /Root 1 0 R /Size ${objects.length + 1} >>\nstartxref\n${xrefOffset}\n%%EOF`; |
| 98 | |
| 99 | return new Blob([pdf], { type: "application/pdf" }); |
| 100 | } |