-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestEndpoints.js
More file actions
135 lines (118 loc) · 3.78 KB
/
testEndpoints.js
File metadata and controls
135 lines (118 loc) · 3.78 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
async function testTokenizeIndex() {
const data = {
action: "index",
files: [
{ filePath: "demo.py", content: "print('Hello World')" }
]
};
const res = await fetch("http://localhost:3000/api/tokenize", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data)
});
const json = await res.json();
console.log("Tokenize index response:", json);
}
async function testTokenizeQuery() {
const data = {
action: "query",
query: "print"
};
const res = await fetch("http://localhost:3000/api/tokenize", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data)
});
const json = await res.json();
console.log("Tokenize query response:", json);
}
async function testCodeFix() {
const response = await fetch("http://localhost:3000/api/code-fix", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
issueDescription: "fix the syntax error in the print statement",
currentCode: "print('Hello, world)",
}),
});
const resultText = await response.text();
console.log("output:\n", resultText);
}
async function testCodeFixStreaming() {
const response = await fetch("http://localhost:3000/api/code-fix", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
issueDescription: "fix the syntax error in the print statement",
currentCode: "print('Hello, world)",
}),
});
if (!response.body) {
console.error("ReadableStream not supported in this browser.");
return;
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
let output = "";
// Stream and process each chunk as it arrives.
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
output += chunk;
console.log("Received chunk:", chunk);
}
console.log("Final output:", output);
}
/**
* Function to test the tokenize route.
*
* This function performs the following:
* 1. Makes a POST request with action "index" to build the embedding index
* with sample source files.
* 2. Makes a POST request with action "query" to search over the indexed files.
* 3. Logs the responses from both requests.
*
* Ensure your local server is running (e.g., at http://localhost:3000)
* and that the environment variable OPENAI_API_KEY is correctly configured.
*/
async function testTokenizeFlow() {
try {
// Step 1: Index sample files.
const indexData = {
action: "index",
files: [
{ filePath: "demo.py", content: "print('Hello World')" },
{ filePath: "utils.js", content: "export function greet() { console.log('Hello'); }" }
]
};
const indexResponse = await fetch("http://localhost:3000/api/tokenize", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(indexData)
});
const indexJson = await indexResponse.json();
console.log("Indexing response:", indexJson);
// Step 2: Query the indexed files.
const queryData = {
action: "query",
query: "print"
};
const queryResponse = await fetch("http://localhost:3000/api/tokenize", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(queryData)
});
const queryJson = await queryResponse.json();
console.log("Query response:", queryJson);
} catch (error) {
console.error("Error during testing:", error);
}
}
// Execute the test function.
testTokenizeFlow();
// Run tests
//testTokenizeIndex()
//testTokenizeQuery()
// Run the streaming test
//testCodeFixStreaming();