import { resolve } from "node:path"; const root = resolve(import.meta.dir, ".."); const lab = (await Bun.file(resolve(root, "lab.json")).json()).number; const base = process.env.BASE_URL ?? "http://127.0.0.1:28204"; const kubernetes = process.env.VP_RUNTIME !== "compose"; if (kubernetes) { const p = Bun.spawn( [ process.execPath, "--no-env-file", resolve(root, "scripts/k3s.ts"), "info", ], { cwd: root, stdout: "pipe", stderr: "pipe" }, ); const [out, err] = await Promise.all([ new Response(p.stdout).text(), new Response(p.stderr).text(), ]); if ((await p.exited) !== 0) throw new Error("Claimed Kubernetes endpoint unavailable"); const info = JSON.parse(out); if (new URL(base).origin !== "http://127.0.0.1:" + info.port) throw new Error("HTTP target differs from the claimed cluster"); } const prefix = `st_${crypto.randomUUID().replaceAll("-", "").slice(0, 16)}`; let assertions = 0; let operation = "initialization"; function check(value: unknown, message: string): asserts value { if (!value) throw new Error(message); assertions++; } async function call( method: string, path: string, status: number, token?: string, body?: unknown, headers: Record = {}, ) { operation = method + " " + path; const response = await fetch(base + path, { method, headers: { ...(token ? { Authorization: `Bearer ${token}` } : {}), ...(body !== undefined ? { "Content-Type": "application/json" } : {}), ...headers, }, body: body === undefined ? undefined : JSON.stringify(body), signal: AbortSignal.timeout(8000), }); const bytes = await response.arrayBuffer(); let json: any; try { json = JSON.parse(new TextDecoder().decode(bytes)); } catch {} check( response.status === status, `${method} ${path}: expected ${status}, got ${response.status}`, ); check(Boolean(response.headers.get("X-Request-Id")), "Missing request ID"); if (status >= 400) check( json?.error?.request_id === response.headers.get("X-Request-Id"), "Error request ID mismatch", ); return { json, response, bytes }; } const password = "synthetic:пароль🙂123"; async function user(suffix: string) { const registration = { username: prefix + suffix, display_name: "Синтетический тест", password, }; const { json } = await call( "POST", "/api/v1/users", 201, undefined, registration, ); const login = await call( "POST", "/api/v1/auth/sessions", 201, undefined, undefined, { Authorization: "Basic " + Buffer.from(registration.username + ":" + password).toString("base64"), }, ); check(!JSON.stringify(json).includes("password"), "Password leaked in User"); check( typeof login.json.token === "string" && login.json.token.length >= 43, "Token entropy/shape", ); return { id: json.id, token: login.json.token, registration }; } async function cleanup() { const p = Bun.spawn( kubernetes ? [ process.execPath, "--no-env-file", resolve(root, "scripts/k3s.ts"), "task", "fixture-cleanup", prefix, ] : [ resolve(root, "scripts/compose.sh"), "run", "--rm", "--no-deps", "fixture-cleanup", prefix, ], { cwd: root, stdout: "pipe", stderr: "pipe" }, ); await Promise.all([ new Response(p.stdout).text(), new Response(p.stderr).text(), ]); if ((await p.exited) !== 0) throw new Error("Fixture object/metadata cleanup failed"); } let primary: unknown; try { const alice = await user("a"), bob = await user("b"), eve = await user("e"); await call("POST", "/api/v1/users", 400, undefined, { username: prefix + "x", display_name: "x", }); await call("POST", "/api/v1/users", 400, undefined, { ...alice.registration, password: "short", }); await call("POST", "/api/v1/users", 400, undefined, { ...alice.registration, extra: 1, }); const race = await Promise.all( [0, 1].map(async () => { const r = await fetch(base + "/api/v1/users", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ ...alice.registration, username: prefix + "race", }), }); await r.arrayBuffer(); return r.status; }), ); check( race.sort().join(",") === "201,409", "Registration race must create exactly one user", ); const denied = await call( "GET", "/api/v1/users/me", 401, undefined, undefined, { "X-User-Id": alice.id }, ); check( denied.response.headers.get("WWW-Authenticate") === 'Bearer realm="messenger"', "Bearer challenge", ); const wrong = await call( "POST", "/api/v1/auth/sessions", 401, undefined, undefined, { Authorization: "Basic " + Buffer.from(alice.registration.username + ":wrong-password").toString( "base64", ), }, ); check( wrong.response.headers.get("WWW-Authenticate") === 'Basic realm="messenger", charset="UTF-8"', "Basic challenge", ); const invalidChat = { title: "rollback", member_ids: [alice.id, crypto.randomUUID()], }; await call("POST", "/api/v1/chats", 400, alice.token, invalidChat); let page = (await call("GET", "/api/v1/chats", 200, alice.token)).json; check( page.items.length === 0 && page.next_cursor === null, "Failed composite create left a chat", ); const chat = ( await call("POST", "/api/v1/chats", 201, alice.token, { title: "Пагинация", member_ids: [bob.id, alice.id], }) ).json; const path = "/api/v1/chats/" + chat.id + "/messages"; check( (await call("GET", path, 200, alice.token)).json.items.length === 0, "Empty page", ); for (const limit of ["0", "-1", "101", "1.2", "abc"]) await call("GET", path + "?limit=" + limit, 400, alice.token); await call("GET", path + "?cursor=%%%", 400, alice.token); await call("GET", path, 404, eve.token); const sent = []; for (let i = 0; i < 102; i++) { const h = lab >= 3 ? { "Idempotency-Key": crypto.randomUUID() } : {}; sent.push( ( await call( "POST", path, 201, alice.token, { text: `Сообщение ${i}` }, h, ) ).json, ); } check( (await call("GET", path, 200, bob.token)).json.items.length === 50, "Default bound", ); check( (await call("GET", path + "?limit=100", 200, bob.token)).json.items .length === 100, "Maximum bound", ); const seen = []; let cursor: string | null = null; do { page = ( await call( "GET", path + "?limit=7" + (cursor ? "&cursor=" + encodeURIComponent(cursor) : ""), 200, bob.token, ) ).json; check(page.items.length <= 7, "Page bound"); seen.push(...page.items); cursor = page.next_cursor; } while (cursor); check( JSON.stringify(seen) === JSON.stringify(sent), "Traversal must preserve every item exactly once", ); const other = ( await call("POST", "/api/v1/chats", 201, alice.token, { title: "Другой", member_ids: [alice.id], }) ).json; const first = (await call("GET", path + "?limit=1", 200, alice.token)).json; await call( "GET", "/api/v1/chats/" + other.id + "/messages?cursor=" + encodeURIComponent(first.next_cursor), 400, alice.token, ); if (lab >= 3) { const key = crypto.randomUUID(), headers = { "Idempotency-Key": key }, body = { text: "один конкурентный результат" }; const results = await Promise.all( Array.from({ length: 12 }, () => call("POST", path, 201, alice.token, body, headers), ), ); check( new Set(results.map((r) => r.json.id)).size === 1, "Concurrent retries duplicated a message", ); await call("POST", path, 409, alice.token, { text: "другой" }, headers); await call("POST", path, 400, alice.token, { text: "без ключа" }); const ids = new Set(); for (let i = 0; i < 20; i++) ids.add( ( await call("GET", "/api/v1/users/me", 200, alice.token) ).response.headers.get("X-Instance-Id")!, ); check( ids.size >= 2 && !ids.has(null as any), "One session must reach multiple actual replicas", ); } if (lab >= 4) { const png = new Uint8Array( await Bun.file(resolve(root, "tests/fixtures/sample.png")).arrayBuffer(), ); async function upload( bytes: Uint8Array, name: string, mime: string, status: number, token = alice.token, ) { operation = "upload " + mime + " expecting " + status; const r = await fetch( base + "/api/v1/chats/" + chat.id + "/attachments?filename=" + encodeURIComponent(name), { method: "POST", headers: { Authorization: "Bearer " + token, "Content-Type": mime }, body: bytes, signal: AbortSignal.timeout(25000), }, ); const json = (await r.json()) as any; check( r.status === status, `upload ${mime}: expected ${status}, got ${r.status}`, ); return json; } const att = await upload(png, "оригинал.png", "image/png", 201); const sha = new Bun.CryptoHasher("sha256").update(png).digest("hex"); check( att.sha256 === sha && att.size_bytes === png.length && att.status === "ready", "Original metadata must match actual bytes", ); const downloaded = await call( "GET", "/api/v1/attachments/" + att.id + "/content", 200, bob.token, ); check( Buffer.from(downloaded.bytes).equals(Buffer.from(png)), "Original download must be byte-exact", ); check( downloaded.response.headers.get("Content-Type") === "image/png", "Original MIME", ); for (const suffix of ["", "/content"]) await call( "GET", "/api/v1/attachments/" + att.id + suffix, 404, eve.token, ); const linked = ( await call( "POST", path, 201, bob.token, { text: "", attachment_ids: [att.id] }, { "Idempotency-Key": crypto.randomUUID() }, ) ).json; check( linked.attachment_ids.length === 1 && linked.attachment_ids[0] === att.id, "Message must retain attachment IDs", ); await call( "POST", "/api/v1/chats/" + other.id + "/messages", 404, alice.token, { text: "", attachment_ids: [att.id] }, { "Idempotency-Key": crypto.randomUUID() }, ); await call( "POST", path, 400, alice.token, { text: "", attachment_ids: [att.id, att.id] }, { "Idempotency-Key": crypto.randomUUID() }, ); for (const body of [ { attachment_ids: [att.id] }, { text: null, attachment_ids: [att.id] }, { text: "text", attachment_ids: null }, ]) await call("POST", path, 400, alice.token, body, { "Idempotency-Key": crypto.randomUUID(), }); const semanticKey = crypto.randomUUID(); const absent = ( await call( "POST", path, 201, alice.token, { text: "семантический повтор" }, { "Idempotency-Key": semanticKey }, ) ).json; const empty = ( await call( "POST", path, 201, alice.token, { attachment_ids: [], text: "семантический повтор" }, { "Idempotency-Key": semanticKey }, ) ).json; check( JSON.stringify(absent) === JSON.stringify(empty), "Omitted attachments and empty array must be semantically equal", ); await upload(png, "a.png", "image/jpeg", 415); await upload(png.slice(0, 20), "a.png", "image/png", 415); await upload(png, "../a.png", "image/png", 400); await upload(png, "a.png", "image/png", 404, eve.token); await upload( new Uint8Array(10 * 1024 * 1024 + 1), "huge.png", "image/png", 413, ); const anon = Bun.spawn( [ ...(kubernetes ? [resolve(root, "scripts/kube.sh"), "exec", "deployment/s3", "--"] : [resolve(root, "scripts/compose.sh"), "exec", "-T", "s3"]), "curl", "--silent", "--show-error", "-o", "/dev/null", "-w", "%{http_code}", "http://127.0.0.1:9000/messenger/originals/" + att.id, ], { cwd: root, stdout: "pipe", stderr: "pipe" }, ); const anonCode = await new Response(anon.stdout).text(); await new Response(anon.stderr).text(); check( (await anon.exited) === 0 && anonCode === "403", "Bucket must deny unauthenticated direct object GET", ); if (process.env.FAIL_S3 === "1") { async function storageAction(action: string) { const p = Bun.spawn( kubernetes ? [ resolve(root, "scripts/kube.sh"), "scale", "deployment/s3", "--replicas=" + (action === "stop" ? "0" : "1"), ] : [resolve(root, "scripts/compose.sh"), action, "s3"], { cwd: root, stdout: "pipe", stderr: "pipe", signal: AbortSignal.timeout(30000), }, ); await Promise.all([ new Response(p.stdout).text(), new Response(p.stderr).text(), ]); check((await p.exited) === 0, "S3 lifecycle action failed"); if (kubernetes && action === "stop") { const wait = Bun.spawn( [ resolve(root, "scripts/kube.sh"), "wait", "--for=delete", "pod", "-l", "app=s3", "--timeout=25s", ], { cwd: root, stdout: "pipe", stderr: "pipe" }, ); await Promise.all([ new Response(wait.stdout).text(), new Response(wait.stderr).text(), ]); check((await wait.exited) === 0, "S3 pod did not stop"); } } try { await storageAction("stop"); operation = "upload with S3 stopped"; const outageStarted = performance.now(); const unavailable = await fetch( base + "/api/v1/chats/" + chat.id + "/attachments?filename=unavailable.png", { method: "POST", headers: { Authorization: "Bearer " + alice.token, "Content-Type": "image/png", }, body: png, // Upload has a 20-second server context; Kubernetes without S3 // endpoints can exhaust SDK retries instead of refusing immediately. signal: AbortSignal.timeout(25000), }, ); await unavailable.arrayBuffer(); check( [502, 503].includes(unavailable.status), "Unavailable S3 must produce a bounded failure", ); console.log( "S3 outage: HTTP " + unavailable.status + "; elapsed_ms=" + Math.round(performance.now() - outageStarted), ); } finally { await storageAction("start"); const wait = Bun.spawn( [process.execPath, "--no-env-file", "student-tests/wait.ts"], { cwd: root, env: { ...process.env, BASE_URL: base }, stdout: "pipe", stderr: "pipe", }, ); await Promise.all([ new Response(wait.stdout).text(), new Response(wait.stderr).text(), ]); check( (await wait.exited) === 0, "S3 restart did not restore readiness", ); } const restored = await call( "GET", "/api/v1/attachments/" + att.id + "/content", 200, bob.token, ); check( Buffer.from(restored.bytes).equals(Buffer.from(png)), "S3 outage lost an acknowledged original", ); } } await call("DELETE", "/api/v1/auth/sessions/current", 204, alice.token); await call("GET", "/api/v1/users/me", 401, alice.token); if (process.env.FAILURES === "1") { async function compose(args: string[], ttl?: string) { const p = Bun.spawn([resolve(root, "scripts/compose.sh"), ...args], { cwd: root, env: { ...process.env, ...(ttl ? { SESSION_TTL_SECONDS: ttl } : {}) }, stdout: "pipe", stderr: "pipe", }); await Promise.all([ new Response(p.stdout).text(), new Response(p.stderr).text(), ]); check((await p.exited) === 0, "Compose failure action failed"); } async function ready() { for (let i = 0; i < 150; i++) { try { const r = await fetch(base + "/health/ready", { signal: AbortSignal.timeout(1500), }); await r.arrayBuffer(); if (r.status === 200) return; } catch {} await Bun.sleep(100); } throw new Error("Readiness did not recover"); } try { await compose(["stop", "valkey"]); await call("GET", "/api/v1/users/me", 200, bob.token); await call("GET", "/api/v1/users/me", 401, undefined, undefined, { "X-User-Id": bob.id, }); } finally { await compose(["start", "valkey"]); } try { await compose(["stop", "postgres"]); await call("GET", "/api/v1/users/me", 503, bob.token); await call("GET", "/health/ready", 503); await call("GET", "/health/live", 200); } finally { await compose(["start", "postgres"]); await ready(); } await call("GET", "/api/v1/users/me", 200, bob.token); if (lab === 2) { try { await compose( ["up", "-d", "--no-deps", "--force-recreate", "api"], "2", ); await ready(); const short = await user("ttl"); await call("GET", "/api/v1/users/me", 200, short.token); await Bun.sleep(2200); await call("GET", "/api/v1/users/me", 401, short.token); } finally { await compose( ["up", "-d", "--no-deps", "--force-recreate", "api"], "3600", ); await ready(); } } } } catch (error) { primary = new Error("Acceptance failed during " + operation, { cause: error, }); } try { await cleanup(); } catch (error) { console.error(String(error)); if (!primary) primary = error; } if (primary) throw primary; console.log( `PASS lab${lab}: ${assertions} HTTP assertions, registration race, rollback, pagination, authentication${lab >= 3 ? ", concurrent idempotency and replica sharing" : ""}; fixture cleanup completed`, );