const assert = require('assert')
const fs = require('fs')
const path = require('path')
const vm = require('vm')

function loadComponentOptions(filePath) {
  const source = fs.readFileSync(filePath, 'utf8')
  const scriptMatch = source.match(/<script>([\s\S]*?)<\/script>/)

  if (!scriptMatch) {
    throw new Error('未找到组件脚本内容')
  }

  const importNames = []
  const importPattern = /^import\s+([A-Za-z0-9_]+)\s+from\s+.*$/gm
  let importMatch = importPattern.exec(scriptMatch[1])
  while (importMatch) {
    importNames.push(importMatch[1])
    importMatch = importPattern.exec(scriptMatch[1])
  }

  const stubImports = importNames.map(name => `const ${name} = {};`).join('\n')
const namedImportStubs = `
const listWorkflow = function() {};
const getWorkflowEdit = function() {};
const formatRate = function(value) { return value; };
const mapGetters = function(names) {
  const result = {};
  names.forEach((name) => {
    result[name] = function() { return ''; };
  });
  return result;
};`
  const transformed = `${stubImports}\n${namedImportStubs}\n${scriptMatch[1]}`
    .replace(/^import .*$/gm, '')
    .replace(/export default/, 'module.exports =')

  const sandbox = {
    module: { exports: {} },
    exports: {},
    require,
    console
  }

  vm.runInNewContext(transformed, sandbox, { filename: filePath })
  return sandbox.module.exports
}

const filePath = path.resolve(__dirname, '../src/views/loanPricing/workflow/index.vue')
const component = loadComponentOptions(filePath)
const source = fs.readFileSync(filePath, 'utf8')

assert.strictEqual(typeof component.activated, 'function', '流程列表页应在激活时刷新数据')
assert(source.includes('icon="el-icon-edit"') && source.includes('>编辑</el-button>'), '流程列表操作列应包含编辑按钮')
assert(source.includes('v-if="canEdit(scope.row)"'), '流程列表编辑按钮应只对可编辑行显示')
assert(source.includes('row.createBy === this.currentCreateBy'), '流程列表应按创建者判断编辑按钮权限')
assert(source.includes('getWorkflowEdit(row.serialNum)'), '编辑操作应先查询编辑数据')
assert(source.includes('v-model="queryParams.custType"'), '流程列表应包含客户类型筛选')
assert(source.includes('v-model="queryParams.guarType"'), '流程列表应包含担保方式筛选')
assert(source.includes('v-model="dateRange"'), '流程列表应包含创建时间范围筛选')
assert(source.includes('value-format="yyyy-MM-dd"'), '流程列表创建时间筛选应使用日期值')
assert(source.includes('beginCreateTime: hasDateRange ? `${this.dateRange[0]} 00:00:00` : undefined'), '流程列表应传递当天零点的顶层创建时间起始参数')
assert(source.includes('endCreateTime: hasDateRange ? `${this.dateRange[1]} 23:59:59` : undefined'), '流程列表应传递当天末秒的顶层创建时间结束参数')
assert(source.includes('label="机构号"') && source.includes('prop="deptId"'), '流程列表应展示机构号')
assert(source.indexOf('label="机构号"') > source.indexOf('label="创建者"'), '机构号列应位于创建者列之后')

let refreshCount = 0
component.activated.call({
  getList() {
    refreshCount += 1
  }
})

assert.strictEqual(refreshCount, 1, '流程列表页激活时应调用一次 getList')

assert.strictEqual(component.methods.canEdit.call({
  currentCreateBy: '张三-8920001'
}, {
  createBy: '张三-8920001'
}), true, '创建者应允许编辑')

assert.strictEqual(component.methods.canEdit.call({
  currentCreateBy: '张三-8920001'
}, {
  createBy: '李四-8920002'
}), false, '非创建者不应允许编辑')

console.log('workflow-index-refresh test passed')
