forked from mtusi2026/mtusi2026lab5
182 lines
5.8 KiB
TypeScript
182 lines
5.8 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
|
import { join } from 'node:path';
|
|
import { Readable } from 'node:stream';
|
|
import type { PrismaService } from '../prisma/prisma.service';
|
|
import type { StorageService } from '../Storage/storage.service';
|
|
import { AttachmentService } from './attachment.service';
|
|
|
|
const chatId = '11111111-1111-4111-8111-111111111111';
|
|
const userId = '22222222-2222-4222-8222-222222222222';
|
|
const attachmentId = '33333333-3333-4333-8333-333333333333';
|
|
const png = readFileSync(join(__dirname, '../../tests/fixtures/sample.png'));
|
|
|
|
function fixture() {
|
|
const stored = {
|
|
id: attachmentId,
|
|
chatId,
|
|
ownerId: userId,
|
|
objectKey: 'stored-key',
|
|
filename: 'test.png',
|
|
contentType: 'image/png',
|
|
sizeBytes: png.length,
|
|
sha256: 'checksum',
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
};
|
|
const tx = {
|
|
$executeRaw: jest.fn().mockResolvedValue(0),
|
|
$queryRaw: jest.fn().mockResolvedValue([{ locked: true }]),
|
|
attachment: {
|
|
count: jest.fn().mockResolvedValue(0),
|
|
create: jest.fn().mockResolvedValue(stored),
|
|
findUnique: jest.fn().mockResolvedValue(null),
|
|
},
|
|
};
|
|
const prisma = {
|
|
chatMember: { findUnique: jest.fn().mockResolvedValue({ chatId }) },
|
|
attachment: { findFirst: jest.fn() },
|
|
$transaction: jest.fn(
|
|
async (callback: (value: typeof tx) => Promise<unknown>) => callback(tx),
|
|
),
|
|
};
|
|
const storage = {
|
|
get: jest.fn().mockResolvedValue(Readable.from(png)),
|
|
put: jest.fn().mockResolvedValue(undefined),
|
|
remove: jest.fn().mockResolvedValue(undefined),
|
|
};
|
|
const service = new AttachmentService(
|
|
prisma as unknown as PrismaService,
|
|
storage as unknown as StorageService,
|
|
);
|
|
return {
|
|
stored,
|
|
tx,
|
|
prisma,
|
|
storage,
|
|
service,
|
|
upload: () => service.upload(chatId, userId, 'test.png', 'image/png', png),
|
|
};
|
|
}
|
|
|
|
describe('upload compensation', () => {
|
|
it('rejects an upload at queue capacity and removes the unadmitted staging object', async () => {
|
|
const f = fixture();
|
|
f.tx.attachment.count.mockResolvedValue(10000);
|
|
await expect(f.upload()).rejects.toMatchObject({ status: 429 });
|
|
expect(f.tx.attachment.create).not.toHaveBeenCalled();
|
|
expect(f.storage.remove).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('compensates a PUT whose acknowledgement was lost before metadata creation', async () => {
|
|
const f = fixture();
|
|
f.storage.put.mockRejectedValue(new Error('uncertain PUT'));
|
|
await expect(f.upload()).rejects.toThrow('uncertain PUT');
|
|
expect(f.tx.attachment.create).not.toHaveBeenCalled();
|
|
expect(f.storage.remove).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('removes bytes after a confirmed metadata failure', async () => {
|
|
const f = fixture();
|
|
f.tx.attachment.create.mockRejectedValue(new Error('metadata failure'));
|
|
await expect(f.upload()).rejects.toThrow('metadata failure');
|
|
expect(f.storage.remove).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('preserves a committed attachment if the transaction acknowledgement was lost', async () => {
|
|
const f = fixture();
|
|
f.prisma.$transaction.mockImplementationOnce(async (callback) => {
|
|
await callback(f.tx);
|
|
throw new Error('lost commit acknowledgement');
|
|
});
|
|
f.tx.attachment.findUnique.mockResolvedValue(f.stored);
|
|
await expect(f.upload()).resolves.toMatchObject({
|
|
id: f.stored.id,
|
|
status: 'queued',
|
|
});
|
|
expect(f.storage.remove).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('attachment variants', () => {
|
|
it('reports and serves the crop and optimized variants of a ready attachment', async () => {
|
|
const f = fixture();
|
|
f.prisma.attachment.findFirst.mockResolvedValue({
|
|
...f.stored,
|
|
status: 'ready',
|
|
cropSize: 42,
|
|
cropSha256: 'crop-hash',
|
|
optimizedSize: 128,
|
|
optimizedWidth: 320,
|
|
optimizedHeight: 200,
|
|
optimizedSha256: 'webp-hash',
|
|
});
|
|
const metadata = await f.service.get(attachmentId, userId);
|
|
expect(metadata.variants).toEqual([
|
|
{
|
|
name: 'crop',
|
|
content_type: 'image/jpeg',
|
|
size_bytes: 42,
|
|
width: 128,
|
|
height: 128,
|
|
sha256: 'crop-hash',
|
|
},
|
|
{
|
|
name: 'optimized',
|
|
content_type: 'image/webp',
|
|
size_bytes: 128,
|
|
width: 320,
|
|
height: 200,
|
|
sha256: 'webp-hash',
|
|
},
|
|
]);
|
|
await expect(
|
|
f.service.content(attachmentId, userId, 'crop'),
|
|
).resolves.toMatchObject({
|
|
attachment: {
|
|
filename: 'test.png.crop.jpg',
|
|
contentType: 'image/jpeg',
|
|
sizeBytes: 42,
|
|
},
|
|
});
|
|
await expect(
|
|
f.service.content(attachmentId, userId, 'optimized'),
|
|
).resolves.toMatchObject({
|
|
attachment: {
|
|
filename: 'test.png.optimized.webp',
|
|
contentType: 'image/webp',
|
|
sizeBytes: 128,
|
|
},
|
|
});
|
|
expect(f.storage.get.mock.calls).toEqual([
|
|
[`variants/${chatId}/${attachmentId}/v1/crop.jpg`],
|
|
[`variants/${chatId}/${attachmentId}/v1/optimized.webp`],
|
|
]);
|
|
});
|
|
|
|
it('distinguishes unavailable content from variants missing on older ready attachments', async () => {
|
|
const f = fixture();
|
|
f.prisma.attachment.findFirst.mockResolvedValueOnce({
|
|
...f.stored,
|
|
status: 'processing',
|
|
});
|
|
await expect(
|
|
f.service.content(attachmentId, userId, 'optimized'),
|
|
).rejects.toMatchObject({ status: 409 });
|
|
f.prisma.attachment.findFirst.mockResolvedValue({
|
|
...f.stored,
|
|
status: 'ready',
|
|
cropSize: null,
|
|
optimizedSize: null,
|
|
});
|
|
await expect(
|
|
f.service.content(attachmentId, userId, 'optimized'),
|
|
).rejects.toMatchObject({ status: 404 });
|
|
await expect(
|
|
f.service.content(attachmentId, userId, undefined),
|
|
).resolves.toMatchObject({
|
|
attachment: { contentType: 'image/png' },
|
|
});
|
|
expect(f.storage.get.mock.calls).toEqual([[f.stored.objectKey]]);
|
|
});
|
|
});
|