/** 정규화 로직 회귀 검증 — node scripts/test-threshold-normalize.mjs */ function isThresholdOverridden(cond) { return cond.thresholdOverride === true; } function parseThresholdField(field) { if (!field?.startsWith('K_')) return null; const n = parseFloat(field.slice(2)); return Number.isFinite(n) ? n : null; } const DEFAULT = { RSI: { over: 70, mid: 50, under: 30 } }; function inferThresholdRoleForValue(value, indicatorType) { const key = indicatorType?.toUpperCase?.() ?? indicatorType; const th = DEFAULT[key] ?? {}; const eps = 0.0001; if (th.over != null && Math.abs(th.over - value) < eps) return 'HL_OVER'; if (th.mid != null && Math.abs(th.mid - value) < eps) return 'HL_MID'; if (th.under != null && Math.abs(th.under - value) < eps) return 'HL_UNDER'; return null; } function inferFromK(field, indicatorType) { if (!field?.startsWith('K_')) return null; const val = parseFloat(field.slice(2)); return inferThresholdRoleForValue(val, indicatorType); } function coerceTargetValue(raw) { if (raw == null) return null; if (typeof raw === 'number') return Number.isFinite(raw) ? raw : null; if (typeof raw === 'string') { const n = parseFloat(raw.trim()); return Number.isFinite(n) ? n : null; } return null; } function normalizeCondition(cond) { let next = { ...cond, indicatorType: cond.indicatorType?.toUpperCase?.() ?? cond.indicatorType }; const targetNum = coerceTargetValue(next.targetValue); if (isThresholdOverridden(next)) { if (targetNum != null) { const role = inferThresholdRoleForValue(targetNum, next.indicatorType); if (role) { const { targetValue, ...rest } = next; next = { ...rest, rightField: role, thresholdOverride: false }; } } else if (next.rightField?.startsWith('K_')) { const role = inferFromK(next.rightField, next.indicatorType); if (role) { const { targetValue, ...rest } = next; next = { ...rest, rightField: role, thresholdOverride: false }; } } } if (!isThresholdOverridden(next) && next.rightField?.startsWith('K_')) { const role = inferFromK(next.rightField, next.indicatorType); if (role) next = { ...next, rightField: role, thresholdOverride: false }; } return next; } function isConditionCarrier(node) { return !!node.condition && (node.type === 'CONDITION' || node.type == null); } function normalizeNode(node) { let next = { ...node }; if (isConditionCarrier(node)) { next = { ...node, type: node.type ?? 'CONDITION', condition: normalizeCondition(node.condition) }; } if (next.children?.length) { next.children = next.children.map(normalizeNode); } return next; } const cases = [ { type: 'CONDITION', condition: { indicatorType: 'RSI', rightField: 'K_50', thresholdOverride: true, targetValue: 50 } }, { condition: { indicatorType: 'RSI', rightField: 'K_50', thresholdOverride: true, targetValue: 50 } }, { type: 'TIMEFRAME', children: [{ condition: { indicatorType: 'RSI', rightField: 'K_50', thresholdOverride: true, targetValue: 50 } }] }, ]; for (const c of cases) { const r = normalizeNode(c); const cond = r.condition ?? r.children?.[0]?.condition; const ok = cond?.rightField === 'HL_MID' && cond?.thresholdOverride === false; console.log(ok ? 'PASS' : 'FAIL', JSON.stringify(c).slice(0, 60), '=>', cond?.rightField, cond?.thresholdOverride); }