/* 설계 가게 · 저장 한 줄기(계획서 24-2a) 단위검증 헬퍼 — * `A00_Common/design/design_store.ts` · `design_save.ts` · `B05_Profile_UI_Profile_Edit.ts` 를 * 그 자리에서 트랜스파일해 Node 로 돌린다. 서버 창구는 가짜로 두고 부른 차례만 적는다. */ const fs = require("fs"); const path = require("path"); const ts = require(path.join(__dirname, "..", "..", "config", "node_modules", "typescript")); const ROOT = path.join(__dirname, "..", ".."); function memoryStorage() { const map = new Map(); return { getItem: (key) => (map.has(key) ? map.get(key) : null), setItem: (key, value) => map.set(key, String(value)), removeItem: (key) => map.delete(key), key: (index) => [...map.keys()][index] ?? null, get length() { return map.size; }, }; } const session = memoryStorage(); globalThis.window = { sessionStorage: session, localStorage: memoryStorage() }; globalThis.sessionStorage = session; const calls = []; const record = (name, fail) => async () => { calls.push(name); if (fail) throw new Error(`${name} 실패`); }; let failStructures = false; const STUBS = { "@ui/ui_template_elements": { showToast: (text, kind) => calls.push(`toast:${kind}`), showLoadingOverlay: () => calls.push("overlay:on"), hideLoadingOverlay: () => calls.push("overlay:off"), }, "../../B05_Profile/B05_Profile_Api_Pipes_Draft": { flushPendingPipes: () => record("pipes")(), }, "../../B05_Profile/B05_Profile_Api_Structures": { flushPendingStructures: () => record("structures", failStructures)(), }, "../../B05_Profile/B05_Profile_Api_Fetch": { flushUphillOverrides: () => record("uphill")(), saveProfileAlignment: async (_p, routeId, edits) => calls.push(`alignment:${routeId}:${JSON.stringify(edits.station_offsets)}`), }, "../../B06_Section/B06_Section_Api_Culvert_Options": { flushCulvertOptions: async (_p, key) => calls.push(`culvert:${key}`), }, }; const cache = new Map(); function loadTs(file) { const full = path.resolve(file); if (cache.has(full)) return cache.get(full); const js = ts.transpileModule(fs.readFileSync(full, "utf8"), { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 }, }).outputText; const box = { exports: {} }; cache.set(full, box.exports); const localRequire = (request) => { if (STUBS[request]) return STUBS[request]; if (request.startsWith(".")) return loadTs(path.join(path.dirname(full), `${request}.ts`)); return require(request); }; new Function("exports", "module", "require", js)(box.exports, box, localRequire); cache.set(full, box.exports); return box.exports; } const { useDesignStore } = loadTs(path.join(ROOT, "A00_Common", "design", "design_store.ts")); const edit = loadTs(path.join(ROOT, "B05_Profile", "B05_Profile_UI_Profile_Edit.ts")); const { saveDesign, DESIGN_SAVE_ORDER } = loadTs( path.join(ROOT, "A00_Common", "design", "design_save.ts"), ); async function run() { const out = {}; // ① 가게 — 등록 키 밑 · 같은 칸이면 두 가게가 같은 값 · 듣는 쪽 const a = useDesignStore("p1", 7); const b = useDesignStore("p1", 99); // 프로젝트 칸은 노선과 무관 const heard = []; const off = b.subscribe("pipes", (value) => heard.push(value.length)); a.set("pipes", [{ chainage_m: 10, source: "user" }]); out.pipesShared = b.get("pipes"); out.pipesKey = [...Array(session.length).keys()].map((i) => session.key(i)); off(); a.set("pipes", [{ chainage_m: 10 }, { chainage_m: 20 }]); out.heard = heard; out.otherProject = useDesignStore("p2", 7).get("pipes"); out.noRoute = useDesignStore("p1", null).get("alignmentDraft"); a.set("selection", { chainageM: 12.5, structureId: "s-1" }); out.selection = b.get("selection"); a.set("pipes", null); a.set("selection", null); // ② 계획고 가게 한 벌 — B05 패널 · B06 계획선 편집이 같은 벌 const saved = { station_offsets: {}, curve_radii: { "40.000": 30 } }; const b05 = edit.createProfileEditStore(7, saved, () => undefined); const b06 = edit.createProfileEditStore(7, saved, () => undefined); b06.replace({ station_offsets: { "20.000": 0.5 }, curve_radii: saved.curve_radii }); out.b05SeesB06 = b05.edits().station_offsets; out.b05Dirty = b05.dirty(); // B05 가 자기 손에 쥔 값으로 이어 고쳐도 B06 편집이 안 사라짐(옛 두 가게 흠) b05.replace({ station_offsets: { ...b05.edits().station_offsets, "60.000": -0.2 }, curve_radii: b05.edits().curve_radii, }); out.afterBoth = b06.edits().station_offsets; out.draft = useDesignStore(null, 7).get("alignmentDraft").station_offsets; out.pending = edit.pendingAlignment(7).station_offsets; // 되돌리기가 초안을 바꾼 뒤 B05 가 다시 세움 — B06 쪽 손잡이도 같은 벌을 봄 useDesignStore(null, 7).set("alignmentDraft", { station_offsets: { "20.000": 0.1 }, curve_radii: {}, }); edit.createProfileEditStore(7, saved, () => undefined); out.rebuiltSeenByB06 = b06.edits().station_offsets; // ③ 저장 한 줄기 — 차례 · 기본 창구 · 보기 몫 · 계획고 저장됨 표시 calls.length = 0; const ok = await saveDesign({ projectId: "p1", routeId: () => 7, prepare: record("prepare"), steps: { pipes: record("pipes:screen") }, cross: async (confirm) => calls.push(`cross:${confirm}`), done: async (confirm) => calls.push(`done:${confirm}`), failed: () => calls.push("failed"), }); out.saveOk = ok; out.saveCalls = [...calls]; out.afterSave = { dirty: b05.dirty(), draft: useDesignStore(null, 7).get("alignmentDraft") }; out.order = DESIGN_SAVE_ORDER; // ④ 구조물이 실패해도 다음 차례로(알림) · 확정 · 노선 없으면 안 나감 calls.length = 0; failStructures = true; await saveDesign( { projectId: "p1", routeId: () => 7, steps: { uphill: record("uphill:screen") }, cross: async (confirm) => calls.push(`cross:${confirm}`), failed: () => calls.push("failed"), }, { confirm: true }, ); failStructures = false; out.tolerant = [...calls]; calls.length = 0; out.noRouteSave = await saveDesign({ projectId: "p1", routeId: () => null, cross: async () => calls.push("cross"), failed: () => calls.push("failed"), }); out.noRouteCalls = [...calls]; // ⑤ [초기화] — 초안 · 한 벌 모두 버림 b05.replace({ station_offsets: { "80.000": 1 }, curve_radii: {} }); edit.clearAlignmentDrafts(); out.cleared = { draft: useDesignStore(null, 7).get("alignmentDraft"), pending: edit.pendingAlignment(7), }; return out; } run().then( (out) => console.log(JSON.stringify(out)), (error) => { console.error(error); process.exit(1); }, );