|
| 1 | +/** |
| 2 | + * photoCrop 坐标换算测试(v0.11.7 图文截屏)。 |
| 3 | + * |
| 4 | + * @ai-context: DPI 关键路径——归一化坐标 × 物理像素;覆盖边界(0/1)、 |
| 5 | + * 越界(clamp)、非整数(取整)、零宽(防御 1px)。 |
| 6 | + */ |
| 7 | +import { describe, expect, it } from "vitest"; |
| 8 | +import { normToPixels } from "./photoCrop"; |
| 9 | + |
| 10 | +describe("normToPixels", () => { |
| 11 | + it("标准区域按物理像素换算", () => { |
| 12 | + // 1920×1080 全屏截图,框选左上 1/4 |
| 13 | + const r = normToPixels(0, 0, 0.5, 0.5, 1920, 1080); |
| 14 | + expect(r).toEqual({ x: 0, y: 0, w: 960, h: 540 }); |
| 15 | + }); |
| 16 | + |
| 17 | + it("DPI 150% 缩放下仍按物理像素(不经 devicePixelRatio)", () => { |
| 18 | + // 逻辑窗口 1280×720 → 物理 1920×1080;归一化坐标不变 |
| 19 | + const r = normToPixels(0.25, 0.25, 0.5, 0.5, 1920, 1080); |
| 20 | + expect(r).toEqual({ x: 480, y: 270, w: 960, h: 540 }); |
| 21 | + }); |
| 22 | + |
| 23 | + it("越界输入 clamp 到图像内", () => { |
| 24 | + const r = normToPixels(-0.5, 1.5, 2, 2, 100, 100); |
| 25 | + expect(r).toEqual({ x: 0, y: 100, w: 100, h: 100 }); |
| 26 | + }); |
| 27 | + |
| 28 | + it("非整数坐标取整(含四舍五入)", () => { |
| 29 | + const r = normToPixels(0.333, 0.333, 0.333, 0.333, 1000, 1000); |
| 30 | + expect(r).toEqual({ x: 333, y: 333, w: 333, h: 333 }); |
| 31 | + }); |
| 32 | + |
| 33 | + it("零宽/负宽防御为至少 1 像素", () => { |
| 34 | + const r = normToPixels(0, 0, 0, 0, 800, 600); |
| 35 | + expect(r.w).toBe(1); |
| 36 | + expect(r.h).toBe(1); |
| 37 | + }); |
| 38 | +}); |
0 commit comments