const { test } = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const ts = require('typescript'); function load(file, mocks) { const source = fs.readFileSync(path.join(__dirname, '..', file), 'utf8'); const code = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 } }).outputText; const result = { exports: {} }; new Function('require', 'module', 'exports', code)(name => { if (name === 'next/cache') return { revalidatePath() {} }; if (!mocks[name]) throw new Error('Missing mock: ' + name); return mocks[name]; }, result, result.exports); return result.exports; } const context = { anlageId: 'a', verbindungId: 'v', verbindungName: 'Test' }; function parameterFixture() { const calls = []; const comparisons = { ...context, version: 'hash', rows: [ { key: 'local', local: { id: 'local', parameterNr: 1 }, remote: { Nummer: 1 }, selectable: true, issue: '' }, { key: 'remote-2', local: null, remote: { Nummer: 2 }, selectable: false, issue: '', status: 'Nur ProCon' }, ] }; const elementTable = { ...context, kind: 'aggregate', version: 1, rows: [{ id: 'e1', targetNr: 1, kategorie: 1, parameterName: 2 }] }; const mocks = { './api-zuordnung': {}, './api-position-table': {}, '@/actions/anzeigen/api-anzeigewerte': {}, '@/actions/sensoren/api-analog': {}, '@/actions/sensoren/api-digital': {}, '@/actions/texte/api-texte': {}, '@/actions/steuerbuttons/api-steuerbuttons': {}, '@/actions/fehlerliste/api-fehlerliste': {}, '@/actions/parameter/api-parameter': { api_compareAllParameters: async () => ({ success: true, data: comparisons }), api_syncAllParameters: async dto => { calls.push(dto); return { success: true, data: {} }; }, }, '@/actions/parameter/api-element-parameter': { api_elementParameters: async () => elementTable, api_importElementParameters: async table => { calls.push('import'); return table; }, api_compareElementParameter: async () => ({ ...context, version: 'element-hash' }), api_syncElementParameter: async (kind, id, dto) => { calls.push({ kind, id, ...dto }); }, }, }; return { calls, comparisons, elementTable, mocks, api: () => load('src/actions/procon/api-bulk-sync.ts', mocks) }; } test('general parameter read includes remote-only IDs; writing uses existing local IDs', async () => { const f = parameterFixture(); let result = await f.api().api_bulkParameterArea(context, 'allgemein', 'toLocal'); assert.equal(result.completed, 2); assert.deepEqual(f.calls[0].ids, ['local', 'remote-2']); f.calls.length = 0; result = await f.api().api_bulkParameterArea(context, 'allgemein', 'toProcon'); assert.equal(result.completed, 1); assert.deepEqual(f.calls[0].ids, ['local']); assert.equal(result.errors.length, 1); }); test('each element parameter area uses the requested direction and imports missing local rows only when reading', async () => { for (const kind of ['aggregate', 'analog', 'digital', 'beschriftungen']) { const f = parameterFixture(); let result = await f.api().api_bulkParameterArea(context, kind, 'toLocal'); assert.equal(result.completed, 1); assert.equal(f.calls[0], 'import'); assert.equal(f.calls[1].direction, 'toLocal'); f.calls.length = 0; result = await f.api().api_bulkParameterArea(context, kind, 'toProcon'); assert.equal(result.completed, 1); assert.equal(f.calls.length, 1); assert.equal(f.calls[0].direction, 'toProcon'); } }); test('context changes and invalid directions cause no parameter writes', async () => { const f = parameterFixture(); f.comparisons.verbindungId = 'other'; const result = await f.api().api_bulkParameterArea(context, 'allgemein', 'toProcon'); assert.equal(result.completed, 0); assert.match(result.errors[0], /Verbindung/); assert.equal(f.calls.length, 0); await f.api().api_bulkParameterArea(context, 'analog', 'invalid'); assert.equal(f.calls.length, 0); }); test('parameter errors are reported with their target while successful rows remain counted', async () => { const f = parameterFixture(); f.elementTable.rows.push({ id: 'e2', targetNr: 2, kategorie: 1, parameterName: 2 }); f.mocks['@/actions/parameter/api-element-parameter'].api_syncElementParameter = async (kind, id) => { if (id === 'e2') throw new Error('Conflict'); }; const result = await f.api().api_bulkParameterArea(context, 'aggregate', 'toProcon'); assert.equal(result.completed, 1); assert.match(result.errors[0], /2.*Conflict/); }); test('mapping checks are read-only and applying mappings uses the reviewed version', async () => { const f = parameterFixture(); const applied = []; f.mocks['./api-zuordnung'] = { api_mappingPreview: async () => ({ ...context, version: 'mapping-hash', rows: [{ status: 'gespeichert', spsAntriebId: 1 }] }), api_mappingAccept: async dto => { applied.push(dto); return { success: true, count: 1, conflicts: 0 }; }, }; f.mocks['./api-position-table'] = { api_positionOverview: async () => ({ aggregates: context, displays: { rows: [] }, analog: { rows: [] }, digital: { rows: [] }, texts: { rows: [] }, buttons: { rows: [] } }) }; let result = await f.api().api_bulkMappings(context, false); assert.equal(applied.length, 0); assert.equal(result.completed, 0); assert.ok(result.unchanged >= 5); result = await f.api().api_bulkMappings(context, true); assert.equal(result.completed, 1); assert.equal(applied[0].version, 'mapping-hash'); assert.equal(f.calls.length, 0); await f.api().api_bulkMappings({ ...context, verbindungId: 'other' }, true); assert.equal(applied.length, 1); }); function layoutFixture() { const calls = []; const p = { id: 'main', x: 0, y: 0, width: 100, height: 40, z: 0, show: true }; const remote = { Id: 1, PositionX: 100, PositionY: 0, Width: 100, Height: 40, PositionZ: 0, Show: true }; const group = { ...context, version: 'group', masterId: 'master', mainId: 'master', elements: [{ id: 'master', local: p, procon: { ...p, x: 100 } }] }; const overview = { ...context, warnings: [], aggregates: [{ id: 1, spsAntriebId: 1, data: group }], displays: { rows: [{ id: 'd', datenNr: 1, typ: 'Text', placements: [p] }] }, analog: { rows: [{ id: 'a', datenNr: 1, placements: [p] }] }, digital: { rows: [{ id: 's', datenNr: 1, placements: [p] }] }, buttons: { rows: [{ id: 'b', tasterNr: 1, typ: 'ButtonView', beschreibung: 'Button', placements: [p] }] }, texts: { rows: [{ id: 't', datenNr: 1, text: '', placements: [p] }] } }; const compared = { ...context, version: 'hash', local: { placements: [p], graphics: [], valuePlacementId: 'main', typ: 'ButtonView' }, remote, visuals: [{ ...remote, SensorVisualisierung_State: 2, SensorVisualisierung_ViewId: null }] }; const save = kind => async (id, dto) => { calls.push({ kind, ...dto }); return { success: true }; }; const mocks = { './api-overview': { api_visualizationOverview: async () => overview }, '@/actions/procon/api-group-sync': { api_groupCompare: async () => group, api_groupApply: save('aggregate') }, '@/lib/visualisierung/layout': { getMaster: aggregate => aggregate.data.elements[0] }, '@/types/sensoren/analog': { knownAnalogStates: {} }, '@/types/sensoren/digital': { knownDigitalStates: { ein: 1 } }, '@/actions/anzeigen/api-anzeigewerte': { api_compareDisplay: async () => ({ success: true, data: { ...compared, visuals: [{ ...remote, Discriminator: 'Text' }] } }), api_syncDisplay: save('display') }, '@/actions/sensoren/api-analog': { api_compareAnalog: async () => compared, api_syncAnalog: save('analog') }, '@/actions/sensoren/api-digital': { api_compareDigital: async () => ({ ...compared, local: { ...compared.local, graphics: [{ slot: 'ein', placementId: 'main' }] }, visuals: [{ ...remote, SensorVisualisierung_State: 1 }] }), api_syncDigital: save('digital') }, '@/actions/texte/api-texte': { api_compareText: async () => compared, api_syncText: save('text') }, '@/actions/steuerbuttons/api-steuerbuttons': { api_compareSteuerbutton: async () => ({ ...compared, remote: { ...remote, Typ: 2 } }), api_syncSteuerbutton: save('button') }, }; return { calls, overview, compared, mocks, api: () => load('src/actions/visualisierung/api-layout-sync.ts', mocks) }; } test('layout reading covers all six kinds including locally empty text and only transfers layouts', async () => { const f = layoutFixture(); const result = await f.api().api_syncAllVisualizationLayouts('a', 'v', 'toLocal'); assert.deepEqual(result.errors, []); assert.equal(result.completed, 6); for (const call of f.calls) { assert.equal(call.direction, 'toLocal'); if (call.kind !== 'aggregate') { assert.equal(call.fields, false); assert.equal(call.layout, true); assert.equal(call.graphics, false); } } }); test('layout writing retains default direction and skips empty text; context errors prevent writes', async () => { const f = layoutFixture(); const result = await f.api().api_syncAllVisualizationLayouts('a', 'v'); assert.equal(result.completed, 5); assert.ok(f.calls.every(call => call.direction === 'toProcon')); f.calls.length = 0; f.overview.verbindungId = 'other'; const failed = await f.api().api_syncAllVisualizationLayouts('a', 'v', 'toLocal'); assert.match(failed.errors[0], /Verbindung/); assert.equal(f.calls.length, 0); }); test('ambiguous layout targets are reported and never chosen automatically', async () => { const f = layoutFixture(); f.compared.visuals.push({ ...f.compared.visuals[0], Id: 2 }); const result = await f.api().api_syncAllVisualizationLayouts('a', 'v', 'toLocal'); assert.ok(result.errors.some(error => error.includes('mehrdeutig'))); assert.equal(f.calls.filter(call => call.kind === 'analog' || call.kind === 'button').length, 0); }); function listFixture(area) { const f = parameterFixture(), compared = [], written = []; let revision = 0, failId = '', wrongContext = false; const rows = [0, 1].map(n => ({ id: 'r' + n, datenNr: n, tasterNr: n, fehlerNr: n, beschreibung: n ? 'Beschreibung' : '', text: n ? 'Text' : '', meldung: 'Fehler', placements: [{ x: n }] })); const get = async () => ({ anlageId: 'a', version: revision, rows }); const compare = async id => { compared.push(id); if (id === failId) throw new Error('Missing mapping'); return { ...context, verbindungId: wrongContext ? 'other' : 'v', version: String(revision), local: rows.find(r => r.id === id) }; }; const sync = async (id, dto) => { assert.equal(dto.version, String(revision)); written.push({ id, dto }); revision++; }; const config = { analog: ['@/actions/sensoren/api-analog', 'api_analog', 'api_compareAnalog', 'api_syncAnalog'], digital: ['@/actions/sensoren/api-digital', 'api_digital', 'api_compareDigital', 'api_syncDigital'], buttons: ['@/actions/steuerbuttons/api-steuerbuttons', 'api_steuerbuttons', 'api_compareSteuerbutton', 'api_syncSteuerbutton'], texte: ['@/actions/texte/api-texte', 'api_texte', 'api_compareText', 'api_syncText'], fehlerliste: ['@/actions/fehlerliste/api-fehlerliste', 'api_fehlerliste', 'api_compareFehler', 'api_syncFehler'], }[area]; f.mocks[config[0]] = { [config[1]]: get, [config[2]]: compare, [config[3]]: sync }; return { ...f, rows, compared, written, fail(id) { failId = id; }, changeContext() { wrongContext = true; } }; } test('all five list kinds transfer in both directions with fresh revisions and layout/graphics disabled', async () => { for (const area of ['analog', 'digital', 'buttons', 'texte', 'fehlerliste']) for (const direction of ['toProcon', 'toLocal']) { const f = listFixture(area); const result = await f.api().api_bulkListArea(context, area, direction); assert.deepEqual(result.errors, []); const skipped = direction === 'toProcon' && ['buttons', 'texte'].includes(area) ? 1 : 0; assert.equal(result.completed, 2 - skipped); assert.equal(result.unchanged, skipped); for (const { dto } of f.written) { assert.equal(dto.direction, direction); if (area !== 'fehlerliste') { assert.equal(dto.fields, true); assert.equal(dto.layout, false); } if (['analog', 'digital'].includes(area)) assert.equal(dto.graphics, false); } assert.deepEqual(f.rows[0].placements, [{ x: 0 }]); } }); test('list preflight and connection mismatches prevent all writes in the area', async () => { const f = listFixture('digital'); f.fail('r1'); const result = await f.api().api_bulkListArea(context, 'digital', 'toProcon'); assert.equal(result.completed, 0); assert.match(result.errors[0], /Daten-Nr. 1.*Missing mapping/); assert.equal(f.written.length, 0); const other = listFixture('fehlerliste'); other.changeContext(); await other.api().api_bulkListArea(context, 'fehlerliste', 'toLocal'); assert.equal(other.written.length, 0); });