Files
Aislo/resources/tester/helper_ts_stub_loader.cjs
T
eomsangdonandClaude Sonnet 5.5 026ca74c52 test(507-4): 검증 스크립트 정비 — 계산 자리 화면 시험 안전장치 · 505-3 교각점 요소 자리 · 검증_sub7 시험 12 파일 지금 검증 프로젝트로 · 공용 TS 불러오기 첫 걸음
- 309-2 화면 시험: 5177 이 8004 로 갈 때만 돎(남의 마스터 안 씀) · 실패해도 판 되돌림(finally) · 읽기는 서버 응답으로 판정
- 505-3 첫 시험: 픽셀 (1199,296) 대신 캔버스 교각점 색으로 찾음 · 포트 · 프로젝트는 환경 변수로
- 349 계열 · 346-2 · 356-4: helper_verify_project 가 OWNERS.md 검증_subN 중 가장 오래 안 바뀐 프로젝트를 읽기만 · 없으면 까닭 글로 건너뜀 · 낡은 기대 xfail
- helper_ts_stub_loader 공용 불러오기 + 새 시험 · 헬퍼 3 곳 옮김

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-21 16:02:51 +09:00

60 lines
2.8 KiB
JavaScript

/* 진짜 TS 를 CommonJS 로 트랜스파일해 부르는 공용 불러오기 + 가짜 모듈 끼워 넣기(507-4 · 새 시험부터 이걸 씀).
* 옛 헬퍼 198 벌이 저마다 베낀 `loadTs`(+ `STUBS`) 한 벌 — 진짜 모듈이 새 export · import 를 늘리면 헬퍼마다 낡아
* 깨졌음(482). 여기만 고치면 되게 모음.
*
* const { makeLoader, memoryStorage } = require("./helper_ts_stub_loader.cjs");
* const STUBS = {}; // 끝이 이 이름으로 끝나는 import 를 가짜로(헬퍼가 나중에 채움)
* const { load, loadTs } = makeLoader({ root: ROOT, stubs: STUBS, ui: () => anyModule() });
* const store = load("B06_Section", "B06_Section_Section_Store.ts");
*
* 풀이 순서: 카탈로그 · STUBS · @config(가짜 주소) · @ui(`ui` 쪽 값 · 기본 showToast) · .css · .json · @util · 상대 경로 · node.
*/
const fs = require("fs");
const path = require("path");
const ts = require(path.join(__dirname, "..", "..", "config", "node_modules", "typescript"));
const CATALOG = require("./helper_322_catalog_module.cjs");
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;
},
};
}
function makeLoader({ root, stubs = {}, ui = () => ({ showToast: () => undefined }) }) {
const cache = new Map();
function loadTs(full) {
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 (CATALOG.isCatalogRequest(request)) return CATALOG;
const key = Object.keys(stubs).find((name) => request.endsWith(name));
if (key) return stubs[key];
if (request.startsWith("@config/")) return { API_BASE_URL: "http://fake" };
if (request.startsWith("@ui/")) return ui();
if (request.endsWith(".css")) return {};
if (request.endsWith(".json")) return require(path.join(path.dirname(full), request));
if (request.startsWith("@util/"))
return loadTs(path.join(root, "common_util", `${request.slice(6)}.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);
return box.exports;
}
return { loadTs, load: (...parts) => loadTs(path.join(root, ...parts)) };
}
module.exports = { makeLoader, memoryStorage };