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; assert.equal("buildingId" in updateWithoutBuilding, false); }); });