/** * gcodePath — 웹 슬라이서 결과 gcode 조회 위치 * * 사건(2026-09-30): 슬라이싱은 성공하는데 미리보기가 "gcode 불러오기 실패: HTTP 404". 저장은 * print3d/K2웹슬라이서/ 로 바뀌었는데(09-25 폴더 규칙 통일) 조회는 print3d/ 바로 아래만 봤다. */ 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 { gcodePath } from '../src/gateway/routes/routes-k2'; const ws = fs.mkdtempSync(path.join(os.tmpdir(), 'k2gcode-')); const newDir = path.join(ws, 'print3d', 'K2웹슬라이서'); const oldDir = path.join(ws, 'print3d'); fs.mkdirSync(newDir, { recursive: true }); describe('gcodePath', () => { test('새 위치(print3d/K2웹슬라이서/)의 파일을 찾는다 — 사건의 본체', () => { fs.writeFileSync(path.join(newDir, 'k2ui_1.gcode'), 'x'); assert.equal(gcodePath(ws, 'k2ui_1.gcode'), path.join(newDir, 'k2ui_1.gcode')); }); test('옛 위치(print3d/ 바로 아래)에만 있는 파일도 찾는다(폴더 규칙 통일 전 파일)', () => { fs.writeFileSync(path.join(oldDir, 'k2ui_2.gcode'), 'x'); assert.equal(gcodePath(ws, 'k2ui_2.gcode'), path.join(oldDir, 'k2ui_2.gcode')); }); test('두 곳에 모두 있으면 새 위치가 우선한다', () => { fs.writeFileSync(path.join(newDir, 'k2ui_3.gcode'), 'new'); fs.writeFileSync(path.join(oldDir, 'k2ui_3.gcode'), 'old'); assert.equal(gcodePath(ws, 'k2ui_3.gcode'), path.join(newDir, 'k2ui_3.gcode')); }); test('어디에도 없으면 새 위치 경로를 돌려준다(호출부가 existsSync로 404 처리)', () => { const p = gcodePath(ws, 'k2ui_nope.gcode'); assert.equal(p, path.join(newDir, 'k2ui_nope.gcode')); assert.equal(fs.existsSync(p), false); }); test('경로 이탈·잘못된 이름은 거부한다', () => { assert.throws(() => gcodePath(ws, '../secret.gcode'), /잘못된 gcode 파일명/); assert.throws(() => gcodePath(ws, 'a/b.gcode'), /잘못된 gcode 파일명/); assert.throws(() => gcodePath(ws, 'x.txt'), /잘못된 gcode 파일명/); }); }); import { saveGcodeToProject } from '../src/gateway/routes/routes-k2'; describe('saveGcodeToProject — 슬라이싱 결과를 프로젝트 출력/ 폴더로 복사', () => { const proj = path.join(ws, 'workshop', 'proj_abc'); fs.mkdirSync(proj, { recursive: true }); fs.writeFileSync(path.join(newDir, 'k2ui_10.gcode'), 'G1 X1'); test('읽을 수 있는 이름으로 출력/ 에 복사하고 원본은 남긴다', () => { const r = saveGcodeToProject(ws, 'k2ui_10.gcode', 'proj_abc', 'body_a_서포트'); assert.equal(r.ok, true); if (r.ok) { assert.equal(r.relPath, '출력/body_a_서포트.gcode'); assert.equal(fs.readFileSync(path.join(proj, '출력', 'body_a_서포트.gcode'), 'utf8'), 'G1 X1'); } assert.equal(fs.existsSync(path.join(newDir, 'k2ui_10.gcode')), true); }); test('같은 이름이면 덮어쓰지 않고 시각을 붙인다', () => { const r = saveGcodeToProject(ws, 'k2ui_10.gcode', 'proj_abc', 'body_a_서포트', 111); assert.equal(r.ok && r.name, 'body_a_서포트_111.gcode'); }); test('이름이 비었거나 .gcode·경로 문자가 섞여도 안전한 이름으로 만든다', () => { const empty = saveGcodeToProject(ws, 'k2ui_10.gcode', 'proj_abc', ' '); assert.equal(empty.ok && empty.name, 'k2ui_10.gcode'); const evil = saveGcodeToProject(ws, 'k2ui_10.gcode', 'proj_abc', '../../etc/x.gcode'); assert.equal(evil.ok, true); if (evil.ok) assert.equal(evil.name.includes('/'), false); }); test('없는 gcode·없는 프로젝트·잘못된 id는 거부한다', () => { const a = saveGcodeToProject(ws, 'k2ui_nope.gcode', 'proj_abc', 'x'); assert.equal(!a.ok && a.status, 404); const b = saveGcodeToProject(ws, 'k2ui_10.gcode', 'proj_none', 'x'); assert.equal(!b.ok && b.status, 404); const c = saveGcodeToProject(ws, 'k2ui_10.gcode', '../x', 'x'); assert.equal(!c.ok && c.status, 400); const d = saveGcodeToProject(ws, '../k2ui_10.gcode', 'proj_abc', 'x'); assert.equal(!d.ok && d.status, 400); }); }); import { ensureOutputFolder } from '../src/gateway/routes/workshop-layout'; describe('ensureOutputFolder — 출력/ 폴더가 파일 탭에 보이게 항상 만든다', () => { test('없으면 만들고, 있으면 파일을 건드리지 않는다', () => { const w = fs.mkdtempSync(path.join(os.tmpdir(), 'k2out-')); fs.mkdirSync(path.join(w, 'workshop', 'proj_z'), { recursive: true }); const d = ensureOutputFolder(w, 'proj_z'); assert.equal(d, path.join(w, 'workshop', 'proj_z', '출력')); assert.equal(fs.statSync(d).isDirectory(), true); fs.writeFileSync(path.join(d, 'keep.gcode'), 'x'); assert.equal(ensureOutputFolder(w, 'proj_z'), d); assert.equal(fs.readFileSync(path.join(d, 'keep.gcode'), 'utf8'), 'x'); }); }); import { resolveProjectGcode, moonrakerGcodeName } from '../src/gateway/routes/routes-k2'; describe('resolveProjectGcode — 파일 탭 G-code 미리보기·업로드 경로 검증', () => { const w = fs.mkdtempSync(path.join(os.tmpdir(), 'k2proj-')); fs.mkdirSync(path.join(w, 'workshop', 'proj_a', '출력'), { recursive: true }); fs.mkdirSync(path.join(w, 'secret'), { recursive: true }); fs.writeFileSync(path.join(w, 'workshop', 'proj_a', '출력', 'x.gcode'), 'G1'); fs.writeFileSync(path.join(w, 'secret', 'leak.gcode'), 'SECRET'); test('프로젝트 안의 gcode는 절대경로로 돌려준다', () => { assert.equal(resolveProjectGcode(w, 'workshop/proj_a/출력/x.gcode'), path.join(w, 'workshop', 'proj_a', '출력', 'x.gcode')); }); test('.gcode가 아니거나 작업실 밖이거나 ../ 가 섞인 경로는 거부한다', () => { for (const bad of ['workshop/proj_a/출력/x.txt', 'secret/leak.gcode', 'workshop/proj_a/../../secret/leak.gcode', '/etc/passwd.gcode', 'workshop//x.gcode', 'workshop/proj_a/./x.gcode', '', 'workshop/proj a/x.gcode']) { assert.throws(() => resolveProjectGcode(w, bad), undefined, bad); } }); test('심볼릭 링크로 작업실 밖 파일을 가리키면 거부한다', () => { fs.symlinkSync(path.join(w, 'secret', 'leak.gcode'), path.join(w, 'workshop', 'proj_a', '출력', 'link.gcode')); assert.throws(() => resolveProjectGcode(w, 'workshop/proj_a/출력/link.gcode'), /작업실 폴더 밖/); }); }); describe('moonrakerGcodeName', () => { test('한글·공백은 _로, 확장자는 .gcode로 정리한다', () => { assert.equal(moonrakerGcodeName('body_a_외3_2026-09-30_1318.gcode'), 'body_a_3_2026-09-30_1318.gcode'); assert.equal(moonrakerGcodeName('집진 커버.gcode'), 'model.gcode'); assert.equal(moonrakerGcodeName('plate.v2.gcode'), 'plate.v2.gcode'); }); });