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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899import type { INestApplication } from '@nestjs/common';import { Test } from '@nestjs/testing';import fs from 'node:fs';import os from 'node:os';import path from 'node:path';import { AppModule } from '../src/app.module';import { configureApp } from '../src/setup';import { DOCUMENT_CONVERTER, EXERCISE_GENERATOR, OCR_SERVICE, PDF_TEXT_EXTRACTOR,} from '../src/domain/ports';import type { GeneratedExercise } from '../src/domain/models';
// Test doubles for external services: no real network calls.export const fakeGenerator = { generated: [] as GeneratedExercise[], async generate() { return this.generated; },};export const fakeOcr = { text: 'texte OCR simulé assez long pour dépasser le seuil de cent caractères imposé par le service documents.', async ocrPdf() { return { text: this.text }; },};const fakePdfText = { result: { text: 'x'.repeat(200), pageCount: 3 }, async extract() { return this.result; },};export const fakeConverter = { failWith: null as string | null, async supports() { return true; }, async convertToPdf(_input: string, outPath: string) { if (this.failWith) throw new Error(this.failWith); fs.writeFileSync(outPath, '%PDF-1.4 fake converted preview'); }, async extractText() { if (this.failWith) throw new Error(this.failWith); return 'converted text '.repeat(20); },};
export interface TestContext { app: INestApplication; dbPath: string; uploadsDir: string;}
// Starts an isolated Nest instance on a disposable temporary SQLite database.//// `auth` opts the instance into the auth gate; by default it stays disabled so// the pre-auth integration suites keep hitting endpoints without a session.// Auth-specific tests pass { auth: true } and drive login themselves.export async function createTestApp( opts: { auth?: boolean; throttle?: boolean } = {},): Promise<TestContext> { const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'revision-test-')); const dbPath = path.join(dir, 'test.db'); process.env.DB_PATH = dbPath; // The CAS writes next to the db, never into the repo's working tree. const uploadsDir = path.join(dir, 'uploads'); process.env.UPLOADS_DIR = uploadsDir; process.env.NODE_ENV = 'test'; // The throttler skips everything while this is 'false' (see the module's // skipIf); tests hit login and register far more often than a real user, so // it stays off unless a test flips it on for its own requests. process.env.THROTTLE_ENABLED = opts.throttle ? 'true' : 'false';
process.env.AUTH_DISABLED = opts.auth ? 'false' : 'true';
const moduleRef = await Test.createTestingModule({ imports: [AppModule] }) .overrideProvider(EXERCISE_GENERATOR) .useValue(fakeGenerator) .overrideProvider(OCR_SERVICE) .useValue(fakeOcr) .overrideProvider(PDF_TEXT_EXTRACTOR) .useValue(fakePdfText) .overrideProvider(DOCUMENT_CONVERTER) .useValue(fakeConverter) .compile();
const app = moduleRef.createNestApplication(); configureApp(app, 'test-session-secret-abcdefghijklmnop'); await app.init(); return { app, dbPath, uploadsDir };}
export async function closeTestApp(ctx: TestContext): Promise<void> { await ctx.app.close(); fs.rmSync(path.dirname(ctx.dbPath), { recursive: true, force: true });}