189 lines
11 KiB
TypeScript
189 lines
11 KiB
TypeScript
// stl_cad 단위 점검/일괄 변환 (2026-09-29) — 미터 단위 STL 경고, stl_paths 일괄 transform.
|
||
import { test, describe } from 'node:test';
|
||
import assert from 'node:assert/strict';
|
||
import fs from 'fs';
|
||
import os from 'os';
|
||
import path from 'path';
|
||
import { spawnSync } from 'child_process';
|
||
import { OPENSCAD_BIN, readStlBBox } from '../src/tools/stl-cad-core';
|
||
import { stlCadTool } from '../src/tools/stl-cad';
|
||
|
||
describe('stl_cad — 미터 단위 경고 + 일괄 변환', () => {
|
||
const ws = fs.mkdtempSync(path.join(os.tmpdir(), 'stl-unit-'));
|
||
const mk = (name: string, scad: string) => {
|
||
const f = path.join(ws, name + '.scad');
|
||
fs.writeFileSync(f, scad);
|
||
const r = spawnSync(OPENSCAD_BIN, ['-o', path.join(ws, name + '.stl'), '--export-format=binstl', f], { timeout: 60_000 });
|
||
assert.equal(r.status, 0, `fixture 실패: ${r.stderr}`);
|
||
};
|
||
test.before(() => {
|
||
mk('m1', 'cube([0.1,0.2,0.05]);'); // 미터 단위 부품(최대 0.2)
|
||
mk('m2', 'cube([0.08,0.04,0.02]);');
|
||
mk('ok', 'cube([40,20,10]);'); // mm 정상
|
||
});
|
||
|
||
test('info — 미터 단위면 변환 명령을 안내', { timeout: 60_000 }, async () => {
|
||
const r: any = await stlCadTool.execute({ action: 'info', stl_path: 'm1.stl', _workspacePath: ws });
|
||
assert.ok(r.success);
|
||
assert.match(r.stdout, /미터\(m\) 단위/);
|
||
assert.match(r.stdout, /scale=1000/);
|
||
});
|
||
|
||
test('info — mm 정상이면 경고 없음', { timeout: 60_000 }, async () => {
|
||
const r: any = await stlCadTool.execute({ action: 'info', stl_path: 'ok.stl', _workspacePath: ws });
|
||
assert.ok(r.success);
|
||
assert.doesNotMatch(r.stdout, /미터\(m\) 단위/);
|
||
});
|
||
|
||
test('transform stl_paths — 여러 파일 ×1000 제자리 변환, 실제 bbox로 검증', { timeout: 180_000 }, async () => {
|
||
const r: any = await stlCadTool.execute({ action: 'transform', stl_paths: ['m1.stl', 'm2.stl', 'nope.stl'], scale: 1000, _workspacePath: ws });
|
||
assert.ok(r.success);
|
||
assert.match(r.stdout, /2\/3개 성공/);
|
||
assert.match(r.stdout, /nope\.stl[\s\S]*❌/);
|
||
const b1 = readStlBBox(path.join(ws, 'm1.stl'));
|
||
assert.ok(Math.abs((b1.max[1] - b1.min[1]) - 200) < 0.5, `m1 Y 크기 200mm 아님: ${b1.max[1] - b1.min[1]}`);
|
||
const b2 = readStlBBox(path.join(ws, 'm2.stl'));
|
||
assert.ok(Math.abs((b2.max[0] - b2.min[0]) - 80) < 0.5, `m2 X 크기 80mm 아님`);
|
||
});
|
||
|
||
test('stl_paths + output_path 조합은 거부', async () => {
|
||
const r: any = await stlCadTool.execute({ action: 'transform', stl_paths: ['ok.stl'], scale: 2, output_path: 'x.stl', _workspacePath: ws });
|
||
assert.equal(r.success, false);
|
||
});
|
||
});
|
||
|
||
describe('stl_cad — 대소문자만 다른 경로', () => {
|
||
const ws = fs.mkdtempSync(path.join(os.tmpdir(), 'stl-case-'));
|
||
test('L1.stl 로 요청해도 L1.STL 을 찾는다', { timeout: 60_000 }, async () => {
|
||
const f = path.join(ws, 'L1.scad');
|
||
fs.writeFileSync(f, 'cube([10,10,10]);');
|
||
const r0 = spawnSync(OPENSCAD_BIN, ['-o', path.join(ws, 'L1.STL'), '--export-format=binstl', f], { timeout: 60_000 });
|
||
assert.equal(r0.status, 0);
|
||
const r: any = await stlCadTool.execute({ action: 'info', stl_path: 'L1.stl', _workspacePath: ws });
|
||
assert.ok(r.success, r.error);
|
||
});
|
||
});
|
||
|
||
describe('scad import 경로 — 워크스페이스 기준 상대경로 자동 보정', () => {
|
||
const ws = fs.mkdtempSync(path.join(os.tmpdir(), 'stl-import-'));
|
||
test.before(() => {
|
||
fs.mkdirSync(path.join(ws, 'CAD', 'groups'), { recursive: true });
|
||
const f = path.join(ws, 'CAD', 'src.scad');
|
||
fs.writeFileSync(f, 'cube([10,10,10]);');
|
||
const r = spawnSync(OPENSCAD_BIN, ['-o', path.join(ws, 'CAD', 'part.stl'), '--export-format=binstl', f], { timeout: 60_000 });
|
||
assert.equal(r.status, 0);
|
||
});
|
||
test('import("CAD/part.stl")(워크스페이스 기준)이 .scad가 다른 폴더에 있어도 컴파일된다', { timeout: 120_000 }, async () => {
|
||
fs.writeFileSync(path.join(ws, 'CAD', 'groups', 'g.scad'), 'import("CAD/part.stl");\ntranslate([20,0,0]) import("CAD/part.stl");\n');
|
||
const r: any = await stlCadTool.execute({ action: 'compile', scad_path: 'CAD/groups/g.scad', output_path: 'CAD/groups/g.stl', _workspacePath: ws });
|
||
assert.ok(r.success, r.error);
|
||
const b = readStlBBox(path.join(ws, 'CAD', 'groups', 'g.stl'));
|
||
assert.ok(Math.abs((b.max[0] - b.min[0]) - 30) < 0.5, '두 큐브(10+간격20+10=30) 아님');
|
||
assert.equal(fs.readdirSync(path.join(ws, 'CAD', 'groups')).filter((f) => f.startsWith('.__tmp_')).length, 0, '임시 scad가 남음');
|
||
assert.match(fs.readFileSync(path.join(ws, 'CAD', 'groups', 'g.scad'), 'utf8'), /import\("CAD\/part\.stl"\)/, '원본 scad가 바뀜');
|
||
});
|
||
test('실제로 없는 파일이면 실패 + 안내 문구', { timeout: 120_000 }, async () => {
|
||
fs.writeFileSync(path.join(ws, 'CAD', 'groups', 'bad.scad'), 'import("CAD/nope.stl");\n');
|
||
const r: any = await stlCadTool.execute({ action: 'compile', scad_path: 'CAD/groups/bad.scad', output_path: 'CAD/groups/bad.stl', _workspacePath: ws });
|
||
assert.equal(r.success, false);
|
||
});
|
||
});
|
||
|
||
describe('stl_cad merge — 삼각형 병합 + 베드 배치', () => {
|
||
const ws = fs.mkdtempSync(path.join(os.tmpdir(), 'stl-merge-'));
|
||
const mk = (name: string, scad: string) => {
|
||
const f = path.join(ws, name + '.scad');
|
||
fs.writeFileSync(f, scad);
|
||
const r = spawnSync(OPENSCAD_BIN, ['-o', path.join(ws, name + '.stl'), '--export-format=binstl', f], { timeout: 60_000 });
|
||
assert.equal(r.status, 0, `fixture 실패: ${r.stderr}`);
|
||
};
|
||
test.before(() => {
|
||
mk('a', 'translate([100,100,50]) cube([40,20,10]);'); // 원점에서 멀리 떠 있는 부품
|
||
mk('b', 'cube([30,30,5]);');
|
||
mk('c', 'cube([50,50,8]);');
|
||
});
|
||
|
||
test('grid — 바닥 z=0, 중심 (0,0), 베드 안에 배치, 삼각형 수 합계 보존', { timeout: 60_000 }, async () => {
|
||
const r: any = await stlCadTool.execute({ action: 'merge', stl_paths: ['a.stl', 'b.stl', 'c.stl'], output_path: 'out/plate.stl', _workspacePath: ws });
|
||
assert.ok(r.success, r.error);
|
||
const bb = readStlBBox(path.join(ws, 'out', 'plate.stl'));
|
||
assert.ok(Math.abs(bb.min[2]) < 1e-3, '바닥이 z=0이 아님');
|
||
assert.ok(Math.abs(bb.min[0] + bb.max[0]) < 0.1 && Math.abs(bb.min[1] + bb.max[1]) < 0.1, 'XY 중심이 (0,0)이 아님');
|
||
assert.ok(bb.max[0] - bb.min[0] <= 260 && bb.max[1] - bb.min[1] <= 260, '베드 초과');
|
||
const tris = (f: string) => fs.readFileSync(path.join(ws, f)).readUInt32LE(80);
|
||
assert.equal(tris('out/plate.stl'), tris('a.stl') + tris('b.stl') + tris('c.stl'), '삼각형 수가 안 맞음');
|
||
});
|
||
|
||
test('grid — 베드가 작으면 판을 나눔(plate_2.stl)', { timeout: 60_000 }, async () => {
|
||
const r: any = await stlCadTool.execute({ action: 'merge', stl_paths: ['a.stl', 'b.stl', 'c.stl'], output_path: 'out2/p.stl', bed_x: 60, bed_y: 60, gap: 5, _workspacePath: ws });
|
||
assert.ok(r.success, r.error);
|
||
assert.ok(fs.existsSync(path.join(ws, 'out2', 'p.stl')) && fs.existsSync(path.join(ws, 'out2', 'p_2.stl')), '판이 나뉘지 않음');
|
||
assert.match(r.stdout, /\d개 파일/);
|
||
});
|
||
|
||
test('keep — 위치 그대로 합침', { timeout: 60_000 }, async () => {
|
||
const r: any = await stlCadTool.execute({ action: 'merge', stl_paths: ['a.stl', 'b.stl'], output_path: 'keep.stl', layout: 'keep', _workspacePath: ws });
|
||
assert.ok(r.success, r.error);
|
||
const bb = readStlBBox(path.join(ws, 'keep.stl'));
|
||
assert.ok(bb.max[0] >= 139.9, '원래 위치(x=140)가 유지되지 않음');
|
||
});
|
||
|
||
test('입력 검증 — 1개/없는 파일/output_path 없음', async () => {
|
||
const one: any = await stlCadTool.execute({ action: 'merge', stl_paths: ['a.stl'], output_path: 'x.stl', _workspacePath: ws });
|
||
assert.equal(one.success, false);
|
||
const miss: any = await stlCadTool.execute({ action: 'merge', stl_paths: ['a.stl', 'zz.stl'], output_path: 'x.stl', _workspacePath: ws });
|
||
assert.equal(miss.success, false);
|
||
assert.match(miss.error, /zz\.stl/);
|
||
const noOut: any = await stlCadTool.execute({ action: 'merge', stl_paths: ['a.stl', 'b.stl'], _workspacePath: ws });
|
||
assert.equal(noOut.success, false);
|
||
});
|
||
});
|
||
|
||
describe('모든 STL 바닥(z=0) 기본 규칙 — dropStlToFloor + transform 기본 align_bottom', () => {
|
||
const ws = fs.mkdtempSync(path.join(os.tmpdir(), 'stl-floor-'));
|
||
const mk = (name: string, scad: string) => {
|
||
const f = path.join(ws, name + '.scad');
|
||
fs.writeFileSync(f, scad);
|
||
const r = spawnSync(OPENSCAD_BIN, ['-o', path.join(ws, name + '.stl'), '--export-format=binstl', f], { timeout: 60_000 });
|
||
assert.equal(r.status, 0, `fixture 실패: ${r.stderr}`);
|
||
};
|
||
test.before(() => {
|
||
mk('buried', 'translate([0,0,-20]) cube([10,10,10]);'); // z:[-20,-10]
|
||
mk('floating', 'translate([0,0,15]) cube([10,10,10]);'); // z:[15,25]
|
||
mk('floor', 'cube([10,10,10]);');
|
||
});
|
||
|
||
test('파묻힌 STL(z<0)을 z=0으로', async () => {
|
||
const { dropStlToFloor } = await import('../src/tools/stl-cad-core');
|
||
const r: any = dropStlToFloor(path.join(ws, 'buried.stl'));
|
||
assert.ok(r.ok && Math.abs(r.shifted - 20) < 0.01);
|
||
assert.ok(Math.abs(readStlBBox(path.join(ws, 'buried.stl')).min[2]) < 0.001);
|
||
});
|
||
test('떠 있는 STL(z>0)도 z=0으로, XY는 불변', async () => {
|
||
const { dropStlToFloor } = await import('../src/tools/stl-cad-core');
|
||
const before = readStlBBox(path.join(ws, 'floating.stl'));
|
||
const r: any = dropStlToFloor(path.join(ws, 'floating.stl'));
|
||
assert.ok(r.ok && Math.abs(r.shifted + 15) < 0.01);
|
||
const after = readStlBBox(path.join(ws, 'floating.stl'));
|
||
assert.ok(Math.abs(after.min[2]) < 0.001 && Math.abs(after.min[0] - before.min[0]) < 1e-3 && Math.abs(after.max[1] - before.max[1]) < 1e-3);
|
||
});
|
||
test('이미 바닥이면 shifted 0, ASCII는 ok:false', async () => {
|
||
const { dropStlToFloor } = await import('../src/tools/stl-cad-core');
|
||
const r: any = dropStlToFloor(path.join(ws, 'floor.stl'));
|
||
assert.ok(r.ok && r.shifted === 0);
|
||
fs.writeFileSync(path.join(ws, 'ascii.stl'), 'solid x\nfacet normal 0 0 1\nouter loop\nvertex 0 0 5\nvertex 1 0 5\nvertex 0 1 5\nendloop\nendfacet\nendsolid x\n');
|
||
const a: any = dropStlToFloor(path.join(ws, 'ascii.stl'));
|
||
assert.equal(a.ok, false);
|
||
});
|
||
test('transform은 기본으로 바닥에 붙인다(align_bottom 생략), false면 안 붙인다', { timeout: 120_000 }, async () => {
|
||
mk('t1', 'translate([0,0,30]) cube([10,10,10]);');
|
||
const r: any = await stlCadTool.execute({ action: 'transform', stl_path: 't1.stl', rz: 90, _workspacePath: ws });
|
||
assert.ok(r.success, r.error);
|
||
assert.ok(Math.abs(readStlBBox(path.join(ws, 't1.stl')).min[2]) < 0.05, '기본으로 바닥에 안 붙음');
|
||
mk('t2', 'translate([0,0,30]) cube([10,10,10]);');
|
||
const r2: any = await stlCadTool.execute({ action: 'transform', stl_path: 't2.stl', rz: 90, align_bottom: false, _workspacePath: ws });
|
||
assert.ok(r2.success);
|
||
assert.ok(readStlBBox(path.join(ws, 't2.stl')).min[2] > 25, 'align_bottom=false인데 붙음');
|
||
});
|
||
});
|