leistungsbilanz-ts/tests/building-structure-frontend-api.test.ts
Grovy311 cd89c4a499 Add buildings above floors and a building structure page
Projects with several buildings could not be structured: floors and rooms
hung directly off the project. Floors can now belong to a building
(migration 0009: table buildings, nullable floors.building_id). Rooms and
distribution boards keep their floor; their building follows from it.
Existing floors stay without a building.

Buildings use the location-structure command store with
project-building.insert/.update/.delete, inverses and persistent Undo/Redo;
deleting a building with assigned floors is rejected. Floor commands carry
buildingId since command version 2. Version 1 floor commands from existing
history predate buildings and are upgraded with buildingId = null before
they are executed and persisted, so forward and inverse keep one version.

Project snapshot schema 7 stores buildings and floor.buildingId; versions
1-6 are upgraded without buildings. Restore and portable transfer include
buildings, and the transfer remaps building ids.

Buildings, floors and rooms move from the project overview to their own
page /projects/:id/struktur with three columns (Gebäude -> Etagen -> Räume)
that filter each other, and a sidebar entry. Floor selects in the board and
room dialogs are grouped by building. Unchanged dialogs close without a
command. Details in docs/building-structure.md; see issue #6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 12:52:34 +02:00

100 lines
3.1 KiB
TypeScript

import assert from "node:assert/strict";
import { describe, it } from "node:test";
import {
createBuilding,
createFloor,
deleteBuilding,
listBuildings,
updateBuilding,
updateFloor,
} from "../src/frontend/utils/api.js";
describe("building structure frontend API", () => {
it("sends building commands and optional floor building assignments", async () => {
const requests: Array<{ url: string; method: string; body: unknown }> = [];
const originalFetch = globalThis.fetch;
globalThis.fetch = async (input, init) => {
requests.push({
url: String(input),
method: init?.method ?? "GET",
body: init?.body === undefined ? undefined : JSON.parse(String(init.body)),
});
return new Response(JSON.stringify([]), {
status: 200,
headers: { "Content-Type": "application/json" },
});
};
try {
await listBuildings("project-1");
await createBuilding("project-1", { name: "Haus A" }, 3);
await updateBuilding("project-1", "building-1", { name: "Haus B" }, 4);
await deleteBuilding("project-1", "building-1", 5);
await createFloor(
"project-1",
{ name: "EG", buildingId: "building-1" },
6
);
await createFloor("project-1", { name: "Dach", buildingId: null }, 7);
await createFloor("project-1", { name: "UG", buildingId: undefined }, 8);
await updateFloor(
"project-1",
"floor-1",
{ name: "1. OG", buildingId: null },
9
);
await updateFloor("project-1", "floor-1", { name: "2. OG" }, 10);
} finally {
globalThis.fetch = originalFetch;
}
assert.deepEqual(requests, [
{
url: "/api/projects/project-1/buildings",
method: "GET",
body: undefined,
},
{
url: "/api/projects/project-1/buildings",
method: "POST",
body: { name: "Haus A", expectedRevision: 3 },
},
{
url: "/api/projects/project-1/buildings/building-1",
method: "PUT",
body: { name: "Haus B", expectedRevision: 4 },
},
{
url: "/api/projects/project-1/buildings/building-1",
method: "DELETE",
body: { expectedRevision: 5 },
},
{
url: "/api/projects/project-1/floors",
method: "POST",
body: { name: "EG", buildingId: "building-1", expectedRevision: 6 },
},
{
url: "/api/projects/project-1/floors",
method: "POST",
body: { name: "Dach", buildingId: null, expectedRevision: 7 },
},
{
url: "/api/projects/project-1/floors",
method: "POST",
body: { name: "UG", expectedRevision: 8 },
},
{
url: "/api/projects/project-1/floors/floor-1",
method: "PUT",
body: { name: "1. OG", buildingId: null, expectedRevision: 9 },
},
{
url: "/api/projects/project-1/floors/floor-1",
method: "PUT",
body: { name: "2. OG", expectedRevision: 10 },
},
]);
const updateWithoutBuilding = requests[8].body as Record<string, unknown>;
assert.equal("buildingId" in updateWithoutBuilding, false);
});
});