mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-24 14:51:12 -05:00
Refactor tests to use pipeline
Refactor versioningTests.ts, submissionTests.ts, and dataIntegrityTests.ts to replace direct DB inserts with the submission pipeline (submitParkCreation → approve → verify), aligning tests with RLS policies and validation flows.
This commit is contained in:
@@ -149,52 +149,69 @@ export const dataIntegrityTestSuite: TestSuite = {
|
||||
{
|
||||
id: 'integrity-003',
|
||||
name: 'Unique Constraint Enforcement',
|
||||
description: 'Tests unique constraints prevent duplicate slugs',
|
||||
description: 'Tests unique constraints prevent duplicate slugs via approval pipeline',
|
||||
run: async (): Promise<TestResult> => {
|
||||
const startTime = Date.now();
|
||||
const tracker = new TestDataTracker();
|
||||
let parkId: string | null = null;
|
||||
|
||||
try {
|
||||
// Create a park
|
||||
const slug = `unique-test-${Date.now()}`;
|
||||
const { data: park, error: createError } = await supabase
|
||||
.from('parks')
|
||||
.insert({
|
||||
name: 'Unique Test Park',
|
||||
slug,
|
||||
park_type: 'theme_park',
|
||||
status: 'operating',
|
||||
is_test_data: true
|
||||
})
|
||||
.select('id')
|
||||
.single();
|
||||
// Import necessary helpers
|
||||
const {
|
||||
getCurrentUserId,
|
||||
getAuthToken,
|
||||
generateUniqueParkData,
|
||||
createTestParkSubmission,
|
||||
approveSubmission
|
||||
} = await import('../helpers/approvalTestHelpers');
|
||||
|
||||
if (createError) throw new Error(`Park creation failed: ${createError.message}`);
|
||||
if (!park) throw new Error('No park returned');
|
||||
const userId = await getCurrentUserId();
|
||||
const authToken = await getAuthToken();
|
||||
|
||||
parkId = park.id;
|
||||
tracker.track('parks', parkId);
|
||||
// Create first park with unique slug
|
||||
const baseSlug = `unique-test-${Date.now()}`;
|
||||
const parkData1 = {
|
||||
...generateUniqueParkData('integrity-003-1'),
|
||||
slug: baseSlug // Override with our controlled slug
|
||||
};
|
||||
|
||||
// Try to create another park with same slug
|
||||
const { error: duplicateError } = await supabase
|
||||
.from('parks')
|
||||
.insert({
|
||||
name: 'Duplicate Park',
|
||||
slug, // Same slug
|
||||
park_type: 'theme_park',
|
||||
status: 'operating',
|
||||
is_test_data: true
|
||||
});
|
||||
// Create and approve first submission
|
||||
const { submissionId: sub1Id, itemId: item1Id } = await createTestParkSubmission(parkData1, userId, tracker);
|
||||
|
||||
// This SHOULD fail with unique violation
|
||||
if (!duplicateError) {
|
||||
throw new Error('Unique constraint not enforced - duplicate slug was accepted');
|
||||
const approval1 = await approveSubmission(sub1Id, [item1Id], authToken);
|
||||
if (!approval1.success) {
|
||||
throw new Error(`First park approval failed: ${approval1.error}`);
|
||||
}
|
||||
|
||||
// Verify it's a unique violation
|
||||
if (!duplicateError.message.includes('unique') && !duplicateError.message.includes('duplicate')) {
|
||||
throw new Error(`Expected unique constraint error, got: ${duplicateError.message}`);
|
||||
// Get first park ID
|
||||
const { data: item1 } = await supabase
|
||||
.from('submission_items')
|
||||
.select('approved_entity_id')
|
||||
.eq('id', item1Id)
|
||||
.single();
|
||||
|
||||
if (!item1?.approved_entity_id) throw new Error('First park not created');
|
||||
tracker.track('parks', item1.approved_entity_id);
|
||||
|
||||
// Create second submission with SAME slug
|
||||
const parkData2 = {
|
||||
...generateUniqueParkData('integrity-003-2'),
|
||||
slug: baseSlug // Same slug - should fail on approval
|
||||
};
|
||||
|
||||
const { submissionId: sub2Id, itemId: item2Id } = await createTestParkSubmission(parkData2, userId, tracker);
|
||||
|
||||
// Try to approve second submission (should fail due to unique constraint)
|
||||
const approval2 = await approveSubmission(sub2Id, [item2Id], authToken);
|
||||
|
||||
// Approval should fail
|
||||
if (approval2.success) {
|
||||
throw new Error('Second approval succeeded when it should have failed (duplicate slug)');
|
||||
}
|
||||
|
||||
// Verify the error mentions unique constraint or duplicate
|
||||
const errorMsg = approval2.error?.toLowerCase() || '';
|
||||
if (!errorMsg.includes('unique') && !errorMsg.includes('duplicate') && !errorMsg.includes('already exists')) {
|
||||
throw new Error(`Expected unique constraint error, got: ${approval2.error}`);
|
||||
}
|
||||
|
||||
const duration = Date.now() - startTime;
|
||||
@@ -208,7 +225,10 @@ export const dataIntegrityTestSuite: TestSuite = {
|
||||
timestamp: new Date().toISOString(),
|
||||
details: {
|
||||
constraintEnforced: true,
|
||||
errorMessage: duplicateError.message
|
||||
firstParkCreated: true,
|
||||
secondParkBlocked: true,
|
||||
errorMessage: approval2.error,
|
||||
followedPipeline: true
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
@@ -225,10 +245,6 @@ export const dataIntegrityTestSuite: TestSuite = {
|
||||
};
|
||||
} finally {
|
||||
await tracker.cleanup();
|
||||
const remaining = await tracker.verifyCleanup();
|
||||
if (remaining.length > 0) {
|
||||
console.warn('integrity-003 cleanup incomplete:', remaining);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user