feat: sesli CAD asistani, bellek optimizasyonlari ve akilli cozunurluk
CI / Typecheck and unit tests (push) Canceled after 0s
Build and Push Docker Images / build (push) Canceled after 0s

- 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
This commit is contained in:
Enes Yakıştır
2026-11-02 13:37:37 +03:00
parent c09407bcb3
commit e7b6db9db6
32 changed files with 5503 additions and 285 deletions
+140
View File
@@ -0,0 +1,140 @@
import { describe, expect, it } from "vitest";
import {
audit3DScene,
audit2DDrawing,
parseDxfToCustomCadEntities,
fitEntitiesToDrawingCanvas,
type CustomCadEntity,
} from "@/lib/cadAuditor";
import type { WorkplaneShape } from "@/types/sketchforge";
describe("CAD Auditor - 3D Scene Inspection", () => {
it("returns clear empty report when scene has no shapes", () => {
const report = audit3DScene([]);
expect(report.totalShapes).toBe(0);
expect(report.solidCount).toBe(0);
expect(report.holeCount).toBe(0);
expect(report.speechSummary).toContain("Çalışma düzlemi boş");
});
it("calculates accurate bounding box, counts, and volume for solid and hole shapes", () => {
const shapes: WorkplaneShape[] = [
{
id: "box1",
name: "Plaka Gövde",
kind: "box",
x: 0,
z: 0,
elevation: 0,
width: 100,
depth: 60,
height: 20,
hole: false,
color: "#0ea5e9",
},
{
id: "hole1",
name: "Montaj Deliği",
kind: "cylinder",
x: 0,
z: 0,
elevation: -2,
width: 16,
depth: 16,
height: 24,
hole: true,
color: "#f43f5e",
},
];
const report = audit3DScene(shapes);
expect(report.totalShapes).toBe(2);
expect(report.solidCount).toBe(1);
expect(report.holeCount).toBe(1);
expect(report.boundingBox.width).toBe(100);
expect(report.boundingBox.depth).toBe(60);
expect(report.boundingBox.height).toBe(24);
expect(report.netEstimatedVolumeCm3).toBeGreaterThan(110);
expect(report.netEstimatedVolumeCm3).toBeLessThan(125);
expect(report.overlappingPairs.length).toBe(1);
expect(report.overlappingPairs[0].reason).toContain("Montaj Deliği");
expect(report.speechSummary).toContain("3B Sahnede 2 nesne var");
expect(report.fullTextReport).toContain("SNAPCAD 3B SAHNE DENETİM RAPORU");
});
});
describe("CAD Auditor - 2D Drawing Inspection", () => {
it("returns empty report when pafta has no entities", () => {
const report = audit2DDrawing([]);
expect(report.totalEntities).toBe(0);
expect(report.speechSummary).toContain("Teknik resim paftasında henüz kullanıcı çizimi bulunmuyor");
});
it("analyzes closed loops, entity counts, layers, and open endpoints", () => {
const entities: CustomCadEntity[] = [
{ id: "1", type: "line", x1: 0, y1: 0, x2: 100, y2: 0, layer: "VISIBLE" },
{ id: "2", type: "line", x1: 100, y1: 0, x2: 100, y2: 50, layer: "VISIBLE" },
{ id: "3", type: "line", x1: 100, y1: 50, x2: 0, y2: 50, layer: "VISIBLE" },
{ id: "4", type: "line", x1: 0, y1: 50, x2: 0, y2: 0, layer: "VISIBLE" },
{ id: "5", type: "circle", x1: 50, y1: 25, radius: 10, layer: "CENTER" },
{ id: "6", type: "dim", x1: 0, y1: 0, x2: 100, y2: 0, label: "100 mm", layer: "DIMENSION" },
];
const report = audit2DDrawing(entities);
expect(report.totalEntities).toBe(6);
expect(report.countsByType.line).toBe(4);
expect(report.countsByType.circle).toBe(1);
expect(report.countsByType.dim).toBe(1);
expect(report.countsByLayer.VISIBLE).toBe(4);
expect(report.countsByLayer.CENTER).toBe(1);
expect(report.countsByLayer.DIMENSION).toBe(1);
expect(report.openEndpointsCount).toBe(0); // All 4 line vertices form a continuous closed loop
expect(report.speechSummary).toContain("6 geometrik eleman var");
});
});
describe("Technical Drawing Studio - DXF Entity Parser & Canvas Fitting", () => {
it("parses ASCII DXF text into CustomCadEntity list with layer mappings", () => {
const sampleDxf = [
"0", "SECTION",
"2", "ENTITIES",
"0", "LINE",
"8", "CENTER_LINES",
"10", "10.0",
"20", "20.0",
"11", "50.0",
"21", "20.0",
"0", "CIRCLE",
"8", "VISIBLE_OUTLINE",
"10", "30.0",
"20", "20.0",
"40", "15.0",
"0", "ENDSEC",
"0", "EOF",
].join("\n");
const parsed = parseDxfToCustomCadEntities(sampleDxf);
expect(parsed.length).toBe(2);
expect(parsed[0].type).toBe("line");
expect(parsed[0].layer).toBe("CENTER");
expect(parsed[0].x1).toBe(10);
expect(parsed[0].x2).toBe(50);
expect(parsed[1].type).toBe("circle");
expect(parsed[1].layer).toBe("VISIBLE");
expect(parsed[1].radius).toBe(15);
});
it("scales and centers distant CAD entities to fit within the 2D pafta drawing sheet", () => {
const distantEntities: CustomCadEntity[] = [
{ id: "1", type: "line", x1: 10000, y1: 50000, x2: 15000, y2: 50000, layer: "VISIBLE" },
{ id: "2", type: "circle", x1: 12500, y1: 52000, radius: 500, layer: "VISIBLE" },
];
const fitted = fitEntitiesToDrawingCanvas(distantEntities, 400, 300);
expect(fitted.length).toBe(2);
expect(fitted[0].x1).toBeGreaterThanOrEqual(100);
expect(fitted[0].x1).toBeLessThanOrEqual(600);
expect(fitted[0].y1).toBeGreaterThanOrEqual(100);
expect(fitted[0].y1).toBeLessThanOrEqual(500);
});
});
+11
View File
@@ -4,6 +4,8 @@ import path from "node:path";
import {
MAX_DWG_BYTES,
convertDwgBytesToDxf,
convertWithMlightLibreDwg,
getMlightLibreDwg,
importedShapeFromDwg,
loadLibreDwgKernel,
readDwgVersion,
@@ -65,6 +67,15 @@ describe("LibreDWG WebAssembly Kernel", () => {
);
});
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)) {
+19 -1
View File
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { exportMeshesToDxf, type DxfExportMesh } from "@/lib/dxfExport";
import { dxfExtrusionDepthForSpan, importedShapeFromDxf } from "@/lib/dxfImport";
import { compressDxfText, decompressDxfText, dxfExtrusionDepthForSpan, getImportedDxfText, importedShapeFromDxf } from "@/lib/dxfImport";
function makePrismMesh(): DxfExportMesh {
// A simple 10x20x30 prism made of 12 triangles (6 rectangular faces)
@@ -642,4 +642,22 @@ describe("DXF extrusion depth scales with drawing size", () => {
expect(shape.importedMesh?.baseDepth).toBeCloseTo(20_000, 0);
expect(shape.importedMesh?.baseHeight).toBeCloseTo(400, 0);
});
it("compresses large DXF text into base64 deflated payload and retrieves pristine text", () => {
const original = rectangleDxf(100, 50).repeat(50);
expect(original.length).toBeGreaterThan(5000);
const compressed = compressDxfText(original);
expect(compressed.length).toBeLessThan(original.length * 0.1); // >90% compression
const decompressed = decompressDxfText(compressed);
expect(decompressed).toBe(original);
const shape = importedShapeFromDxf("large_plan.dxf", original);
expect(shape.importedMesh?.sourceDxfCompressed).toBeDefined();
expect(shape.importedMesh?.sourceDxfText).toBeUndefined();
const retrieved = getImportedDxfText(shape.importedMesh);
expect(retrieved).toBe(original);
});
});
+97
View File
@@ -0,0 +1,97 @@
import { describe, it, expect } from "vitest";
import {
extractContoursFromCadEntities,
generateCadGCode,
type GCodeGeneratorOptions,
} from "../../apps/web/src/lib/gcodeGenerator";
import type { CustomCadEntity } from "../../apps/web/src/lib/cadAuditor";
describe("SnapCAD G-Code CAM Engine", () => {
const sampleEntities: CustomCadEntity[] = [
{
id: "line1",
type: "line",
x1: 0,
y1: 0,
x2: 100,
y2: 0,
layer: "VISIBLE",
},
{
id: "rect1",
type: "rect",
x1: 20,
y1: 20,
width: 60,
height: 40,
layer: "VISIBLE",
},
{
id: "circle1",
type: "circle",
x1: 50,
y1: 40,
radius: 10,
layer: "VISIBLE",
},
{
id: "dim1",
type: "dim",
x1: 0,
y1: -10,
x2: 100,
y2: -10,
layer: "DIMENSION", // should be ignored for cutting
},
];
it("extracts cutting contours and excludes annotation/dimension layers", () => {
const contours = extractContoursFromCadEntities(sampleEntities);
expect(contours.length).toBe(3); // line, rect, circle (dim is skipped)
expect(contours[0].isClosed).toBe(false);
expect(contours[1].isClosed).toBe(true);
expect(contours[2].isClosed).toBe(true);
});
it("generates valid CNC Router G-Code with multipass stepdown and spindle commands", () => {
const options: GCodeGeneratorOptions = {
machineType: "cnc_router",
feedRate: 1000,
safeZ: 5,
cutDepth: -6,
passes: 2,
spindleRpm: 12000,
};
const result = generateCadGCode(sampleEntities, options);
expect(result.gcode).toContain("G21");
expect(result.gcode).toContain("G90");
expect(result.gcode).toContain("M3 S12000");
expect(result.gcode).toContain("G0 Z5.00");
expect(result.gcode).toContain("Z-3.00"); // Pass 1
expect(result.gcode).toContain("Z-6.00"); // Pass 2
expect(result.gcode).toContain("M5");
expect(result.gcode).toContain("M30");
expect(result.contourCount).toBe(3);
expect(result.totalCutDistanceMm).toBeGreaterThan(250);
expect(result.estimatedTimeSeconds).toBeGreaterThan(0);
expect(result.speechSummary).toContain("CNC G-Code");
});
it("generates clean Laser cutting G-Code with M3/M5 pulses and pierce dwell", () => {
const options: GCodeGeneratorOptions = {
machineType: "laser",
feedRate: 1800,
spindleRpm: 1000,
};
const result = generateCadGCode(sampleEntities, options);
expect(result.gcode).toContain("M3 S1000 (Laser ON)");
expect(result.gcode).toContain("G4 P0.05");
expect(result.gcode).toContain("M5 (Laser OFF)");
expect(result.gcode).not.toContain("Z-"); // Laser has no plunge depth
expect(result.bounds.width).toBe(100);
});
});
+69
View File
@@ -0,0 +1,69 @@
import { describe, it, expect } from "vitest";
import {
calculateSpatialSonarParameters,
formatShapeAccessibleDescription,
} from "../../apps/web/src/lib/spatialAudioSonar";
describe("Spatial Audio Sonar & Acoustic Radar", () => {
it("calculates stereo panning correctly based on X coordinate", () => {
const left = calculateSpatialSonarParameters(-150, 0, 0);
expect(left.pan).toBe(-1);
const right = calculateSpatialSonarParameters(150, 0, 0);
expect(right.pan).toBe(1);
const center = calculateSpatialSonarParameters(0, 0, 0);
expect(center.pan).toBe(0);
});
it("maps elevation/height to audio frequency (pitch)", () => {
const low = calculateSpatialSonarParameters(0, 0, 0);
const mid = calculateSpatialSonarParameters(0, 50, 0);
const high = calculateSpatialSonarParameters(0, 100, 0);
expect(low.frequency).toBe(220);
expect(mid.frequency).toBeGreaterThan(low.frequency);
expect(high.frequency).toBe(880);
});
it("shifts hole/cavity frequency to darker tone", () => {
const solid = calculateSpatialSonarParameters(0, 50, 0, false);
const hole = calculateSpatialSonarParameters(0, 50, 0, true);
expect(hole.isHole).toBe(true);
expect(hole.frequency).toBeLessThan(solid.frequency);
});
it("formats rich Turkish description for screen readers", () => {
const boxDesc = formatShapeAccessibleDescription({
name: "Ana Kutu",
hole: false,
width: 50,
depth: 30,
height: 20,
x: 10,
y: 0,
z: -15,
});
expect(boxDesc).toContain("Ana Kutu seçildi");
expect(boxDesc).toContain("Katı gövde");
expect(boxDesc).toContain("50'e 30");
expect(boxDesc).toContain("yükseklik 20 milimetre");
expect(boxDesc).toContain("Konum: X 10, Z -15");
const holeCylDesc = formatShapeAccessibleDescription({
name: "M6 Civata Deliği",
hole: true,
radius: 3,
height: 25,
x: 0,
y: 5,
z: 0,
});
expect(holeCylDesc).toContain("M6 Civata Deliği");
expect(holeCylDesc).toContain("Delik ve boşaltma gövdesi");
expect(holeCylDesc).toContain("Yarıçap 3");
});
});
+89
View File
@@ -0,0 +1,89 @@
import { describe, expect, it } from "vitest";
import { parseCadVoiceCommand } from "@/lib/voiceCadCommander";
import { compute2DSheetNesting, nestingResultToCadEntities } from "@/lib/sheetNesting";
describe("SnapVoice CAD - Voice Command Parser", () => {
it("parses box dimensions and hole status from natural Turkish voice", () => {
const cmd1 = parseCadVoiceCommand("50'ye 30 kutu ekle");
expect(cmd1.action).toBe("add_shape");
expect(cmd1.payload?.kind).toBe("box");
expect(cmd1.payload?.width).toBe(50);
expect(cmd1.payload?.depth).toBe(30);
expect(cmd1.payload?.isHole).toBe(false);
const cmd2 = parseCadVoiceCommand("25 çapında delik silindir ekle");
expect(cmd2.action).toBe("add_shape");
expect(cmd2.payload?.kind).toBe("cylinder");
expect(cmd2.payload?.width).toBe(25);
expect(cmd2.payload?.isHole).toBe(true);
expect(cmd2.speechFeedback).toContain("delik kesici");
});
it("parses operation commands: group, ungroup, delete, audit, undo", () => {
expect(parseCadVoiceCommand("seçili nesneleri grupla").action).toBe("group");
expect(parseCadVoiceCommand("grubu çöz").action).toBe("ungroup");
expect(parseCadVoiceCommand("seçileni sil").action).toBe("delete");
expect(parseCadVoiceCommand("sahneyi denetle ve rapor ver").action).toBe("open_audit");
expect(parseCadVoiceCommand("geri al").action).toBe("undo");
expect(parseCadVoiceCommand("ölçüleri oku").action).toBe("speak_selection");
});
it("parses slash commands and quick shortcuts seamlessly", () => {
expect(parseCadVoiceCommand("/denetle").action).toBe("open_audit");
expect(parseCadVoiceCommand("/dxf").action).toBe("export_dxf");
expect(parseCadVoiceCommand("/pdf").action).toBe("export_pdf");
expect(parseCadVoiceCommand("/gcode").action).toBe("gcode");
expect(parseCadVoiceCommand("/sac").action).toBe("nesting");
expect(parseCadVoiceCommand("/radar").action).toBe("sonar_radar");
expect(parseCadVoiceCommand("/yardım").action).toBe("help");
expect(parseCadVoiceCommand("/sessiz").action).toBe("mute_voice");
expect(parseCadVoiceCommand("/ses kapat").action).toBe("mute_voice");
expect(parseCadVoiceCommand("/ses aç").action).toBe("unmute_voice");
expect(parseCadVoiceCommand("/ram").action).toBe("ram_cleanup");
expect(parseCadVoiceCommand("/bellek temizle").action).toBe("ram_cleanup");
expect(parseCadVoiceCommand("/eko").action).toBe("toggle_eco_mode");
expect(parseCadVoiceCommand("/düşük ram").action).toBe("toggle_eco_mode");
expect(parseCadVoiceCommand("/otomatik").action).toBe("set_resolution_auto");
expect(parseCadVoiceCommand("/kutu 50 30").action).toBe("add_shape");
expect(parseCadVoiceCommand("/kutu 50 30").payload?.width).toBe(50);
});
it("handles unknown or empty inputs gracefully with guidance", () => {
const res = parseCadVoiceCommand("merhaba nasılsın");
expect(res.action).toBe("unknown");
expect(res.speechFeedback).toContain("anlaşılamadı");
});
});
describe("2D Sheet Nesting Optimizer (CNC Laser Cutting)", () => {
it("packs rectangular parts onto a standard plate with margins and computes efficiency", () => {
const items = [
{ id: "1", name: "Parça A", width: 200, height: 100 },
{ id: "2", name: "Parça B", width: 150, height: 80 },
{ id: "3", name: "Parça C", width: 100, height: 60 },
{ id: "4", name: "Parça D", width: 80, height: 40 },
];
const result = compute2DSheetNesting(items, 600, 400, 5);
expect(result.placedItems.length).toBe(4);
expect(result.unplacedItems.length).toBe(0);
expect(result.sheetAreaMm2).toBe(240000);
expect(result.usedAreaMm2).toBe(200 * 100 + 150 * 80 + 100 * 60 + 80 * 40);
expect(result.efficiencyPercent).toBeGreaterThan(15);
expect(result.wastePercent).toBeLessThan(85);
expect(result.speechSummary).toContain("Nesting yerleşimi tamamlandı");
expect(result.textReport).toContain("SNAPCAD 2B SAC & LAZER YERLEŞİM");
});
it("converts nesting layout into CAD entities with border and part outlines", () => {
const items = [{ id: "1", name: "Kare Plaka", width: 100, height: 100 }];
const nesting = compute2DSheetNesting(items, 400, 300, 5);
const cadEntities = nestingResultToCadEntities(nesting);
expect(cadEntities.length).toBe(2); // Sheet border + 1 part
expect(cadEntities[0].type).toBe("rect");
expect(cadEntities[0].layer).toBe("DIMENSION");
expect(cadEntities[1].type).toBe("rect");
expect(cadEntities[1].layer).toBe("VISIBLE");
});
});
+22 -1
View File
@@ -1,7 +1,7 @@
import { describe, expect, it } from "vitest";
import type { WorkplaneWorkspaceSettings } from "@/types/sketchforge";
import { formatMeasurementNumber, lengthDisplayUnit, millimetersToDisplay, normalizeScaleForUnits, parseMeasurementInput, scaleOptionsForUnits } from "@/lib/measurementUnits";
import { canBeginShapeDrag, DEFAULT_SNAP_GRID, DEFAULT_WORKPLANE_WORKSPACE, normalizeSnapGrid, normalizeWorkspaceSettings, shapeDimensionLimit, workplaneSettingsFingerprint, workspaceHydrationSyncDecision } from "@/lib/workplaneSettings";
import { canBeginShapeDrag, computeEffectivePixelRatio, DEFAULT_SNAP_GRID, DEFAULT_WORKPLANE_WORKSPACE, normalizeSnapGrid, normalizeWorkspaceSettings, shapeDimensionLimit, workplaneSettingsFingerprint, workspaceHydrationSyncDecision } from "@/lib/workplaneSettings";
describe("workplane settings helpers", () => {
it("accepts known snap grid values and falls back for unknown values", () => {
@@ -160,4 +160,25 @@ describe("workplane settings helpers", () => {
expect(workspaceHydrationSyncDecision(hydratedCommit.pendingFingerprint, projectTwo).shouldSync).toBe(true);
});
it("computes effective pixel ratio automatically according to monitor DPI and scene load", () => {
// 1. Auto mode with high-DPI monitor (e.g. 2.0x 4K/Retina)
const autoSettings: WorkplaneWorkspaceSettings = { ...DEFAULT_WORKPLANE_WORKSPACE, resolutionMode: "auto" };
expect(computeEffectivePixelRatio(autoSettings, 5, 2.0)).toBe(2.0); // Modest scene: native crystal clear 2x
expect(computeEffectivePixelRatio(autoSettings, 30, 2.0)).toBe(1.25); // Medium scene: balanced 1.25x
expect(computeEffectivePixelRatio(autoSettings, 60, 2.0)).toBe(1.0); // Heavy scene: auto-fallback to 1.0x to prevent RAM crash
// 2. 1080p standard monitor (1.0x) stays 1.0x
expect(computeEffectivePixelRatio(autoSettings, 10, 1.0)).toBe(1.0);
// 3. Native mode: always preserves native monitor DPR
const nativeSettings: WorkplaneWorkspaceSettings = { ...DEFAULT_WORKPLANE_WORKSPACE, resolutionMode: "native" };
expect(computeEffectivePixelRatio(nativeSettings, 80, 2.0)).toBe(2.0);
// 4. Eco mode: always 1.0x
const ecoSettings: WorkplaneWorkspaceSettings = { ...DEFAULT_WORKPLANE_WORKSPACE, resolutionMode: "eco" };
expect(computeEffectivePixelRatio(ecoSettings, 2, 2.0)).toBe(1.0);
const lowMemSettings: WorkplaneWorkspaceSettings = { ...DEFAULT_WORKPLANE_WORKSPACE, lowMemoryMode: true };
expect(computeEffectivePixelRatio(lowMemSettings, 2, 2.0)).toBe(1.0);
});
});