Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr Vision8 * File : test/helpers.ts9 * Module : test10 * Repository : https://git.spboucher.ai/maclustr-vision11 * Project Website : https://camera.spboucher.ai12 * Created : 2026-11-3013 * Last Modified : 2026-11-3014 *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: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Environnement de test jetable : configuration, secrets et stockage dans un dossier temporaire.43import fs from 'node:fs';44import os from 'node:os';45import path from 'node:path';46import { loadConfig, ensureDirs, ROOT_DIR, type Config } from '../server/src/config.ts';47import { openAndMigrate, type DB } from '../server/src/db.ts';48import { initVolume } from '../server/src/storage.ts';4950export function makeEnv(overrides: Record<string, unknown> = {}): { cfg: Config; db: DB; dir: string; cleanup: () => void } {51 const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'mlv-test-'));52 const base = JSON.parse(fs.readFileSync(path.join(ROOT_DIR, 'config', 'vision.json'), 'utf8'));53 const raw = { ...base, dataDir: path.join(dir, 'data'), configDir: path.join(dir, 'conf'), binDir: path.join(dir, 'bin'), ...overrides };54 fs.mkdirSync(raw.configDir, { recursive: true, mode: 0o700 });55 const cfgFile = path.join(dir, 'vision.json');56 fs.writeFileSync(cfgFile, JSON.stringify(raw));57 fs.writeFileSync(path.join(raw.configDir, 'server-secret'), 'x'.repeat(64) + '\n', { mode: 0o600 });58 fs.writeFileSync(59 path.join(raw.configDir, 'cameras.json'),60 JSON.stringify({ cameras: [{ id: 'cam-t', name: 'Test', host: '192.0.2.10', mac: '00:00:5e:00:53:01', rtspPort: 554, onvifPort: 2020, username: 'u', password: 'secret-camera-pw', mainPath: '/stream1', subPath: '/stream2' }] }),61 { mode: 0o600 },62 );63 const cfg = loadConfig(cfgFile);64 ensureDirs(cfg);65 initVolume(cfg);66 const db = openAndMigrate(cfg);67 return { cfg, db, dir, cleanup: () => (db.close(), fs.rmSync(dir, { recursive: true, force: true })) };68}6970export function addSegment(cfg: Config, db: DB, cameraId: string, startMs: number, opts: { bytes?: number; durationMs?: number; protected?: boolean } = {}): string {71 const d = new Date(startMs);72 const p = (n: number) => String(n).padStart(2, '0');73 const rel = `${cameraId}/${d.getUTCFullYear()}/${p(d.getUTCMonth() + 1)}/${p(d.getUTCDate())}/${d.getUTCFullYear()}${p(d.getUTCMonth() + 1)}${p(d.getUTCDate())}T${p(d.getUTCHours())}${p(d.getUTCMinutes())}${p(d.getUTCSeconds())}Z.mkv`;74 const abs = path.join(cfg.archiveDir, rel);75 fs.mkdirSync(path.dirname(abs), { recursive: true });76 fs.writeFileSync(abs, Buffer.alloc(opts.bytes ?? 1000));77 const dur = opts.durationMs ?? 60000;78 db.prepare("INSERT INTO segments (camera_id, start_ms, end_ms, duration_ms, codec, size_bytes, rel_path, state, protected, created_ms) VALUES (?, ?, ?, ?, 'hevc', ?, ?, 'archived', ?, ?)").run(cameraId, startMs, startMs + dur, dur, opts.bytes ?? 1000, rel, opts.protected ? 1 : 0, Date.now());79 return rel;80}81