import assert from 'node:assert/strict' import { readFile } from 'node:fs/promises' import { test } from 'node:test' import * as Vue from 'vue' import { parse } from '@vue/compiler-sfc' import { compile } from '@vue/compiler-dom' import { renderToString } from '@vue/server-renderer' // 单独编译真实日期列和其插槽,验证绑定字段与历史空值展示,不检索源码字符串。 async function renderDateColumns(resource, row) { const file = new URL(`../src/views/report/${resource}/index.vue`, import.meta.url) const { descriptor } = parse(await readFile(file, 'utf8')) const { ast } = compile(descriptor.template.content) const columns = [] function visit(node) { if (node.tag === 'el-table-column' && node.props.some(prop => prop.name === 'prop' && ['productionDate', 'expiryDate'].includes(prop.value?.content))) { columns.push(node.loc.source) } for (const child of node.children || []) visit(child) } visit(ast) assert.equal(columns.length, 2, `${resource} 应展示生产日期和有效期至`) const render = new Function('Vue', compile(`
${columns.join('')}
`, { mode: 'function', prefixIdentifiers: true }).code)(Vue) const app = Vue.createSSRApp({ render }) app.component('el-table-column', { props: ['prop', 'label'], setup(props, { slots }) { return () => Vue.h('section', { 'data-column': props.prop, 'aria-label': props.label }, slots.default ? slots.default({ row }) : String(row[props.prop] ?? '')) } }) return renderToString(app) } for (const resource of ['palletInventory', 'consumption', 'return', 'palletTrace']) { test(`${resource} 展示接口的原装填日期,历史 null 为未登记`, async () => { const dated = await renderDateColumns(resource, { productionDate: '2025-12-31', expiryDate: '2026-12-31', occurredAt: '2026-10-08T23:59:59', updatedAt: '2026-10-08T23:59:59' }) assert.match(dated, /2025-12-31/) assert.match(dated, /2026-12-31/) assert.doesNotMatch(dated, /2026-10-08|00:00:00/) const legacy = await renderDateColumns(resource, { productionDate: null, expiryDate: null, occurredAt: '2026-10-08T23:59:59', updatedAt: '2026-10-08T23:59:59' }) assert.equal((legacy.match(/未登记/g) || []).length, 2) assert.doesNotMatch(legacy, /2026-10-08/) }) }