Files
ad_toolbox/vscode-appdaemon/tests/logModel.test.js
T
2026-09-27 22:54:08 +02:00

36 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const { test } = require('node:test');
const assert = require('node:assert/strict');
const { parseLine, rawTime } = require('../out/logModel');
test('raw log levels, app names and readable IDs', () => {
const e = parseLine('27-09-26 10:12:13 WARNING my_app: Alerte : détail [event=e1 trace=t1]', '/logs/main.log', 123);
assert.equal(e.event_id, 'e1'); assert.equal(e.app, 'my_app'); assert.equal(e.level, 'WARNING'); assert.equal(e.trace_id, 't1');
});
test('timestamps use the AppDaemon timezone instead of the workstation timezone', () => {
assert.equal(rawTime('27-09-26 10:12:13', 'Europe/Berlin'), Date.parse('2026-09-27T08:12:13Z'));
assert.equal(rawTime('27-01-26 10:12:13', 'Europe/Berlin'), Date.parse('2026-01-27T09:12:13Z'));
});
test('structured events retain causal links and evaluation values', () => {
const entry = {version:1,event_id:'e2',cause_ids:['e1'],probable_cause_ids:['e1'],message:'Condition',app:'app',kind:'evaluation',timestamp:'2026-09-27T08:12:13Z',values:{'sensor.a':true}};
assert.deepEqual(parseLine(JSON.stringify(entry), '/logs/events.jsonl', 0).values, entry.values);
});
test('invalid structured records and continuations are rejected', () => {
for (const value of ['{broken}', JSON.stringify({version:1,event_id:'e',cause_ids:[null],message:'bad',app:'app',timestamp:'now'}), ' File "app.py", line 3']) assert.equal(parseLine(value,'/logs/log',0),undefined);
});
test('decodes UTF-8, Windows-1252 and mixed historical log bytes', () => {
const { decodeLogLine } = require('../out/logModel');
assert.equal(decodeLogLine(Buffer.from('Échec – déjà 🔥')), 'Échec – déjà 🔥');
assert.equal(decodeLogLine(Buffer.from([0xc9,0x63,0x68,0x65,0x63,0x20,0x96,0x20,0xe9])), 'Échec – é');
assert.equal(decodeLogLine(Buffer.concat([Buffer.from('déjà → '),Buffer.from([0xe9])])), 'déjà → é');
assert.equal(decodeLogLine(Buffer.from([0xe9]), 'utf8'), '\ufffd');
});
test('raw initialization IDs are retained without structured events', () => {
const e=parseLine('27-09-26 10:12:13 INFO app: Setting up [event=e1 trace=t1] [init=init1]','log',0);
assert.equal(e.initialization_id,'init1'); assert.equal(e.phase,'initialization');
});
test('parses readable group tags without changing the log message format', () => {
const e=parseLine('27-09-26 10:12:13 INFO app: Selected value [group=value_selector:0123456789ab]','log',0);
assert.equal(e.group_id,'0123456789ab'); assert.equal(e.group_kind,'value_selector');
assert.equal(e.initialization_id,undefined); assert.match(e.message,/\[group=value_selector:/);
});