mizzkey/packages/backend/test/unit/FileInfoService.ts

238 lines
5.9 KiB
TypeScript
Raw Normal View History

2022-09-17 20:27:08 +02:00
process.env.NODE_ENV = 'test';
import * as assert from 'assert';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';
2022-09-17 20:27:08 +02:00
import { ModuleMocker } from 'jest-mock';
import { Test } from '@nestjs/testing';
import { GlobalModule } from '@/GlobalModule.js';
import { FileInfoService } from '@/core/FileInfoService.js';
import { DI } from '@/di-symbols.js';
import { AiService } from '@/core/AiService.js';
import type { TestingModule } from '@nestjs/testing';
import type { jest } from '@jest/globals';
import type { MockFunctionMetadata } from 'jest-mock';
const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
2022-09-17 20:27:08 +02:00
const resources = `${_dirname}/../resources`;
const moduleMocker = new ModuleMocker(global);
describe('FileInfoService', () => {
let app: TestingModule;
let fileInfoService: FileInfoService;
beforeAll(async () => {
app = await Test.createTestingModule({
imports: [
GlobalModule,
],
providers: [
AiService,
FileInfoService,
],
})
.useMocker((token) => {
//if (token === AiService) {
// return { };
//}
if (typeof token === 'function') {
const mockMetadata = moduleMocker.getMetadata(token) as MockFunctionMetadata<any, any>;
const Mock = moduleMocker.generateFromMetadata(mockMetadata);
return new Mock();
}
})
.compile();
app.enableShutdownHooks();
fileInfoService = app.get<FileInfoService>(FileInfoService);
});
afterAll(async () => {
await app.close();
});
2023-02-02 10:18:25 +01:00
test('Empty file', async () => {
2022-09-17 20:27:08 +02:00
const path = `${resources}/emptyfile`;
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
delete info.warnings;
2020-10-18 09:03:51 +02:00
delete info.blurhash;
delete info.sensitive;
2022-07-08 04:21:11 +02:00
delete info.porn;
assert.deepStrictEqual(info, {
size: 0,
md5: 'd41d8cd98f00b204e9800998ecf8427e',
type: {
mime: 'application/octet-stream',
2022-05-21 15:21:41 +02:00
ext: null,
},
width: undefined,
height: undefined,
orientation: undefined,
});
2022-09-17 20:27:08 +02:00
});
2023-02-02 10:18:25 +01:00
test('Generic JPEG', async () => {
2022-09-17 20:27:08 +02:00
const path = `${resources}/Lenna.jpg`;
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
delete info.warnings;
2020-10-18 09:03:51 +02:00
delete info.blurhash;
delete info.sensitive;
2022-07-08 04:21:11 +02:00
delete info.porn;
assert.deepStrictEqual(info, {
size: 25360,
md5: '091b3f259662aa31e2ffef4519951168',
type: {
mime: 'image/jpeg',
2022-05-21 15:21:41 +02:00
ext: 'jpg',
},
width: 512,
height: 512,
orientation: undefined,
});
2022-09-17 20:27:08 +02:00
});
2023-02-02 10:18:25 +01:00
test('Generic APNG', async () => {
2022-09-17 20:27:08 +02:00
const path = `${resources}/anime.png`;
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
delete info.warnings;
2020-10-18 09:03:51 +02:00
delete info.blurhash;
delete info.sensitive;
2022-07-08 04:21:11 +02:00
delete info.porn;
assert.deepStrictEqual(info, {
size: 1868,
md5: '08189c607bea3b952704676bb3c979e0',
type: {
mime: 'image/apng',
2022-05-21 15:21:41 +02:00
ext: 'apng',
},
width: 256,
height: 256,
orientation: undefined,
});
2022-09-17 20:27:08 +02:00
});
2023-02-02 10:18:25 +01:00
test('Generic AGIF', async () => {
2022-09-17 20:27:08 +02:00
const path = `${resources}/anime.gif`;
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
delete info.warnings;
2020-10-18 09:03:51 +02:00
delete info.blurhash;
delete info.sensitive;
2022-07-08 04:21:11 +02:00
delete info.porn;
assert.deepStrictEqual(info, {
size: 2248,
md5: '32c47a11555675d9267aee1a86571e7e',
type: {
mime: 'image/gif',
2022-05-21 15:21:41 +02:00
ext: 'gif',
},
width: 256,
height: 256,
orientation: undefined,
});
2022-09-17 20:27:08 +02:00
});
2023-02-02 10:18:25 +01:00
test('PNG with alpha', async () => {
2022-09-17 20:27:08 +02:00
const path = `${resources}/with-alpha.png`;
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
delete info.warnings;
2020-10-18 09:03:51 +02:00
delete info.blurhash;
delete info.sensitive;
2022-07-08 04:21:11 +02:00
delete info.porn;
assert.deepStrictEqual(info, {
size: 3772,
md5: 'f73535c3e1e27508885b69b10cf6e991',
type: {
mime: 'image/png',
2022-05-21 15:21:41 +02:00
ext: 'png',
},
width: 256,
height: 256,
orientation: undefined,
});
2022-09-17 20:27:08 +02:00
});
2023-02-02 10:18:25 +01:00
test('Generic SVG', async () => {
2022-09-17 20:27:08 +02:00
const path = `${resources}/image.svg`;
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
delete info.warnings;
2020-10-18 09:03:51 +02:00
delete info.blurhash;
delete info.sensitive;
2022-07-08 04:21:11 +02:00
delete info.porn;
assert.deepStrictEqual(info, {
size: 505,
md5: 'b6f52b4b021e7b92cdd04509c7267965',
type: {
mime: 'image/svg+xml',
2022-05-21 15:21:41 +02:00
ext: 'svg',
},
width: 256,
height: 256,
orientation: undefined,
});
2022-09-17 20:27:08 +02:00
});
2023-02-02 10:18:25 +01:00
test('SVG with XML definition', async () => {
2021-03-24 04:23:05 +01:00
// https://github.com/misskey-dev/misskey/issues/4413
2022-09-17 20:27:08 +02:00
const path = `${resources}/with-xml-def.svg`;
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
delete info.warnings;
2020-10-18 09:03:51 +02:00
delete info.blurhash;
delete info.sensitive;
2022-07-08 04:21:11 +02:00
delete info.porn;
assert.deepStrictEqual(info, {
size: 544,
md5: '4b7a346cde9ccbeb267e812567e33397',
type: {
mime: 'image/svg+xml',
2022-05-21 15:21:41 +02:00
ext: 'svg',
},
width: 256,
height: 256,
orientation: undefined,
});
2022-09-17 20:27:08 +02:00
});
2023-02-02 10:18:25 +01:00
test('Dimension limit', async () => {
2022-09-17 20:27:08 +02:00
const path = `${resources}/25000x25000.png`;
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
delete info.warnings;
2020-10-18 09:03:51 +02:00
delete info.blurhash;
delete info.sensitive;
2022-07-08 04:21:11 +02:00
delete info.porn;
assert.deepStrictEqual(info, {
size: 75933,
md5: '268c5dde99e17cf8fe09f1ab3f97df56',
type: {
mime: 'application/octet-stream', // do not treat as image
2022-05-21 15:21:41 +02:00
ext: null,
},
width: 25000,
height: 25000,
orientation: undefined,
});
2022-09-17 20:27:08 +02:00
});
2023-02-02 10:18:25 +01:00
test('Rotate JPEG', async () => {
2022-09-17 20:27:08 +02:00
const path = `${resources}/rotate.jpg`;
const info = await fileInfoService.getFileInfo(path, { skipSensitiveDetection: true }) as any;
delete info.warnings;
delete info.blurhash;
delete info.sensitive;
2022-07-08 04:21:11 +02:00
delete info.porn;
assert.deepStrictEqual(info, {
size: 12624,
md5: '68d5b2d8d1d1acbbce99203e3ec3857e',
type: {
mime: 'image/jpeg',
2022-05-21 15:21:41 +02:00
ext: 'jpg',
},
width: 512,
height: 256,
orientation: 8,
});
2022-09-17 20:27:08 +02:00
});
});