Files
snapcad-studio/tests/unit/dwgKernel.test.ts
T
Enes Yakıştır e7b6db9db6
CI / Typecheck and unit tests (push) Canceled after 0s
Build and Push Docker Images / build (push) Canceled after 0s
feat: sesli CAD asistani, bellek optimizasyonlari ve akilli cozunurluk
- SnapVoice Turkce sesli CAD ve komut konsolu (/denetle, /ram, /eko, /otomatik, /sessiz)
- 3B uzamsal sesli radar ve gorme engelli gelistirici geometri denetim motoru
- LibreDWG/DXF ikili sikistirma, font tembel yukleme ve RAM optimizasyonu
- Monitor cozunurlugune gore otomatik uyarlamali dinamik grafik modu
2026-11-02 13:37:37 +03:00

91 lines
3.5 KiB
TypeScript

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from "vitest";
import fs from "node:fs";
import path from "node:path";
import {
MAX_DWG_BYTES,
convertDwgBytesToDxf,
convertWithMlightLibreDwg,
getMlightLibreDwg,
importedShapeFromDwg,
loadLibreDwgKernel,
readDwgVersion,
} from "@/lib/dwgKernel";
function asciiBytes(text: string) {
return new Uint8Array(Array.from(text, (char) => char.charCodeAt(0)));
}
describe("LibreDWG WebAssembly Kernel", () => {
it("initializes LibreDWG WebAssembly module with virtual filesystem and cwrap", async () => {
const wasm = await loadLibreDwgKernel();
expect(wasm).toBeDefined();
expect(wasm.FS).toBeDefined();
expect(typeof wasm.FS.writeFile).toBe("function");
expect(typeof wasm.FS.readFile).toBe("function");
expect(typeof wasm.cwrap).toBe("function");
});
it("reads the DWG version signature", () => {
expect(readDwgVersion(asciiBytes("AC1032"))).toBe("AC1032");
expect(readDwgVersion(asciiBytes("AC1015xxxxxxxx"))).toBe("AC1015");
expect(readDwgVersion(asciiBytes("DXF"))).toBeNull();
expect(readDwgVersion(asciiBytes("hello world"))).toBeNull();
expect(readDwgVersion(new Uint8Array([]))).toBeNull();
});
it("rejects files without a DWG signature with a clear Turkish message", async () => {
const notADwg = asciiBytes("Bu bir metin dosyasi, DWG degil. ".repeat(4));
await expect(convertDwgBytesToDxf(notADwg)).rejects.toThrow(
/geçerli bir AutoCAD DWG çizimi değil/,
);
});
it("rejects unsupported DWG versions with the supported range", async () => {
const futureDwg = asciiBytes(`AC1035${"x".repeat(120)}`);
await expect(convertDwgBytesToDxf(futureDwg)).rejects.toThrow(
/Bu DWG sürümü \(AC1035\) desteklenmiyor/,
);
});
it("rejects oversized DWG files before loading the kernel", async () => {
const tooLarge = new Uint8Array(MAX_DWG_BYTES + 1);
tooLarge.set(asciiBytes("AC1032"), 0);
await expect(convertDwgBytesToDxf(tooLarge)).rejects.toThrow(/DWG dosyası çok büyük/);
});
it("handles corrupted/truncated DWG files with clear descriptive error", async () => {
// Valid version signature but no readable DWG sections at all.
const fakeDwgBytes = asciiBytes("AC1015\u0000\u0000\u0000\u0000");
await expect(convertDwgBytesToDxf(fakeDwgBytes)).rejects.toThrow(
/DWG çizimi DXF'e dönüştürülemedi \(dosya sürümü: AC1015/,
);
});
it("rejects empty bytes immediately", async () => {
await expect(convertDwgBytesToDxf(new Uint8Array([]))).rejects.toThrow(
/Geçersiz veya boş DWG dosyası/,
);
});
it("initializes modern GNU LibreDWG WebAssembly kernel instance with dwg_write_dxf", async () => {
const wasm = await getMlightLibreDwg();
expect(wasm).toBeDefined();
expect(wasm.FS).toBeDefined();
expect(typeof wasm.FS.writeFile).toBe("function");
expect(typeof wasm.FS.readFile).toBe("function");
expect(typeof wasm.dwg_write_dxf).toBe("function");
});
it("converts a real AutoCAD DWG file to DXF and constructs a 3D solid shape", async () => {
const dwgPath = path.resolve(process.cwd(), "test_circle_2010.dwg");
if (fs.existsSync(dwgPath)) {
const bytes = fs.readFileSync(dwgPath);
const shape = await importedShapeFromDwg("test_circle_2010.dwg", bytes);
expect(shape.kind).toBe("mesh");
expect(shape.importedMesh).toBeDefined();
expect(shape.importedMesh?.triangleCount).toBeGreaterThan(0);
expect(shape.importedMesh?.baseWidth).toBeGreaterThan(0);
}
});
});