import { createClient } from '@supabase/supabase-js'; import * as dotenv from 'dotenv'; import * as path from 'path'; import * as fs from 'fs'; // Load environment variables const envLocalPath = path.resolve(process.cwd(), '.env.local'); if (fs.existsSync(envLocalPath)) { console.log(`Loading environment from: ${envLocalPath}`); dotenv.config({ path: envLocalPath }); } else { console.log(`No .env.local file found at: ${envLocalPath}`); dotenv.config(); // Try loading from default .env } async function testCategorySeeding() { console.log('Testing category seeding...'); // Initialize Supabase client const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL; const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY; if (!supabaseUrl || !supabaseServiceKey) { console.error('Missing Supabase environment variables. Please set NEXT_PUBLIC_SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY.'); process.exit(1); } const supabase = createClient(supabaseUrl, supabaseServiceKey); try { // Test schema setup console.log('\n1. Checking if categories table exists with the correct schema...'); const { data: categories, error: categoriesError } = await supabase .from('categories') .select('*') .limit(1); if (categoriesError) { console.error('Error checking categories table:', categoriesError); return; } if (categories && categories.length > 0) { console.log('Categories table exists. Sample category:'); console.log(categories[0]); } else { console.log('Categories table exists but is empty.'); } // List all categories console.log('\n2. Listing all categories...'); const { data: allCategories, error: allCategoriesError } = await supabase .from('categories') .select('*'); if (allCategoriesError) { console.error('Error listing categories:', allCategoriesError); return; } console.log(`Found ${allCategories?.length || 0} categories:`); allCategories?.forEach(cat => { console.log(`- ${cat.name} (${cat.slug}): ${cat.icon} - ${cat.tool_count} tools`); }); // Specific check for LLMs category console.log('\n2a. Checking specifically for LLMs category...'); const { data: llmsCategory, error: llmsError } = await supabase .from('categories') .select('*') .eq('name', 'LLMs') .single(); if (llmsError) { console.error('Error finding LLMs category:', llmsError); console.log('⚠️ WARNING: LLMs category not found! The name may still be "Large Language Models"'); } else { console.log('✅ LLMs category found:'); console.log(`- ${llmsCategory.name} (${llmsCategory.slug}): ${llmsCategory.icon} - ${llmsCategory.tool_count} tools`); } // Check for any remaining Large Language Models category console.log('\n2b. Checking if any "Large Language Models" category remains...'); const { data: oldLLMCategory, error: oldLLMError } = await supabase .from('categories') .select('*') .or('name.eq.Large Language Models,name.eq.Large Language Model') .maybeSingle(); if (oldLLMCategory) { console.log('⚠️ WARNING: Found old category that should have been renamed:'); console.log(`- ${oldLLMCategory.name} (${oldLLMCategory.slug}): ${oldLLMCategory.icon} - ${oldLLMCategory.tool_count} tools`); } else { console.log('✅ No "Large Language Models" category found - migration successful'); } // Check tools with categories console.log('\n3. Checking tools with their categories...'); const { data: tools, error: toolsError } = await supabase .from('tools') .select(` name, categories:category_id(name, icon) `) .limit(5); if (toolsError) { console.error('Error checking tools with categories:', toolsError); return; } console.log('Sample tools with categories:'); tools?.forEach(tool => { // TypeScript workaround for the category join result const categoryName = Array.isArray(tool.categories) ? tool.categories[0]?.name : (tool.categories as any)?.name; console.log(`- ${tool.name}: Category = ${categoryName || 'Unknown'}`); }); // Check tools that should be in LLMs category console.log('\n3a. Checking tools in LLMs category...'); const { data: llmsTools, error: llmsToolsError } = await supabase .from('tools') .select(` name, categories:category_id(name, icon) `) .eq('categories.name', 'LLMs'); if (llmsToolsError) { console.error('Error checking LLMs tools:', llmsToolsError); } else { console.log(`Found ${llmsTools?.length || 0} tools in LLMs category:`); llmsTools?.slice(0, 5).forEach(tool => { const categoryName = Array.isArray(tool.categories) ? tool.categories[0]?.name : (tool.categories as any)?.name; console.log(`- ${tool.name}: Category = ${categoryName || 'Unknown'}`); }); if ((llmsTools?.length || 0) > 5) { console.log(`... and ${llmsTools!.length - 5} more`); } } // Check news with categories console.log('\n4. Checking news with their categories...'); const { data: news, error: newsError } = await supabase .from('news') .select(` title, category_name, categories:category_id(name, icon) `) .limit(5); if (newsError) { console.error('Error checking news with categories:', newsError); return; } console.log('Sample news with categories:'); news?.forEach(item => { // TypeScript workaround for the category join result const categoryName = Array.isArray(item.categories) ? item.categories[0]?.name : (item.categories as any)?.name; console.log(`- ${item.title}: Category = ${categoryName || 'Unknown'} (Original: ${item.category_name})`); }); console.log('\nCategory seeding test completed.'); } catch (error) { console.error('Error during testing:', error); } } // Run the function testCategorySeeding() .then(() => { console.log('Testing completed'); process.exit(0); }) .catch((error) => { console.error('Testing failed:', error); process.exit(1); });