import { test } from 'node:test'; import assert from 'node:assert/strict'; // @ts-ignore Direkte Ausführung mit Node Type-Stripping. import { applyDisplayPlacements, applyDisplayValues, assignDisplayGraphic } from './bulk.ts'; import type { DisplayGraphic, DisplayRow } from '../../types/anzeigen/anzeigewerte'; function row(id: string): DisplayRow { return { id, datenNr: Number(id), beschreibung: `Anzeige ${id}`, typ: 'Xaml', statistikTyp: 'Temperatur', statistikProconId: null, zuweisungGauge: null, min: 0, max: 100, einheit: '°C', skalierung: 1, hinzugefuegtAm: '', graphics: [], placements: [ { id: 'main', x: 10, y: 20, width: 120, height: 80, z: 1, show: true }, { id: 'extra', x: 50, y: 60, width: 40, height: 30, z: 2, show: false }, ], }; } const graphic: DisplayGraphic = { slot: 'permanent', placementId: '', variant: 'active', libraryId: 'library1', name: 'Anzeige_gruen', generator: { grafikTyp: 'punkt', config: {}, options: {}, ergebnis: { name: 'punkt', width: 24, height: 24, varianten: [{ state: 'active', svg: '', xaml: '', xamlBase64: '' }] } }, }; test('applies selected display values including zero and resets while preserving other data', () => { const rows = [row('1'), row('2')]; const result = applyDisplayValues(rows, new Set(['1']), { statistikProconId: 0, zuweisungGauge: 3, min: null, skalierung: 0.1 }); assert.equal(result.changed, 1); assert.equal(result.rows[0].statistikProconId, 0); assert.equal(result.rows[0].zuweisungGauge, 3); assert.equal(result.rows[0].min, null); assert.equal(result.rows[0].skalierung, 0.1); assert.equal(result.rows[0].max, 100); assert.equal(result.rows[0].placements, rows[0].placements); assert.equal(result.rows[1], rows[1]); }); test('validates the resulting range against each selected row and rejects invalid IDs', () => { const rows = [row('1'), { ...row('2'), max: 10 }]; assert.throws(() => applyDisplayValues(rows, new Set(['1', '2']), { min: 20 }), /Anzeige 2/); assert.throws(() => applyDisplayValues(rows, new Set(['1']), { statistikProconId: -1 })); assert.throws(() => applyDisplayValues(rows, new Set(['1']), { zuweisungGauge: 1.5 })); assert.throws(() => applyDisplayValues(rows, new Set(['1']), { skalierung: Infinity })); assert.equal(rows[0].min, 0); }); test('changes only selected rows and enabled placement fields without touching metadata', () => { const rows = [row('1'), row('2')]; const result = applyDisplayPlacements(rows, new Set(['1']), { width: 200, show: false }, 'first', 'absolute'); assert.equal(result.changed, 1); assert.deepEqual(result.rows[0].placements[0], { ...rows[0].placements[0], width: 200, show: false }); assert.equal(result.rows[0].placements[1], rows[0].placements[1]); assert.equal(result.rows[1], rows[1]); assert.equal(result.rows[0].einheit, '°C'); assert.equal(rows[0].placements[0].width, 120); }); test('offsets all placements together and rejects invalid dimensions before changes are applied', () => { const rows = [row('1')]; const result = applyDisplayPlacements(rows, new Set(['1']), { x: 5, y: -10 }, 'all', 'offset'); assert.deepEqual(result.rows[0].placements.map(p => [p.x, p.y]), [[15, 10], [55, 50]]); assert.throws(() => applyDisplayPlacements(rows, new Set(['1']), { height: 0 }, 'all', 'absolute')); assert.throws(() => applyDisplayPlacements(rows, new Set(['1']), { z: 1.5 }, 'all', 'absolute')); assert.equal(rows[0].placements[0].x, 10); }); test('assigns library graphics while preserving existing placement references and other slots', () => { const rows = [row('1'), row('2'), row('3')]; rows[0].graphics = [{ ...graphic, placementId: 'extra' }, { ...graphic, slot: 'aus', placementId: 'main' }]; const result = assignDisplayGraphic(rows, new Set(['1', '2']), graphic, true); assert.equal(result.changed, 2); assert.equal(result.rows[0].graphics.find(g => g.slot === 'permanent')?.placementId, 'extra'); assert.equal(result.rows[0].graphics.find(g => g.slot === 'aus'), rows[0].graphics[1]); assert.equal(result.rows[1].graphics[0].placementId, 'main'); assert.equal(result.rows[1].graphics[0].libraryId, 'library1'); assert.equal(result.rows[0].placements, rows[0].placements); assert.equal(result.rows[2], rows[2]); assert.notEqual(result.rows[0].graphics[1].generator, result.rows[1].graphics[0].generator); }); test('skips existing slots unless replacing and creates missing placements for new graphics', () => { const rows = [row('1'), row('2')]; rows[0].graphics = [{ ...graphic, placementId: 'main' }]; rows[1].placements = []; const result = assignDisplayGraphic(rows, new Set(['1', '2']), graphic, false); assert.equal(result.skipped, 1); assert.equal(result.changed, 1); assert.equal(result.rows[0], rows[0]); assert.equal(result.rows[1].placements[0].id, result.rows[1].graphics[0].placementId); assert.equal(result.rows[1].placements[0].width, 24); });