#!/usr/bin/env node
/**
* TMOAT Atomic Test 5: Database Creation & Verification
* Tests that .folder-mcp/embeddings.db is created at correct location with proper structure
*/
import WebSocket from 'ws';
import path from 'path';
import fs from 'fs/promises';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
console.log('š ATOMIC TEST 5: Database Creation & Verification');
console.log('='.repeat(50));
const ws = new WebSocket('ws://127.0.0.1:31850');
// Test folder path
const testFolderPath = path.resolve(__dirname, '../tests/fixtures/tmp/smoke-small');
const expectedDbPath = path.join(testFolderPath, '.folder-mcp', 'embeddings.db');
console.log(`š Test folder: ${testFolderPath}`);
console.log(`šļø Expected DB: ${expectedDbPath}`);
let folderActive = false;
let testPhase = 'setup'; // setup -> waiting-active -> checking-db -> complete
ws.on('open', () => {
console.log('ā
Connected to WebSocket');
// Send connection init first
console.log('š¤ Sending connection.init with clientType: cli');
ws.send(JSON.stringify({
type: 'connection.init',
clientType: 'cli'
}));
// Wait a bit then add the folder
setTimeout(() => {
console.log('š¤ Adding folder for database test');
const folderId = `db-test-${Date.now()}`;
ws.send(JSON.stringify({
type: 'folder.add',
id: folderId,
payload: {
path: testFolderPath,
model: 'gpu:all-MiniLM-L6-v2'
}
}));
testPhase = 'waiting-active';
}, 1000);
});
async function checkDatabaseCreation() {
try {
console.log('\nš CHECKING DATABASE CREATION...');
// Check if .folder-mcp directory exists
const folderMcpDir = path.dirname(expectedDbPath);
try {
const stat = await fs.stat(folderMcpDir);
if (stat.isDirectory()) {
console.log('ā
.folder-mcp directory exists');
} else {
console.log('ā .folder-mcp exists but is not a directory');
return false;
}
} catch (error) {
console.log('ā .folder-mcp directory does not exist');
return false;
}
// Check if embeddings.db exists
try {
const dbStat = await fs.stat(expectedDbPath);
if (dbStat.isFile() && dbStat.size > 0) {
console.log(`ā
embeddings.db exists (${dbStat.size} bytes)`);
// Try to read the file to verify it's a valid SQLite database
const buffer = await fs.readFile(expectedDbPath);
const header = buffer.slice(0, 16).toString();
if (header.includes('SQLite')) {
console.log('ā
Database file appears to be valid SQLite');
return true;
} else {
console.log('ā Database file does not appear to be SQLite format');
return false;
}
} else {
console.log('ā embeddings.db exists but is empty or not a file');
return false;
}
} catch (error) {
console.log('ā embeddings.db does not exist');
return false;
}
} catch (error) {
console.log(`ā Error checking database: ${error.message}`);
return false;
}
}
ws.on('message', async (data) => {
try {
const message = JSON.parse(data);
const timestamp = new Date().toISOString().substring(11, 23);
if (message.type === 'connection.ack') {
console.log(`[${timestamp}] ā
Connection acknowledged by daemon`);
} else if (message.type === 'fmdm.update') {
if (message.fmdm?.folders?.length > 0) {
message.fmdm.folders.forEach(async folder => {
const folderName = folder.path.split('/').pop();
console.log(`[${timestamp}] š ${folderName}: ${folder.status}${folder.progress !== undefined ? ` (${folder.progress}%)` : ''}`);
// Check when folder becomes active
if (folder.status === 'active' && folder.progress === 100 && testPhase === 'waiting-active') {
console.log(`[${timestamp}] šÆ Folder is active! Starting database verification...`);
testPhase = 'checking-db';
folderActive = true;
// Wait a moment for any final database operations
setTimeout(async () => {
const dbExists = await checkDatabaseCreation();
console.log('\nš DATABASE VERIFICATION SUMMARY:');
console.log(`Test folder: ${testFolderPath}`);
console.log(`Expected DB path: ${expectedDbPath}`);
console.log(`Database created: ${dbExists ? 'ā
YES' : 'ā NO'}`);
if (dbExists) {
console.log('\nā
ATOMIC TEST 5: PASSED - Database created correctly');
} else {
console.log('\nā ATOMIC TEST 5: FAILED - Database not created or invalid');
}
testPhase = 'complete';
ws.close();
}, 3000);
}
});
}
} else if (message.type === 'error') {
console.log(`[${timestamp}] ā Error from daemon: ${message.message || 'unknown error'}`);
}
} catch (e) {
console.log(`ā Failed to parse message: ${data}`);
}
});
ws.on('error', (err) => {
console.log(`ā WebSocket error: ${err.message}`);
process.exit(1);
});
ws.on('close', () => {
console.log('š Connection closed');
process.exit(0);
});
// Auto-close after 30 seconds
setTimeout(() => {
console.log('ā±ļø Test timeout - closing connection');
if (!folderActive) {
console.log('ā ATOMIC TEST 5: FAILED - Folder never reached active state');
} else if (testPhase !== 'complete') {
console.log('ā ATOMIC TEST 5: INCOMPLETE - Database verification did not complete');
}
ws.close();
}, 30000);
console.log('ā³ Connecting to daemon and testing database creation...');