import { describe, expect, it } from "vitest"; import { estimateCloudProjectJsonBytes, formatByteSize, measureCloudProjectJsonBytes, shouldApplyCloudProjectShapes, shouldUploadLocalProjectToCloud, } from "@/lib/cloudProjectSync"; import type { WorkplaneShape } from "@/types/sketchforge"; function meshShape(id: string, positionCount: number): WorkplaneShape { return { id, name: id, kind: "box", x: 0, y: 0, z: 0, width: 10, height: 10, depth: 10, rotation: 0, color: "#ffffff", importedMesh: { positions: new Array(positionCount).fill(0.5), baseWidth: 10, baseDepth: 10, baseHeight: 10, triangleCount: Math.floor(positionCount / 9), sourceFormat: "dwg", }, } as WorkplaneShape; } describe("cloudProjectSync", () => { it("never lets an empty cloud copy erase local geometry", () => { expect( shouldApplyCloudProjectShapes({ cloudRevision: 5_000, cloudShapeCount: 0, localRevision: 1_000, localShapeCount: 3, }), ).toBe(false); }); it("restores geometry from the cloud when the local copy is empty", () => { expect( shouldApplyCloudProjectShapes({ cloudRevision: 5_000, cloudShapeCount: 2, localRevision: 9_000, localShapeCount: 0, }), ).toBe(true); }); it("keeps local geometry when the local revision is newer", () => { expect( shouldApplyCloudProjectShapes({ cloudRevision: 1_000, cloudShapeCount: 2, localRevision: 2_000, localShapeCount: 2, }), ).toBe(false); }); it("applies a cloud copy that is at least as new as the local copy", () => { expect( shouldApplyCloudProjectShapes({ cloudRevision: 2_000, cloudShapeCount: 2, localRevision: 2_000, localShapeCount: 2, }), ).toBe(true); }); it("does not upload an empty payload for a project that reports shapes", () => { expect(shouldUploadLocalProjectToCloud({ metadataShapeCount: 4, resolvedShapeCount: 0 })).toBe(false); expect(shouldUploadLocalProjectToCloud({ metadataShapeCount: 0, resolvedShapeCount: 0 })).toBe(true); expect(shouldUploadLocalProjectToCloud({ metadataShapeCount: 4, resolvedShapeCount: 4 })).toBe(true); }); it("skips DWG-scale meshes before serializing them", () => { const shapes = [meshShape("dwg", 3_873_672)]; const estimate = estimateCloudProjectJsonBytes(shapes); expect(estimate).toBeGreaterThan(4 * 1024 * 1024); const measured = measureCloudProjectJsonBytes(shapes, { shapes }, 4 * 1024 * 1024); expect(measured).toBe(estimate); }); it("measures small payloads exactly in UTF-8 bytes", () => { const shapes = [meshShape("small", 9)]; const payload = { shapes, name: "Apartman Projesi" }; expect(measureCloudProjectJsonBytes(shapes, payload, 4 * 1024 * 1024)).toBe( new TextEncoder().encode(JSON.stringify(payload)).length, ); }); it("formats byte sizes for screen readers", () => { expect(formatByteSize(0)).toBe("0 KB"); expect(formatByteSize(1536)).toBe("2 KB"); expect(formatByteSize(7 * 1024 * 1024)).toBe("7,0 MB"); }); });