Marker mit einer Kamera - Gaurds
This commit is contained in:
59
test/boardViewerHasXYZ.test.js
Normal file
59
test/boardViewerHasXYZ.test.js
Normal file
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* 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);
|
||||
});
|
||||
});
|
||||
@@ -193,3 +193,42 @@ describe('computeYAxis – Edge Cases', () => {
|
||||
expect(Math.abs(r.axisDir[2])).toBeGreaterThan(0.99);
|
||||
});
|
||||
});
|
||||
|
||||
// ── Fehlende position_mm (z.B. 1-Kamera-Marker, von 3b nicht trianguliert) ───
|
||||
|
||||
describe('computeYAxis – Marker ohne position_mm werden ignoriert statt zu crashen', () => {
|
||||
|
||||
test('position_mm fehlt bei Pos A → Marker landet in skipped, kein Crash', () => {
|
||||
const a = [{ marker_id: 5 }]; // keine position_mm
|
||||
const b = [{ marker_id: 5, position_mm: [0, 0, 0] }];
|
||||
const c = [{ marker_id: 5, position_mm: [0, 1, 0] }];
|
||||
expect(() => computeYAxis(a, b, c)).not.toThrow();
|
||||
const r = computeYAxis(a, b, c);
|
||||
expect(r.ok).toBe(false);
|
||||
expect(r.skipped.some(s => s.id === 5)).toBe(true);
|
||||
});
|
||||
|
||||
test('position_mm fehlt bei Pos B/C → ebenfalls kein Crash', () => {
|
||||
const a = [{ marker_id: 6, position_mm: [0, 0, 0] }];
|
||||
const b = [{ marker_id: 6, position_mm: null }];
|
||||
const c = [{ marker_id: 6 }];
|
||||
expect(() => computeYAxis(a, b, c)).not.toThrow();
|
||||
});
|
||||
|
||||
test('Mix aus gültigen und ungültigen Markern: gültige werden trotzdem genutzt', () => {
|
||||
const R = 100;
|
||||
const angle = (deg) => deg * Math.PI / 180;
|
||||
const mk = (id, deg) => ({
|
||||
marker_id: id,
|
||||
position_mm: [R * Math.cos(angle(deg)), R * Math.sin(angle(deg)), 50],
|
||||
});
|
||||
const a = [mk(1, 0), { marker_id: 2 /* fehlt */ }];
|
||||
const b = [mk(1, 90), { marker_id: 2, position_mm: [1, 2, 3] }];
|
||||
const c = [mk(1, 180), { marker_id: 2 }]; // bei C wieder fehlend
|
||||
const r = computeYAxis(a, b, c);
|
||||
expect(r.ok).toBe(true);
|
||||
expect(r.numMarkers).toBe(1);
|
||||
expect(r.markerData.map(m => m.markerId)).toEqual([1]);
|
||||
expect(r.skipped.some(s => s.id === 2)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user