forked from mtusi2026/mtusi2026lab5
feat: accept uploads into staging with jobs, outbox and job processor
This commit is contained in:
@@ -7,7 +7,13 @@ import request from 'supertest';
|
||||
import { reconcile } from '../src/attachments/reconciler';
|
||||
import { PrismaService } from '../src/db/prisma.service';
|
||||
import { S3Service } from '../src/storage/s3.service';
|
||||
import { bearer, createTestApp, registerUser, TestUser } from './helpers';
|
||||
import {
|
||||
bearer,
|
||||
createTestApp,
|
||||
processAttachment,
|
||||
registerUser,
|
||||
TestUser,
|
||||
} from './helpers';
|
||||
import { SAMPLE_PNG } from './images';
|
||||
|
||||
const HOUR_MS = 60 * 60 * 1000;
|
||||
@@ -100,6 +106,7 @@ describe('Сверка объектов S3 с метаданными (e2e)', ()
|
||||
sizeBytes: SAMPLE_PNG.length,
|
||||
sha256: createHash('sha256').update(SAMPLE_PNG).digest('hex'),
|
||||
objectKey: key,
|
||||
status: 'ready',
|
||||
},
|
||||
});
|
||||
const report = await reconcile(prisma, s3, {
|
||||
@@ -164,13 +171,75 @@ describe('Сверка объектов S3 с метаданными (e2e)', ()
|
||||
.set('Authorization', bearer(alice))
|
||||
.set('Content-Type', 'image/png')
|
||||
.send(SAMPLE_PNG)
|
||||
.expect(201);
|
||||
.expect(202);
|
||||
expect(await processAttachment(app, upload.body.id)).toBe('ready');
|
||||
const report = await reconcile(prisma, s3, { apply: true, graceMs: HOUR_MS });
|
||||
expect(report.fresh_skipped).toBeGreaterThanOrEqual(1);
|
||||
await request(server)
|
||||
.get(`/api/v1/attachments/${upload.body.id}/content`)
|
||||
expect(report.fresh_skipped).toBeGreaterThanOrEqual(4);
|
||||
for (const variant of ['original', 'thumbnail', 'crop', 'optimized']) {
|
||||
await request(server)
|
||||
.get(`/api/v1/attachments/${upload.body.id}/content?variant=${variant}`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(200);
|
||||
}
|
||||
});
|
||||
|
||||
it('после grace period объекты вложения и staging в очереди не считаются сиротами', async () => {
|
||||
const queued = await request(server)
|
||||
.post(`/api/v1/chats/${chatId}/attachments?filename=a.png`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.expect(200);
|
||||
.set('Content-Type', 'image/png')
|
||||
.send(SAMPLE_PNG)
|
||||
.expect(202);
|
||||
const ready = await request(server)
|
||||
.post(`/api/v1/chats/${chatId}/attachments?filename=b.png`)
|
||||
.set('Authorization', bearer(alice))
|
||||
.set('Content-Type', 'image/png')
|
||||
.send(SAMPLE_PNG)
|
||||
.expect(202);
|
||||
expect(await processAttachment(app, ready.body.id)).toBe('ready');
|
||||
for (const id of [queued.body.id, ready.body.id]) {
|
||||
for (const prefix of [`attachments/${id}/`, `staging/${id}`]) {
|
||||
const report = await reconcile(prisma, s3, {
|
||||
apply: true,
|
||||
graceMs: HOUR_MS,
|
||||
prefix,
|
||||
now: later(),
|
||||
});
|
||||
expect(report).toMatchObject({ orphans: 0, deleted: 0 });
|
||||
}
|
||||
}
|
||||
expect(await exists(`staging/${queued.body.id}`)).toBe(true);
|
||||
expect(await exists(`staging/${ready.body.id}`)).toBe(false);
|
||||
});
|
||||
|
||||
it('staging failed-вложения удаляется после срока хранения', 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);
|
||||
await prisma.attachment.update({
|
||||
where: { id: res.body.id },
|
||||
data: { status: 'failed', errorCode: 'invalid_image' },
|
||||
});
|
||||
const options = { graceMs: HOUR_MS, prefix: `staging/${res.body.id}`, now: later() };
|
||||
const kept = await reconcile(prisma, s3, {
|
||||
...options,
|
||||
apply: true,
|
||||
stagingRetentionMs: 3 * HOUR_MS,
|
||||
});
|
||||
expect(kept.staging_expired).toBe(0);
|
||||
expect(await exists(`staging/${res.body.id}`)).toBe(true);
|
||||
const expired = await reconcile(prisma, s3, {
|
||||
...options,
|
||||
apply: true,
|
||||
stagingRetentionMs: HOUR_MS,
|
||||
});
|
||||
expect(expired.staging_expired).toBeGreaterThanOrEqual(1);
|
||||
expect(await exists(`staging/${res.body.id}`)).toBe(false);
|
||||
const row = await prisma.attachment.findUniqueOrThrow({ where: { id: res.body.id } });
|
||||
expect(row.stagingKey).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user