Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr Vision8 * File : test/motion.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 test from 'node:test';44import { MotionAnalyzer } from '../server/src/motion.ts';4546const W = 160;47const H = 90;48// scène fixe avec un léger grain (comme une image infrarouge de nuit)49function scene(seed: number, base = 90): Uint8Array {50 const f = new Uint8Array(W * H);51 let s = seed;52 for (let i = 0; i < f.length; i++) {53 s = (s * 1103515245 + 12345) & 0x7fffffff;54 f[i] = base + ((i % W) >> 3) + ((s >> 16) % 5) - 2;55 }56 return f;57}58function withBlock(f: Uint8Array, x0: number, y0: number, w: number, h: number, v = 220): Uint8Array {59 const g = Uint8Array.from(f);60 for (let y = y0; y < y0 + h; y++) for (let x = x0; x < x0 + w; x++) g[y * W + x] = v;61 return g;62}63function warm(a: MotionAnalyzer, n = 24): void {64 for (let i = 0; i < n; i++) a.feed(scene(i));65}6667test('scène fixe avec grain : aucun mouvement', () => {68 const a = new MotionAnalyzer();69 warm(a);70 for (let i = 0; i < 60; i++) assert.equal(a.feed(scene(100 + i)).motion, false);71});7273test('un objet qui traverse la pièce déclenche après deux images', () => {74 const a = new MotionAnalyzer();75 warm(a);76 const r1 = a.feed(withBlock(scene(200), 60, 40, 24, 30));77 assert.equal(r1.motion, false, 'une seule image ne suffit pas');78 assert.ok(r1.cells.length >= 4);79 const r2 = a.feed(withBlock(scene(201), 66, 40, 24, 30));80 assert.equal(r2.motion, true);81});8283test('l’horodatage incrusté par la caméra (haut gauche) ne déclenche pas', () => {84 const a = new MotionAnalyzer();85 warm(a);86 for (let i = 0; i < 10; i++) assert.equal(a.feed(withBlock(scene(300 + i), 2 + (i % 3), 1, 60, 8, i % 2 ? 250 : 10)).motion, false);87});8889test('changement global (éclairage, bascule infrarouge, PTZ) : ignoré', () => {90 const a = new MotionAnalyzer();91 warm(a);92 const r = a.feed(scene(400, 170));93 assert.equal(r.global, true);94 assert.equal(r.motion, false);95 for (let i = 0; i < 30; i++) assert.equal(a.feed(scene(401 + i, 170)).motion, false, 'le fond est réappris');96});9798test('zone ignorée et sensibilité', () => {99 const a = new MotionAnalyzer();100 warm(a);101 // bloc dans les cellules (col 6-7, ligne 4-5) → indices 70, 71, 86, 87102 a.ignore = new Set([70, 71, 86, 87]);103 a.feed(withBlock(scene(500), 60, 40, 20, 20));104 assert.equal(a.feed(withBlock(scene(501), 60, 40, 20, 20)).motion, false, 'mouvement dans une zone ignorée');105 const low = new MotionAnalyzer();106 low.sensitivity = 1;107 warm(low);108 // faible contraste : invisible à la sensibilité 1, vu à la sensibilité 10109 const faint = (seed: number) => withBlock(scene(seed), 100, 50, 20, 20, 112);110 low.feed(faint(600));111 assert.equal(low.feed(faint(601)).motion, false);112 const high = new MotionAnalyzer();113 high.sensitivity = 10;114 warm(high);115 high.feed(faint(600));116 assert.equal(high.feed(faint(601)).motion, true);117});118119test('pendant un déplacement PTZ commandé, le fond est réappris sans déclenchement', () => {120 const a = new MotionAnalyzer();121 warm(a);122 const moved = (seed: number) => withBlock(scene(seed), 30, 30, 60, 40);123 for (let i = 0; i < 12; i++) a.relearn(moved(700 + i));124 for (let i = 0; i < 20; i++) assert.equal(a.feed(moved(720 + i)).motion, false, 'la nouvelle vue est devenue le fond');125});126