Files
AgentHub/server/__tests__/transcript-sender.test.js
T
nntrivi2001 0547488f13 chore: v2.8.13 — Artifacts reinstall/edit/installs, agent steps in chat, pick a local file to attach, Windows paths as links
- Chat: an Agent step shows its task, status and tool calls and opens its conversation in place; Windows paths (E:\…, C:/…) in replies open as files
- Prompt: attach a file on this computer by path (native picker), clearer too-large messages
- Artifacts: edit title/description and a skill's files (new snapshot), Reinstall, install history, Hub-only rows, skills installed into ~/.claude/skills and ~/.agents/skills
- Files: batch upload of small files in one extract
- AI Tools: npm install falls back to --prefix ~/.local when the global prefix is not writable
2026-10-20 16:44:55 +07:00

236 lines
7.6 KiB
JavaScript

/**
* @file transcript-sender.test.js
* @description Unit tests for classifyTranscriptSender — the transcript viewer
* must attribute each JSONL line to its TRUE sender, not blanket-label every
* `type:"user"` line as the human. Cases mirror real Claude Code transcripts:
* tool results, harness task-notifications, /loop (isMeta) re-injections, and a
* subagent's orchestrator-assigned task. (Reported transcript mis-attribution.)
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
*/
const { describe, it, after } = require("node:test");
const assert = require("node:assert/strict");
const path = require("node:path");
const os = require("node:os");
const fs = require("node:fs");
// Point the db at a throwaway file before requiring the router (it pulls in db).
const TEST_DB = path.join(os.tmpdir(), `transcript-sender-${Date.now()}-${process.pid}.db`);
process.env.DASHBOARD_DB_PATH = TEST_DB;
const {
classifyTranscriptSender,
patchCounts,
askAnswers,
agentInfo,
} = require("../routes/sessions");
const { db } = require("../db");
after(() => {
if (db) db.close();
for (const s of ["", "-wal", "-shm"]) {
try {
fs.unlinkSync(TEST_DB + s);
} catch {
/* ignore */
}
}
});
// Shapes lifted from real ~/.claude transcripts.
const realUser = (text) => ({
type: "user",
message: { role: "user", content: text },
promptSource: "user_input",
origin: "cli",
});
const toolResult = () => ({
type: "user",
message: { role: "user", content: [{ type: "tool_result", tool_use_id: "t1", content: "ok" }] },
toolUseResult: { stdout: "ok" },
});
const assistant = () => ({ type: "assistant", message: { role: "assistant", content: [] } });
describe("classifyTranscriptSender — main transcript", () => {
it("real human message → user", () => {
assert.equal(classifyTranscriptSender(realUser("spin up a team of agents"), false), "user");
});
it("tool result (toolUseResult / tool_result content) → tool", () => {
assert.equal(classifyTranscriptSender(toolResult(), false), "tool");
});
it("harness task-notification → system", () => {
const e = {
type: "user",
message: { role: "user", content: "<task-notification>\n<task-id>abc</task-id>\n" },
promptSource: "task_notification",
origin: "system",
};
assert.equal(classifyTranscriptSender(e, false), "system");
});
it("[SYSTEM NOTIFICATION …] background-task banner → system", () => {
const e = {
type: "user",
message: {
role: "user",
content:
"[SYSTEM NOTIFICATION - NOT USER INPUT]\nThis is an automated background-task event…\n<task-notification>…",
},
};
assert.equal(classifyTranscriptSender(e, false), "system");
});
it("/loop re-injection (isMeta) → system", () => {
const e = {
type: "user",
isMeta: true,
message: { role: "user", content: "Sonnet cognition agent last one — stitch the brief" },
};
assert.equal(classifyTranscriptSender(e, false), "system");
});
it("assistant turn → assistant", () => {
assert.equal(classifyTranscriptSender(assistant(), false), "assistant");
});
it("@agent mention typed by the human → user", () => {
assert.equal(classifyTranscriptSender(realUser("@agent-ai-engineer hi"), false), "user");
});
it("local slash-command (type=system) → user", () => {
assert.equal(
classifyTranscriptSender(
{ type: "system", subtype: "local_command", content: "/color" },
false
),
"user"
);
});
});
describe("classifyTranscriptSender — subagent transcript", () => {
it("orchestrator-assigned task (no promptSource/origin) → orchestrator", () => {
const task = {
type: "user",
isSidechain: true,
agentId: "a484",
message: { role: "user", content: "Light research/synthesis task. Write a synthesis…" },
};
assert.equal(classifyTranscriptSender(task, true), "orchestrator");
});
it("tool result inside a subagent → tool", () => {
assert.equal(classifyTranscriptSender(toolResult(), true), "tool");
});
it("human directly messaging the subagent (has provenance) → user", () => {
const direct = {
type: "user",
isSidechain: true,
agentId: "a484",
message: { role: "user", content: "actually, focus on cognition" },
promptSource: "user_input",
origin: "cli",
};
assert.equal(classifyTranscriptSender(direct, true), "user");
});
it("assistant turn in a subagent → assistant", () => {
assert.equal(classifyTranscriptSender(assistant(), true), "assistant");
});
});
describe("patchCounts — the +/- lines of a Write/Edit result", () => {
it("counts a structuredPatch's added and removed lines, not its context", () => {
const r = {
filePath: "/w/a.ts",
structuredPatch: [
{
oldStart: 1,
oldLines: 3,
newStart: 1,
newLines: 4,
lines: [" a", "-b", "+c", "+d", " e"],
},
{ oldStart: 9, oldLines: 1, newStart: 10, newLines: 0, lines: ["-z"] },
],
};
assert.deepEqual(patchCounts(r), { added: 2, removed: 2 });
});
it("counts a created file's lines as added", () => {
assert.deepEqual(patchCounts({ type: "create", content: "a\nb\nc\n", structuredPatch: [] }), {
added: 3,
removed: 0,
});
});
it("answers null for a result that is no edit", () => {
assert.equal(patchCounts({ stdout: "ok" }), null);
assert.equal(patchCounts("text"), null);
assert.equal(patchCounts(undefined), null);
});
});
describe("askAnswers — what the user picked or typed for an AskUserQuestion", () => {
it("returns each question's answer", () => {
const r = {
questions: [{ question: "Commit?", options: [{ label: "Yes" }, { label: "No" }] }],
answers: { "Commit?": "Yes", "Notes?": "typed text" },
annotations: {},
};
assert.deepEqual(askAnswers(r), { "Commit?": "Yes", "Notes?": "typed text" });
});
it("drops a non-string answer and answers null for a result that is no question", () => {
assert.deepEqual(askAnswers({ answers: { a: "x", b: 3 } }), { a: "x" });
assert.equal(askAnswers({ answers: {} }), null);
assert.equal(askAnswers({ answers: ["x"] }), null);
assert.equal(askAnswers({ stdout: "ok" }), null);
assert.equal(askAnswers("text"), null);
});
});
describe("agentInfo — what an Agent call's result says about its subagent", () => {
it("reads a foreground agent's id, model, time, tokens and tool calls", () => {
const r = {
status: "completed",
agentId: "a2f215755130779a8",
resolvedModel: "claude-opus-5-5",
totalDurationMs: 132350,
totalTokens: 88007,
totalToolUseCount: 31,
content: [{ type: "text", text: "report" }],
};
assert.deepEqual(agentInfo(r), {
id: "a2f215755130779a8",
model: "claude-opus-5-5",
status: "completed",
duration_ms: 132350,
tokens: 88007,
tool_uses: 31,
});
});
it("leaves unknown facts null for a background launch", () => {
const r = { isAsync: true, status: "async_launched", agentId: "ab12", description: "x" };
assert.deepEqual(agentInfo(r), {
id: "ab12",
model: null,
status: "async_launched",
duration_ms: null,
tokens: null,
tool_uses: null,
});
});
it("answers null for a result that is no agent, or an id that is no plain name", () => {
assert.equal(agentInfo({ stdout: "ok" }), null);
assert.equal(agentInfo({ agentId: "../etc" }), null);
assert.equal(agentInfo("text"), null);
assert.equal(agentInfo(undefined), null);
});
});