const { createClient } = require('@supabase/supabase-js'); require('dotenv').config({ path: '../../.env' }); const supabaseUrl = process.env.SUPABASE_URL; const supabaseKey = process.env.SUPABASE_SERVICE_ROLE_KEY || process.env.SUPABASE_KEY; if (!supabaseUrl || !supabaseKey) { console.error("Missing Supabase credentials in .env"); process.exit(1); } const supabase = createClient(supabaseUrl, supabaseKey); async function runStressTest() { console.log("🚀 Starting TA Dashboard Stress Test Data Generation..."); // 1. Get an active space const { data: spaces, error: spaceError } = await supabase.from('spaces').select('id, name').limit(1); if (spaceError || !spaces.length) { console.error("No spaces found to test.", spaceError?.message); return; } const spaceId = spaces[0].id; console.log(`Using Space: ${spaces[0].name} (${spaceId})`); // 2. Create 20 Fake Students const students = []; for (let i = 1; i <= 20; i++) { const email = `stress_student_${i}_${Math.random().toString(36).substring(7)}@test.com`; const password = 'Password123!'; console.log(`Creating user ${i}: ${email}`); // Create Auth User (using admin API to bypass confirmation) const { data: authData, error: authError } = await supabase.auth.admin.createUser({ email: email, password: password, email_confirm: true, user_metadata: { display_name: `Học viên Stress Test ${i}` } }); if (authError) { console.error(`Error creating auth user ${i}:`, authError.message); continue; } const userId = authData.user.id; console.log(`User created in Auth: ${userId}. Creating profile...`); // Create Profile manually const { error: pError } = await supabase.from('profiles').upsert({ id: userId, display_name: `Học viên Stress Test ${i}`, username: `student_stress_${i}_${Math.random().toString(36).substring(7)}`, email: email, last_seen: new Date(Date.now() - Math.random() * 864000000).toISOString() }); if (pError) { console.error(`Error creating profile for user ${i}:`, pError.message); continue; } // Join Space const { error: mError } = await supabase.from('space_members').upsert({ space_id: spaceId, user_id: userId, role: 'member' }); if (!mError) { students.push({ id: userId, name: `Học viên Stress Test ${i}` }); } else { console.error(`Error joining space for user ${i}:`, mError.message); } } console.log(`✅ Created ${students.length} real auth students and joined them to the space.`); // 3. Generate At-Risk Snapshots with varying levels const levels = ['critical', 'warning']; const reasons = [ 'Không online trong 72h qua', 'Chưa nộp bài tập SQL tuần 4', 'Điểm Quiz 1 cực thấp (2/10)', 'Phát hiện từ khóa tiêu cực: "muốn bỏ học", "khó quá"', 'Vắng mặt 3 buổi liên tiếp' ]; for (const student of students) { const level = levels[Math.floor(Math.random() * levels.length)]; const reason = reasons[Math.floor(Math.random() * reasons.length)]; const score = level === 'critical' ? 5 + Math.floor(Math.random() * 5) : 2 + Math.floor(Math.random() * 3); const { error: sError } = await supabase.from('at_risk_snapshots').insert({ space_id: spaceId, user_id: student.id, level: level, reason: reason, hours_since_active: Math.floor(Math.random() * 168), metadata: { score: score, signals: [reason, 'Thao tác giả lập stress test'], last_meaningful_seen: new Date(Date.now() - 86400000).toISOString() } }); if (sError) console.error(`Error creating snapshot for ${student.name}:`, sError.message); } console.log("🏁 Stress test data generated! Check your TA Dashboard now."); } runStressTest();