60 lines
2.2 KiB
JavaScript
60 lines
2.2 KiB
JavaScript
/**
|
|
* boardViewerHasXYZ.test.js
|
|
* =========================
|
|
* Unit-Test für die reine Hilfsfunktion `hasXYZ()` aus public/boardViewer.html.
|
|
*
|
|
* boardViewer.html ist kein ladbares JS-Modul (Inline-<script type="module">
|
|
* mit THREE.js/DOM/fetch-Abhängigkeiten) — ein normales require() ist daher
|
|
* nicht möglich, ohne die Datei in Module aufzuteilen (nicht Teil dieser
|
|
* Änderung). Stattdessen wird die `hasXYZ`-Funktionsdefinition per Regex aus
|
|
* der Datei extrahiert und isoliert ausgewertet — testet exakt die Guard-Logik,
|
|
* die an allen position_mm-Zugriffsstellen in boardViewer.html verwendet wird,
|
|
* ohne Three.js/DOM laden zu müssen.
|
|
*/
|
|
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const SRC = fs.readFileSync(path.join(__dirname, '../public/boardViewer.html'), 'utf8');
|
|
|
|
const match = SRC.match(/function hasXYZ\(arr\)\s*\{[^}]*\}/);
|
|
if (!match) {
|
|
throw new Error('hasXYZ() nicht in boardViewer.html gefunden — Guard wurde entfernt/umbenannt?');
|
|
}
|
|
// eslint-disable-next-line no-eval
|
|
const hasXYZ = eval(`(${match[0]})`);
|
|
|
|
describe('boardViewer.html: hasXYZ() (Guard für fehlende position_mm)', () => {
|
|
test('gültiges [x,y,z] → true', () => {
|
|
expect(hasXYZ([1, 2, 3])).toBe(true);
|
|
expect(hasXYZ([0, 0, 0])).toBe(true);
|
|
expect(hasXYZ([-1.5, 200.25, 0])).toBe(true);
|
|
});
|
|
|
|
test('undefined/null (fehlendes position_mm) → false, kein Crash', () => {
|
|
expect(() => hasXYZ(undefined)).not.toThrow();
|
|
expect(hasXYZ(undefined)).toBe(false);
|
|
expect(hasXYZ(null)).toBe(false);
|
|
});
|
|
|
|
test('zu kurzes Array → false', () => {
|
|
expect(hasXYZ([1, 2])).toBe(false);
|
|
expect(hasXYZ([])).toBe(false);
|
|
});
|
|
|
|
test('nicht-numerische/NaN-Werte → false', () => {
|
|
expect(hasXYZ([1, NaN, 3])).toBe(false);
|
|
expect(hasXYZ(['a', 'b', 'c'])).toBe(false);
|
|
expect(hasXYZ([1, Infinity, 3])).toBe(false);
|
|
});
|
|
|
|
test('kein Array (z.B. Objekt oder String) → false', () => {
|
|
expect(hasXYZ({ x: 1, y: 2, z: 3 })).toBe(false);
|
|
expect(hasXYZ('1,2,3')).toBe(false);
|
|
});
|
|
|
|
test('Array mit mehr als 3 Werten (z.B. mit Zusatzfeld) → true (erste 3 zählen)', () => {
|
|
expect(hasXYZ([1, 2, 3, 4])).toBe(true);
|
|
});
|
|
});
|