SPB Git

Repository Public

global-setup.ts 3.2 KB 73 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : UQO Éval — Le Grand Défi Immobilier8 * File             : tests/global-setup.ts9 * Module           : tests10 * Repository       : https://git.spboucher.ai/uqo-eval-jeux11 * Project Website  : https://jeux.uqo-eval.app12 * Created          : 2026-11-2513 * Last Modified    : 2026-11-2514 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243/**44 * Préparation unique de la base de test : schéma remis à neuf, migrations,45 * puis synchronisation de l'extrait réel (data/dev/listings.db, lecture seule).46 */47import fs from "node:fs";48import { TEST_ENV } from "../vitest.config.ts";4950export default async function setup(): Promise<() => Promise<void>> {51  for (const [key, value] of Object.entries(TEST_ENV)) process.env[key] = value;52  const url = process.env.DATABASE_URL ?? "";53  // Garde-fou : on ne touche qu'à la base réservée aux tests.54  if (!/\/uqo_jeux_test$/.test(url)) throw new Error(`Base de test attendue (uqo_jeux_test), reçu : ${url}`);55  fs.mkdirSync(TEST_ENV.IMAGE_STORAGE_PATH, { recursive: true });5657  const { migrate, sql } = await import("../apps/api/src/db.ts");58  try {59    const [row] = await sql<{ current: string }[]>`select current_database() as current`;60    if (row?.current !== "uqo_jeux_test") throw new Error(`Connexion inattendue à la base ${row?.current}`);61    await sql.unsafe("drop schema public cascade; create schema public;");62    await migrate();63    const { runSync } = await import("../apps/api/src/sync.ts");64    const stats = await runSync();65    if (stats.eligible < 1000) throw new Error(`Synchronisation de l'extrait insuffisante : ${stats.eligible} propriétés admissibles`);66    fs.writeFileSync(`${TEST_ENV.IMAGE_STORAGE_PATH}/sync-stats.json`, JSON.stringify(stats));67  } finally {68    // Toujours fermer la connexion, même en cas d'échec : sinon vitest ne se termine pas.69    await sql.end({ timeout: 5 });70  }71  return async () => {};72}73
Files main
Menu

Theme