AI Resource Pro / website / scripts / test-category-seeding.ts
| 1 | import { createClient } from '@supabase/supabase-js'; |
| 2 | import * as dotenv from 'dotenv'; |
| 3 | import * as path from 'path'; |
| 4 | import * as fs from 'fs'; |
| 5 | |
| 6 | // Load environment variables |
| 7 | const envLocalPath = path.resolve(process.cwd(), '.env.local'); |
| 8 | if (fs.existsSync(envLocalPath)) { |
| 9 | console.log(`Loading environment from: ${envLocalPath}`); |
| 10 | dotenv.config({ path: envLocalPath }); |
| 11 | } else { |
| 12 | console.log(`No .env.local file found at: ${envLocalPath}`); |
| 13 | dotenv.config(); // Try loading from default .env |
| 14 | } |
| 15 | |
| 16 | async function testCategorySeeding() { |
| 17 | console.log('Testing category seeding...'); |
| 18 | |
| 19 | // Initialize Supabase client |
| 20 | const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL; |
| 21 | const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY; |
| 22 | |
| 23 | if (!supabaseUrl || !supabaseServiceKey) { |
| 24 | console.error('Missing Supabase environment variables. Please set NEXT_PUBLIC_SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY.'); |
| 25 | process.exit(1); |
| 26 | } |
| 27 | |
| 28 | const supabase = createClient(supabaseUrl, supabaseServiceKey); |
| 29 | |
| 30 | try { |
| 31 | // Test schema setup |
| 32 | console.log('\n1. Checking if categories table exists with the correct schema...'); |
| 33 | const { data: categories, error: categoriesError } = await supabase |
| 34 | .from('categories') |
| 35 | .select('*') |
| 36 | .limit(1); |
| 37 | |
| 38 | if (categoriesError) { |
| 39 | console.error('Error checking categories table:', categoriesError); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | if (categories && categories.length > 0) { |
| 44 | console.log('Categories table exists. Sample category:'); |
| 45 | console.log(categories[0]); |
| 46 | } else { |
| 47 | console.log('Categories table exists but is empty.'); |
| 48 | } |
| 49 | |
| 50 | // List all categories |
| 51 | console.log('\n2. Listing all categories...'); |
| 52 | const { data: allCategories, error: allCategoriesError } = await supabase |
| 53 | .from('categories') |
| 54 | .select('*'); |
| 55 | |
| 56 | if (allCategoriesError) { |
| 57 | console.error('Error listing categories:', allCategoriesError); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | console.log(`Found ${allCategories?.length || 0} categories:`); |
| 62 | allCategories?.forEach(cat => { |
| 63 | console.log(`- ${cat.name} (${cat.slug}): ${cat.icon} - ${cat.tool_count} tools`); |
| 64 | }); |
| 65 | |
| 66 | // Specific check for LLMs category |
| 67 | console.log('\n2a. Checking specifically for LLMs category...'); |
| 68 | const { data: llmsCategory, error: llmsError } = await supabase |
| 69 | .from('categories') |
| 70 | .select('*') |
| 71 | .eq('name', 'LLMs') |
| 72 | .single(); |
| 73 | |
| 74 | if (llmsError) { |
| 75 | console.error('Error finding LLMs category:', llmsError); |
| 76 | console.log('⚠️ WARNING: LLMs category not found! The name may still be "Large Language Models"'); |
| 77 | } else { |
| 78 | console.log('✅ LLMs category found:'); |
| 79 | console.log(`- ${llmsCategory.name} (${llmsCategory.slug}): ${llmsCategory.icon} - ${llmsCategory.tool_count} tools`); |
| 80 | } |
| 81 | |
| 82 | // Check for any remaining Large Language Models category |
| 83 | console.log('\n2b. Checking if any "Large Language Models" category remains...'); |
| 84 | const { data: oldLLMCategory, error: oldLLMError } = await supabase |
| 85 | .from('categories') |
| 86 | .select('*') |
| 87 | .or('name.eq.Large Language Models,name.eq.Large Language Model') |
| 88 | .maybeSingle(); |
| 89 | |
| 90 | if (oldLLMCategory) { |
| 91 | console.log('⚠️ WARNING: Found old category that should have been renamed:'); |
| 92 | console.log(`- ${oldLLMCategory.name} (${oldLLMCategory.slug}): ${oldLLMCategory.icon} - ${oldLLMCategory.tool_count} tools`); |
| 93 | } else { |
| 94 | console.log('✅ No "Large Language Models" category found - migration successful'); |
| 95 | } |
| 96 | |
| 97 | // Check tools with categories |
| 98 | console.log('\n3. Checking tools with their categories...'); |
| 99 | const { data: tools, error: toolsError } = await supabase |
| 100 | .from('tools') |
| 101 | .select(` |
| 102 | name, |
| 103 | categories:category_id(name, icon) |
| 104 | `) |
| 105 | .limit(5); |
| 106 | |
| 107 | if (toolsError) { |
| 108 | console.error('Error checking tools with categories:', toolsError); |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | console.log('Sample tools with categories:'); |
| 113 | tools?.forEach(tool => { |
| 114 | // TypeScript workaround for the category join result |
| 115 | const categoryName = Array.isArray(tool.categories) |
| 116 | ? tool.categories[0]?.name |
| 117 | : (tool.categories as any)?.name; |
| 118 | |
| 119 | console.log(`- ${tool.name}: Category = ${categoryName || 'Unknown'}`); |
| 120 | }); |
| 121 | |
| 122 | // Check tools that should be in LLMs category |
| 123 | console.log('\n3a. Checking tools in LLMs category...'); |
| 124 | const { data: llmsTools, error: llmsToolsError } = await supabase |
| 125 | .from('tools') |
| 126 | .select(` |
| 127 | name, |
| 128 | categories:category_id(name, icon) |
| 129 | `) |
| 130 | .eq('categories.name', 'LLMs'); |
| 131 | |
| 132 | if (llmsToolsError) { |
| 133 | console.error('Error checking LLMs tools:', llmsToolsError); |
| 134 | } else { |
| 135 | console.log(`Found ${llmsTools?.length || 0} tools in LLMs category:`); |
| 136 | llmsTools?.slice(0, 5).forEach(tool => { |
| 137 | const categoryName = Array.isArray(tool.categories) |
| 138 | ? tool.categories[0]?.name |
| 139 | : (tool.categories as any)?.name; |
| 140 | |
| 141 | console.log(`- ${tool.name}: Category = ${categoryName || 'Unknown'}`); |
| 142 | }); |
| 143 | |
| 144 | if ((llmsTools?.length || 0) > 5) { |
| 145 | console.log(`... and ${llmsTools!.length - 5} more`); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // Check news with categories |
| 150 | console.log('\n4. Checking news with their categories...'); |
| 151 | const { data: news, error: newsError } = await supabase |
| 152 | .from('news') |
| 153 | .select(` |
| 154 | title, |
| 155 | category_name, |
| 156 | categories:category_id(name, icon) |
| 157 | `) |
| 158 | .limit(5); |
| 159 | |
| 160 | if (newsError) { |
| 161 | console.error('Error checking news with categories:', newsError); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | console.log('Sample news with categories:'); |
| 166 | news?.forEach(item => { |
| 167 | // TypeScript workaround for the category join result |
| 168 | const categoryName = Array.isArray(item.categories) |
| 169 | ? item.categories[0]?.name |
| 170 | : (item.categories as any)?.name; |
| 171 | |
| 172 | console.log(`- ${item.title}: Category = ${categoryName || 'Unknown'} (Original: ${item.category_name})`); |
| 173 | }); |
| 174 | |
| 175 | console.log('\nCategory seeding test completed.'); |
| 176 | } catch (error) { |
| 177 | console.error('Error during testing:', error); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // Run the function |
| 182 | testCategorySeeding() |
| 183 | .then(() => { |
| 184 | console.log('Testing completed'); |
| 185 | process.exit(0); |
| 186 | }) |
| 187 | .catch((error) => { |
| 188 | console.error('Testing failed:', error); |
| 189 | process.exit(1); |
| 190 | }); |