Files
web-cad/frontend/tests/helpers/inMemoryDoc.ts
T
2026-08-26 21:04:17 +02:00

53 lines
1.4 KiB
TypeScript
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.
/**
* InMemoryDoc-Helper Netzwerkfreie YjsDocument-Fabrik für Tests.
*
* YjsDocument läuft bereits komplett In-Memory (Y.Doc ohne Provider);
* dieser Wrapper bietet bequemes Seeding und Reset für isolierte Tests.
* Genutzt ab A3 (Tool-Workflow-Tests) und A2b (CADDocument-Tests).
*/
import { YjsDocument } from '../../src/crdt/YjsDocument';
import type { CADElement } from '../../src/types/cad.types';
export interface SeededDoc {
ydoc: YjsDocument;
/** Fügt Elemente in EINER Transaktion hinzu (1 Undo-Schritt). */
addAll(elements: CADElement[]): void;
/** Entfernt alle Elemente/Layer-Inhalte (Dokument bleibt verwendbar). */
clear(): void;
}
/** Erzeugt ein frisches, leeres Test-Dokument. */
export function createInMemoryDoc(): SeededDoc {
const ydoc = new YjsDocument();
return {
ydoc,
addAll(elements) {
ydoc.doc.transact(() => {
for (const el of elements) ydoc.data.elements.set(el.id, el);
});
},
clear() {
ydoc.doc.transact(() => {
for (const id of Array.from(ydoc.data.elements.keys())) {
ydoc.data.elements.delete(id);
}
});
},
};
}
/** Basis-CADLayer für Tests. */
export function makeTestLayer(id = 'layer-1', name = 'Test-Layer') {
return {
id,
name,
visible: true,
locked: false,
color: '#ffffff',
lineType: 'solid',
transparency: 0,
sortOrder: 0,
parentId: null,
};
}