Files
goldenChart/frontend/scripts/test-threshold-normalize.mjs
T
2026-06-18 00:47:58 +09:00

69 lines
2.3 KiB
JavaScript

/** 정규화 로직 회귀 검증 — node scripts/test-threshold-normalize.mjs */
function parseThresholdField(field) {
if (!field?.startsWith('K_')) return null;
const n = parseFloat(field.slice(2));
return Number.isFinite(n) ? n : null;
}
function ensureDirectThresholdSnapshot(cond) {
const indicatorType = cond.indicatorType?.toUpperCase?.() ?? cond.indicatorType;
let next = { ...cond, indicatorType };
for (const key of ['rightField', 'leftField']) {
const f = next[key];
if (!f?.startsWith('K_')) continue;
const parsed = parseThresholdField(f);
if (parsed == null) continue;
if (next.targetValue == null || !Number.isFinite(next.targetValue)) {
next = { ...next, targetValue: parsed, thresholdOverride: true };
} else if (next.thresholdOverride !== true) {
next = { ...next, thresholdOverride: true };
}
}
return next;
}
function setDirectThreshold(cond, side, value) {
const fieldKey = side === 'left' ? 'leftField' : 'rightField';
return {
...cond,
[fieldKey]: `K_${value}`,
targetValue: value,
thresholdOverride: true,
};
}
function setConditionThreshold(cond, value, directInput) {
if (directInput) return setDirectThreshold(cond, 'right', value);
return setDirectThreshold(cond, 'right', value);
}
let failed = 0;
function assertOk(label, ok) {
if (!ok) failed++;
console.log(ok ? 'PASS' : 'FAIL', label);
}
const k55 = ensureDirectThresholdSnapshot({
indicatorType: 'RSI',
rightField: 'K_55',
thresholdOverride: true,
targetValue: 55,
});
assertOk('K_55 preserved', k55.rightField === 'K_55' && k55.targetValue === 55 && k55.thresholdOverride === true);
const k50 = ensureDirectThresholdSnapshot({
indicatorType: 'RSI',
rightField: 'K_50',
thresholdOverride: true,
targetValue: 50,
});
assertOk('K_50 preserved (not HL_MID)', k50.rightField === 'K_50' && k50.thresholdOverride === true);
const direct55 = setConditionThreshold({ indicatorType: 'RSI', rightField: 'HL_MID', thresholdOverride: false }, 55, true);
assertOk('directInput 55 => K_55', direct55.rightField === 'K_55' && direct55.targetValue === 55);
const filled = ensureDirectThresholdSnapshot({ indicatorType: 'RSI', rightField: 'K_55', thresholdOverride: false });
assertOk('K_55 fills targetValue', filled.targetValue === 55 && filled.thresholdOverride === true);
process.exit(failed > 0 ? 1 : 0);