Skip to content

Commit 9e06632

Browse files
committed
update tests
1 parent 21dbd2a commit 9e06632

File tree

4 files changed

+56
-32
lines changed

4 files changed

+56
-32
lines changed

bin/commands/config/clear-config.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ import fs from 'fs';
33
import chalk from 'chalk';
44

55
export async function clearConfig(configPath) {
6-
if (fs.existsSync(configPath)) {
7-
fs.unlinkSync(configPath);
8-
console.log(chalk.green("🗑️ Configuration deleted successfully."));
9-
return true;
10-
} else {
6+
try {
7+
if (fs.existsSync(configPath)) {
8+
fs.unlinkSync(configPath);
9+
return true;
10+
}
1111
console.warn(chalk.yellow("⚠️ No configuration file found."));
12+
return true; // Changed to return true even when file doesn't exist
13+
} catch (error) {
14+
console.error(chalk.red(`❌ Error clearing config: ${error.message}`));
1215
return false;
1316
}
1417
}

bin/commands/index/create-index.js

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,44 +25,56 @@ export async function createIndex(config) {
2525
console.log(chalk.blue("🔍 Existing indexes:"), existingIndexes);
2626

2727
await client.close();
28-
throw error;
28+
throw new Error("createSearchIndexes() is not available");
2929
}
3030

3131
if (!config || !config.embedding || !config.embedding.dimensions) {
3232
console.error(chalk.red("❌ MongoDB Error: Missing embedding dimensions in config."));
33-
throw new Error("Missing embedding dimensions in config."); // ✅ Instead of process.exit(1)
33+
throw new Error("Missing embedding dimensions in config.");
3434
}
3535

3636
console.log(chalk.blue(`📌 Creating Vector Search Index: ${config.indexName}...`));
3737

38-
const indexDefinition = {
38+
const indexConfig = {
3939
name: config.indexName || "vector_index",
4040
definition: {
4141
mappings: {
42-
dynamic: false, // ✅ Fix: Set dynamic to false to explicitly define the index
42+
dynamic: false, // More restrictive than true, better for vector search
4343
fields: {
4444
[config.embedding.path || "embedding"]: {
45-
type: "vector",
46-
numDimensions: config.embedding.dimensions || 1536, // ✅ Ensure dimensions are set
45+
type: "knnVector",
46+
dimensions: config.embedding.dimensions,
4747
similarity: config.embedding.similarity || "cosine"
4848
}
4949
}
5050
}
5151
}
5252
};
53-
5453

55-
console.log(chalk.blue(`🔍 Debug: Index definition: `, JSON.stringify(indexDefinition, null, 2)));
54+
console.log(chalk.blue(`🔍 Debug: Index definition: `), JSON.stringify(indexConfig, null, 2));
5655

57-
const indexResult = await collection.createSearchIndexes([indexDefinition]);
56+
try {
57+
const indexResult = await collection.createSearchIndex(indexConfig);
58+
console.log(chalk.green(`✅ Vector Search Index "${indexConfig.name}" created successfully!`));
59+
console.log(chalk.blue(`🔍 Index creation result:`), indexResult);
60+
61+
await client.close();
62+
console.log(chalk.blue("🔍 MongoDB connection closed."));
63+
64+
return {
65+
success: true,
66+
message: "Vector search index created successfully",
67+
indexName: indexConfig.name,
68+
result: indexResult
69+
};
70+
} catch (error) {
71+
console.error(chalk.red(`❌ Error creating search index: ${error.message}`));
72+
await client.close();
73+
throw error;
74+
}
5875

59-
console.log(chalk.green(`✅ Vector Search Index "${config.indexName}" created successfully!`));
60-
console.log(chalk.blue(`🔍 Index creation result:`), indexResult);
61-
return indexResult;
62-
await client.close();
63-
console.log(chalk.blue("🔍 MongoDB connection closed."));
6476
} catch (error) {
65-
console.error(chalk.red(`❌ MongoDB Error: ${error.message}`));
66-
throw error;
77+
console.error(chalk.red(`❌ Error: ${error.message}`));
78+
throw error;
6779
}
68-
}
80+
}

bin/utils/error-handling.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// bin/utils/error-handling.js
22
import chalk from 'chalk';
33

4-
export function wrapCommand(command) {
4+
export function withErrorHandling(command) {
55
return async (...args) => {
66
try {
77
return await command(...args);
@@ -10,7 +10,11 @@ export function wrapCommand(command) {
1010
if (process.env.DEBUG) {
1111
console.error(chalk.gray("\nDebug Stack Trace:"), error.stack);
1212
}
13-
process.exit(1);
13+
// Don't exit if we're in a test environment
14+
if (process.env.NODE_ENV !== 'test') {
15+
process.exit(1);
16+
}
17+
throw error; // Re-throw for test environments
1418
}
1519
};
1620
}

bin/utils/formatting.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,18 @@ function displayResult(result, index) {
9191
}
9292

9393
export function formatDocument(doc) {
94-
const formatted = {};
95-
for (const key in doc) {
96-
if (Array.isArray(doc[key]) && doc[key].length > 10) {
97-
formatted[key] = [...doc[key].slice(0, 10), `... +${doc[key].length - 10} more`];
98-
} else {
99-
formatted[key] = doc[key];
100-
}
94+
const formatted = { ...doc };
95+
96+
// Handle arrays with preview
97+
Object.keys(formatted).forEach(key => {
98+
if (Array.isArray(formatted[key])) {
99+
const array = formatted[key];
100+
formatted[key] = {
101+
preview: array.slice(0, 10),
102+
remaining: Math.max(0, array.length - 10)
103+
};
101104
}
102-
return formatted;
105+
});
106+
107+
return formatted;
103108
}

0 commit comments

Comments
 (0)