-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-ai-chat.ts
More file actions
151 lines (134 loc) · 5.55 KB
/
test-ai-chat.ts
File metadata and controls
151 lines (134 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* Quick test to verify AI chat flow
* Run with: npx ts-node --esm test-ai-chat.ts
* Or: npx tsx test-ai-chat.ts
*/
import { config } from 'dotenv'
config({ path: '.env.local' })
import { hash } from 'bcryptjs'
import pool from './lib/db'
async function test() {
try {
console.log('🧪 Testing SmartNotes AI Chat Flow\n')
// 1. Create a test teacher
console.log('1️⃣ Creating test teacher...')
const teacherPassword = await hash('password123', 10)
const teacher = await pool.query(
`INSERT INTO users (email, name, password_hash, role, plan)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (email) DO UPDATE SET password_hash = $3
RETURNING id, email`,
)
const teacherId = teacher.rows[0].id
console.log(` ✓ Teacher created: ${teacher.rows[0].email} (id: ${teacherId})\n`)
// 2. Create a test note
console.log('2️⃣ Creating test note...')
const note = await pool.query(
`INSERT INTO notes
(teacher_id, title, learning_objective, system_prompt, initial_prompt, is_active, share_token)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id, share_token`,
[
teacherId,
'AI Chat Test',
'Test the AI chat functionality',
'You are a helpful educational assistant. Be concise and clear.',
'Hello! I am ready to help you learn. What would you like to discuss?',
true,
'test' + Math.random().toString(36).slice(2)
]
)
const noteId = note.rows[0].id
const shareToken = note.rows[0].share_token
console.log(` ✓ Note created: "${note.rows[0].share_token}" (id: ${noteId})\n`)
// 3. Create a test student
console.log('3️⃣ Creating test student...')
const studentPassword = await hash('student123', 10)
const student = await pool.query(
`INSERT INTO users (email, name, password_hash, role, plan)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (email) DO UPDATE SET password_hash = $3
RETURNING id, email`,
)
const studentId = student.rows[0].id
console.log(` ✓ Student created: ${student.rows[0].email} (id: ${studentId})\n`)
// 4. Create a conversation
console.log('4️⃣ Creating conversation...')
const conv = await pool.query(
`INSERT INTO conversations (note_id, student_id, started_at, is_complete)
VALUES ($1, $2, NOW(), false)
RETURNING id`,
[noteId, studentId]
)
const convId = conv.rows[0].id
console.log(` ✓ Conversation created (id: ${convId})\n`)
// 5. Save initial AI message
console.log('5️⃣ Saving initial AI message...')
await pool.query(
`INSERT INTO messages (conversation_id, role, content, sequence_number)
VALUES ($1, $2, $3, $4)`,
[convId, 'ai', 'Hello! I am ready to help you learn. What would you like to discuss?', 0]
)
console.log(' ✓ Initial message saved\n')
// 6. Save a student message
console.log('6️⃣ Saving student message...')
await pool.query(
`INSERT INTO messages (conversation_id, role, content, sequence_number)
VALUES ($1, $2, $3, $4)`,
[convId, 'student', 'Can you explain machine learning in simple terms?', 1]
)
console.log(' ✓ Student message saved\n')
// 7. Test AI response
console.log('7️⃣ Testing Gemini API...')
if (!process.env.GEMINI_API_KEY) {
console.log(' ⚠️ GEMINI_API_KEY not set, skipping AI test')
} else {
const OpenAI = (await import('openai')).default
const client = new OpenAI({
apiKey: process.env.GEMINI_API_KEY,
baseURL: process.env.AI_BASE_URL ?? 'https://generativelanguage.googleapis.com/v1beta/openai/',
})
const completion = await client.chat.completions.create({
model: process.env.GEMINI_MODEL ?? 'gemini-2.0-flash',
messages: [
{ role: 'system', content: 'You are a helpful educational assistant. Be concise and clear.' },
{ role: 'user', content: 'Can you explain machine learning in simple terms?' },
],
})
const aiResponse = completion.choices[0].message.content
console.log(' ✓ AI responded:', aiResponse?.slice(0, 100), '...\n')
// 8. Save AI response
console.log('8️⃣ Saving AI response...')
await pool.query(
`INSERT INTO messages (conversation_id, role, content, sequence_number)
VALUES ($1, $2, $3, $4)`,
[convId, 'ai', aiResponse, 2]
)
console.log(' ✓ AI response saved\n')
}
// 9. Verify conversation history
console.log('9️⃣ Verifying conversation history...')
const messages = await pool.query(
'SELECT role, content, sequence_number FROM messages WHERE conversation_id = $1 ORDER BY sequence_number',
[convId]
)
console.log(` ✓ ${messages.rows.length} messages in conversation:`)
messages.rows.forEach((m: any) => {
console.log(` [${m.sequence_number}] ${m.role}: ${m.content.slice(0, 50)}...`)
})
console.log()
console.log('✅ All tests passed!')
console.log(`\n📋 Test Data:`)
console.log(` Note Share Token: ${shareToken}`)
console.log(` Conversation ID: ${convId}\n`)
process.exit(0)
} catch (err) {
console.error('❌ Test failed:', err)
process.exit(1)
}
}
test()