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');
});
+9 -5
View File
@@ -13,6 +13,7 @@ import type { Page } from '../src/common/http/page.js';
import type { Message } from '../src/messages/message.js';
import type { User } from '../src/users/user.js';
import { noisePng } from './support/png.js';
import { waitReady } from './support/e2e.js';
// Automated counterpart of the manual Redis/PostgreSQL outage checks. Four
// topologies, because the load balancer probes /health/ready (the same route as
@@ -924,7 +925,8 @@ describe.skipIf(!enabled)('dependency outages', () => {
},
);
const before = await upload(samplePng, `s3before-${runId}.png`);
expect(before.response.status).toBe(201);
expect(before.response.status).toBe(202);
await waitReady(token, before.body.id);
await compose('stop', s3Service);
try {
@@ -969,7 +971,8 @@ describe.skipIf(!enabled)('dependency outages', () => {
Buffer.from(await bytes.arrayBuffer()).equals(samplePng),
).toBe(true);
const after = await upload(samplePng, `s3after-${runId}.png`);
expect(after.response.status).toBe(201);
expect(after.response.status).toBe(202);
await waitReady(token, after.body.id);
// A `deleting` row is re-claimed one minute after its last change
// (an in-flight compensation is never raced), then swept.
@@ -1001,7 +1004,8 @@ describe.skipIf(!enabled)('dependency outages', () => {
body: samplePng as unknown as BodyInit,
},
);
expect(created.response.status).toBe(201);
expect(created.response.status).toBe(202);
await waitReady(token, created.body.id);
// A paused container keeps its TCP port open but never answers: the
// failure mode a stopped container (connection refused) does not show.
const container = (await compose('ps', '-q', s3Service)).split(
@@ -1084,9 +1088,9 @@ describe.skipIf(!enabled)('dependency outages', () => {
await waitForReadiness(200);
const key = await psql(
`select object_key from attachments where ${filter}`,
`select staging_key from attachments where ${filter}`,
);
expect(key).toMatch(/^attachments\/[0-9a-f-]{36}\/original$/u);
expect(key).toMatch(/^staging\/[0-9a-f-]{36}\/original$/u);
// Without PostgreSQL the API cannot tell a lost commit from a failed
// one, so it deletes nothing; the pending row keeps the object tracked.
expect(await objectExists(key)).toBe(true);
+7 -3
View File
@@ -17,6 +17,7 @@ import {
http,
json,
postMessage,
waitReady,
} from './support/e2e.js';
import { noisePng, pixelBombPng } from './support/png.js';
@@ -102,7 +103,7 @@ describe('direct upload (presigned PUT)', () => {
);
expect(Date.parse(ticket.expires_at)).toBeGreaterThan(Date.now());
expect(new URL(ticket.url).pathname).toBe(
`/messenger-attachments/attachments/${ticket.attachment_id}/original`,
`/messenger-attachments/staging/${ticket.attachment_id}/original`,
);
const hidden = await http<ApiErrorBody>(
@@ -143,11 +144,14 @@ describe('direct upload (presigned PUT)', () => {
content_type: 'image/png',
size_bytes: png.length,
sha256: sha256(png),
status: 'ready',
status: 'queued',
});
const ready = await waitReady(alice.token, ticket.attachment_id);
expect(ready.status).toBe('ready');
expect(ready.variants).toHaveLength(3);
const again = await complete(alice.token, ticket.attachment_id);
expect(again.response.status).toBe(200);
expect(again.body).toEqual(done.body);
expect(again.body.status).toBe('ready');
const content = await download(bob.token, ticket.attachment_id);
expect(content.response.status).toBe(200);
+29
View File
@@ -134,6 +134,35 @@ export function upload(
);
}
/**
* Lab5 uploads answer 202 before the worker runs. Poll metadata until ready;
* a failed attachment is a test failure, never a silent timeout.
*/
export async function waitReady(
token: string,
attachmentId: string,
timeoutMs = 60_000,
): Promise<Attachment> {
const deadline = Date.now() + timeoutMs;
for (;;) {
const result = await http<Attachment>(
`/api/v1/attachments/${attachmentId}`,
{ headers: bearer(token) },
);
expect(result.response.status).toBe(200);
if (result.body.status === 'failed')
throw new Error(
`attachment ${attachmentId} failed: ${result.body.error_code ?? 'unknown'}`,
);
if (result.body.status === 'ready') return result.body;
if (Date.now() >= deadline)
throw new Error(
`attachment ${attachmentId} did not become ready within ${timeoutMs}ms`,
);
await new Promise((resolve) => setTimeout(resolve, 200));
}
}
export async function download(
token: string,
attachmentId: string,