// Remove only fixtures belonging to this one concurrency run, using restricted API keys. import assert from 'node:assert/strict'; import { createRequire } from 'node:module'; const require = createRequire(`${process.cwd()}/package.json`); const { PrismaService } = require('./dist/prisma/prisma.service.js'); const { StorageService } = require('./dist/Storage/storage.service.js'); const { tryAttachmentObjectLock, OBJECT_TRANSACTION, } = require('./dist/Attachment/object-lock.js'); let input = ''; for await (const part of process.stdin) input += part; const { runId, chatId, chatIds = chatId ? [chatId] : [], userIds, kind = 'concurrency', } = JSON.parse(input); assert.ok( ['concurrency', 'upload-interruption', 'direct-upload'].includes(kind), ); const uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; assert.match(runId, uuid); assert.ok(Array.isArray(userIds) && userIds.length <= 2); for (const id of userIds) assert.match(id, uuid); if (chatId) assert.match(chatId, uuid); assert.ok( Array.isArray(chatIds) && chatIds.length <= 3 && new Set(chatIds).size === chatIds.length, ); for (const value of chatIds) assert.match(value, uuid); const db = new PrismaService(); const storage = new StorageService(); try { await db.onModuleInit(); const users = await db.user.findMany({ where: { id: { in: userIds } } }); assert.ok( users.every((user) => user.username.startsWith(`u_cu${runId.slice(0, 8)}`)), ); for (const chatId of chatIds) { const chat = await db.chat.findUnique({ where: { id: chatId }, include: { members: true }, }); assert.equal(chat?.title, `${kind}:${runId}`); assert.ok(chat.members.every((member) => userIds.includes(member.userId))); const directUploads = await db.directUpload.findMany({ where: { chatId } }); for (const upload of directUploads) { assert.ok(userIds.includes(upload.ownerId)); await storage.remove(`staging/${chatId}/${upload.id}`); } await db.directUpload.deleteMany({ where: { chatId } }); const attachments = await db.attachment.findMany({ where: { chatId } }); for (const attachment of attachments) { assert.ok(userIds.includes(attachment.ownerId)); assert.equal( attachment.objectKey, `originals/${chatId}/${attachment.id}`, ); await db.$transaction(async (tx) => { assert.ok(await tryAttachmentObjectLock(tx, attachment.objectKey)); await storage.remove(attachment.objectKey); await tx.attachment.delete({ where: { id: attachment.id } }); }, OBJECT_TRANSACTION); } await db.chat.delete({ where: { id: chatId } }); } await db.user.deleteMany({ where: { id: { in: users.map((user) => user.id) } }, }); console.log('PASS: concurrency fixtures removed'); } finally { storage.onModuleDestroy(); await db.onModuleDestroy(); }