Files
Genarrative/src/components/image-editor/ImageCanvasRasterEditModel.test.ts
T
kdletters 387a1c26e3 完成全量检查整改
清理已跟踪原始日志与个人本地配置
修复前后端有效门禁、测试和构建问题
补齐生产 Jenkins、SpacetimeDB 本地命令与文档约束
收紧图片编辑器状态、附件与媒体引用测试
排除已下线旧创作入口测试并清理 warning
2026-07-17 16:56:46 +08:00

98 lines
2.2 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import {
buildCropExpandGeometry,
buildCropExpandInsetsFromFrame,
buildCropExpandLayerPatch,
parseCropExpandInsetValue,
} from './ImageCanvasRasterEditModel';
describe('ImageCanvasRasterEditModel', () => {
it('builds transparent expansion geometry around the source image', () => {
expect(
buildCropExpandGeometry({
width: 100,
height: 80,
insets: { left: 10, top: 6, right: 12, bottom: 8 },
}),
).toEqual({
outputWidth: 122,
outputHeight: 94,
sourceX: 0,
sourceY: 0,
sourceWidth: 100,
sourceHeight: 80,
targetX: 10,
targetY: 6,
});
});
it('builds crop geometry and moves the layer origin to the cropped bounds', () => {
const insets = { left: -10, top: -6, right: -12, bottom: -8 };
expect(
buildCropExpandGeometry({
width: 100,
height: 80,
insets,
}),
).toMatchObject({
outputWidth: 78,
outputHeight: 66,
sourceX: 10,
sourceY: 6,
sourceWidth: 78,
sourceHeight: 66,
targetX: 0,
targetY: 0,
});
expect(
buildCropExpandLayerPatch({
layerX: 50,
layerY: 40,
insets,
outputWidth: 78,
outputHeight: 66,
}),
).toEqual({
x: 60,
y: 46,
width: 78,
height: 66,
originalWidth: 78,
originalHeight: 66,
});
});
it('parses integer crop-expand values and rejects invalid input', () => {
expect(parseCropExpandInsetValue(' 12 ')).toBe(12);
expect(parseCropExpandInsetValue('')).toBe(0);
expect(() => parseCropExpandInsetValue('左')).toThrow('裁扩数值必须是整数');
});
it('converts a dragged canvas frame into transparent crop-expand insets', () => {
expect(
buildCropExpandInsetsFromFrame({
layer: {
x: 100,
y: 80,
width: 200,
height: 120,
originalWidth: 400,
originalHeight: 240,
},
frame: {
x: 90,
y: 90,
width: 240,
height: 90,
},
}),
).toEqual({
left: 20,
top: -20,
right: 60,
bottom: -40,
});
});
});