forked from aiurda/devcontext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_code_processing.js
More file actions
177 lines (153 loc) · 4.87 KB
/
test_code_processing.js
File metadata and controls
177 lines (153 loc) · 4.87 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* Test script for code entity processing in the MCP server
*
* This script tests the code entity processing system by sending code changes
* to the update_conversation_context tool and analyzing the results.
*/
import fs from "fs";
import path from "path";
import { executeQuery } from "./src/db.js";
import updateConversationContextTool from "./src/tools/updateConversationContext.tool.js";
// Get the handler from the tool's default export
const updateConversationContextHandler = updateConversationContextTool.handler;
// Create a mock conversation ID for testing
const TEST_CONVERSATION_ID = "test-conversation-" + Date.now();
// Test files to process
const TEST_FILES = [
"test_files/simple_function.js",
"test_files/complex_class.js",
"test_files/refactored_file.js",
"test_files/edge_case.js",
];
/**
* Read the content of a file
*
* @param {string} filePath - Path to the file
* @returns {string} File content
*/
function readFileContent(filePath) {
return fs.readFileSync(filePath, "utf8");
}
/**
* Get code entities from database for a specific file
*
* @param {string} filePath - Path to the file
* @returns {Promise<Array>} Code entities
*/
async function getCodeEntitiesForFile(filePath) {
const query = `
SELECT entity_id, file_path, entity_type, name, content_hash,
summary, importance_score, start_line, end_line
FROM code_entities
WHERE file_path = ?
ORDER BY entity_type, name
`;
const result = await executeQuery(query, [filePath]);
return result.rows || [];
}
/**
* Process a file through the update_conversation_context tool
*
* @param {string} filePath - Path to the file to process
* @returns {Promise<object>} Processing result
*/
async function processFile(filePath) {
const fileContent = readFileContent(filePath);
const codeChange = {
filePath,
newContent: fileContent,
languageHint: "javascript",
};
const input = {
conversationId: TEST_CONVERSATION_ID,
codeChanges: [codeChange],
preserveContextOnTopicShift: true,
contextIntegrationLevel: "balanced",
trackIntentTransitions: true,
};
console.log(`\n=== Processing ${filePath} ===`);
// Get database state before processing
console.log("Getting entities before processing...");
const entitiesBefore = await getCodeEntitiesForFile(filePath);
// Call the handler directly (bypassing API layer)
console.log("Calling update_conversation_context handler...");
const result = await updateConversationContextHandler(input, {});
// Get database state after processing
console.log("Getting entities after processing...");
const entitiesAfter = await getCodeEntitiesForFile(filePath);
return {
filePath,
result,
entitiesBefore,
entitiesAfter,
};
}
/**
* Display the results of processing a file
*
* @param {object} processResult - Result of processing
*/
function displayResults(processResult) {
const { filePath, entitiesBefore, entitiesAfter } = processResult;
console.log(`\n=== Results for ${filePath} ===`);
// Show entity counts
console.log(`Entities before: ${entitiesBefore.length}`);
console.log(`Entities after: ${entitiesAfter.length}`);
// Show all entities after processing
console.log("\nEntities after processing:");
entitiesAfter.forEach((entity) => {
console.log(` • ${entity.entity_type}: ${entity.name || "[unnamed]"}`);
console.log(` - Importance score: ${entity.importance_score}`);
console.log(
` - Content hash: ${entity.content_hash?.substring(0, 10)}...`
);
console.log(
` - Summary: ${
entity.summary
? entity.summary.length > 50
? entity.summary.substring(0, 50) + "..."
: entity.summary
: "[none]"
}`
);
console.log("");
});
// Show importance score statistics for this file
if (entitiesAfter.length > 0) {
const scores = entitiesAfter.map(
(e) => parseFloat(e.importance_score) || 0
);
const avg = scores.reduce((sum, score) => sum + score, 0) / scores.length;
const max = Math.max(...scores);
const min = Math.min(...scores);
console.log("\nImportance score statistics:");
console.log(` Average: ${avg.toFixed(2)}`);
console.log(` Maximum: ${max.toFixed(2)}`);
console.log(` Minimum: ${min.toFixed(2)}`);
}
}
/**
* Run the test
*/
async function runTest() {
try {
console.log("Starting code entity processing test");
// Process each test file
for (const filePath of TEST_FILES) {
try {
const result = await processFile(filePath);
displayResults(result);
} catch (error) {
console.error(`Error processing ${filePath}:`, error);
}
}
console.log("\nTest completed successfully");
} catch (error) {
console.error("Test failed:", error);
}
}
// Run the test
runTest().catch((error) => {
console.error("Unhandled error:", error);
process.exit(1);
});