Files
juzi/taobao/test/report-side-zoom.test.cjs
T
xsl 774376fa16 feat: 完善淘宝小程序拍照、报告与登录流程
接入本地实时识别和自动三连拍,调整报告裁切、商品跳转、分享标题及居中页眉;完善游客会话、后台渠道隔离、部署脚本与回归测试。
2026-11-04 15:07:51 +08:00

34 lines
2.2 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const zoom = require('../utils/report-side-zoom');
test('side crop reduces fill by 20 percent, centers horizontally and moves down by 5 percent of photo height', () => {
for (const [width, height] of [[720, 1280], [1280, 720], [1000, 1000]]) {
const rect = zoom.rectangle(width, height);
assert(Math.abs(rect.left + rect.width / 2 - 208.7 / 2) < 1e-8);
assert(Math.abs(rect.top + rect.height / 2 - 184.5 / 2 - rect.height * .05) < 1e-8);
const previousScale = Math.max(208.7 / width, 184.5 / height) * 1.4;
assert(Math.abs(rect.width / (width * previousScale) - .8) < 1e-8);
assert(Math.abs(rect.height / (height * previousScale) - .8) < 1e-8);
}
});
test('side zoom bakes a single bitmap with the same draw rectangle for photo and its own annotation', async () => {
const calls = [], jpeg = 'data:image/jpeg;base64,/9j/2Q==';
const context = { clearRect(...args) { calls.push(['clear', ...args]); },
setFillStyle(...args) { calls.push(['color', ...args]); }, fillRect(...args) { calls.push(['fill', ...args]); },
drawImage(...args) { calls.push(['image', ...args]); }, draw(keep, done) { assert.equal(keep, false); done(); },
toDataURL(options) { options.success(jpeg); return Promise.resolve(jpeg); } };
const platform = { createCanvasContext(id) { assert.equal(id, 'report-side-zoom-canvas'); return context; } };
const result = await zoom.bake(platform, { photo: 'side-photo', width: 720, height: 1280, dark: .55, layers: ['side-layer'] });
assert.equal(result, jpeg);
const rect = zoom.rectangle(720, 1280), expected = [rect.left * 4, rect.top * 4, rect.width * 4, rect.height * 4];
assert.deepEqual(calls.filter(row => row[0] === 'image'), [['image', 'side-photo', ...expected], ['image', 'side-layer', ...expected]]);
assert.equal(zoom.rectangle(0, 1280), null);
});
test('unsupported or failed native exports reject instead of returning an aggressively cropped native image', async () => {
await assert.rejects(zoom.bake({}, { photo: 'side', width: 720, height: 1280 }));
await assert.rejects(zoom.bake({ createCanvasContext() { throw new Error('unsupported'); } }, { width: 720, height: 1280 }));
});