SPB Git

Repository Public

storage.test.ts 8.2 KB 164 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : MacLustr Vision8 * File             : test/storage.test.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 */4142import assert from 'node:assert/strict';43import fs from 'node:fs';44import path from 'node:path';45import test from 'node:test';46import { inSchedule, validateSchedule } from '../server/src/cameras.ts';47import { coverage } from '../server/src/catalog.ts';48import { getSetting, setSetting } from '../server/src/db.ts';49import { runRetention } from '../server/src/retention.ts';50import { deleteArchiveFile, parseStamp, relPathFor, resolveArchivePath, verifyVolume } from '../server/src/storage.ts';51import { addSegment, makeEnv } from './helpers.ts';5253test('chemins d’archive : aucun accès hors du périmètre géré', () => {54  const { cfg, cleanup } = makeEnv();55  const rel = relPathFor('cam-t', Date.UTC(2026, 10, 30, 23, 59, 58));56  assert.equal(rel, 'cam-t/2026/11/30/20261130T235958Z.mkv');57  assert.equal(parseStamp('20261130T235958Z.mkv'), Date.UTC(2026, 10, 30, 23, 59, 58));58  assert.ok(resolveArchivePath(cfg, rel).startsWith(cfg.archiveDir));59  for (const bad of ['../../etc/passwd', 'cam-t/../../../x.mkv', '/etc/passwd', 'cam-t/2026/11/30/x.mkv', 'cam-t/2026/11/30/20261130T235958Z.mkv/../../../../a', 'cam-t/2026/11/30/20261130T235958Z.mp4'])60    assert.throws(() => resolveArchivePath(cfg, bad), /invalide|périmètre/);61  // un lien symbolique placé dans l'archive ne permet pas de supprimer un fichier extérieur62  const outside = path.join(cfg.dataDir, 'important.txt');63  fs.writeFileSync(outside, 'ne pas supprimer');64  const linkRel = 'cam-t/2026/11/30/20261130T000000Z.mkv';65  fs.mkdirSync(path.dirname(path.join(cfg.archiveDir, linkRel)), { recursive: true });66  fs.symlinkSync(outside, path.join(cfg.archiveDir, linkRel));67  assert.throws(() => deleteArchiveFile(cfg, linkRel), /hors périmètre/);68  assert.ok(fs.existsSync(outside));69  cleanup();70});7172test('rétention : âge, quota, protection, et rien hors périmètre', () => {73  const { cfg, db, cleanup } = makeEnv();74  const now = Date.now();75  const sentinel = path.join(cfg.dataDir, 'sentinelle.txt');76  fs.writeFileSync(sentinel, 'x');77  const old = addSegment(cfg, db, 'cam-t', now - 40 * 86400000);78  const oldProtected = addSegment(cfg, db, 'cam-t', now - 39 * 86400000, { protected: true });79  const recent = addSegment(cfg, db, 'cam-t', now - 3600000);80  let r = runRetention(cfg, db);81  assert.equal(r.purgedSegments, 1);82  assert.ok(!fs.existsSync(path.join(cfg.archiveDir, old)));83  assert.ok(fs.existsSync(path.join(cfg.archiveDir, oldProtected)), 'un segment protégé survit à la purge');84  assert.ok(fs.existsSync(path.join(cfg.archiveDir, recent)));85  // quota : 1 Go autorisé, 3 × 0,5 Go « catalogués » → les plus anciens non protégés partent86  setSetting(db, 'retention', { days: 30, maxArchiveGB: 1 });87  db.prepare('DELETE FROM segments').run();88  const a = addSegment(cfg, db, 'cam-t', now - 3 * 3600000);89  const b = addSegment(cfg, db, 'cam-t', now - 2 * 3600000);90  const c = addSegment(cfg, db, 'cam-t', now - 1 * 3600000);91  db.prepare('UPDATE segments SET size_bytes = ?').run(0.5 * 1024 ** 3);92  r = runRetention(cfg, db);93  assert.ok(r.purgedSegments >= 1);94  assert.ok(!fs.existsSync(path.join(cfg.archiveDir, a)), 'le plus ancien est purgé en premier');95  assert.ok(fs.existsSync(path.join(cfg.archiveDir, c)), 'le plus récent est conservé');96  void b;97  assert.ok(fs.existsSync(sentinel), 'aucun fichier extérieur à l’archive n’est touché');98  cleanup();99});100101test('disque plein simulé : enregistrement suspendu plutôt que disque système saturé', () => {102  // seuil critique placé au-dessus de l'espace réellement libre → le garde-fou doit se déclencher103  const { cfg, db, cleanup } = makeEnv();104  cfg.retention.criticalFreeGB = 10_000_000;105  cfg.retention.minFreeGB = 10_000_000;106  const now = Date.now();107  const keep = addSegment(cfg, db, 'cam-t', now - 60000, { protected: true });108  addSegment(cfg, db, 'cam-t', now - 120000);109  const r = runRetention(cfg, db);110  assert.equal(r.suspended, true);111  assert.equal(getSetting(db, 'recording_suspended', false), true);112  assert.ok(fs.existsSync(path.join(cfg.archiveDir, keep)));113  const ev = db.prepare("SELECT COUNT(*) AS n FROM events WHERE type = 'recording_suspended' AND severity = 'critical'").get() as { n: number };114  assert.equal(ev.n, 1);115  cleanup();116});117118test('archive non identifiée : aucune suppression', () => {119  const { cfg, db, cleanup } = makeEnv();120  const rel = addSegment(cfg, db, 'cam-t', Date.now() - 90 * 86400000);121  fs.renameSync(path.join(cfg.archiveDir, '.vision-volume'), path.join(cfg.archiveDir, '.absent'));122  assert.equal(verifyVolume(cfg).ok, false);123  assert.equal(runRetention(cfg, db).purgedSegments, 0);124  assert.ok(fs.existsSync(path.join(cfg.archiveDir, rel)));125  fs.renameSync(path.join(cfg.archiveDir, '.absent'), path.join(cfg.archiveDir, '.vision-volume'));126  fs.writeFileSync(path.join(cfg.archiveDir, '.vision-volume'), JSON.stringify({ volumeId: 'un-autre-volume' }));127  assert.equal(verifyVolume(cfg).ok, false);128  assert.equal(runRetention(cfg, db).purgedSegments, 0);129  cleanup();130});131132test('couverture : plages continues et trous', () => {133  const { cfg, db, cleanup } = makeEnv();134  const t0 = Date.UTC(2026, 10, 30, 12, 0, 0);135  addSegment(cfg, db, 'cam-t', t0);136  addSegment(cfg, db, 'cam-t', t0 + 60000);137  addSegment(cfg, db, 'cam-t', t0 + 121000); // 1 s d'écart : toléré138  addSegment(cfg, db, 'cam-t', t0 + 600000); // trou réel139  const c = coverage(db, 'cam-t', t0 - 1000, t0 + 3600000);140  assert.equal(c.segments, 4);141  assert.equal(c.ranges.length, 2);142  assert.deepEqual(c.gaps, [{ startMs: t0 + 181000, endMs: t0 + 600000 }]);143  cleanup();144});145146test('horaires : plages hebdomadaires, minuit et changement d’heure', () => {147  const tz = 'America/Toronto';148  assert.equal(inSchedule(null, new Date(), tz), true);149  const night = validateSchedule([{ days: [1, 2, 3, 4, 5], from: '18:00', to: '07:00' }]);150  // lundi 30 nov. 2026, 20:00 à Toronto = mardi 01:00 UTC151  assert.equal(inSchedule(night, new Date(Date.UTC(2026, 11, 1, 1, 0)), tz), true);152  // mardi 06:30 à Toronto (suite de la plage du lundi soir)153  assert.equal(inSchedule(night, new Date(Date.UTC(2026, 11, 1, 11, 30)), tz), true);154  // mardi midi à Toronto155  assert.equal(inSchedule(night, new Date(Date.UTC(2026, 11, 1, 17, 0)), tz), false);156  // dimanche 20:00 : hors des jours choisis157  assert.equal(inSchedule(night, new Date(Date.UTC(2026, 11, 7, 1, 0)), tz), false);158  // heure d'été : 15 juillet 2026, 19:00 à Toronto = 23:00 UTC (mercredi)159  assert.equal(inSchedule(night, new Date(Date.UTC(2026, 6, 15, 23, 0)), tz), true);160  assert.throws(() => validateSchedule([{ days: [9], from: '18:00', to: '07:00' }]));161  assert.throws(() => validateSchedule([{ days: [1], from: '25:00', to: '07:00' }]));162  assert.throws(() => validateSchedule('rtsp://x'));163});164
Files main
Menu

Theme