Something went wrong. Try again.
Synapse is a study workspace. Organize courses read and annotate the material, train with generated exercises and watch your progress forked from denishovart.com/synapse
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126import request from 'supertest';import type { TestContext } from './helpers';import { closeTestApp, createTestApp, fakeConverter, fakeOcr } from './helpers';
// The real upload depends on multer + files; here we cover the metadata /// delete / ocr endpoints with a document inserted via the SQLite driver.describe('Documents', () => { let ctx: TestContext; let http: ReturnType<typeof request>;
beforeAll(async () => { ctx = await createTestApp(); http = request(ctx.app.getHttpServer()); }); afterAll(() => closeTestApp(ctx));
// Inserts a document directly into the database to test the endpoints without upload. function insertDoc( overrides: Partial<{ original_name: string; extracted_text: string; render_path: string }> = {}, ) { const db = new (require('better-sqlite3'))(ctx.dbPath); const docId = crypto.randomUUID(); db.prepare( `INSERT INTO documents (id, user_id, original_name, filename, filepath, extracted_text, page_count, render_path) VALUES (?, NULL, ?, ?, ?, ?, ?, ?)`, ).run( docId, overrides.original_name ?? 'cours.pdf', 'file.pdf', '/tmp/does-not-exist.pdf', overrides.extracted_text ?? 'contenu extrait', 4, overrides.render_path ?? null, ); db.close(); return docId; }
it('uploads a pandoc-convertible file: converted preview + extracted text', async () => { const res = await http .post('/api/documents/upload') .attach('file', Buffer.from('# Chapter\n\nSome course content.'), 'notes.md') .expect(201); expect(res.body.original_name).toBe('notes.md'); expect(res.body.has_preview).toBe(1);
const doc = await http.get(`/api/documents/${res.body.id}`).expect(200); expect(doc.body.extracted_text).toContain('converted text'); expect(doc.body.page_count).toBe(3); // from the (faked) pdf extractor on the render
// Cleanup removes the uploaded file + render from uploads/. await http.delete(`/api/documents/${res.body.id}`).expect(204); });
it('rejects an unsupported extension with 400', async () => { await http .post('/api/documents/upload') .attach('file', Buffer.from('MZ'), 'setup.exe') .expect(400); });
it('rejects the upload with 422 when the conversion fails (pandoc missing)', async () => { fakeConverter.failWith = 'pandoc is not installed on the server'; try { const res = await http .post('/api/documents/upload') .attach('file', Buffer.from('# x'), 'notes.md') .expect(422); expect(JSON.stringify(res.body)).toContain('pandoc is not installed'); } finally { fakeConverter.failWith = null; } });
it('returns the metadata with has_preview', async () => { const id = insertDoc({ render_path: '/tmp/render.pdf' }); const res = await http.get(`/api/documents/${id}`).expect(200); expect(res.body).toMatchObject({ id, original_name: 'cours.pdf', has_preview: 1 }); expect(res.body.filepath).toBeUndefined(); // the summary does not leak the disk path // The detail exposes extracted_text for the viewer's OCR panel. expect(res.body.extracted_text).toBe('contenu extrait'); });
it('404 on nonexistent document', async () => { await http.get('/api/documents/00000000-0000-4000-8000-000000000000').expect(404); });
it('re-runs the OCR (mock) and updates the text', async () => { const id = insertDoc(); // the OCR mock returns text > threshold; nonexistent filepath -> 404 expected. await http.post(`/api/documents/${id}/ocr`).expect(404); // file missing from disk });
it('OCR returns 422 if the text is too short', async () => { const id = insertDoc(); // We create a real temporary file to pass the existence check. const fs = require('node:fs'); const os = require('node:os'); const path = require('node:path'); const tmp = path.join(os.tmpdir(), `ocr-${id}.pdf`); fs.writeFileSync(tmp, 'dummy'); const db = new (require('better-sqlite3'))(ctx.dbPath); db.prepare('UPDATE documents SET filepath = ? WHERE id = ?').run(tmp, id); db.close();
const prev = fakeOcr.text; fakeOcr.text = 'court'; await http.post(`/api/documents/${id}/ocr`).expect(422); fakeOcr.text = prev;
fakeOcr.text = 'texte suffisamment long pour dépasser le seuil de cent caractères imposé côté service documents ok ok'; const ok = await http.post(`/api/documents/${id}/ocr`).expect(200); expect(ok.body.id).toBe(id); expect(ok.body.chars).toBeGreaterThanOrEqual(100); fakeOcr.text = prev; fs.unlinkSync(tmp); });
it('deletes a document (204)', async () => { const id = insertDoc(); await http.delete(`/api/documents/${id}`).expect(204); await http.get(`/api/documents/${id}`).expect(404); });});