|
| 1 | +/** |
| 2 | + * Retention cleanup of deleted workflow tests against the disposable TEST_DATABASE_URL database: |
| 3 | + * the foreign keys, the storage ledger, and the deletes all run for real. |
| 4 | + */ |
| 5 | +import { readTestDatabaseUrl } from '@sim/db/testing/test-infrastructure' |
| 6 | +import { generateId } from '@sim/utils/id' |
| 7 | +import postgres from 'postgres' |
| 8 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest' |
| 9 | +import { runCleanupSoftDeletes } from '@/background/cleanup-soft-deletes' |
| 10 | + |
| 11 | +const control = postgres(readTestDatabaseUrl(), { max: 2, onnotice: () => {} }) |
| 12 | +const userId = generateId() |
| 13 | +const workspaceId = generateId() |
| 14 | +const expired = '2020-01-01T00:00:00Z' |
| 15 | + |
| 16 | +async function seedTest(deletedAt: string | null, sizeBytes: number) { |
| 17 | + const fileId = generateId() |
| 18 | + const testId = generateId() |
| 19 | + await control`INSERT INTO workspace_files (id, key, user_id, workspace_id, context, original_name, content_type, size_bytes, deleted_at) |
| 20 | + VALUES (${fileId}, ${`test/${workspaceId}/${fileId}`}, ${userId}, ${workspaceId}, 'test', |
| 21 | + ${`${testId}.test.js`}, 'text/javascript', ${sizeBytes}, ${deletedAt})` |
| 22 | + await control`INSERT INTO workflow_test (id, workspace_id, name, title, body_file_id, source_hash, deleted_at) |
| 23 | + VALUES (${testId}, ${workspaceId}, ${testId}, 'Fixture', ${fileId}, 'hash', ${deletedAt})` |
| 24 | + await control`INSERT INTO workflow_test_run (id, test_id, workspace_id, version, triggered_by_actor) |
| 25 | + VALUES (${generateId()}, ${testId}, ${workspaceId}, 'draft', ${control.json({ type: 'user' })})` |
| 26 | + return { fileId, testId } |
| 27 | +} |
| 28 | + |
| 29 | +describe('Retention cleanup of deleted workflow tests', () => { |
| 30 | + beforeAll(async () => { |
| 31 | + await control`INSERT INTO "user" (id, name, email, email_verified, created_at, updated_at) |
| 32 | + VALUES (${userId}, 'Test cleanup fixture', ${`${userId}@example.test`}, true, now(), now())` |
| 33 | + await control`INSERT INTO user_stats (id, user_id, storage_used_bytes) |
| 34 | + VALUES (${generateId()}, ${userId}, 100)` |
| 35 | + await control`INSERT INTO workspace (id, name, owner_id, billed_account_user_id, storage_used_bytes) |
| 36 | + VALUES (${workspaceId}, 'Test cleanup fixtures', ${userId}, ${userId}, 100)` |
| 37 | + }) |
| 38 | + |
| 39 | + afterAll(async () => { |
| 40 | + await control`DELETE FROM workspace WHERE id = ${workspaceId}` |
| 41 | + await control`DELETE FROM "user" WHERE id = ${userId}` |
| 42 | + await control.end() |
| 43 | + }) |
| 44 | + |
| 45 | + it('purges an expired test with its source and releases the source bytes', async () => { |
| 46 | + const deleted = await seedTest(expired, 40) |
| 47 | + const live = await seedTest(null, 60) |
| 48 | + |
| 49 | + await runCleanupSoftDeletes({ |
| 50 | + label: 'tests-integration', |
| 51 | + plan: 'free', |
| 52 | + retentionHours: 24, |
| 53 | + workspaceIds: [workspaceId], |
| 54 | + }) |
| 55 | + |
| 56 | + const tests = await control< |
| 57 | + { id: string }[] |
| 58 | + >`SELECT id FROM workflow_test WHERE workspace_id = ${workspaceId}` |
| 59 | + expect(tests.map(({ id }) => id)).toEqual([live.testId]) |
| 60 | + const files = await control< |
| 61 | + { id: string }[] |
| 62 | + >`SELECT id FROM workspace_files WHERE workspace_id = ${workspaceId}` |
| 63 | + expect(files.map(({ id }) => id)).toEqual([live.fileId]) |
| 64 | + const runs = await control`SELECT 1 FROM workflow_test_run WHERE test_id = ${deleted.testId}` |
| 65 | + expect(runs).toHaveLength(0) |
| 66 | + const [ws] = await control< |
| 67 | + { storage_used_bytes: number }[] |
| 68 | + >`SELECT storage_used_bytes::int FROM workspace WHERE id = ${workspaceId}` |
| 69 | + expect(ws.storage_used_bytes).toBe(60) |
| 70 | + const [stats] = await control< |
| 71 | + { storage_used_bytes: number }[] |
| 72 | + >`SELECT storage_used_bytes::int FROM user_stats WHERE user_id = ${userId}` |
| 73 | + expect(stats.storage_used_bytes).toBe(60) |
| 74 | + }) |
| 75 | +}) |
0 commit comments