|
| 1 | +/** |
| 2 | + * Tests for export-meta.ts configuration validation |
| 3 | + * |
| 4 | + * These tests validate that the export-meta config uses correct table names |
| 5 | + * and includes all required fields for exporting meta_public and collections_public data. |
| 6 | + */ |
| 7 | + |
| 8 | +import { readFileSync } from 'fs'; |
| 9 | +import { join } from 'path'; |
| 10 | + |
| 11 | +describe('Export Meta Config Validation', () => { |
| 12 | + let exportMetaSource: string; |
| 13 | + |
| 14 | + beforeAll(() => { |
| 15 | + // Read the source file to validate the config |
| 16 | + const filePath = join(__dirname, '../../src/export/export-meta.ts'); |
| 17 | + exportMetaSource = readFileSync(filePath, 'utf-8'); |
| 18 | + }); |
| 19 | + |
| 20 | + describe('table name validation', () => { |
| 21 | + it('should use singular table name "database_extension" (not plural "database_extensions")', () => { |
| 22 | + // The actual table in collections_public is database_extension (singular) |
| 23 | + // This test ensures the config uses the correct table name |
| 24 | + |
| 25 | + // Check that the config defines the table correctly |
| 26 | + expect(exportMetaSource).toContain("table: 'database_extension'"); |
| 27 | + |
| 28 | + // Ensure we're not using the incorrect plural form in the table definition |
| 29 | + // Note: We check specifically in the config section, not the query section |
| 30 | + const configSection = exportMetaSource.split('const config:')[1]?.split('interface ExportMetaParams')[0] || ''; |
| 31 | + expect(configSection).not.toContain("table: 'database_extensions'"); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should query the correct table name in collections_public.database_extension', () => { |
| 35 | + // The query should use the correct singular table name |
| 36 | + expect(exportMetaSource).toContain('FROM collections_public.database_extension'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should include field table in queries', () => { |
| 40 | + // The field table should be queried (it was missing before) |
| 41 | + expect(exportMetaSource).toContain("queryAndParse('field'"); |
| 42 | + expect(exportMetaSource).toContain('FROM collections_public.field'); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('collections_public tables', () => { |
| 47 | + it('should include all required collections_public tables in config', () => { |
| 48 | + const requiredTables = [ |
| 49 | + 'database', |
| 50 | + 'database_extension', |
| 51 | + 'schema', |
| 52 | + 'table', |
| 53 | + 'field' |
| 54 | + ]; |
| 55 | + |
| 56 | + for (const table of requiredTables) { |
| 57 | + expect(exportMetaSource).toContain(`queryAndParse('${table}'`); |
| 58 | + } |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('meta_public tables', () => { |
| 63 | + it('should include all required meta_public tables in config', () => { |
| 64 | + const requiredTables = [ |
| 65 | + 'domains', |
| 66 | + 'sites', |
| 67 | + 'apis', |
| 68 | + 'apps', |
| 69 | + 'site_modules', |
| 70 | + 'site_themes', |
| 71 | + 'api_modules', |
| 72 | + 'api_extensions', |
| 73 | + 'api_schemata', |
| 74 | + 'rls_module', |
| 75 | + 'user_auth_module' |
| 76 | + ]; |
| 77 | + |
| 78 | + for (const table of requiredTables) { |
| 79 | + expect(exportMetaSource).toContain(`queryAndParse('${table}'`); |
| 80 | + } |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('query order validation', () => { |
| 85 | + it('should query database before dependent tables', () => { |
| 86 | + // database should be queried first as other tables depend on it |
| 87 | + const databaseQueryIndex = exportMetaSource.indexOf("queryAndParse('database',"); |
| 88 | + const schemaQueryIndex = exportMetaSource.indexOf("queryAndParse('schema',"); |
| 89 | + const tableQueryIndex = exportMetaSource.indexOf("queryAndParse('table',"); |
| 90 | + |
| 91 | + expect(databaseQueryIndex).toBeLessThan(schemaQueryIndex); |
| 92 | + expect(schemaQueryIndex).toBeLessThan(tableQueryIndex); |
| 93 | + }); |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +describe('Export Meta Config Drift Detection', () => { |
| 98 | + it('should document the expected table names in collections_public', () => { |
| 99 | + // This test documents the expected table names that export-meta.ts should use |
| 100 | + // If these change, the export-meta.ts config needs to be updated |
| 101 | + const expectedCollectionsPublicTables = [ |
| 102 | + 'database', |
| 103 | + 'database_extension', // NOT 'database_extensions' (plural) |
| 104 | + 'schema', |
| 105 | + 'table', |
| 106 | + 'field' |
| 107 | + ]; |
| 108 | + |
| 109 | + // Document the expected tables |
| 110 | + expect(expectedCollectionsPublicTables).toContain('database_extension'); |
| 111 | + expect(expectedCollectionsPublicTables).not.toContain('database_extensions'); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should document the expected table names in meta_public', () => { |
| 115 | + // This test documents the expected table names that export-meta.ts should use |
| 116 | + const expectedMetaPublicTables = [ |
| 117 | + 'apis', |
| 118 | + 'api_extensions', |
| 119 | + 'api_modules', |
| 120 | + 'api_schemata', |
| 121 | + 'apps', |
| 122 | + 'domains', |
| 123 | + 'site_modules', |
| 124 | + 'site_themes', |
| 125 | + 'sites', |
| 126 | + 'rls_module', |
| 127 | + 'user_auth_module' |
| 128 | + ]; |
| 129 | + |
| 130 | + // Document the expected tables |
| 131 | + expect(expectedMetaPublicTables.length).toBe(11); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should document the bug: config uses database_extensions but table is database_extension', () => { |
| 135 | + // BUG DOCUMENTATION: |
| 136 | + // In export-meta.ts, line 26, the config defines: |
| 137 | + // table: 'database_extensions' (plural) |
| 138 | + // But the actual table in db-meta-schema is: |
| 139 | + // collections_public.database_extension (singular) |
| 140 | + // |
| 141 | + // This causes the Parser to generate INSERT statements with the wrong table name, |
| 142 | + // which will fail when the exported SQL is replayed. |
| 143 | + // |
| 144 | + // FIX: Change line 26 in export-meta.ts from: |
| 145 | + // table: 'database_extensions' |
| 146 | + // to: |
| 147 | + // table: 'database_extension' |
| 148 | + |
| 149 | + const buggyConfigTableName = 'database_extensions'; |
| 150 | + const correctTableName = 'database_extension'; |
| 151 | + |
| 152 | + expect(buggyConfigTableName).not.toBe(correctTableName); |
| 153 | + }); |
| 154 | +}); |
0 commit comments