Files
snapcad-studio/tests/unit/dwgKernel.test.ts
T

79 lines
3.1 KiB
TypeScript
Raw Normal View History

import { describe, expect, it } from "vitest";
import fs from "node:fs";
import path from "node:path";
import {
MAX_DWG_BYTES,
convertDwgBytesToDxf,
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("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);
}
});
});