44 lines
2.9 KiB
JavaScript
44 lines
2.9 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { readFileSync } from 'node:fs';
|
|
import ts from 'typescript';
|
|
|
|
const compiled = ts.transpileModule(readFileSync(new URL('../src/sourceWorkbookDetection.ts', import.meta.url), 'utf8'), {
|
|
compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS },
|
|
}).outputText;
|
|
const module = { exports: {} };
|
|
new Function('exports', compiled)(module.exports);
|
|
const { detectSourceWorkbook } = module.exports;
|
|
const fields = Object.fromEntries(Object.entries({ black: ['序号', '成品规格', '送电镀重量', '责废总重量'], bright: ['序号', '成品规格', '合格重量', '是否电镀来料/二等品库来料'] })
|
|
.map(([stage, names]) => [stage, names.map(name => ({ name, required: true }))]));
|
|
const workbook = (sheetName, headers, sheets) => ({ uploadId: 'upload', sourceFile: '10月.xlsx', sheets: sheets.map(sheetName => ({ sheetName })), fields,
|
|
preview: { sheetName, headerRow: 1, headers, sampleRows: [] } });
|
|
|
|
test('bright detail following a summary is selected from its business columns', async () => {
|
|
const sheets = ['合格率报表', '明细', '成品报废'];
|
|
const calls = [];
|
|
const detail = workbook('明细', fields.bright.map(x => x.name), sheets);
|
|
const result = await detectSourceWorkbook(workbook(sheets[0], ['成品质量统计表(汇总)'], sheets), fields, async (_, params) => { calls.push(params); return detail; });
|
|
assert.equal(result.stage, 'bright'); assert.equal(result.workbook, detail); assert.equal(calls.length, 1);
|
|
assert.equal(calls[0].sheetName, '明细');
|
|
});
|
|
|
|
test('black specification summary is rejected in favor of the complete detail', async () => {
|
|
const sheets = ['黑片报废(按规格)', '报废明细'];
|
|
const result = await detectSourceWorkbook(workbook(sheets[0], ['成品规格', '合格重量'], sheets), fields,
|
|
async () => workbook(sheets[1], fields.black.map(x => x.name), sheets));
|
|
assert.equal(result.stage, 'black'); assert.equal(result.workbook.preview.sheetName, '报废明细');
|
|
});
|
|
|
|
test('a complete first sheet needs no further reads; incomplete sheets remain incomplete', async () => {
|
|
const complete = workbook('明细', fields.bright.map(x => x.name), ['明细', '汇总']);
|
|
assert.equal((await detectSourceWorkbook(complete, fields, async () => { throw Error('unexpected request'); })).workbook, complete);
|
|
const partial = workbook('汇总', ['成品规格'], ['汇总', '其他']);
|
|
const result = await detectSourceWorkbook(partial, fields, async () => workbook('其他', [], ['汇总', '其他']));
|
|
assert.equal(result.workbook, partial); assert.equal(result.workbook.preview.headers.length, 1);
|
|
});
|
|
|
|
test('preview errors propagate instead of silently offering the wrong worksheet', async () => {
|
|
await assert.rejects(detectSourceWorkbook(workbook('汇总', [], ['汇总', '明细']), fields, async () => { throw Error('读取失败'); }), /读取失败/);
|
|
});
|