AI Resource Pro / website / scripts / test-category-61.ts
| 1 | import fetch from 'node-fetch'; |
| 2 | import { createClient } from '@supabase/supabase-js'; |
| 3 | import * as dotenv from 'dotenv'; |
| 4 | import * as path from 'path'; |
| 5 | import * as fs from 'fs'; |
| 6 | |
| 7 | // Load environment variables |
| 8 | const envLocalPath = path.resolve(process.cwd(), '../.env.local'); |
| 9 | if (fs.existsSync(envLocalPath)) { |
| 10 | console.log(`Loading environment from: ${envLocalPath}`); |
| 11 | dotenv.config({ path: envLocalPath }); |
| 12 | } else { |
| 13 | console.log(`No .env.local file found at: ${envLocalPath}`); |
| 14 | dotenv.config(); // Try loading from default .env |
| 15 | } |
| 16 | |
| 17 | async function testCategory61() { |
| 18 | console.log('Testing Category 61 (LLMs) counts...'); |
| 19 | |
| 20 | // Initialize Supabase client for direct database verification |
| 21 | const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL; |
| 22 | const supabaseKey = process.env.SUPABASE_SERVICE_ROLE_KEY; |
| 23 | |
| 24 | if (!supabaseUrl || !supabaseKey) { |
| 25 | console.error('Missing Supabase environment variables. Add NEXT_PUBLIC_SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY to .env.local'); |
| 26 | process.exit(1); |
| 27 | } |
| 28 | |
| 29 | const supabase = createClient(supabaseUrl, supabaseKey); |
| 30 | const categoryId = 61; // LLMs |
| 31 | |
| 32 | // Adjust URL based on your local development setup |
| 33 | const baseUrl = 'http://localhost:3000'; |
| 34 | |
| 35 | try { |
| 36 | // 1. Check category details directly from the database |
| 37 | console.log('\n1. Checking category details in the database...'); |
| 38 | const { data: categoryData, error: categoryError } = await supabase |
| 39 | .from('categories') |
| 40 | .select('*') |
| 41 | .eq('id', categoryId) |
| 42 | .single(); |
| 43 | |
| 44 | if (categoryError) { |
| 45 | console.error('Error fetching category:', categoryError); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | console.log('Category in database:'); |
| 50 | console.log(categoryData); |
| 51 | |
| 52 | // 2. Check all tools in the database that have category_id = 61 |
| 53 | console.log('\n2a. Checking tools directly linked to category ID 61...'); |
| 54 | const { data: directTools, error: directToolsError } = await supabase |
| 55 | .from('tools') |
| 56 | .select('id, name, published, category_id') |
| 57 | .eq('category_id', categoryId); |
| 58 | |
| 59 | if (directToolsError) { |
| 60 | console.error('Error fetching direct tools:', directToolsError); |
| 61 | } else { |
| 62 | console.log(`Found ${directTools?.length || 0} tools directly linked to category ${categoryId}:`); |
| 63 | directTools?.forEach(item => { |
| 64 | console.log(`- ${item.name} (ID: ${item.id}, Published: ${item.published})`); |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | // 3. Check the tools_categories junction table |
| 69 | console.log('\n2b. Checking tools_categories junction table...'); |
| 70 | |
| 71 | // Debug: Log all entries in the tools_categories table |
| 72 | const { data: allToolsCategories, error: allToolsCategoriesError } = await supabase |
| 73 | .from('tools_categories') |
| 74 | .select('*') |
| 75 | .limit(10); |
| 76 | |
| 77 | if (allToolsCategoriesError) { |
| 78 | console.error('Error fetching tools_categories sample:', allToolsCategoriesError); |
| 79 | } else { |
| 80 | console.log(`Sample of tools_categories table (first 10 entries):`); |
| 81 | console.log(allToolsCategories); |
| 82 | |
| 83 | // Check field names and types |
| 84 | if (allToolsCategories && allToolsCategories.length > 0) { |
| 85 | console.log('Field names:', Object.keys(allToolsCategories[0])); |
| 86 | console.log('tool_id type:', typeof allToolsCategories[0].tool_id); |
| 87 | console.log('category_id type:', typeof allToolsCategories[0].category_id); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // Now check entries specifically for category 61 |
| 92 | const { data: toolsJunctionData, error: toolsJunctionError } = await supabase |
| 93 | .from('tools_categories') |
| 94 | .select('tool_id, category_id') |
| 95 | .eq('category_id', categoryId); |
| 96 | |
| 97 | if (toolsJunctionError) { |
| 98 | console.error('Error fetching tools junction data:', toolsJunctionError); |
| 99 | } else { |
| 100 | console.log(`\nFound ${toolsJunctionData?.length || 0} entries in tools_categories for category ${categoryId}`); |
| 101 | if (toolsJunctionData && toolsJunctionData.length > 0) { |
| 102 | console.log('Raw junction entries:'); |
| 103 | toolsJunctionData.forEach(item => { |
| 104 | console.log(`- tool_id: ${item.tool_id}, category_id: ${item.category_id}`); |
| 105 | }); |
| 106 | |
| 107 | // Get details of these tools |
| 108 | const toolIds = toolsJunctionData.map(item => item.tool_id); |
| 109 | |
| 110 | console.log(`\nTool IDs from junction: [${toolIds.join(', ')}]`); |
| 111 | |
| 112 | // Get all tools regardless of published status to see what's there |
| 113 | const { data: allJunctionTools, error: allJunctionToolsError } = await supabase |
| 114 | .from('tools') |
| 115 | .select('id, name, published') |
| 116 | .in('id', toolIds); |
| 117 | |
| 118 | if (allJunctionToolsError) { |
| 119 | console.error('Error fetching all tools from junction:', allJunctionToolsError); |
| 120 | } else { |
| 121 | console.log(`\nAll tools found via junction table (${allJunctionTools?.length || 0}):`); |
| 122 | allJunctionTools?.forEach(item => { |
| 123 | console.log(`- ${item.name} (ID: ${item.id}, Published: ${item.published})`); |
| 124 | }); |
| 125 | } |
| 126 | |
| 127 | // Get only published tools |
| 128 | const { data: junctionTools, error: junctionToolsError } = await supabase |
| 129 | .from('tools') |
| 130 | .select('id, name, published') |
| 131 | .in('id', toolIds) |
| 132 | .eq('published', true); |
| 133 | |
| 134 | if (junctionToolsError) { |
| 135 | console.error('Error fetching published tools from junction:', junctionToolsError); |
| 136 | } else { |
| 137 | console.log(`\nPublished tools found via junction table (${junctionTools?.length || 0}):`); |
| 138 | junctionTools?.forEach(item => { |
| 139 | console.log(`- ${item.name} (ID: ${item.id}, Published: ${item.published})`); |
| 140 | }); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // 4. Fetch from API and check counts |
| 146 | console.log('\n3. Fetching from categories API...'); |
| 147 | const response = await fetch(`${baseUrl}/api/categories`); |
| 148 | |
| 149 | if (!response.ok) { |
| 150 | throw new Error(`API request failed with status ${response.status}`); |
| 151 | } |
| 152 | |
| 153 | interface Category { |
| 154 | id: number; |
| 155 | name: string; |
| 156 | slug: string; |
| 157 | icon: string; |
| 158 | description: string; |
| 159 | tools_count: number; |
| 160 | news_count: number; |
| 161 | learning_count: number; |
| 162 | posts_count: number; |
| 163 | total_count: number; |
| 164 | [key: string]: any; |
| 165 | } |
| 166 | |
| 167 | const categories = await response.json() as Category[]; |
| 168 | const apiCategory = categories.find((c: Category) => c.id === categoryId); |
| 169 | |
| 170 | if (!apiCategory) { |
| 171 | console.error('Category not found in API response'); |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | console.log('Category from API:'); |
| 176 | console.log(JSON.stringify(apiCategory, null, 2)); |
| 177 | |
| 178 | // 5. Verify our ID comparison logic - Type conversion test |
| 179 | console.log('\n4. Testing ID comparison logic...'); |
| 180 | |
| 181 | // Get first few tools entries to test |
| 182 | const { data: testTools } = await supabase |
| 183 | .from('tools') |
| 184 | .select('id') |
| 185 | .limit(5); |
| 186 | |
| 187 | // Get first few tools_categories entries |
| 188 | const { data: testJunctions } = await supabase |
| 189 | .from('tools_categories') |
| 190 | .select('tool_id, category_id') |
| 191 | .limit(5); |
| 192 | |
| 193 | if (testTools && testTools.length > 0 && testJunctions && testJunctions.length > 0) { |
| 194 | console.log('Sample tool ID:', testTools[0].id, 'Type:', typeof testTools[0].id); |
| 195 | console.log('Sample junction tool_id:', testJunctions[0].tool_id, 'Type:', typeof testJunctions[0].tool_id); |
| 196 | |
| 197 | const toolIdSet = new Set(testTools.map(t => t.id)); |
| 198 | |
| 199 | // Test if we can find a match |
| 200 | const matchFound = testJunctions.some(j => toolIdSet.has(j.tool_id)); |
| 201 | console.log('Match test using Set: ', matchFound); |
| 202 | |
| 203 | // Test with explicit comparison for first items |
| 204 | console.log('Direct comparison:', testTools[0].id === testJunctions[0].tool_id); |
| 205 | console.log('String comparison:', String(testTools[0].id) === String(testJunctions[0].tool_id)); |
| 206 | console.log('Number comparison:', Number(testTools[0].id) === Number(testJunctions[0].tool_id)); |
| 207 | } |
| 208 | |
| 209 | // 6. Calculate what counts should be from database data |
| 210 | console.log('\n5. Computing expected counts directly...'); |
| 211 | |
| 212 | // Get all published tools |
| 213 | const { data: allPublishedTools } = await supabase |
| 214 | .from('tools') |
| 215 | .select('id') |
| 216 | .eq('published', true); |
| 217 | |
| 218 | // Get all junction entries for this category |
| 219 | const { data: allCategoryJunctions } = await supabase |
| 220 | .from('tools_categories') |
| 221 | .select('tool_id') |
| 222 | .eq('category_id', categoryId); |
| 223 | |
| 224 | if (allPublishedTools && allCategoryJunctions) { |
| 225 | const publishedToolIds = new Set(allPublishedTools.map(t => t.id)); |
| 226 | const junctionToolIds = allCategoryJunctions.map(j => j.tool_id); |
| 227 | |
| 228 | // Count how many junction entries match published tools |
| 229 | const expectedCount = junctionToolIds.filter(id => publishedToolIds.has(id)).length; |
| 230 | |
| 231 | console.log(`Expected tools count: ${expectedCount}`); |
| 232 | console.log(`API tools_count: ${apiCategory.tools_count}`); |
| 233 | |
| 234 | if (expectedCount === apiCategory.tools_count) { |
| 235 | console.log('✅ Counts match expected value!'); |
| 236 | } else { |
| 237 | console.log('❌ Counts do not match expected value!'); |
| 238 | |
| 239 | // Show differences |
| 240 | console.log('\nDetailed analysis:'); |
| 241 | console.log(`Number of published tools: ${publishedToolIds.size}`); |
| 242 | console.log(`Number of junction entries for category ${categoryId}: ${junctionToolIds.length}`); |
| 243 | |
| 244 | // Find the specific tool IDs that should be counted |
| 245 | const matchingIds = junctionToolIds.filter(id => publishedToolIds.has(id)); |
| 246 | console.log(`Tool IDs that should be counted: [${matchingIds.join(', ')}]`); |
| 247 | |
| 248 | // Get details for these tools |
| 249 | if (matchingIds.length > 0) { |
| 250 | const { data: matchingTools } = await supabase |
| 251 | .from('tools') |
| 252 | .select('id, name, published') |
| 253 | .in('id', matchingIds); |
| 254 | |
| 255 | console.log('Tools that should be counted:'); |
| 256 | matchingTools?.forEach(tool => { |
| 257 | console.log(`- ${tool.name} (ID: ${tool.id}, Published: ${tool.published})`); |
| 258 | }); |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | } catch (error) { |
| 264 | console.error('Error during testing:', error); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // Run the function |
| 269 | testCategory61() |
| 270 | .then(() => { |
| 271 | console.log('Testing completed'); |
| 272 | process.exit(0); |
| 273 | }) |
| 274 | .catch((error) => { |
| 275 | console.error('Testing failed:', error); |
| 276 | process.exit(1); |
| 277 | }); |