const assert = require('node:assert/strict') const fs = require('node:fs') const path = require('node:path') const test = require('node:test') const { parse, compileScript } = require('@vue/compiler-sfc') const ts = require('typescript') const vue = require('vue') const { renderToString } = require('vue/server-renderer') const filename = path.join(__dirname, '../src/views/first_visit/doctor_dashboard/index.vue') const { descriptor, errors } = parse(fs.readFileSync(filename, 'utf8'), { filename }) assert.deepEqual(errors, []) const compiled = compileScript(descriptor, { id: 'doctor-dashboard', inlineTemplate: true, templateOptions: { ssr: true } }) function compile(content) { return ts.transpileModule(content, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText } const outputText = compile(compiled.content) function loadPage(code, imports) { const mod = { exports: {} } new Function('require', 'module', 'exports', code)(id => imports[id] || require(id), mod, mod.exports) return mod.exports.default } const light = { surface: '#ffffff', ink: '#323251', border: '#e2e8ee', muted: '#62718b', grid: '#e6eaeb', accentInk: '#5d87ff', accentText: '#4569bf' } const dark = { surface: '#161618', ink: '#e3e3e8', border: '#383840', muted: '#9999a9', grid: '#323238', accentInk: '#ba9aff', accentText: '#d0c3ff' } async function renderDashboard(values, palette = null) { const skin = vue.shallowRef(palette) const stub = { render: () => null } let option const Chart = { props: ['option'], render() { option = this.option; return null } } const imports = { vue: { ...vue, onMounted: vue.onServerPrefetch }, '@/api/first_visit': { firstVisitDoctorDashboardOverview: async () => ({ summary: { registration_total: 18, appointment_total: 12 }, rankings: { amounts: values.map((value, i) => ({ doctor_id: i + 1, name: '医生' + i, value })), conversion: [] }, trend: { labels: ['12-01', '12-02'], amounts: [0, 10000] } }) }, '@/hooks/useStatsChartPalette': { useStatsChartPalette: () => skin }, '@element-plus/icons-vue': new Proxy({}, { get: () => stub }), 'element-plus': {}, 'vue-echarts': { default: Chart } } const app = vue.createSSRApp(loadPage(outputText, imports)) for (const [, tag] of descriptor.template.content.matchAll(/<(el-[\w-]+)/g)) { if (!app.component(tag)) app.component(tag, stub) } app.directive('loading', {}) const html = await renderToString(app) return { html, option } } test('挂号和预约在各自的指标下显示接口原值', async () => { const { html } = await renderDashboard([]) const cards = [...html.matchAll(/
]*>(.*?)<\/article>/gs)] .map(([, body]) => body.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ')) assert.equal(cards.length, 6) assert.match(cards[0], /总挂号 18 /) assert.match(cards[5], /预约完成率 .*总预约 12/) }) for (const [values, widths] of [ [[10000, 100, 0], ['100%', '1%', '0%']], [[0, 0], ['0%', '0%']], [['10', 0.025, -1, 'invalid'], ['100%', '0.25%', '0%', '0%']] ]) { test('金额排行按真实比例并保留顺序:' + values.join('/'), async () => { const { html } = await renderDashboard(values) const rows = [...html.matchAll(/
]*>]*>(\d+)<\/b>.*?style="width:([^;"]+).*?]*>([^<]+)<\/strong>/gs)] assert.deepEqual(rows.map(([,rank,width]) => [Number(rank), width]), widths.map((w,i) => [i+1, w])) if (values[0] === 10000) assert.deepEqual(rows.map(r => r[3]), ['¥10,000.00', '¥100.00', '¥0.00']) }) } for (const [name, skin] of [['浅色', light], ['暗色自选主色', dark]]) { test(name + '趋势的实际序列、坐标和 tooltip 使用主题,业务数值不变', async () => { const { option } = await renderDashboard([], skin) const [amount] = option.series assert.equal(amount.name, '成交金额') assert.deepEqual([...amount.data], [0, 10000]) assert.equal(amount.lineStyle.color, skin.accentInk) assert.equal(amount.itemStyle.color, skin.accentInk) assert.equal(amount.areaStyle.color, skin.accentInk) assert.equal(option.xAxis.axisLabel.color, skin.muted) assert.equal(option.yAxis.splitLine.lineStyle.color, skin.grid) assert.equal(option.tooltip.textStyle.color, skin.ink) assert.equal(option.tooltip.backgroundColor, skin.surface) }) } test('经典版保留既有趋势和提示框外观', async () => { const { option } = await renderDashboard([]) assert.equal(option.series[0].lineStyle.color, '#139a8c') assert.equal(option.series[0].itemStyle.color, '#ffffff') assert.equal(option.tooltip.backgroundColor, undefined) }) // Exercise the compiled page's computed option on one ECharts instance, not a remount per color. test('原地切色后 ECharts 实际绘制新色,切回经典色也能还原', () => { const echarts = require('echarts') const skin = vue.shallowRef(light) const page = loadPage(compile(compileScript(descriptor, { id: 'doctor-dashboard' }).content), { vue: { ...vue, onMounted() {} }, '@/api/first_visit': {}, 'vue-echarts': {}, 'element-plus': {}, '@element-plus/icons-vue': {}, '@/hooks/useStatsChartPalette': { useStatsChartPalette: () => skin } }) const { dashboard, trendChartOption } = page.setup({}, { expose() {} }) dashboard.trend.labels = ['12-01', '12-02'] dashboard.trend.amounts = [25, 100] const plot = echarts.init(null, null, { ssr: true, renderer: 'svg', width: 500, height: 200 }) try { for (const current of [light, dark, null]) { skin.value = current plot.setOption({ ...trendChartOption.value, animation: false }, { notMerge: true }) const color = current?.accentInk ?? '#139a8c' assert.equal(plot.getOption().series[0].lineStyle.color, color) assert.ok(plot.renderToSVGString().includes('stroke="' + color + '"')) } } finally { plot.dispose() } })