UnitTests + APP_BASE_PATH

This commit is contained in:
chk
2026-05-15 07:20:09 +02:00
parent 08cefd7abb
commit 1fa356221e
4 changed files with 3658 additions and 6 deletions

3540
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -6,7 +6,8 @@
"type": "commonjs", "type": "commonjs",
"scripts": { "scripts": {
"start": "node server.js", "start": "node server.js",
"dev": "PORT=8443 nodemon server.js" "dev": "PORT=8443 nodemon server.js",
"test": "jest"
}, },
"dependencies": { "dependencies": {
"compression": "^1.7.4", "compression": "^1.7.4",
@@ -15,6 +16,7 @@
"ws": "^8.18.0" "ws": "^8.18.0"
}, },
"devDependencies": { "devDependencies": {
"jest": "^29.7.0",
"nodemon": "^3.1.7" "nodemon": "^3.1.7"
} }
} }

View File

@@ -126,6 +126,7 @@ function snapshot(outDir, cam0, cam1, ws){
console.log('Taking snapshot from cam0 async'); console.log('Taking snapshot from cam0 async');
(async () => { (async () => {
try { try {
const BASE_PATH = process.env.APP_BASE_PATH || '/usr/src/app';
console.log('Taking snapshot from cam1 a…'); console.log('Taking snapshot from cam1 a…');
await cam0.snapshotHighRes(path.join(outDir, name0)); await cam0.snapshotHighRes(path.join(outDir, name0));
await cam1.snapshotHighRes(path.join(outDir, name1)); await cam1.snapshotHighRes(path.join(outDir, name1));
@@ -134,8 +135,16 @@ function snapshot(outDir, cam0, cam1, ws){
strFile0 = path.join(outDir, name0); strFile0 = path.join(outDir, name0);
strFile1 = path.join(outDir, name1); strFile1 = path.join(outDir, name1);
//const command = `python3 /usr/src/app/programs/readTwoImages.py -i ${strFile0} -i ${strFile1} -npz /usr/src/app/data/settings/callibration_cam0.npz -npz /usr/src/app/data/settings/callibration_cam1.npz -settings /usr/src/app/data/settings/settings.json`; //const command = `python3 /usr/src/app/programs/readTwoImages.py -i ${strFile0} -i ${strFile1} -npz /usr/src/app/data/settings/callibration_cam0.npz -npz /usr/src/app/data/settings/callibration_cam1.npz -settings /usr/src/app/data/settings/settings1m.json`;
const command = `python3 /usr/src/app/programs/readTwoImages.py -i ${strFile0} -i ${strFile1} -npz /usr/src/app/data/settings/callibration_cam0.npz -npz /usr/src/app/data/settings/callibration_cam1.npz -settings /usr/src/app/data/settings/settings1m.json`;
const command = `python3 ${path.join(BASE_PATH, 'programs/readTwoImages.py')} \
-i ${strFile0} \
-i ${strFile1} \
-npz ${path.join(BASE_PATH, 'data/settings/callibration_cam0.npz')} \
-npz ${path.join(BASE_PATH, 'data/settings/callibration_cam1.npz')} \
-settings ${path.join(BASE_PATH, 'data/settings/settings1m.json')}`;
console.log("Executing Python " + command); console.log("Executing Python " + command);
processPython(command, name0, name1, outDir, ws); processPython(command, name0, name1, outDir, ws);
} catch (err) { } catch (err) {

View File

@@ -0,0 +1,107 @@
const fs = require('fs');
const path = require('path');
jest.mock('child_process', () => ({
exec: jest.fn(),
}));
jest.mock('../programs/log', () => ({
logSnapshot: jest.fn(),
}));
const { exec } = require('child_process');
const { snapshot } = require('../programs/screenShot');
class DummyCamera {
async snapshotHighRes() {
return Promise.resolve();
}
}
const tempDir = path.join(__dirname, 'data', 'temp');
const fixedNow = 1778819665744;
beforeEach(() => {
jest.clearAllMocks();
if (typeof Date.now.mockRestore === 'function') {
Date.now.mockRestore();
}
jest.spyOn(Date, 'now').mockReturnValue(fixedNow);
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir, { recursive: true });
}
// Quellpfade
const sourceDir = path.join(__dirname, 'data', 'screenShots');
const filesToCopy = [
'snapshot_video0_1778819665744.jpg',
'snapshot_video1_1778819665744.jpg',
];
// Dateien kopieren
for (const file of filesToCopy) {
const src = path.join(sourceDir, file);
const dest = path.join(tempDir, file);
if (fs.existsSync(src)) {
fs.copyFileSync(src, dest);
} else {
throw new Error(`Testdatei fehlt: ${src}`);
}
}
});
afterEach(() => {
if (fs.existsSync(tempDir)) {
fs.rmSync(tempDir, { recursive: true, force: true });
}
if (typeof Date.now.mockRestore === 'function') {
Date.now.mockRestore();
}
});
test('snapshot calls both camera snapshotHighRes methods and sends a snapshot response', async () => {
const cam0 = new DummyCamera();
const cam1 = new DummyCamera();
cam0.snapshotHighRes = jest.fn(() => Promise.resolve());
cam1.snapshotHighRes = jest.fn(() => Promise.resolve());
const ws = { send: jest.fn() };
const expectedName0 = `snapshot_video0_${fixedNow}.jpg`;
const expectedName1 = `snapshot_video1_${fixedNow}.jpg`;
const expectedAnnotated = path.join(tempDir, expectedName0.replace('.jpg', '_two_cam_annotated.jpg'));
const expectedOverlay = path.join(tempDir, expectedName0.replace('.jpg', '_two_cam_overlay.png'));
const expectedCsv = path.join(tempDir, expectedName0.replace('.jpg', '_two_cam.csv'));
exec.mockImplementation((command, callback) => {
fs.writeFileSync(expectedAnnotated, '');
fs.writeFileSync(expectedOverlay, '');
fs.writeFileSync(expectedCsv, '');
callback(null, 'ok', '');
});
const sentPromise = new Promise((resolve) => {
ws.send.mockImplementation((message) => resolve(message));
});
snapshot(tempDir, cam0, cam1, ws);
const rawMessage = await sentPromise;
const response = JSON.parse(rawMessage);
expect(cam0.snapshotHighRes).toHaveBeenCalledWith(path.join(tempDir, expectedName0));
expect(cam1.snapshotHighRes).toHaveBeenCalledWith(path.join(tempDir, expectedName1));
expect(exec).toHaveBeenCalled();
expect(response).toEqual({
type: 'snapshot',
ok: true,
url: `/snapshots/${expectedName0}`,
urlApp: `/snapshots/${expectedName0.replace('.jpg', '_two_cam_annotated.jpg')}`,
overlay: `/snapshots/${expectedName0.replace('.jpg', '_two_cam_overlay.png')}`,
overlayCSV: `/snapshots/${expectedName0.replace('.jpg', '_two_cam.csv')}`,
});
});