071faa482c
纳入 AGC Cargo workspace 的统一 rustfmt 检查与格式化入口 完成项目 TypeScript/Prettier 与 Rust 全量格式化 修复 Pingora expected executable 门禁的空白敏感误报 同步开发运维文档与 AGC skill pack 格式化忽略规则
191 lines
3.3 KiB
TypeScript
191 lines
3.3 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
|
|
import { probeImageFileDimensions } from './ImageCanvasFileModel';
|
|
|
|
function makeFile(bytes: number[], name: string, type: string) {
|
|
return new File([new Uint8Array(bytes)], name, { type });
|
|
}
|
|
|
|
function uint16be(value: number) {
|
|
return [(value >> 8) & 0xff, value & 0xff];
|
|
}
|
|
|
|
function uint32be(value: number) {
|
|
return [
|
|
(value >> 24) & 0xff,
|
|
(value >> 16) & 0xff,
|
|
(value >> 8) & 0xff,
|
|
value & 0xff,
|
|
];
|
|
}
|
|
|
|
function uint16le(value: number) {
|
|
return [value & 0xff, (value >> 8) & 0xff];
|
|
}
|
|
|
|
function uint24le(value: number) {
|
|
return [value & 0xff, (value >> 8) & 0xff, (value >> 16) & 0xff];
|
|
}
|
|
|
|
function uint32le(value: number) {
|
|
return [
|
|
value & 0xff,
|
|
(value >> 8) & 0xff,
|
|
(value >> 16) & 0xff,
|
|
(value >> 24) & 0xff,
|
|
];
|
|
}
|
|
|
|
describe('ImageCanvasFileModel', () => {
|
|
it('probes common upload image dimensions from file headers', async () => {
|
|
const png = makeFile(
|
|
[
|
|
0x89,
|
|
0x50,
|
|
0x4e,
|
|
0x47,
|
|
0x0d,
|
|
0x0a,
|
|
0x1a,
|
|
0x0a,
|
|
...uint32be(13),
|
|
0x49,
|
|
0x48,
|
|
0x44,
|
|
0x52,
|
|
...uint32be(1536),
|
|
...uint32be(1024),
|
|
8,
|
|
6,
|
|
0,
|
|
0,
|
|
0,
|
|
],
|
|
'画布.png',
|
|
'image/png',
|
|
);
|
|
const jpeg = makeFile(
|
|
[
|
|
0xff,
|
|
0xd8,
|
|
0xff,
|
|
0xe0,
|
|
...uint16be(16),
|
|
0x4a,
|
|
0x46,
|
|
0x49,
|
|
0x46,
|
|
0,
|
|
1,
|
|
1,
|
|
0,
|
|
0,
|
|
1,
|
|
0,
|
|
1,
|
|
0,
|
|
0,
|
|
0xff,
|
|
0xc0,
|
|
...uint16be(17),
|
|
8,
|
|
...uint16be(720),
|
|
...uint16be(1280),
|
|
3,
|
|
0,
|
|
0,
|
|
0,
|
|
0,
|
|
0,
|
|
0,
|
|
0,
|
|
0,
|
|
0,
|
|
],
|
|
'画布.jpg',
|
|
'image/jpeg',
|
|
);
|
|
const gif = makeFile(
|
|
[
|
|
0x47,
|
|
0x49,
|
|
0x46,
|
|
0x38,
|
|
0x39,
|
|
0x61,
|
|
...uint16le(640),
|
|
...uint16le(480),
|
|
0,
|
|
0,
|
|
0,
|
|
],
|
|
'画布.gif',
|
|
'image/gif',
|
|
);
|
|
const webp = makeFile(
|
|
[
|
|
0x52,
|
|
0x49,
|
|
0x46,
|
|
0x46,
|
|
...uint32le(22),
|
|
0x57,
|
|
0x45,
|
|
0x42,
|
|
0x50,
|
|
0x56,
|
|
0x50,
|
|
0x38,
|
|
0x58,
|
|
...uint32le(10),
|
|
0,
|
|
0,
|
|
0,
|
|
0,
|
|
...uint24le(799),
|
|
...uint24le(599),
|
|
],
|
|
'画布.webp',
|
|
'image/webp',
|
|
);
|
|
const bmp = makeFile(
|
|
[
|
|
0x42,
|
|
0x4d,
|
|
...uint32le(54),
|
|
0,
|
|
0,
|
|
0,
|
|
0,
|
|
...uint32le(54),
|
|
...uint32le(40),
|
|
...uint32le(300),
|
|
...uint32le(200),
|
|
],
|
|
'画布.bmp',
|
|
'image/bmp',
|
|
);
|
|
|
|
await expect(probeImageFileDimensions(png)).resolves.toEqual({
|
|
width: 1536,
|
|
height: 1024,
|
|
});
|
|
await expect(probeImageFileDimensions(jpeg)).resolves.toEqual({
|
|
width: 1280,
|
|
height: 720,
|
|
});
|
|
await expect(probeImageFileDimensions(gif)).resolves.toEqual({
|
|
width: 640,
|
|
height: 480,
|
|
});
|
|
await expect(probeImageFileDimensions(webp)).resolves.toEqual({
|
|
width: 800,
|
|
height: 600,
|
|
});
|
|
await expect(probeImageFileDimensions(bmp)).resolves.toEqual({
|
|
width: 300,
|
|
height: 200,
|
|
});
|
|
});
|
|
});
|