|
| 1 | +#!/usr/bin/env bun |
| 2 | +import fs from 'node:fs/promises' |
| 3 | +import path from 'node:path' |
| 4 | +import crypto from 'node:crypto' |
| 5 | +import os from 'node:os' |
| 6 | + |
| 7 | +import db from '../../common/src/db' // Corrected import path |
| 8 | +import * as schema from '../../common/src/db/schema' // Corrected import path |
| 9 | +import { genAuthCode } from '../../common/src/util/credentials' // Corrected import path |
| 10 | + |
| 11 | +async function seedTestUser() { |
| 12 | + console.log('🌱 Starting test user seeding...') |
| 13 | + |
| 14 | + const nextAuthSecret = process.env.NEXTAUTH_SECRET |
| 15 | + if (!nextAuthSecret) { |
| 16 | + console.error('❌ NEXTAUTH_SECRET environment variable is not set.') |
| 17 | + process.exit(1) |
| 18 | + } |
| 19 | + |
| 20 | + const userId = `test-user-${crypto.randomUUID()}` |
| 21 | + const userEmail = `test-${crypto.randomBytes(8).toString('hex')}@example.com` |
| 22 | + const userName = 'E2E Test User' |
| 23 | + const fingerprintId = `test-fp-${crypto.randomUUID()}` |
| 24 | + const sessionToken = `test-session-${crypto.randomUUID()}` |
| 25 | + |
| 26 | + // For the fingerprintHash, we need an expiry. Let's set it far in the future for the test. |
| 27 | + const expiresAt = new Date(Date.now() + 365 * 24 * 60 * 60 * 1000) // 1 year |
| 28 | + const fingerprintHash = genAuthCode(fingerprintId, expiresAt.getTime().toString(), nextAuthSecret) |
| 29 | + |
| 30 | + try { |
| 31 | + // 1. Create User |
| 32 | + await db.insert(schema.user).values({ |
| 33 | + id: userId, |
| 34 | + email: userEmail, |
| 35 | + name: userName, |
| 36 | + emailVerified: new Date(), // Mark as verified for simplicity |
| 37 | + }) |
| 38 | + console.log(`👤 Created user: ${userId} (${userEmail})`) |
| 39 | + |
| 40 | + // 2. Create Fingerprint |
| 41 | + await db.insert(schema.fingerprint).values({ |
| 42 | + id: fingerprintId, |
| 43 | + sig_hash: fingerprintHash, // This hash links the fingerprint to the session/credentials |
| 44 | + created_at: new Date(), |
| 45 | + }) |
| 46 | + console.log(`👆 Created fingerprint: ${fingerprintId}`) |
| 47 | + |
| 48 | + // 3. Create Session |
| 49 | + await db.insert(schema.session).values({ |
| 50 | + sessionToken: sessionToken, |
| 51 | + userId: userId, |
| 52 | + expires: expiresAt, |
| 53 | + fingerprint_id: fingerprintId, |
| 54 | + }) |
| 55 | + console.log(`🔑 Created session: ${sessionToken} for user ${userId}`) |
| 56 | + |
| 57 | + // 4. Create credentials.json |
| 58 | + const credentials = { |
| 59 | + default: { |
| 60 | + id: userId, |
| 61 | + email: userEmail, |
| 62 | + name: userName, |
| 63 | + authToken: sessionToken, |
| 64 | + fingerprintId: fingerprintId, |
| 65 | + fingerprintHash: fingerprintHash, |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + // Determine credentials path (mimicking npm-app/src/credentials.ts logic for 'local' env) |
| 70 | + const configDir = path.join(os.homedir(), '.config', 'manicode-local') |
| 71 | + const credentialsPath = path.join(configDir, 'credentials.json') |
| 72 | + |
| 73 | + await fs.mkdir(configDir, { recursive: true }) |
| 74 | + await fs.writeFile(credentialsPath, JSON.stringify(credentials, null, 2)) |
| 75 | + console.log(`📝 Wrote credentials to: ${credentialsPath}`) |
| 76 | + |
| 77 | + console.log('✅ Test user seeding complete!') |
| 78 | + } catch (error) { |
| 79 | + console.error('❌ Error during test user seeding:', error) |
| 80 | + process.exit(1) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +if (require.main === module) { |
| 85 | + seedTestUser().catch((err) => { |
| 86 | + console.error('Unhandled error in seedTestUser:', err) |
| 87 | + process.exit(1) |
| 88 | + }) |
| 89 | +} |
0 commit comments