4aae61297f
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.
142 lines
5.4 KiB
JavaScript
142 lines
5.4 KiB
JavaScript
/**
|
|
* @file Tests server/lib/cursor-usage.js — Cursor sessions from its agent
|
|
* transcripts: a session created as `tool = 'cursor'` with its name, cwd (the
|
|
* project slug walked back onto the filesystem) and no tokens, an unchanged
|
|
* file never re-read, a quiet one settled to completed, a session the user
|
|
* deleted staying deleted, and a non-cursor row with the same id never
|
|
* touched. The Cursor dir and the DB are temp dirs; the transcripts are
|
|
* synthetic, in the shape Cursor writes.
|
|
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
|
|
*/
|
|
|
|
const os = require("node:os");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
|
|
const ROOT = fs.mkdtempSync(path.join(os.tmpdir(), "agenthub-cursor-usage-"));
|
|
process.env.DASHBOARD_DB_PATH = path.join(ROOT, "dashboard.db");
|
|
process.env.AGENTHUB_CURSOR_DIR = path.join(ROOT, "cursor");
|
|
|
|
const { describe, it, beforeEach, after } = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const { db } = require("../db");
|
|
const cursor = require("../lib/cursor-usage");
|
|
|
|
after(() => fs.rmSync(ROOT, { recursive: true, force: true }));
|
|
|
|
const ID = "7481e37e-9157-4145-b985-2ea9a2117f52";
|
|
const PROJECT = path.join(fs.realpathSync(ROOT), "work", "my app");
|
|
fs.mkdirSync(PROJECT, { recursive: true });
|
|
const slugOf = (p) => p.replace(/^\/+/, "").replace(/[^A-Za-z0-9]/g, "-");
|
|
|
|
function transcript(
|
|
id,
|
|
{ slug = slugOf(PROJECT), prompt = "Review the campaign code\nplease" } = {}
|
|
) {
|
|
const dir = path.join(process.env.AGENTHUB_CURSOR_DIR, "projects", slug, "agent-transcripts", id);
|
|
fs.mkdirSync(dir, { recursive: true });
|
|
const file = path.join(dir, `${id}.jsonl`);
|
|
const lines = [
|
|
{
|
|
role: "user",
|
|
message: {
|
|
content: [
|
|
{
|
|
type: "text",
|
|
text: `<timestamp>Thursday, Jun 11, 2026, 4:53 PM (UTC+7)</timestamp>\n<user_query>\n${prompt}\n</user_query>`,
|
|
},
|
|
],
|
|
},
|
|
},
|
|
{ role: "assistant", message: { content: [{ type: "tool_use", name: "Read", input: {} }] } },
|
|
{ type: "turn_ended", status: "success" },
|
|
];
|
|
fs.writeFileSync(file, lines.map((l) => JSON.stringify(l)).join("\n") + "\n");
|
|
return file;
|
|
}
|
|
|
|
function age(file, ms) {
|
|
const t = new Date(Date.now() - ms);
|
|
fs.utimesSync(file, t, t);
|
|
}
|
|
|
|
const session = (id) => db.prepare("SELECT * FROM sessions WHERE id = ?").get(id);
|
|
|
|
beforeEach(() => {
|
|
fs.rmSync(process.env.AGENTHUB_CURSOR_DIR, { recursive: true, force: true });
|
|
db.exec(
|
|
"DELETE FROM token_usage; DELETE FROM agents; DELETE FROM sessions; DELETE FROM cursor_transcripts;"
|
|
);
|
|
});
|
|
|
|
describe("cursor usage", () => {
|
|
it("walks a project slug back to an existing directory, else null", () => {
|
|
assert.equal(cursor.cwdFromSlug(slugOf(PROJECT)), PROJECT);
|
|
assert.equal(cursor.cwdFromSlug(slugOf(`${PROJECT}-gone`)), null);
|
|
assert.equal(cursor.cwdFromSlug("empty-window"), null);
|
|
assert.equal(cursor.cwdFromSlug(""), null);
|
|
});
|
|
|
|
it("creates the session with its name and cwd, and no tokens", async () => {
|
|
transcript(ID);
|
|
assert.deepEqual(await cursor.ingestCursorUsage(), { files: 1, sessions: 1 });
|
|
const s = session(ID);
|
|
assert.equal(s.tool, "cursor");
|
|
assert.equal(s.name, "Review the campaign code");
|
|
assert.equal(s.cwd, PROJECT);
|
|
assert.equal(s.model, null);
|
|
assert.equal(s.status, "active");
|
|
assert.equal(s.transcript_path, null);
|
|
assert.equal(db.prepare("SELECT COUNT(*) c FROM token_usage").get().c, 0);
|
|
// Unchanged: never re-read.
|
|
assert.deepEqual(await cursor.ingestCursorUsage(), { files: 0, sessions: 0 });
|
|
});
|
|
|
|
it("settles a quiet session to completed, and reads an old one completed", async () => {
|
|
const file = transcript(ID);
|
|
await cursor.ingestCursorUsage();
|
|
const seen = db.prepare("SELECT mtime_ms FROM cursor_transcripts").get().mtime_ms;
|
|
assert.deepEqual(await cursor.ingestCursorUsage({ now: seen + 5 * 60_000 }), {
|
|
files: 0,
|
|
sessions: 1,
|
|
});
|
|
assert.equal(session(ID).status, "completed");
|
|
assert.equal(session(ID).ended_at, new Date(seen).toISOString());
|
|
|
|
db.exec("DELETE FROM sessions; DELETE FROM cursor_transcripts;");
|
|
age(file, 10 * 60_000);
|
|
await cursor.ingestCursorUsage();
|
|
assert.equal(session(ID).status, "completed");
|
|
});
|
|
|
|
it("keeps a deleted session deleted until Cursor writes again", async () => {
|
|
const file = transcript(ID);
|
|
age(file, 10 * 60_000);
|
|
await cursor.ingestCursorUsage();
|
|
db.prepare("DELETE FROM sessions WHERE id = ?").run(ID);
|
|
await cursor.ingestCursorUsage();
|
|
assert.equal(session(ID), undefined);
|
|
fs.appendFileSync(file, JSON.stringify({ role: "user", message: { content: [] } }) + "\n");
|
|
await cursor.ingestCursorUsage();
|
|
assert.equal(session(ID).tool, "cursor");
|
|
});
|
|
|
|
it("never touches a session of another tool with the same id", async () => {
|
|
db.prepare(
|
|
"INSERT INTO sessions (id, name, status, tool) VALUES (?, 'mine', 'active', 'claude')"
|
|
).run(ID);
|
|
transcript(ID, { slug: "empty-window" });
|
|
await cursor.ingestCursorUsage();
|
|
const s = session(ID);
|
|
assert.equal(s.tool, "claude");
|
|
assert.equal(s.name, "mine");
|
|
});
|
|
|
|
it("names a session with no prompt by its id, with no cwd", async () => {
|
|
transcript(ID, { slug: "1778550192773", prompt: "" });
|
|
await cursor.ingestCursorUsage();
|
|
assert.equal(session(ID).name, `Cursor ${ID.slice(0, 8)}`);
|
|
assert.equal(session(ID).cwd, null);
|
|
});
|
|
});
|