This commit is contained in:
2026-05-08 11:44:42 +08:00
parent b08127031c
commit abf1f1ebea
249 changed files with 39411 additions and 887 deletions

View File

@@ -4,6 +4,9 @@ import { afterEach, describe, expect, test, vi } from 'vitest';
import {
PUZZLE_REFERENCE_IMAGE_MAX_DATA_URL_LENGTH,
cropPuzzleReferenceImageDataUrl,
isPuzzleReferenceImageSquare,
readPuzzleReferenceImageForUpload,
readPuzzleReferenceImageAsDataUrl,
} from './puzzleReferenceImage';
@@ -115,3 +118,53 @@ describe('readPuzzleReferenceImageAsDataUrl', () => {
);
});
});
describe('puzzle reference image square crop helpers', () => {
test('reports square upload dimensions without opening crop flow', async () => {
const sourceDataUrl = 'data:image/png;base64,square';
stubFileReader(sourceDataUrl);
stubImage(512, 512);
const file = new File(['x'], 'square.png', { type: 'image/png' });
const result = await readPuzzleReferenceImageForUpload(file);
expect(result).toEqual({
dataUrl: sourceDataUrl,
width: 512,
height: 512,
});
expect(isPuzzleReferenceImageSquare(result)).toBe(true);
});
test('crops non-square uploads to a centered square data URL', async () => {
const sourceDataUrl = 'data:image/png;base64,wide';
const croppedDataUrl = 'data:image/jpeg;base64,cropped';
stubImage(800, 600);
const { drawImage, toDataURL } = stubCanvas([
`data:image/jpeg;base64,${'A'.repeat(30)}`,
croppedDataUrl,
`data:image/jpeg;base64,${'B'.repeat(40)}`,
]);
const dataUrl = await cropPuzzleReferenceImageDataUrl({
source: sourceDataUrl,
cropX: 100,
cropY: 0,
cropSize: 600,
});
expect(dataUrl).toBe(croppedDataUrl);
expect(drawImage).toHaveBeenCalledWith(
expect.anything(),
100,
0,
600,
600,
0,
0,
600,
600,
);
expect(toDataURL).toHaveBeenCalledWith('image/jpeg', 0.88);
});
});