Repository navigation
fix(tests): purge deleted tests, bill their storage, and record versions as loaded #8858
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+194
−90
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /** | ||
| * Retention cleanup of deleted workflow tests against the disposable TEST_DATABASE_URL database: | ||
| * the foreign keys, the storage ledger, and the deletes all run for real. | ||
| */ | ||
| import { readTestDatabaseUrl } from '@sim/db/testing/test-infrastructure' | ||
| import { generateId } from '@sim/utils/id' | ||
| import postgres from 'postgres' | ||
| import { afterAll, beforeAll, describe, expect, it } from 'vitest' | ||
| import { runCleanupSoftDeletes } from '@/background/cleanup-soft-deletes' | ||
|
|
||
| const control = postgres(readTestDatabaseUrl(), { max: 2, onnotice: () => {} }) | ||
| const userId = generateId() | ||
| const workspaceId = generateId() | ||
| const expired = '2020-01-01T00:00:00Z' | ||
|
|
||
| async function seedTest(deletedAt: string | null, sizeBytes: number) { | ||
| const fileId = generateId() | ||
| const testId = generateId() | ||
| await control`INSERT INTO workspace_files (id, key, user_id, workspace_id, context, original_name, content_type, size_bytes, deleted_at) | ||
| VALUES (${fileId}, ${`test/${workspaceId}/${fileId}`}, ${userId}, ${workspaceId}, 'test', | ||
| ${`${testId}.test.js`}, 'text/javascript', ${sizeBytes}, ${deletedAt})` | ||
| await control`INSERT INTO workflow_test (id, workspace_id, name, title, body_file_id, source_hash, deleted_at) | ||
| VALUES (${testId}, ${workspaceId}, ${testId}, 'Fixture', ${fileId}, 'hash', ${deletedAt})` | ||
| await control`INSERT INTO workflow_test_run (id, test_id, workspace_id, version, triggered_by_actor) | ||
| VALUES (${generateId()}, ${testId}, ${workspaceId}, 'draft', ${control.json({ type: 'user' })})` | ||
| return { fileId, testId } | ||
| } | ||
|
|
||
| describe('Retention cleanup of deleted workflow tests', () => { | ||
| beforeAll(async () => { | ||
| await control`INSERT INTO "user" (id, name, email, email_verified, created_at, updated_at) | ||
| VALUES (${userId}, 'Test cleanup fixture', ${`${userId}@example.test`}, true, now(), now())` | ||
| await control`INSERT INTO user_stats (id, user_id, storage_used_bytes) | ||
| VALUES (${generateId()}, ${userId}, 100)` | ||
| await control`INSERT INTO workspace (id, name, owner_id, billed_account_user_id, storage_used_bytes) | ||
| VALUES (${workspaceId}, 'Test cleanup fixtures', ${userId}, ${userId}, 100)` | ||
| }) | ||
|
|
||
| afterAll(async () => { | ||
| await control`DELETE FROM workspace WHERE id = ${workspaceId}` | ||
| await control`DELETE FROM "user" WHERE id = ${userId}` | ||
| await control.end() | ||
| }) | ||
|
|
||
| it('purges an expired test with its source and releases the source bytes', async () => { | ||
| const deleted = await seedTest(expired, 40) | ||
| const live = await seedTest(null, 60) | ||
|
|
||
| await runCleanupSoftDeletes({ | ||
| label: 'tests-integration', | ||
| plan: 'free', | ||
| retentionHours: 24, | ||
| workspaceIds: [workspaceId], | ||
| }) | ||
|
|
||
| const tests = await control< | ||
| { id: string }[] | ||
| >`SELECT id FROM workflow_test WHERE workspace_id = ${workspaceId}` | ||
| expect(tests.map(({ id }) => id)).toEqual([live.testId]) | ||
| const files = await control< | ||
| { id: string }[] | ||
| >`SELECT id FROM workspace_files WHERE workspace_id = ${workspaceId}` | ||
| expect(files.map(({ id }) => id)).toEqual([live.fileId]) | ||
| const runs = await control`SELECT 1 FROM workflow_test_run WHERE test_id = ${deleted.testId}` | ||
| expect(runs).toHaveLength(0) | ||
| const [ws] = await control< | ||
| { storage_used_bytes: number }[] | ||
| >`SELECT storage_used_bytes::int FROM workspace WHERE id = ${workspaceId}` | ||
| expect(ws.storage_used_bytes).toBe(60) | ||
| const [stats] = await control< | ||
| { storage_used_bytes: number }[] | ||
| >`SELECT storage_used_bytes::int FROM user_stats WHERE user_id = ${userId}` | ||
| expect(stats.storage_used_bytes).toBe(60) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.