Files
zszq-trs/YLErpWeb/fe-tests/unwindSwapTrade.test.js
T
tengyufan 4987962aca #EQD-6597 国联民生-簿记时新增基本费率字段(开平仓费用),并且自动计算
feat: 完善互换平仓交易费用自动计算

- 支持百分比和单位数量模式自动计算平仓交易费用
- 支持全部平仓和部分平仓联动重算
- 保持手动互换不自动填充平仓交易费用
- 补充前后端相关单测
2026-07-27 11:57:20 +08:00

93 lines
3.0 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const vm = require('vm');
function createNumberFormat(precision) {
const formatter = (value) => Number(Number(value || 0).toFixed(precision));
formatter.precision = precision;
return formatter;
}
function loadUnwindHelpers() {
const filePath = path.join(__dirname, '../wwwroot/Scripts/app/swaptrade/unwindSwapTrade.js');
const code = fs.readFileSync(filePath, 'utf8') + '\nmodule.exports = { swapPosiFeeCalc, consPosiFeeType };';
const stockEqvNotional = createNumberFormat(2);
const sandbox = {
module: { exports: {} },
exports: {},
console,
require,
window: { otcformat: { options: {} } },
otcformat: {
options: {},
trading: {
premiumRateP: { precision: 4 },
tradePrice: { precision: 4 },
notional: { precision: 6 },
StockEqvNotional: stockEqvNotional,
marginRateP: { precision: 4 },
umpriceP: { precision: 4 }
},
fixed6: createNumberFormat(6)
},
model: {
ValueDate: '2026-07-27',
FlowEvents: [],
StructureType: '',
TradeStartDate: ''
},
isUseApproval: false,
Vue: function (options) { return options; },
FastVue: {
vueDatePicker() { return {}; },
vueNumberInput() { return {}; }
},
tradeHelper: { IsBond() { return false; } },
main: {
post() {
return {
done() { return this; }
};
},
message() { }
},
SwapCalc: {
roundHalfAwayFromZero(value) { return value; },
calcCloseQtyByOriginalPercent() { return 0; }
},
_: {
round(value, precision) {
return Number(Number(value || 0).toFixed(precision || 0));
}
}
};
sandbox.window.otcformat = sandbox.otcformat;
vm.runInNewContext(code, sandbox, { filename: filePath });
return sandbox.module.exports;
}
function expectClose(actual, expected, tolerance) {
expect(Math.abs(actual - expected)).toBeLessThanOrEqual(tolerance || 1e-6);
}
describe('unwindSwapTrade 基础费率计算', () => {
const { swapPosiFeeCalc, consPosiFeeType } = loadUnwindHelpers();
test('百分比模式按平仓名义本金计算并保留两位', () => {
const result = swapPosiFeeCalc.calcTradingFee(consPosiFeeType.Percent, 0.1234, 1000000, 5000);
expectClose(result, 1234.00);
});
test('单位数量模式按平仓数量计算并保留两位', () => {
const result = swapPosiFeeCalc.calcTradingFee(consPosiFeeType.Unit, 1.235, 1000000, 10);
expectClose(result, 12.35);
});
test('未知模式默认按百分比模式处理', () => {
const result = swapPosiFeeCalc.calcTradingFee(99, 0.1, 200000, 10);
expectClose(result, 200.00);
});
});