lab5: e2e, fault suite и доказательства

This commit is contained in:
2026-11-05 01:17:58 +03:00
parent d19824f546
commit 551cf70989
28 changed files with 2269 additions and 60 deletions
+50 -17
View File
@@ -20,6 +20,7 @@ import {
postMessage,
upload,
uploadRaw,
waitReady,
} from './support/e2e.js';
import sharp from 'sharp';
import { jpegWithMetadata, jpegWithoutScanData } from './support/jpeg.js';
@@ -55,8 +56,9 @@ async function ready(data: Buffer, type = 'image/png', chat = chatId) {
const result = await upload(alice.token, chat, data, {
contentType: type,
});
expect(result.response.status).toBe(201);
return result.body;
expect(result.response.status).toBe(202);
expect(result.body.status).toBe('queued');
return waitReady(alice.token, result.body.id);
}
describe('attachments (proxy upload)', () => {
@@ -76,27 +78,40 @@ describe('attachments (proxy upload)', () => {
});
it('stores a PNG and serves it only to chat members', async () => {
const created = await upload(alice.token, chatId, png, {
const uploaded = await upload(alice.token, chatId, png, {
filename: 'Снимок.png',
});
expect(created.response.status).toBe(201);
expect(created.body).toMatchObject({
expect(uploaded.response.status).toBe(202);
expect(uploaded.body).toMatchObject({
chat_id: chatId,
owner_id: alice.user.id,
filename: 'Снимок.png',
content_type: 'image/png',
size_bytes: png.length,
sha256: sha256(png),
status: 'ready',
status: 'queued',
variants: [],
error_code: null,
});
const id = created.body.id;
const id = uploaded.body.id;
const metadata = await http<typeof created.body>(
`/api/v1/attachments/${id}`,
{ headers: bearer(bob.token) },
);
const ready = await waitReady(bob.token, id);
expect(ready).toMatchObject({
filename: 'Снимок.png',
size_bytes: png.length,
sha256: sha256(png),
status: 'ready',
error_code: null,
});
expect(ready.variants.map((variant) => variant.name)).toEqual([
'thumbnail',
'crop',
'optimized',
]);
const metadata = await http<typeof ready>(`/api/v1/attachments/${id}`, {
headers: bearer(bob.token),
});
expect(metadata.response.status).toBe(200);
expect(metadata.body.sha256).toBe(sha256(png));
@@ -241,7 +256,7 @@ describe('attachments (proxy upload)', () => {
for (let index = 0; index < 4; index += 1)
expect(
(await upload(bob.token, chatId, png)).response.status,
).toBe(201);
).toBe(202);
} finally {
for (const socket of sockets) socket.destroy();
}
@@ -346,7 +361,7 @@ describe('attachments (proxy upload)', () => {
const created = await upload(alice.token, chatId, png, {
filename: long,
});
expect(created.response.status).toBe(201);
expect(created.response.status).toBe(202);
expect(created.body.filename).toBe(long);
});
@@ -400,7 +415,7 @@ describe('attachments (proxy upload)', () => {
it('rejects invalid attachment references', async () => {
const mine = await ready(png);
const foreign = await upload(eve.token, eveChatId, png);
expect(foreign.response.status).toBe(201);
expect(foreign.response.status).toBe(202);
expectError(
await postMessage(alice.token, chatId, { text: '' }),
400,
@@ -439,8 +454,18 @@ describe('attachments (proxy upload)', () => {
);
});
it('serves unknown content variants as errors, not bytes', async () => {
const created = await ready(png);
it('serves the promised variants only after ready', async () => {
const uploaded = await upload(alice.token, chatId, png);
expect(uploaded.response.status).toBe(202);
expectError(
await http<ApiErrorBody>(
`/api/v1/attachments/${uploaded.body.id}/content`,
{ headers: bearer(alice.token) },
),
409,
'conflict',
);
const created = await waitReady(alice.token, uploaded.body.id);
const variant = (name: string) =>
http<ApiErrorBody>(
`/api/v1/attachments/${created.id}/content?variant=${name}`,
@@ -449,7 +474,15 @@ describe('attachments (proxy upload)', () => {
},
);
expect((await variant('original')).response.status).toBe(200);
expectError(await variant('thumbnail'), 404, 'not_found');
for (const [name, type] of [
['thumbnail', 'image/jpeg'],
['crop', 'image/jpeg'],
['optimized', 'image/webp'],
] as const) {
const response = await variant(name);
expect(response.response.status).toBe(200);
expect(response.response.headers.get('content-type')).toBe(type);
}
expectError(await variant('bogus'), 400, 'invalid_request');
});