Files
DOSSIER/src/plan/planLazy.test.ts
T

102 lines
5.4 KiB
TypeScript

// Lazy Plan ([K3d], `planLazy.ts`): `generatePlan` liest die Kern-Primitive erst bei Bedarf; die
// Verbraucher im Kern (Szene, Auswahl, SVG, DXF, Druck-SVG) und die Text-Einträge kommen ohne aus.
import { describe, expect, it } from "vitest";
import { generatePlan, generateVisibleLevelsPlan, materializeUnderlay, type Primitive } from "./generatePlan";
import { isLazyUnread, planCount, planPrimAt, planTextEntries, planWithExtra, roomStampAt } from "./planLazy";
import { planSceneBin, planToRenderScene } from "./toRenderScene";
import { planToSvgString } from "../export/planToSvgString";
import { PlanPickIndex } from "./planView/planPick";
import { collectPlanDrawing } from "../export/exportDxf";
import { planToPrintSvg } from "../export/planToPrintSvg";
import { sampleProject } from "../model/sampleProject";
import { flattenCategories, type Project } from "../model/types";
const codes = (p: Project) => new Set(flattenCategories(p.layers).map((c) => c.code));
const withText: Project = {
...sampleProject,
drawings2d: [
...sampleProject.drawings2d,
{ id: "t1", type: "drawing2d", levelId: "eg", categoryCode: "60", geom: { shape: "text", at: { x: 1, y: 2 }, text: "Hallo", height: 0.3, angle: 0 } },
{ id: "l1", type: "drawing2d", levelId: "eg", categoryCode: "60", geom: { shape: "line", a: { x: 0, y: 0 }, b: { x: 3, y: 1 } } },
] as Project["drawings2d"],
};
describe("lazy Plan", () => {
it("Verbraucher im Kern lesen die Primitive nicht", () => {
const plan = generatePlan(withText, "eg", codes(withText));
expect(isLazyUnread(plan)).toBe(true);
planSceneBin(plan);
planToRenderScene(plan);
planToSvgString(plan);
const ix = new PlanPickIndex(plan);
ix.topPolygon({ x: 2, y: 2 });
ix.marquee("wallId", { minX: -100, minY: -100, maxX: 100, maxY: 100 }, false);
expect(ix.drawingTextAt({ x: 1.2, y: 2 })).not.toBeNull();
ix.release();
expect(collectPlanDrawing(plan, withText).stats().model.TEXT).toBeGreaterThan(0);
// Druck-SVG mit minimalem DOM-Ersatz (Node ohne DOM).
const el = (): Record<string, unknown> => ({ setAttribute() {}, appendChild: (c: unknown) => c, insertBefore: (c: unknown) => c, querySelector: () => null, firstChild: null });
const g = globalThis as { document?: unknown };
const prev = g.document;
g.document = { createElementNS: () => el() };
try {
planToPrintSvg(plan, { scaleDenominator: 100, pageWidthMm: 420, pageHeightMm: 297 });
} finally {
g.document = prev;
}
expect(planTextEntries(plan).length).toBeGreaterThan(0);
expect(planPrimAt(plan, 0)).toBeDefined();
expect(isLazyUnread(plan)).toBe(true);
});
it("[140] Lage des Raumstempels aus dem Plan (auch weggerückt vom Pol)", () => {
const room = (sampleProject.rooms ?? []).find((r) => r.floorId === "eg")!;
// L-Raum ohne eigenen Anker mit langem Namen: der Kern rückt den Stempel vom Pol weg.
const l = [[0, 0], [8, 0], [8, 1.2], [1.2, 1.2], [1.2, 8], [0, 8]].map(([x, y]) => ({ x: x + 50, y: y + 50 }));
const p: Project = { ...sampleProject, rooms: (sampleProject.rooms ?? []).map((r) => (r.id === room.id ? { ...r, boundary: l, stampAnchor: undefined, stamp: undefined, name: "Wohnen Essen Kochen" } : r)) };
const plan = generatePlan(p, "eg", codes(p));
const at = roomStampAt(plan, room.id)!;
expect(at).toBeDefined();
const text = planTextEntries(plan).find((e) => e.p.kind === "text" && e.p.roomId === room.id)!;
expect(at).toEqual((text.p as { at: { x: number; y: number } }).at);
expect(roomStampAt(plan, "gibt-es-nicht")).toBeUndefined();
});
it("Einzelzugriff, Text-Einträge und Anzahl == gelesene Liste (dieselben Objekte)", () => {
for (const l of ["eg", "og"]) {
const plan = generatePlan(withText, l, codes(withText));
const n = planCount(plan);
const single = Array.from({ length: n }, (_, i) => planPrimAt(plan, i));
const texts = planTextEntries(plan);
expect(plan.primitives.length).toBe(n);
single.forEach((p, i) => expect(p).toBe(plan.primitives[i]));
const want = plan.primitives
.map((p, i) => ({ p, i }))
.filter((e) => e.p.kind === "text" || e.p.kind === "drawingText" || e.p.kind === "drawingTextPath" || e.p.kind === "drawingImage");
expect(texts.map((e) => e.i)).toEqual(want.map((e) => e.i));
texts.forEach((e, k) => expect(e.p).toBe(want[k].p));
expect(planPrimAt(plan, n)).toBeUndefined();
}
});
it("Überlagerung, kompakte Unterlage und Anhängen bleiben lazy und gleich", () => {
const plan = generateVisibleLevelsPlan(withText, "og", codes(withText), undefined, "mittel", false, false, undefined, 100, true);
expect(isLazyUnread(plan)).toBe(true);
const mat = materializeUnderlay(plan);
const extra: Primitive = { kind: "line", a: { x: 0, y: 0 }, b: { x: 1, y: 1 }, cls: "x", weightMm: 0.1 };
const ext = planWithExtra(mat, [extra], [extra]);
expect(isLazyUnread(mat) && isLazyUnread(ext)).toBe(true);
expect(ext.primitives).toEqual([extra, ...mat.primitives, extra]);
expect(ext.greyedRanges).toEqual(mat.greyedRanges?.map(([a, b]) => [a + 1, b + 1]));
// Eager zum Vergleich: dieselbe Liste wie die gelesene.
expect(plan.primitives.length).toBe(planCount(plan));
});
it("Mono rechnet der Kern: auch dann bleibt der Plan lazy", () => {
const plan = generatePlan(sampleProject, "eg", codes(sampleProject), undefined, "mittel", false, true);
expect(isLazyUnread(plan)).toBe(true);
});
});