import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import ts from 'typescript'; import React from 'react'; import { tableTextAlignment, alignReportTableText } from '../src/tableTextAlignment.ts'; const source = fs.readFileSync(new URL('../src/QualityTable.tsx', import.meta.url), 'utf8'); const helper = source.slice(source.indexOf('function titleText'), source.indexOf('function compactColumns')).replaceAll('export function ', 'function '); const alignTableColumns = new Function('isValidElement', 'tableTextAlignment', ts.transpileModule(helper + '\nreturn alignTableColumns;', { compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.ESNext }, }).outputText)(React.isValidElement, tableTextAlignment); test('prose columns align left even with short or empty current values; names and categorical attributes stay centered', () => { for (const name of ['说明', '失败原因', '计算口径', '当前计算公式', '特殊情况处理', '原始行数据', '差异项', '阈值配置', 'description', 'error_message']) { assert.equal(tableTextAlignment(name), 'left', name); } for (const name of ['产品名称', '说明类型', '证据状态', '错误码', '属性', '来源文件', '月份', 'status']) { assert.equal(tableTextAlignment(name, ['很长的名称'.repeat(20)]), 'center', name); } assert.equal(tableTextAlignment('自定义列', ['短', '这是一段需要左对齐的较长业务文本。'.repeat(3)]), 'left'); assert.equal(tableTextAlignment('自定义列', ['首行\n下一行']), 'left'); assert.equal(tableTextAlignment('重量', [123, null]), 'center'); }); test('grouped and dynamic source columns preserve numeric alignment, callbacks and original definitions', () => { const render = () => { throw new Error('classification must not execute cell renderers'); }; const columns = [{ title: '分组', children: [ { title: React.createElement('span', null, '当前计算公式'), align: 'center', className: 'original', render }, { title: '自定义', dataIndex: ['raw', '含.标点'], render }, { title: '产品名称', dataIndex: 'name' }, { title: '重量', dataIndex: 'weight', align: 'right' }, ] }]; const result = alignTableColumns(columns, [{ raw: { '含.标点': '详细记录'.repeat(20) }, name: '名称'.repeat(30), weight: 42 }]); assert.deepEqual(result[0].children.map(column => column.align), ['left', 'left', 'center', 'right']); assert.equal(result[0].children[0].render, render); assert.equal(result[0].children[0].className, 'original quality-table-text-column'); assert.equal(columns[0].children[0].align, 'center'); }); test('native report prose cells and their headers align left without changing names or numbers', () => { const cell = (text, tagName = 'TD') => ({ textContent: text, tagName, colSpan: 1, rowSpan: 1, style: { setProperty(...args) { this.applied = args; } } }); const header = { cells: [cell('产品名称', 'TH'), cell('备注', 'TH'), cell('重量', 'TH')] }; const row = { cells: [cell('产品甲'), cell('需现场核实'), cell('120')] }; const table = { rows: [header, row], tHead: { rows: [header] } }; alignReportTableText({ querySelectorAll: () => [table] }); assert.deepEqual(header.cells[1].style.applied, ['text-align', 'left', 'important']); assert.deepEqual(row.cells[1].style.applied, ['text-align', 'left', 'important']); assert.equal(row.cells[0].style.applied, undefined); assert.equal(row.cells[2].style.applied, undefined); });