const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs/promises'); const os = require('node:os'); const path = require('node:path'); const { normalizeSealPlacementHtml, preparePdfRenderSource, pdfPathForSource } = require('../erp_pdf_conversion'); test('preparePdfRenderSource copies Word-compatible HTML .doc files to .html for Chromium rendering', async (t) => { const tmp = await fs.mkdtemp(path.join(os.tmpdir(), 'erp-pdf-')); t.after(() => fs.rm(tmp, { recursive: true, force: true })); const sourcePath = path.join(tmp, 'LW-260722A-A_confirm.doc'); const html = '确认单'; await fs.writeFile(sourcePath, html, 'utf8'); const prepared = await preparePdfRenderSource(sourcePath, { renderDir: path.join(tmp, 'render-html'), }); assert.equal(prepared.originalPath, sourcePath); assert.equal(prepared.isHtml, true); assert.notEqual(prepared.renderPath, sourcePath); assert.equal(path.extname(prepared.renderPath), '.html'); assert.equal(await fs.readFile(prepared.renderPath, 'utf8'), html); }); test('preparePdfRenderSource leaves non-HTML .doc files unchanged', async (t) => { const tmp = await fs.mkdtemp(path.join(os.tmpdir(), 'erp-pdf-')); t.after(() => fs.rm(tmp, { recursive: true, force: true })); const sourcePath = path.join(tmp, 'binary.doc'); await fs.writeFile(sourcePath, Buffer.from([0xd0, 0xcf, 0x11, 0xe0, 0x00])); const prepared = await preparePdfRenderSource(sourcePath, { renderDir: path.join(tmp, 'render-html'), }); assert.equal(prepared.originalPath, sourcePath); assert.equal(prepared.renderPath, sourcePath); assert.equal(prepared.isHtml, false); }); test('normalizeSealPlacementHtml shifts absolute ERP seal spans into the printable page', () => { const html = [ '', '', 'zhang', '', '', ].join(''); const normalized = normalizeSealPlacementHtml(html); assert.match(normalized, /margin-left:140px/i); assert.match(normalized, /bei2\.png/); }); test('normalizeSealPlacementHtml anchors Xingyou seal near first-page party B block', () => { const html = [ '', '

乙方:海南星游国际旅行社有限公司

', '

乙方联系方式:0898-12345678

', '
', '', 'zhang', '', '', ].join(''); const normalized = normalizeSealPlacementHtml(html, { sealAnchor: 'party-b-first-page' }); assert.match(normalized, /data-erp-seal-normalized="party-b-first-page"/); assert.match(normalized, /margin-top:0/i); assert.match(normalized, /vertical-align:middle/i); assert.ok(normalized.indexOf('xy.png') > normalized.indexOf('乙方联系方式')); }); test('pdfPathForSource replaces document or html extensions with .pdf', () => { assert.equal(pdfPathForSource('C:\\tmp\\order.doc'), 'C:\\tmp\\order.pdf'); assert.equal(pdfPathForSource('C:\\tmp\\order.html'), 'C:\\tmp\\order.pdf'); assert.equal(pdfPathForSource('C:\\tmp\\order.htm'), 'C:\\tmp\\order.pdf'); });