Files
plexus/server/test-db.js
2026-01-13 22:55:46 +01:00

48 lines
1.9 KiB
JavaScript

const duckdb = require('duckdb');
const path = require('path');
const dbPath = path.join(__dirname, 'data', 'chat.duckdb');
const db = new duckdb.Database(dbPath);
const con = db.connect();
console.log('Testing DuckDB operations...');
const walletAddress = 'test_wallet_' + Date.now();
const username = 'TestUser';
const now = new Date().toISOString();
// Test User Upsert
con.run(`INSERT INTO users (wallet_address, username, last_seen) VALUES (?, ?, ?)
ON CONFLICT (wallet_address) DO UPDATE SET last_seen = EXCLUDED.last_seen, username = EXCLUDED.username`,
[walletAddress, username, now], (err) => {
if (err) {
console.error('FAIL: User upsert failed:', err);
process.exit(1);
}
console.log('PASS: User upsert successful');
// Test Message Insert
const channelId = 'nebula';
const content = 'Test message content';
con.run(`INSERT INTO messages (id, channel_id, wallet_address, content, timestamp)
VALUES (nextval('seq_msg_id'), ?, ?, ?, ?)`,
[channelId, walletAddress, content, now], function (err) {
if (err) {
console.error('FAIL: Message insert failed:', err);
process.exit(1);
}
console.log('PASS: Message insert successful');
// Verify Data
con.all(`SELECT * FROM messages WHERE wallet_address = ?`, [walletAddress], (err, rows) => {
if (err || rows.length === 0) {
console.error('FAIL: Verification failed:', err);
process.exit(1);
}
console.log('PASS: Verification successful, found message:', rows[0].content);
console.log('All tests passed!');
process.exit(0);
});
});
});