Add metadata logging to Agheera push functions and implement logging for pushPosition

This commit is contained in:
abiandev
2026-06-01 16:31:41 +02:00
parent 12364bcb44
commit 5212bbad71
5 changed files with 183 additions and 25 deletions
+72
View File
@@ -0,0 +1,72 @@
const assert = require('node:assert/strict');
const fs = require('fs');
const os = require('os');
const path = require('path');
const test = require('node:test');
const agheeraPushClient = require('../src/services/agheeraPushClient');
let originalApiKey;
let originalPushLogPath;
let originalPushLogs;
test.before(() => {
originalApiKey = process.env.AGHEERA_API_KEY;
originalPushLogPath = process.env.AGHEERA_PUSH_LOG_PATH;
originalPushLogs = process.env.AGHEERA_PUSH_LOGS;
});
test.after(() => {
process.env.AGHEERA_API_KEY = originalApiKey;
process.env.AGHEERA_PUSH_LOG_PATH = originalPushLogPath;
process.env.AGHEERA_PUSH_LOGS = originalPushLogs;
agheeraPushClient.__resetHttpClientForTests();
});
test.afterEach(() => {
agheeraPushClient.__resetHttpClientForTests();
});
test('pushPosition escribe log dedicado sin apiKey', async () => {
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'agheera-log-'));
const logPath = path.join(tempDir, 'agheera_push.log');
process.env.AGHEERA_API_KEY = 'secret-api-key';
process.env.AGHEERA_PUSH_LOG_PATH = logPath;
delete process.env.AGHEERA_PUSH_LOGS;
agheeraPushClient.__setHttpClientForTests(async () => ({
ok: true,
status: 200,
text: async () => 'Messages received.'
}));
await agheeraPushClient.pushPosition({
latitude: '40.416775',
longitude: '-3.703790',
vehicleId: '6599LCN',
licensePlate: '6599LCN',
measurementTime: '2026-06-01T13:38:31Z',
metadata: {
source: 'test',
trip_id: 306075
}
});
const lines = fs.readFileSync(logPath, 'utf8').trim().split('\n');
assert.equal(lines.length, 1);
const entry = JSON.parse(lines[0]);
assert.equal(entry.source, 'test');
assert.equal(entry.trip_id, 306075);
assert.equal(entry.vehicleId, '6599LCN');
assert.equal(entry.licensePlate, '6599LCN');
assert.equal(entry.latitude, 40.416775);
assert.equal(entry.longitude, -3.70379);
assert.equal(entry.measurementTime, '2026-06-01T13:38:31Z');
assert.equal(entry.success, true);
assert.equal(entry.http_status, 200);
assert.equal(entry.response_body, 'Messages received.');
assert.equal(entry.error, null);
assert.equal(JSON.stringify(entry).includes('secret-api-key'), false);
});
+4 -4
View File
@@ -719,7 +719,7 @@ test('POST /api/trips/:id/status modo dual replica foto a SFTP y mantiene local'
const recorder = createSftpRecorder();
tripStatusPhotoStorage.__setSftpClientFactoryForTests(createFakeSftpClientFactory(recorder));
process.env.TRIP_STATUS_PHOTO_STORAGE_MODE = 'dual';
process.env.TRIP_STATUS_SFTP_HOST = 'localhost';
process.env.TRIP_STATUS_SFTP_HOST = '194.164.175.51';
process.env.TRIP_STATUS_SFTP_PORT = '22';
process.env.TRIP_STATUS_SFTP_USERNAME = 'ssh_fotos_estado';
process.env.TRIP_STATUS_SFTP_PASSWORD = 'test-password';
@@ -796,7 +796,7 @@ test('POST /api/trips/:id/status modo dual con fallo SFTP mantiene fallback loca
createFakeSftpClientFactory(recorder, { failPut: true })
);
process.env.TRIP_STATUS_PHOTO_STORAGE_MODE = 'dual';
process.env.TRIP_STATUS_SFTP_HOST = 'localhost';
process.env.TRIP_STATUS_SFTP_HOST = '194.164.175.51';
process.env.TRIP_STATUS_SFTP_PORT = '22';
process.env.TRIP_STATUS_SFTP_USERNAME = 'ssh_fotos_estado';
process.env.TRIP_STATUS_SFTP_PASSWORD = 'test-password';
@@ -862,7 +862,7 @@ test('POST /api/trips/:id/status payload inválido tras upload limpia remoto y l
const recorder = createSftpRecorder();
tripStatusPhotoStorage.__setSftpClientFactoryForTests(createFakeSftpClientFactory(recorder));
process.env.TRIP_STATUS_PHOTO_STORAGE_MODE = 'dual';
process.env.TRIP_STATUS_SFTP_HOST = 'localhost';
process.env.TRIP_STATUS_SFTP_HOST = '194.164.175.51';
process.env.TRIP_STATUS_SFTP_PORT = '22';
process.env.TRIP_STATUS_SFTP_USERNAME = 'ssh_fotos_estado';
process.env.TRIP_STATUS_SFTP_PASSWORD = 'test-password';
@@ -2795,7 +2795,7 @@ test('DELETE /api/trips/:id/status en modo dual no borra foto en remoto ni local
const recorder = createSftpRecorder();
tripStatusPhotoStorage.__setSftpClientFactoryForTests(createFakeSftpClientFactory(recorder));
process.env.TRIP_STATUS_PHOTO_STORAGE_MODE = 'dual';
process.env.TRIP_STATUS_SFTP_HOST = 'localhost';
process.env.TRIP_STATUS_SFTP_HOST = '194.164.175.51';
process.env.TRIP_STATUS_SFTP_PORT = '22';
process.env.TRIP_STATUS_SFTP_USERNAME = 'ssh_fotos_estado';
process.env.TRIP_STATUS_SFTP_PASSWORD = 'test-password';