Files
AgentHub/server/__tests__/mcp-server-live.test.js
nntrivi2001 4aae61297f feat: analytics parity — tool cost, date ranges, budgets, query explorer, replay, snapshots (v2.4.0)
Analytics parity phases 2–7 (docs/plans/analytics-parity/), built on
138e706 and ported onto v2.3.1:
- Cost by AI tool (?tool= on cost/analytics/stats/workflows, by_tool,
  Codex fast/priority and flex tiers, agenthub_cost_usd_total{tool,model})
- Date ranges (from/to) on cost/analytics/workflows; Analytics range select
  with Today / This week (Monday) / This month (1st) / 7d / 30d / 90d / All /
  Custom; workflow filters carried into workflow-stats.js (read worker)
- Budgets and cost alerts as alert rule types (alert_rules rebuilt without
  its rule_type CHECK), GET /api/budgets/status, rule preview, spend pace,
  status-bar budget chip; rules stay editable in place
- Query Explorer (/query, allowlisted sort, CSV/JSON export) and scheduled
  reports (report_definitions/report_runs, Settings → Reports)
- Session Replay tab and immutable snapshots (session_snapshots, /snapshot/:token)
- Remote Codex sync, push ingest with a hashed token, privacy redaction and
  ignored folders (Settings → Data), Cursor sessions (no cost)
- Read-only MCP server, statusline, Grafana/Prometheus files

Also: resizable sidebar, pet run/wave animations, the workspace home keeps a
half-typed prompt, and the pre-commit hook runs only the related tests for a
small commit.
2026-10-06 17:12:28 +07:00

80 lines
2.9 KiB
JavaScript

/**
* @file Runs every MCP tool (`mcp/server.js`) against a REAL in-test dashboard
* (throwaway DB and CLAUDE_HOME), so a tool whose path or query drifts from
* the actual API fails here rather than in a user's session. The MCP server is
* spawned asynchronously because the dashboard lives in this process.
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
*/
const { it, before, after } = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const readline = require("node:readline");
const { spawn } = require("node:child_process");
const TMP = path.join(os.tmpdir(), `agenthub-mcp-live-${Date.now()}-${process.pid}`);
process.env.DASHBOARD_DB_PATH = path.join(TMP, "dashboard.db");
process.env.CLAUDE_HOME = path.join(TMP, "home");
process.env.DASHBOARD_DATA_DIR = path.join(TMP, "data");
process.env.DASHBOARD_LIVENESS_PROBE = "0";
const { createApp, startServer } = require("../index");
const { db } = require("../db");
let server;
let child;
let rpc;
before(async () => {
server = await startServer(createApp(), 0);
const url = `http://127.0.0.1:${server.address().port}`;
child = spawn(process.execPath, [path.resolve(__dirname, "..", "..", "mcp", "server.js")], {
env: { ...process.env, AGENTHUB_URL: url },
stdio: ["pipe", "pipe", "inherit"],
});
const waiting = new Map();
readline.createInterface({ input: child.stdout }).on("line", (line) => {
const msg = JSON.parse(line);
waiting.get(msg.id)?.(msg);
});
let next = 1;
rpc = (method, params) => {
const id = next++;
child.stdin.write(JSON.stringify({ jsonrpc: "2.0", id, method, params }) + "\n");
return new Promise((resolve) => waiting.set(id, resolve));
};
await rpc("initialize", { protocolVersion: "2025-06-18" });
});
after(() => {
child?.stdin.end();
server?.close();
db?.close();
fs.rmSync(TMP, { recursive: true, force: true });
});
it("every tool answers JSON from the real API", async () => {
const { result } = await rpc("tools/list", {});
const args = {
get_session_cost: { session_id: "none" },
get_lane_cost: { lane: 1 },
get_session: { session_id: "none" },
get_session_stats: { session_id: "none" },
};
for (const { name } of result.tools) {
const res = await rpc("tools/call", { name, arguments: args[name] || {} });
const out = res.result.content[0].text;
if (name === "get_session" || name === "get_session_stats") {
assert.match(out, /HTTP 404: Session not found/, name);
} else if (name === "get_budget_status" && res.result.isError) {
// Budgets come from another phase; until then the route is missing.
assert.match(out, /Not available on this dashboard/);
} else {
assert.equal(res.result.isError, undefined, `${name}: ${out.slice(0, 200)}`);
JSON.parse(out);
}
}
});