fix: close cleanup and replay races, integer thumbnail sizing, sharp deadline, contract tests

This commit is contained in:
2026-11-16 16:56:50 +03:00
parent 3e0033fc79
commit 484119a6a9
9 changed files with 437 additions and 28 deletions
+43
View File
@@ -6,6 +6,7 @@ import { join } from 'path';
import request from 'supertest';
import { reconcile } from '../src/attachments/reconciler';
import { PrismaService } from '../src/db/prisma.service';
import { replayJob } from '../src/jobs/job-store';
import { S3Service } from '../src/storage/s3.service';
import {
bearer,
@@ -242,6 +243,48 @@ describe('Сверка объектов S3 с метаданными (e2e)', ()
expect(row.stagingKey).toBeNull();
});
it('объекты вариантов без строк у существующего вложения не удаляются: replay после сверки доводит до ready', async () => {
const res = await request(server)
.post(`/api/v1/chats/${chatId}/attachments?filename=a.png`)
.set('Authorization', bearer(alice))
.set('Content-Type', 'image/png')
.send(SAMPLE_PNG)
.expect(202);
const id = res.body.id;
await expect(
processAttachment(app, id, {
afterStore: async () => {
throw new Error('worker crashed');
},
}),
).rejects.toThrow('worker crashed');
const job = await prisma.attachmentJob.findFirstOrThrow({ where: { attachmentId: id } });
await prisma.attachmentJob.update({
where: { id: job.id },
data: { status: 'failed', errorCode: 'retry_exhausted', leaseOwner: null, leaseUntil: null },
});
await prisma.attachment.update({
where: { id },
data: { status: 'failed', errorCode: 'retry_exhausted' },
});
expect(await prisma.attachmentVariant.count({ where: { attachmentId: id } })).toBe(0);
const report = await reconcile(prisma, s3, {
apply: true,
graceMs: HOUR_MS,
prefix: `attachments/${id}/`,
now: later(),
});
expect(report).toMatchObject({ scanned: 4, referenced: 4, orphans: 0, deleted: 0 });
expect(await replayJob(prisma, job.id, 'test-operator')).toBe('replayed');
expect(await processAttachment(app, id)).toBe('ready');
for (const variant of ['original', 'thumbnail', 'crop', 'optimized']) {
await request(server)
.get(`/api/v1/attachments/${id}/content?variant=${variant}`)
.set('Authorization', bearer(alice))
.expect(200);
}
});
it('staging ready-вложения, оставшийся после сбоя worker, удаляется без ожидания срока хранения', async () => {
const res = await request(server)
.post(`/api/v1/chats/${chatId}/attachments?filename=a.png`)