sensorDigital, grafik Fest, drawPunkt eingefügt
This commit is contained in:
1 parent
f43053d7b3
commit
6e4bd5339f
51 files changed
+1203
-31
No files matched your search
@@ -0,0 +1,103 @@
|
||||
import { beforeEach, describe, expect, it, jest } from '@jest/globals';
|
||||
jest.mock('../../rk-anlagen/anlage/anlage.service', () => ({ AnlageService: class {} }));
|
||||
jest.mock('../../draw/draw.service', () => ({ DrawService: class {} }));
|
||||
jest.mock('node:fs/promises', () => ({ mkdir: jest.fn(async () => undefined), writeFile: jest.fn(async () => undefined) }));
|
||||
import { writeFile } from 'node:fs/promises';
|
||||
import { Types } from 'mongoose';
|
||||
import { DrawPunkt } from '../../graphics/elements/drawPunkt';
|
||||
import { GraphicLibraryService } from './graphic-library.service';
|
||||
import { prepareBinding } from '../shared/graphics';
|
||||
|
||||
describe('Feste Grafikbibliothek', () => {
|
||||
let service: GraphicLibraryService, model: any, draw: any, asset: any, collection: any;
|
||||
const id = new Types.ObjectId().toString();
|
||||
const input = { anlageId: 'a1', name: 'Punkt_gruen.xaml', variant: 'active', generator: { grafikTyp: 'punkt' as const, config: { scale: 2, raster: 4 }, options: { colorActive: '#00ff00' } } };
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
const rendered = new DrawPunkt().getObject({ scale: 2, raster: 4 }, input.generator.options);
|
||||
const variant = rendered.varianten[1];
|
||||
asset = { _id: id, anlageId: 'a1', name: 'Punkt_gruen', variant: 'active', generator: input.generator,
|
||||
width: 24, height: 24, svg: variant.svg, xaml: variant.xaml, xamlBase64: variant.xamlBase64 };
|
||||
model = { create: jest.fn(async data => ({ ...data, _id: id })), findOne: jest.fn(() => ({ lean: () => ({ exec: async () => asset }) })),
|
||||
find: jest.fn(() => ({ sort: () => ({ lean: () => ({ exec: async () => [asset] }) }) })), deleteOne: jest.fn(() => ({ exec: async () => ({ deletedCount: 1 }) })) };
|
||||
draw = { renderGraphic: jest.fn(() => rendered) };
|
||||
collection = { findOne: jest.fn<() => Promise<unknown>>().mockResolvedValue(null), updateMany: jest.fn(async () => ({ modifiedCount: 1 })) };
|
||||
model.findOneAndUpdate = jest.fn((_filter: unknown, update: any) => ({ lean: () => ({ exec: async () => ({ ...asset, ...update.$set, revision: 1 }) }) }));
|
||||
service = new GraphicLibraryService(model, { getSelected: async () => ({ id: 'a1', name: 'Anlage', anlageNr: 123 }) } as any, draw,
|
||||
{ collection: () => collection } as any);
|
||||
});
|
||||
it('speichert genau einen festen Zustand unter normalisiertem eindeutigen Namen', async () => {
|
||||
const result = await service.create(input);
|
||||
expect(result.name).toBe('Punkt_gruen'); expect(result.variant).toBe('active');
|
||||
expect(result.xaml).toContain('#00ff00'); expect(result.svg).toContain('#000000');
|
||||
expect(model.create.mock.calls[0][0].nameKey).toBe('punkt_gruen');
|
||||
expect(Buffer.from(result.xamlBase64, 'base64').toString('utf8')).toBe(result.xaml);
|
||||
expect(model.create.mock.calls[0][0].generator.ergebnis).toBeUndefined();
|
||||
});
|
||||
it('aktualisiert alle vier Verwendungsarten ohne Platzierung oder Zustand zu überschreiben', async () => {
|
||||
const result = await service.update(id, { ...input, version: 0 });
|
||||
expect(result.version).toBe(1); expect(result.name).toBe('Punkt_gruen');
|
||||
expect(collection.updateMany).toHaveBeenCalledTimes(4);
|
||||
const calls = collection.updateMany.mock.calls as any[];
|
||||
for (const [, update, options] of calls) {
|
||||
expect(Object.keys(update.$set).some(key => /posX|posY|width|height|placementId|zustand|zIndex/.test(key))).toBe(false);
|
||||
expect(options.arrayFilters).toBeDefined();
|
||||
}
|
||||
expect(calls[0][1].$set['rows.$[].graphics.$[graphic].generator'].ergebnis.varianten[0].xaml).toBe(result.xaml);
|
||||
expect(calls[3][2].arrayFilters).toContainEqual({ 'element.varianten.libraryId': id });
|
||||
expect(model.findOneAndUpdate.mock.calls[0][0]).toMatchObject({ _id: id, anlageId: 'a1', $or: [{ revision: 0 }, { revision: { $exists: false } }] });
|
||||
});
|
||||
it('verhindert Versionskonflikte und ungültige Updates vor dem Verteilen', async () => {
|
||||
await expect(service.update(id, { ...input, version: 0, anlageId: 'fremd' })).rejects.toThrow('Anlage');
|
||||
await expect(service.update(id, { ...input, version: -1 })).rejects.toThrow('Version');
|
||||
await expect(service.update(id, { ...input, version: 0, variant: 'fehlt' })).rejects.toThrow('Variante');
|
||||
model.findOneAndUpdate.mockReturnValueOnce({ lean: () => ({ exec: async () => null }) });
|
||||
await expect(service.update(id, { ...input, version: 0 })).rejects.toThrow('inzwischen');
|
||||
expect(collection.updateMany).not.toHaveBeenCalled();
|
||||
});
|
||||
it('löst veraltete Aggregat-Snapshots beim Lesen auf und erhält Layout und individuelle Varianten', async () => {
|
||||
const custom = { zustand: 'error', xaml: '<Custom/>' };
|
||||
const elements: any[] = [{ darstellung: 'standard', typ: 'text' }, { darstellung: 'xaml', width: 80, height: 30, posX: 50, posY: 70,
|
||||
varianten: [{ zustand: 'activeRight', libraryId: id, xaml: '<Old/>' }, custom] }];
|
||||
const result: any[] = await service.resolveElements(elements, 'a1');
|
||||
expect(result[0]).toBe(elements[0]); expect(result[1]).toMatchObject({ width: 80, height: 30, posX: 50, posY: 70 });
|
||||
expect(result[1].varianten[0]).toMatchObject({ zustand: 'activeRight', libraryId: id, xaml: asset.xaml });
|
||||
expect(result[1].varianten[1]).toBe(custom); expect(draw.renderGraphic).not.toHaveBeenCalled();
|
||||
});
|
||||
it('verhindert ungültige Namen, fremde Anlagen, unbekannte Zustände und Namensduplikate', async () => {
|
||||
await expect(service.create({ ...input, anlageId: 'fremd' })).rejects.toThrow('Anlage');
|
||||
await expect(service.create({ ...input, name: '../fremd' })).rejects.toThrow();
|
||||
await expect(service.create({ ...input, variant: 'fehlt' })).rejects.toThrow('Variante');
|
||||
model.create.mockRejectedValueOnce({ code: 11000 });
|
||||
await expect(service.create(input)).rejects.toThrow('bereits vergeben');
|
||||
});
|
||||
it('ersetzt manipulierte Snapshotdaten durch den festen Serverstand und rendert bei Verwendung nicht neu', async () => {
|
||||
const source: any = { anlageId: 'a1', rows: [{ id: 'r', graphics: [{ slot: 'ein', placementId: 'p', libraryId: id,
|
||||
name: 'manipuliert', variant: 'error', generator: { config: { scale: 500 } }, xamlOverride: '<Fake/>' }] }] };
|
||||
const result = await service.resolveTable(source);
|
||||
const binding = prepareBinding(draw, result.rows[0].graphics[0]);
|
||||
expect(binding.libraryId).toBe(id); expect(binding.name).toBe('Punkt_gruen'); expect(binding.variant).toBe('active');
|
||||
expect(binding.xamlOverride).toBeUndefined(); expect(binding.generator.ergebnis?.varianten).toHaveLength(1);
|
||||
expect(binding.generator.ergebnis?.varianten[0].xaml).toBe(asset.xaml); expect(draw.renderGraphic).not.toHaveBeenCalled();
|
||||
expect(model.findOne).toHaveBeenCalledWith({ _id: id, anlageId: 'a1' });
|
||||
});
|
||||
it('behält individuelle Generatoren unverändert und löst dieselbe Bibliotheks-ID nur einmal auf', async () => {
|
||||
const original: any = { slot: 'aus', placementId: 'p', variant: 'inactive', generator: input.generator };
|
||||
const source: any = { anlageId: 'a1', rows: [{ graphics: [original, { ...original, slot: 'ein', libraryId: id }] },
|
||||
{ graphics: [{ ...original, libraryId: id }] }] };
|
||||
const result = await service.resolveTable(source);
|
||||
expect(result.rows[0].graphics[0]).toBe(original); expect(model.findOne).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
it('blockiert Löschen bei Sensor-, Anzeigen- oder Aggregatverwendung', async () => {
|
||||
collection.findOne.mockResolvedValueOnce({ _id: 'r1' });
|
||||
await expect(service.remove(id, 'a1')).rejects.toThrow('zugeordnet'); expect(model.deleteOne).not.toHaveBeenCalled();
|
||||
collection.findOne.mockReset().mockResolvedValueOnce(null).mockResolvedValueOnce(null).mockResolvedValueOnce(null).mockResolvedValueOnce({ _id: 'agg' });
|
||||
await expect(service.remove(id, 'a1')).rejects.toThrow('Aggregat'); expect(model.deleteOne).not.toHaveBeenCalled();
|
||||
});
|
||||
it('löscht nur unbenutzte Grafiken und exportiert den gespeicherten XAML-Stand ohne Generatoraufruf', async () => {
|
||||
await service.remove(id, 'a1'); expect(model.deleteOne).toHaveBeenCalledWith({ _id: id, anlageId: 'a1' });
|
||||
expect(await service.exportXaml('a1')).toEqual({ count: 1, directory: 'data/123_Anlage/xaml' });
|
||||
expect(writeFile).toHaveBeenCalledWith(expect.stringMatching(/xaml[\\/]Punkt_gruen\.xaml$/), asset.xaml, 'utf8');
|
||||
expect(draw.renderGraphic).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user