'use strict'; const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const vm = require('node:vm'); /** * @param {string} source - 页面源码 * @param {string} name - 函数名 * @returns {string} 可独立执行的函数源码 */ function extractFunction(source, name) { const pattern = new RegExp('function ' + name + '\\('); const match = pattern.exec(source); assert.ok(match, '缺少方法 ' + name); const end = source.indexOf('\n/**', match.index + match[0].length); return source.slice(match.index, end < 0 ? source.length : end); } const sourcePath = path.resolve(__dirname, '../src/timecard-repair.oyd.jsx'); const source = fs.readFileSync(sourcePath, 'utf8'); const methods = vm.runInNewContext( ['toTwoDigitText', 'resolveRepairMonth'] .map((name) => extractFunction(source, name)).join('\n') + '\n({resolveRepairMonth})', ); assert.deepEqual( { ...methods.resolveRepairMonth('', new Date(2026, 9, 21)) }, { year: 2026, month: 9 }, '未传月份时应默认上一个月', ); assert.deepEqual( { ...methods.resolveRepairMonth('202610', new Date(2026, 9, 21)) }, { year: 2026, month: 9 }, '传入当前月时应回退到上一个月', ); assert.deepEqual( { ...methods.resolveRepairMonth('202608', new Date(2026, 9, 21)) }, { year: 2026, month: 8 }, '显式历史月份应保持不变', ); assert.deepEqual( { ...methods.resolveRepairMonth('202701', new Date(2027, 0, 10)) }, { year: 2026, month: 12 }, '一月份回退时应正确跨年', ); console.log('PASS 补填页面默认月份解析');