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

97 lines
2.7 KiB
TypeScript
Raw Normal View History

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);
});
});