Files
DOSSIER/src/native/projectDelta.test.ts
T
Claude 31834aa471 [121] Ressourcen-Fenster: Differenz der geänderten Projekt-Felder statt ganzem Projekt
projectDelta/applyProjectDelta (native/projectDelta.ts, getestet): oberste Ebene per
Referenzvergleich; voller Stand nur bei „ready“, nach geschlossenem Fenster oder Fehler.
Nicht in der Tauri-App geprüft (nur Linux-Container).
2026-11-03 20:59:59 +00:00

55 lines
2.1 KiB
TypeScript

import { describe, expect, it } from "vitest";
import type { Project } from "../model/types";
import { applyProjectDelta, projectDelta } from "./projectDelta";
const base = {
id: "p",
name: "Demo",
walls: [{ id: "w1" }],
hatches: [{ id: "h1" }],
rooms: [],
} as unknown as Project;
describe("projectDelta", () => {
it("ohne Änderung: null", () => {
expect(projectDelta(base, { ...base })).toBeNull();
});
it("nur geänderte Felder (Referenz), zusammengesetzt gleich dem neuen Stand", () => {
const next = { ...base, walls: [{ id: "w1" }, { id: "w2" }] } as unknown as Project;
const d = projectDelta(base, next)!;
expect(Object.keys(d.set)).toEqual(["walls"]);
expect(d.unset).toEqual([]);
expect(applyProjectDelta(base, d)).toEqual(next);
});
it("neue und entfernte Felder", () => {
const next = { ...base, projectNumber: "42" } as Record<string, unknown>;
delete next.rooms;
const d = projectDelta(base, next as unknown as Project)!;
expect(d.set).toEqual({ projectNumber: "42" });
expect(d.unset).toEqual(["rooms"]);
const out = applyProjectDelta(base, d) as unknown as Record<string, unknown>;
expect(out).toEqual(next);
expect("rooms" in out).toBe(false);
// Die Basis bleibt unverändert.
expect((base as unknown as Record<string, unknown>).rooms).toEqual([]);
});
it("überlebt die JSON-Serialisierung der Brücke (undefined-Feld → entfernt)", () => {
const next = { ...base, name: "Neu", projectNumber: undefined } as unknown as Project;
const d = projectDelta(base, next)!;
const wire = JSON.parse(JSON.stringify(d));
expect(applyProjectDelta(base, wire)).toEqual(JSON.parse(JSON.stringify(next)));
});
it("auf undefined gesetztes Feld wird entfernt", () => {
const withNumber = { ...base, projectNumber: "1" } as unknown as Project;
const next = { ...withNumber, projectNumber: undefined } as unknown as Project;
const d = projectDelta(withNumber, next)!;
expect(d.unset).toEqual(["projectNumber"]);
const wire = JSON.parse(JSON.stringify(d));
expect("projectNumber" in applyProjectDelta(withNumber, wire)).toBe(false);
});
});