const assert = require("node:assert/strict"); const fs = require("node:fs"); const path = require("node:path"); const vm = require("node:vm"); const ts = require("typescript"); const { test } = require("node:test"); const { loadTypeScript } = require("./load-typescript.cjs"); const model = loadTypeScript( "src/features/gisManagement/model/upload-retry-guard.ts", ); const partialBody = { success: false, status: 500, code: "GIS_UPLOAD_HISTORY_SAVE_FAILED", uploadId: "GUP-1", msApplied: true, retryAllowed: false, }; test("부분 성공 body는 transport/Axios/wrapper 경계에서 구분되고 일반500은 제외", () => { for (const error of [ { body: partialBody }, { response: { data: partialBody } }, { body: { data: partialBody } }, ]) { assert.deepEqual(model.readGisPartialSuccess(error), { uploadId: "GUP-1" }); } for (const error of [ new Error("500"), { body: { ...partialBody, msApplied: false } }, { body: { ...partialBody, retryAllowed: true } }, { body: { code: partialBody.code } }, ]) { assert.equal(model.readGisPartialSuccess(error), null); } }); test("기관 전환·파일명과 무관하게 차단 유지, 같은/빈 downloadId는 해제하지 않음", () => { const guard = model.createGisUploadRetryGuard(); const block = guard.recordFailure("4173000", "D-1", { body: partialBody }); assert.equal(guard.getBlock("other"), null); assert.equal(guard.getBlock("4173000"), block); assert.equal(guard.acceptFreshDownload("4173000", ""), null); assert.equal(guard.acceptFreshDownload("4173000", "D-1"), null); assert.equal(guard.acceptFreshDownload("other", "D-2"), null); assert.equal(guard.getBlock("4173000"), block); assert.match(model.getGisPartialSuccessNotice(block), /GUP-1/); assert.match(model.getGisPartialSuccessNotice(block), /관리자/); }); test("같은 기관의 새 현재본 downloadId만 새 작업을 허용하며 이전 운영확인ID를 반환", () => { const guard = model.createGisUploadRetryGuard(); guard.recordFailure("4173000", "D-1", { body: partialBody }); const previous = guard.acceptFreshDownload("4173000", "D-2"); assert.deepEqual(previous, { uploadId: "GUP-1", downloadId: "D-1" }); assert.equal(guard.getBlock("4173000"), null); }); test("일반 오류는 기존 차단을 해제하거나 새 차단을 만들지 않음", () => { const guard = model.createGisUploadRetryGuard(); assert.equal(guard.recordFailure("scope", "D-1", new Error("500")), null); guard.recordFailure("scope", "D-1", { body: partialBody }); assert.equal(guard.recordFailure("scope", "D-2", new Error("network")), null); assert.equal(guard.getBlock("scope").uploadId, "GUP-1"); }); /** 실제 JSX 컴포넌트의 업로드 handler만 추출해 외부 HTTP를 모의하고 재호출 차단을 검증한다. */ test("실제 GIS 업로드 handler는 부분 성공 후 같은 세션 두 번째 POST를 보내지 않음", async () => { const filename = path.resolve( __dirname, "../app/(map-layout)/map/components/gisManagement/GisManagementPopupPage.tsx", ); const source = fs.readFileSync(filename, "utf8"); const ast = ts.createSourceFile( filename, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX, ); let initializer; function visit(node) { if ( ts.isVariableDeclaration(node) && node.name.getText(ast) === "handleUpload" ) initializer = node.initializer.getText(ast); ts.forEachChild(node, visit); } visit(ast); assert.ok(initializer); const output = ts.transpileModule(`globalThis.run = ${initializer};`, { compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS, }, }).outputText; const alerts = []; let posts = 0; const guard = model.createGisUploadRetryGuard(); const context = { console: { error() {} }, FormData, uploadRetryGuard: guard, getGisPartialSuccessNotice: model.getGisPartialSuccessNotice, selectedScopeKey: "scope", latestDownloadId: "D-1", selectedMngInstCd: "4173000", isManagementSelected: true, selectedFile: new Blob(["test"]), facility: { layerId: "tb_ms_swage_ppl" }, SCREEN_ID: "SN_MN010302", FACILITY_TYPE_CODE: "PPL", LAYER_ID: "tb_ms_swage_ppl", alert: (message) => alerts.push(message), setIsLoading() {}, setNotice() {}, postMutiPart: async () => { posts++; throw { body: partialBody }; }, }; vm.runInNewContext(output, context); await context.run(); context.selectedFile = new Blob(["renamed-file"]); await context.run(); assert.equal(posts, 1); assert.equal(alerts.length, 2); assert.match(alerts[1], /GUP-1/); });