/* 421 추천 창 「종단 반영」 셈 헬퍼 — `_Initial_Model` 을 그 자리에서 트랜스파일해 Node 로 돌려 * 종단 반영 값이 서버 답 · 보낸 값 · 옛 캐시 · 서버 기록에서 어떻게 정해지는지 JSON 한 벌로. */ const fs = require("fs"); const path = require("path"); const ts = require(path.join(__dirname, "..", "..", "config", "node_modules", "typescript")); const ROOT = path.join(__dirname, "..", ".."); const cache = new Map(); 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; }, _map: map, }; } const session = memoryStorage(); globalThis.window = { localStorage: memoryStorage(), sessionStorage: session }; globalThis.fetch = () => Promise.resolve({ ok: false }); const STUBS = { "@ui/ui_template_palette": { themeColor: (_name, fallback) => fallback } }; 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.endsWith(".css")) return {}; if (request.startsWith("@ui/")) return loadTs(path.join(ROOT, "ui_template", `${request.slice(4)}.ts`)); 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 model = loadTs(path.join(ROOT, "B05_Profile", "B05_Profile_UI_RouteEdit_Initial_Model.ts")); const answer = { nodes: [ { x: 0, y: 0 }, { x: 10, y: 5 }, ], curves: [], }; const fromSent = model.normalizeRouteInitial(answer, "earthwork", null, null, false); const fromServer = model.normalizeRouteInitial( { ...answer, with_profile: true, unsnapped: { planned: [[0, 0]] } }, "earthwork", null, null, false, ); const oldFollow = model.normalizeRouteInitial(answer, "follow", null); const oldEarth = model.normalizeRouteInitial(answer, "earthwork", null); const base = model.serverBase({ logic: "gentle", with_profile: false }); const oldBase = model.serverBase({ logic: "gentle" }); model.writeInitialChoice("p421", fromSent); const readBack = model.readInitialChoice("p421"); process.stdout.write( JSON.stringify({ sent: fromSent.withProfile, server: fromServer.withProfile, serverHasUnsnapped: "unsnapped" in fromServer, oldFollow: model.choiceWithProfile(oldFollow), oldEarth: model.choiceWithProfile(oldEarth), base: base.withProfile, oldBase: oldBase.withProfile ?? null, baseProfile: model.choiceWithProfile(base), oldBaseProfile: model.choiceWithProfile(oldBase), sameOff: model.sameBase({ ...fromSent, logic: "gentle" }, base), sameOn: model.sameBase({ ...fromServer, logic: "gentle" }, base), sameOld: model.sameBase({ ...fromServer, logic: "gentle" }, oldBase), readBack: readBack && readBack.withProfile, }), );