39 lines
2.4 KiB
JavaScript
39 lines
2.4 KiB
JavaScript
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 core = (() => { try { return require('../src/exporter.cjs'); } catch (e) { if (e.code === 'MODULE_NOT_FOUND') return {}; throw e; } })();
|
|
const metadata = { order: '900001', project: 'DEMO-PROJECT-001', model: 'DEMO-MODEL', serial: 'DEMO-CHANGE-001' };
|
|
|
|
test('filename uses all four identifiers in the agreed order', () => {
|
|
assert.equal(typeof core.buildFilename, 'function', 'export core is not implemented');
|
|
assert.equal(core.buildFilename(metadata), '900001_DEMO-PROJECT-001_DEMO-MODEL_DEMO-CHANGE-001.pdf');
|
|
});
|
|
test('missing identifiers stop export instead of silently guessing', () => {
|
|
assert.equal(typeof core.buildFilename, 'function');
|
|
assert.throws(() => core.buildFilename({ ...metadata, project: ' ' }), /项目/);
|
|
});
|
|
test('filename cannot escape output directory or exceed Windows limits', () => {
|
|
assert.equal(typeof core.buildFilename, 'function');
|
|
const filename = core.buildFilename({ ...metadata, order: '../../900:001', project: 'A'.repeat(300) });
|
|
assert.equal(path.basename(filename), filename);
|
|
assert.ok(!/[<>:"/\\|?*\x00-\x1f]/.test(filename));
|
|
assert.ok(filename.length <= 200);
|
|
assert.ok(filename.endsWith('_DEMO-MODEL_DEMO-CHANGE-001.pdf'));
|
|
});
|
|
test('offline rendering creates PDF bytes and repeated exports preserve previous file', async () => {
|
|
assert.equal(typeof core.renderSnapshot, 'function');
|
|
const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'order-tool-test-'));
|
|
try {
|
|
const snapshot = { metadata, html: '<form><h1>订单通知书 900001</h1><p>DEMO-PROJECT-001</p><script>location.href="https://example.com"</script><img src="https://example.com/private.png"></form>', styles: ['body{font-size:16px}'], width: 1264 };
|
|
const first = await core.renderSnapshot(snapshot, { outputDir: dir });
|
|
const original = await fs.readFile(first.path);
|
|
assert.equal(original.subarray(0, 5).toString(), '%PDF-');
|
|
const second = await core.renderSnapshot(snapshot, { outputDir: dir });
|
|
assert.equal(path.basename(second.path), '900001_DEMO-PROJECT-001_DEMO-MODEL_DEMO-CHANGE-001 (2).pdf');
|
|
assert.deepEqual(await fs.readFile(first.path), original);
|
|
assert.ok(first.bytes > 1000);
|
|
} finally { await fs.rm(dir, { recursive: true, force: true }); }
|
|
});
|