UnitTests + APP_BASE_PATH
This commit is contained in:
@@ -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')}`,
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user