AI-native design editor. Open-source Figma alternative.
import { describe, expect, test } from 'bun:test'
import { createCloudTestDatabase } from '#cloud-tests/helpers/database'
import { createMemoryObjectStore } from '#cloud-tests/helpers/objects'
import { createUploadCleanupService } from '#cloud/server'
async function insertExpiredUploads(count: number, multipartUploadId: string | null = null) {
const runtime = await createCloudTestDatabase()
const workspaceId = crypto.randomUUID()
const documentId = crypto.randomUUID()
await runtime.database
.insertInto('workspace')
.values({
id: workspaceId,
name: 'Cleanup',
slug: `cleanup-${crypto.randomUUID()}`,
createdBy: 'alice'
})
.execute()
await runtime.database
.insertInto('document')
.values({ id: documentId, workspaceId, name: 'Cleanup.fig', createdBy: 'alice' })
.execute()
const uploads = Array.from({ length: count }, () => ({
id: crypto.randomUUID(),
documentId,
baseRevisionId: null,
objectKey: `cleanup/${crypto.randomUUID()}.fig`,
checksum: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=',
byteSize: 1,
contentType: 'application/octet-stream',
multipartUploadId,
status: 'pending' as const,
finalizationStartedAt: null,
createdBy: 'alice',
expiresAt: new Date(Date.now() - 60_000)
}))
await runtime.database.insertInto('upload').values(uploads).execute()
return { runtime, uploads }
}
describe('upload cleanup', () => {
test('limits each cleanup batch', async () => {
const context = await insertExpiredUploads(3)
const objects = createMemoryObjectStore()
try {
const cleanup = createUploadCleanupService(context.runtime.database, objects.store)
expect(
await cleanup.cleanupExpiredUploads({ batchSize: 2, leaseDurationMs: 60_000 })
).toEqual({
claimed: 2,
cleaned: 2,
failed: 0
})
expect(objects.deletedKeys).toHaveLength(2)
expect(
await context.runtime.database
.selectFrom('upload')
.select(({ fn }) => fn.countAll<number>().as('count'))
.where('status', '=', 'pending')
.executeTakeFirstOrThrow()
).toEqual({ count: 1 })
} finally {
await context.runtime.close()
}
})
test('removes a completed multipart upload whose commit never landed', async () => {
const context = await insertExpiredUploads(1, 'multipart-upload')
const objects = createMemoryObjectStore()
try {
const cleanup = createUploadCleanupService(context.runtime.database, objects.store)
await cleanup.cleanupExpiredUploads({ batchSize: 10, leaseDurationMs: 60_000 })
expect(objects.deletedKeys).toEqual(context.uploads.map((upload) => upload.objectKey))
} finally {
await context.runtime.close()
}
})
test('reclaims stale finalization leases', async () => {
const context = await insertExpiredUploads(1)
const objects = createMemoryObjectStore()
try {
await context.runtime.database
.updateTable('upload')
.set({
status: 'finalizing',
finalizationStartedAt: new Date(Date.now() - 120_000),
expiresAt: new Date(Date.now() + 60_000)
})
.execute()
const cleanup = createUploadCleanupService(context.runtime.database, objects.store)
expect(
await cleanup.cleanupExpiredUploads({ batchSize: 1, leaseDurationMs: 60_000 })
).toEqual({ claimed: 1, cleaned: 1, failed: 0 })
expect(
await context.runtime.database
.selectFrom('upload')
.select('status')
.executeTakeFirstOrThrow()
).toEqual({ status: 'abandoned' })
} finally {
await context.runtime.close()
}
})
test('reclaims stale cleanup leases', async () => {
const context = await insertExpiredUploads(1)
const objects = createMemoryObjectStore()
try {
await context.runtime.database
.updateTable('upload')
.set({
status: 'cleaning',
cleanupClaimId: crypto.randomUUID(),
cleanupClaimedAt: new Date(Date.now() - 120_000)
})
.execute()
const cleanup = createUploadCleanupService(context.runtime.database, objects.store)
expect(
await cleanup.cleanupExpiredUploads({ batchSize: 1, leaseDurationMs: 60_000 })
).toEqual({
claimed: 1,
cleaned: 1,
failed: 0
})
} finally {
await context.runtime.close()
}
})
})