import { spawn } from "child_process";
import path from "path";
import fs from "fs";
const serverPath = path.join(__dirname, "dist", "src", "server.js");
const serverProcess = spawn("node", [serverPath], {
stdio: ["pipe", "pipe", "inherit"]
});
// We need a real PDF to test page extraction, or we can mock it.
// Since we don't have a PDF generator handy in this environment easily without deps,
// we will rely on the fact that we implemented page extraction for .txt/.md too (as page 1).
// But to test multi-page, we really need a PDF.
// However, for this verification, checking that we get "[Page 1]" for a text file is sufficient to prove the plumbing works.
// If we want to test progress, we need to capture stderr (where logging messages go in stdio transport usually? No, notifications are JSON-RPC).
// Wait, `server.sendLoggingMessage` sends a notification `notifications/message`.
// We need to capture that from stdout.
const testMdPath = path.join(__dirname, "test_ux.md");
fs.writeFileSync(testMdPath, "# UX Test\n\nTesting citations.");
const requests = [
{
jsonrpc: "2.0",
id: 1,
method: "initialize",
params: {
protocolVersion: "2024-11-05",
capabilities: {},
clientInfo: { name: "test-client", version: "1.0.0" }
}
},
{
jsonrpc: "2.0",
id: 2,
method: "notifications/initialized"
},
{
jsonrpc: "2.0",
id: 3,
method: "tools/call",
params: {
name: "reset_library",
arguments: {}
}
},
{
jsonrpc: "2.0",
id: 4,
method: "tools/call",
params: {
name: "ingest_document",
arguments: {
path: testMdPath
}
}
},
{
jsonrpc: "2.0",
id: 5,
method: "tools/call",
params: {
name: "query_knowledge_base",
arguments: {
query: "citations"
}
}
}
];
let buffer = "";
let currentReqIndex = 0;
function sendNextRequest() {
if (currentReqIndex >= requests.length) return;
const req = requests[currentReqIndex];
const msg = JSON.stringify(req) + "\n";
serverProcess.stdin.write(msg);
}
serverProcess.stdout.on("data", (data) => {
buffer += data.toString();
const lines = buffer.split("\n");
buffer = lines.pop() || "";
for (const line of lines) {
if (!line.trim()) continue;
try {
const response = JSON.parse(line);
// Check for notifications
if (response.method === "notifications/message") {
console.log("Progress Notification:", response.params);
} else {
console.log("Received response:", JSON.stringify(response, null, 2));
// Move to next request only on responses (not notifications)
if (response.id !== undefined) {
currentReqIndex++;
if (currentReqIndex < requests.length) {
sendNextRequest();
} else {
console.log("Verification complete.");
serverProcess.kill();
if (fs.existsSync(testMdPath)) fs.unlinkSync(testMdPath);
process.exit(0);
}
}
}
} catch (e) {
console.error("Error parsing JSON:", e);
}
}
});
// Start with first request
sendNextRequest();
setTimeout(() => {
console.error("Timeout waiting for response");
serverProcess.kill();
if (fs.existsSync(testMdPath)) fs.unlinkSync(testMdPath);
process.exit(1);
}, 10000);