'use strict'; // 拖边缘改大小的几何用例(纯计算,不需要 Electron): // node test/resize.test.js // 无边框窗口里这段代码只有真鼠标拖拽才能触发,先用纯函数把它钉死,Windows 上就只剩「手感」要试。 const { computeBounds, MIN_SIZE } = require('../renderer/resize'); let passed = 0; const failures = []; function check(name, ok, detail) { if (ok) { passed += 1; console.log(`PASS ${name}${detail ? ` [${detail}]` : ''}`); } else { failures.push(name); console.log(`FAIL ${name}${detail ? ` [${detail}]` : ''}`); } } const start = { x: 400, y: 300, width: 520, height: 280 }; const same = (a, b) => a.x === b.x && a.y === b.y && a.width === b.width && a.height === b.height; console.log('--- 单边 ---'); check('东边 +120:只有宽度变,左上角不动', same(computeBounds(start, 'e', 120, 0), { x: 400, y: 300, width: 640, height: 280 })); check('南边 +60:只有高度变', same(computeBounds(start, 's', 0, 60), { x: 400, y: 300, width: 520, height: 340 })); check('西边 -80:宽度增加 80,左边界左移 80', same(computeBounds(start, 'w', -80, 0), { x: 320, y: 300, width: 600, height: 280 })); check('北边 -50:高度增加 50,上边界上移 50', same(computeBounds(start, 'n', 0, -50), { x: 400, y: 250, width: 520, height: 330 })); check('东边往回拖 -100:窗口变窄,左上角不动', same(computeBounds(start, 'e', -100, 0), { x: 400, y: 300, width: 420, height: 280 })); console.log('--- 四角 ---'); check('右下角 +40/+30:宽高同时增加', same(computeBounds(start, 'se', 40, 30), { x: 400, y: 300, width: 560, height: 310 })); check('左上角 -40/-30:宽高增加且整体左上移', same(computeBounds(start, 'nw', -40, -30), { x: 360, y: 270, width: 560, height: 310 })); check('右上角 +40/-30:宽度增加、上边界上移', same(computeBounds(start, 'ne', 40, -30), { x: 400, y: 270, width: 560, height: 310 })); check('左下角 -40/+30:左边界左移、高度增加', same(computeBounds(start, 'sw', -40, 30), { x: 360, y: 300, width: 560, height: 310 })); console.log('--- 最小尺寸 ---'); check('东边往死里拖:宽度停在 260', computeBounds(start, 'e', -9999, 0).width === MIN_SIZE.width); check('南边往死里拖:高度停在 150', computeBounds(start, 's', 0, -9999).height === MIN_SIZE.height); { const r = computeBounds(start, 'w', 9999, 0); check('西边往死里拖:宽度停在 260,且右边界不动(窗口不会被推走)', r.width === MIN_SIZE.width && r.x + r.width === start.x + start.width, JSON.stringify(r)); } { const r = computeBounds(start, 'n', 0, 9999); check('北边往死里拖:高度停在 150,且下边界不动', r.height === MIN_SIZE.height && r.y + r.height === start.y + start.height, JSON.stringify(r)); } { const r = computeBounds(start, 'nw', 9999, 9999); check('左上角往死里拖:宽高都停在最小值,右下角不动', r.width === MIN_SIZE.width && r.height === MIN_SIZE.height && r.x + r.width === start.x + start.width && r.y + r.height === start.y + start.height, JSON.stringify(r)); } console.log('--- 边界 ---'); check('位移为 0 时原样返回', same(computeBounds(start, 'e', 0, 0), start)); check('结果都是整数(避免 setBounds 收到小数后抖动)', Number.isInteger(computeBounds(start, 'e', 12.4, 0).width)); check('负坐标(窗口在主屏左侧)也能算', same(computeBounds({ x: -900, y: -200, width: 520, height: 280 }, 'w', -100, 0), { x: -1000, y: -200, width: 620, height: 280 })); console.log(''); if (failures.length === 0) { console.log(`全部通过:${passed}/${passed}`); process.exit(0); } else { console.log(`通过 ${passed},失败 ${failures.length}:${failures.join(';')}`); process.exit(1); }