refactor: align package folder names with npm package names
CI / check (pull_request) Failing after 8m30s
CI / check (pull_request) Failing after 8m30s
Rename packages/ subdirectories to match their @united-workforce/* scope: cli-workflow → cli workflow-agent-builtin → agent-builtin workflow-agent-claude-code → agent-claude-code workflow-agent-hermes → agent-hermes workflow-dashboard → dashboard workflow-protocol → protocol workflow-util-agent → util-agent workflow-util → util Updated all tsconfig references, scripts, and active docs. Historical docs (docs/plans/, docs/superpowers/) left as-is. Closes #21
This commit is contained in:
@@ -0,0 +1,365 @@
|
||||
import { Database } from "bun:sqlite";
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { createMemoryStore, refs, validate, walk } from "@ocas/core";
|
||||
|
||||
import {
|
||||
computeDurationMs,
|
||||
extractLastAssistantContent,
|
||||
getHermesDbPath,
|
||||
loadHermesSessionFromDb,
|
||||
messageToTurnPayload,
|
||||
parseSessionIdFromStdout,
|
||||
storeHermesSessionDetail,
|
||||
} from "../src/session-detail.js";
|
||||
import type { HermesSessionJson, HermesSessionMessage } from "../src/types.js";
|
||||
|
||||
describe("parseSessionIdFromStdout", () => {
|
||||
test("reads session_id from the last non-empty line", () => {
|
||||
const stdout = "Done.\n\nsession_id: 20260518_223724_45ab80\n";
|
||||
expect(parseSessionIdFromStdout(stdout)).toBe("20260518_223724_45ab80");
|
||||
});
|
||||
|
||||
test("reads session_id from the first line (quiet mode)", () => {
|
||||
const stdout = "session_id: 20260518_165315_3467a1\nHello world\n";
|
||||
expect(parseSessionIdFromStdout(stdout)).toBe("20260518_165315_3467a1");
|
||||
});
|
||||
|
||||
test("returns null when no session_id line present", () => {
|
||||
expect(parseSessionIdFromStdout("only assistant text\n")).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("messageToTurnPayload", () => {
|
||||
test("maps assistant tool_calls to toolCalls", () => {
|
||||
const msg: HermesSessionMessage = {
|
||||
role: "assistant",
|
||||
content: "",
|
||||
reasoning: null,
|
||||
tool_calls: [{ function: { name: "read_file", arguments: '{"path":"x"}' } }],
|
||||
};
|
||||
const turn = messageToTurnPayload(msg, 0);
|
||||
expect(turn).toEqual({
|
||||
index: 0,
|
||||
role: "assistant",
|
||||
content: "",
|
||||
toolCalls: [{ name: "read_file", args: '{"path":"x"}' }],
|
||||
reasoning: null,
|
||||
});
|
||||
});
|
||||
|
||||
test("skips user messages", () => {
|
||||
const msg: HermesSessionMessage = {
|
||||
role: "user",
|
||||
content: "hi",
|
||||
reasoning: null,
|
||||
tool_calls: null,
|
||||
};
|
||||
expect(messageToTurnPayload(msg, 0)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("extractLastAssistantContent", () => {
|
||||
test("returns the last non-empty assistant content", () => {
|
||||
const messages: HermesSessionMessage[] = [
|
||||
{ role: "assistant", content: "first", reasoning: null, tool_calls: null },
|
||||
{ role: "tool", content: "tool output", reasoning: null, tool_calls: null },
|
||||
{ role: "assistant", content: "", reasoning: null, tool_calls: null },
|
||||
{ role: "assistant", content: "final answer", reasoning: null, tool_calls: null },
|
||||
];
|
||||
expect(extractLastAssistantContent(messages)).toBe("final answer");
|
||||
});
|
||||
});
|
||||
|
||||
describe("computeDurationMs", () => {
|
||||
test("computes elapsed time from session_start", () => {
|
||||
const now = Date.parse("2026-05-18T13:32:59.028640Z");
|
||||
const duration = computeDurationMs("2026-05-18T13:31:59.028640Z", now);
|
||||
expect(duration).toBe(60_000);
|
||||
});
|
||||
});
|
||||
|
||||
describe("storeHermesSessionDetail", () => {
|
||||
test("stores hermes-detail root with ocas_ref turns walkable", async () => {
|
||||
const session: HermesSessionJson = {
|
||||
session_id: "20260518_133159_6a84e8",
|
||||
model: "claude-opus-4.6",
|
||||
session_start: "2026-05-18T13:31:59.028640",
|
||||
messages: [
|
||||
{ role: "user", content: "task", reasoning: null, tool_calls: null },
|
||||
{
|
||||
role: "assistant",
|
||||
content: "",
|
||||
reasoning: "thinking",
|
||||
tool_calls: [{ function: { name: "terminal", arguments: "{}" } }],
|
||||
},
|
||||
{ role: "tool", content: "ok", reasoning: null, tool_calls: null },
|
||||
{ role: "assistant", content: "done", reasoning: null, tool_calls: null },
|
||||
],
|
||||
};
|
||||
|
||||
const store = createMemoryStore();
|
||||
const now = Date.parse("2026-05-18T13:32:59.028640");
|
||||
const { detailHash, output } = await storeHermesSessionDetail(store, session, now);
|
||||
|
||||
expect(output).toBe("done");
|
||||
|
||||
const detailNode = store.get(detailHash);
|
||||
expect(detailNode).not.toBeNull();
|
||||
if (detailNode === null) {
|
||||
return;
|
||||
}
|
||||
expect(validate(store, detailNode)).toBe(true);
|
||||
expect(detailNode.payload).toMatchObject({
|
||||
sessionId: "20260518_133159_6a84e8",
|
||||
model: "claude-opus-4.6",
|
||||
duration: 60_000,
|
||||
turnCount: 3,
|
||||
});
|
||||
|
||||
const turnRefs = refs(store, detailNode);
|
||||
expect(turnRefs).toHaveLength(3);
|
||||
|
||||
const visited: string[] = [];
|
||||
walk(store, detailHash, (hash) => visited.push(hash));
|
||||
expect(visited).toContain(detailHash);
|
||||
for (const turnHash of turnRefs) {
|
||||
expect(visited).toContain(turnHash);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// ── SQLite fallback tests ──────────────────────────────────────────
|
||||
|
||||
function createTestDb(dbPath: string): Database {
|
||||
const db = new Database(dbPath);
|
||||
db.run(`CREATE TABLE sessions (
|
||||
id TEXT PRIMARY KEY,
|
||||
model TEXT NOT NULL,
|
||||
started_at INTEGER NOT NULL
|
||||
)`);
|
||||
db.run(`CREATE TABLE messages (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
session_id TEXT NOT NULL,
|
||||
role TEXT NOT NULL,
|
||||
content TEXT,
|
||||
reasoning TEXT,
|
||||
tool_calls TEXT,
|
||||
FOREIGN KEY (session_id) REFERENCES sessions(id)
|
||||
)`);
|
||||
return db;
|
||||
}
|
||||
|
||||
describe("getHermesDbPath", () => {
|
||||
test("returns correct path", () => {
|
||||
const { homedir } = require("node:os");
|
||||
const { join } = require("node:path");
|
||||
expect(getHermesDbPath()).toBe(join(homedir(), ".hermes", "state.db"));
|
||||
});
|
||||
});
|
||||
|
||||
describe("loadHermesSessionFromDb", () => {
|
||||
test("returns session data from SQLite", async () => {
|
||||
const tmpDir = await mkdtemp(join(tmpdir(), "hermes-test-"));
|
||||
const dbPath = join(tmpDir, "state.db");
|
||||
const db = createTestDb(dbPath);
|
||||
|
||||
const sessionId = "test-session-001";
|
||||
const startedAt = 1748099519;
|
||||
db.run("INSERT INTO sessions (id, model, started_at) VALUES (?, ?, ?)", [
|
||||
sessionId,
|
||||
"claude-opus-4.6",
|
||||
startedAt,
|
||||
]);
|
||||
db.run(
|
||||
"INSERT INTO messages (session_id, role, content, reasoning, tool_calls) VALUES (?, ?, ?, ?, ?)",
|
||||
[sessionId, "user", "hello", null, null],
|
||||
);
|
||||
db.run(
|
||||
"INSERT INTO messages (session_id, role, content, reasoning, tool_calls) VALUES (?, ?, ?, ?, ?)",
|
||||
[sessionId, "assistant", "hi there", "thinking...", null],
|
||||
);
|
||||
db.close();
|
||||
|
||||
const result = await loadHermesSessionFromDb(sessionId, dbPath);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.session_id).toBe(sessionId);
|
||||
expect(result!.model).toBe("claude-opus-4.6");
|
||||
expect(result!.messages).toHaveLength(2);
|
||||
expect(result!.messages[0]!.role).toBe("user");
|
||||
expect(result!.messages[0]!.content).toBe("hello");
|
||||
expect(result!.messages[1]!.role).toBe("assistant");
|
||||
expect(result!.messages[1]!.content).toBe("hi there");
|
||||
expect(result!.messages[1]!.reasoning).toBe("thinking...");
|
||||
|
||||
await rm(tmpDir, { recursive: true });
|
||||
});
|
||||
|
||||
test("returns null when no session exists in DB", async () => {
|
||||
const tmpDir = await mkdtemp(join(tmpdir(), "hermes-test-"));
|
||||
const dbPath = join(tmpDir, "state.db");
|
||||
const db = createTestDb(dbPath);
|
||||
db.close();
|
||||
|
||||
const result = await loadHermesSessionFromDb("nonexistent", dbPath);
|
||||
expect(result).toBeNull();
|
||||
|
||||
await rm(tmpDir, { recursive: true });
|
||||
});
|
||||
|
||||
test("returns null when DB file does not exist", async () => {
|
||||
const result = await loadHermesSessionFromDb("any-id", "/tmp/nonexistent-hermes-db.db");
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
test("correctly parses tool_calls from DB JSON string", async () => {
|
||||
const tmpDir = await mkdtemp(join(tmpdir(), "hermes-test-"));
|
||||
const dbPath = join(tmpDir, "state.db");
|
||||
const db = createTestDb(dbPath);
|
||||
|
||||
const sessionId = "test-tool-calls";
|
||||
db.run("INSERT INTO sessions (id, model, started_at) VALUES (?, ?, ?)", [
|
||||
sessionId,
|
||||
"gpt-4",
|
||||
1748099519,
|
||||
]);
|
||||
const toolCallsJson = JSON.stringify([
|
||||
{ function: { name: "read_file", arguments: '{"path":"x"}' } },
|
||||
]);
|
||||
db.run(
|
||||
"INSERT INTO messages (session_id, role, content, reasoning, tool_calls) VALUES (?, ?, ?, ?, ?)",
|
||||
[sessionId, "assistant", "", null, toolCallsJson],
|
||||
);
|
||||
db.close();
|
||||
|
||||
const result = await loadHermesSessionFromDb(sessionId, dbPath);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.messages[0]!.tool_calls).toEqual([
|
||||
{ function: { name: "read_file", arguments: '{"path":"x"}' } },
|
||||
]);
|
||||
|
||||
await rm(tmpDir, { recursive: true });
|
||||
});
|
||||
|
||||
test("handles null fields in DB messages gracefully", async () => {
|
||||
const tmpDir = await mkdtemp(join(tmpdir(), "hermes-test-"));
|
||||
const dbPath = join(tmpDir, "state.db");
|
||||
const db = createTestDb(dbPath);
|
||||
|
||||
const sessionId = "test-nulls";
|
||||
db.run("INSERT INTO sessions (id, model, started_at) VALUES (?, ?, ?)", [
|
||||
sessionId,
|
||||
"model",
|
||||
1748099519,
|
||||
]);
|
||||
db.run(
|
||||
"INSERT INTO messages (session_id, role, content, reasoning, tool_calls) VALUES (?, ?, ?, ?, ?)",
|
||||
[sessionId, "assistant", null, null, null],
|
||||
);
|
||||
db.close();
|
||||
|
||||
const result = await loadHermesSessionFromDb(sessionId, dbPath);
|
||||
expect(result).not.toBeNull();
|
||||
const msg = result!.messages[0]!;
|
||||
expect(msg.content).toBeNull();
|
||||
expect(msg.reasoning).toBeNull();
|
||||
expect(msg.tool_calls).toBeNull();
|
||||
|
||||
await rm(tmpDir, { recursive: true });
|
||||
});
|
||||
|
||||
test("messages ordered by insertion order", async () => {
|
||||
const tmpDir = await mkdtemp(join(tmpdir(), "hermes-test-"));
|
||||
const dbPath = join(tmpDir, "state.db");
|
||||
const db = createTestDb(dbPath);
|
||||
|
||||
const sessionId = "test-order";
|
||||
db.run("INSERT INTO sessions (id, model, started_at) VALUES (?, ?, ?)", [
|
||||
sessionId,
|
||||
"model",
|
||||
1748099519,
|
||||
]);
|
||||
db.run(
|
||||
"INSERT INTO messages (session_id, role, content, reasoning, tool_calls) VALUES (?, ?, ?, ?, ?)",
|
||||
[sessionId, "user", "first", null, null],
|
||||
);
|
||||
db.run(
|
||||
"INSERT INTO messages (session_id, role, content, reasoning, tool_calls) VALUES (?, ?, ?, ?, ?)",
|
||||
[sessionId, "assistant", "second", null, null],
|
||||
);
|
||||
db.run(
|
||||
"INSERT INTO messages (session_id, role, content, reasoning, tool_calls) VALUES (?, ?, ?, ?, ?)",
|
||||
[sessionId, "user", "third", null, null],
|
||||
);
|
||||
db.close();
|
||||
|
||||
const result = await loadHermesSessionFromDb(sessionId, dbPath);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.messages.map((m) => m.content)).toEqual(["first", "second", "third"]);
|
||||
|
||||
await rm(tmpDir, { recursive: true });
|
||||
});
|
||||
|
||||
test("converts unix timestamp to ISO string for session_start", async () => {
|
||||
const tmpDir = await mkdtemp(join(tmpdir(), "hermes-test-"));
|
||||
const dbPath = join(tmpDir, "state.db");
|
||||
const db = createTestDb(dbPath);
|
||||
|
||||
const sessionId = "test-timestamp";
|
||||
const startedAt = 1748099519;
|
||||
db.run("INSERT INTO sessions (id, model, started_at) VALUES (?, ?, ?)", [
|
||||
sessionId,
|
||||
"model",
|
||||
startedAt,
|
||||
]);
|
||||
db.close();
|
||||
|
||||
const result = await loadHermesSessionFromDb(sessionId, dbPath);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.session_start).toBe(new Date(startedAt * 1000).toISOString());
|
||||
|
||||
await rm(tmpDir, { recursive: true });
|
||||
});
|
||||
});
|
||||
|
||||
describe("loadHermesSession with SQLite fallback", () => {
|
||||
test("JSON file takes priority over DB", async () => {
|
||||
const tmpDir = await mkdtemp(join(tmpdir(), "hermes-test-"));
|
||||
const dbPath = join(tmpDir, "state.db");
|
||||
const jsonPath = join(tmpDir, "session.json");
|
||||
|
||||
// Create DB with one model value
|
||||
const db = createTestDb(dbPath);
|
||||
const sessionId = "test-priority";
|
||||
db.run("INSERT INTO sessions (id, model, started_at) VALUES (?, ?, ?)", [
|
||||
sessionId,
|
||||
"db-model",
|
||||
1748099519,
|
||||
]);
|
||||
db.run(
|
||||
"INSERT INTO messages (session_id, role, content, reasoning, tool_calls) VALUES (?, ?, ?, ?, ?)",
|
||||
[sessionId, "user", "from db", null, null],
|
||||
);
|
||||
db.close();
|
||||
|
||||
// Create JSON file with a different model value
|
||||
const jsonData: HermesSessionJson = {
|
||||
session_id: sessionId,
|
||||
model: "json-model",
|
||||
session_start: "2026-05-24T12:00:00.000Z",
|
||||
messages: [{ role: "user", content: "from json", reasoning: null, tool_calls: null }],
|
||||
};
|
||||
await writeFile(jsonPath, JSON.stringify(jsonData));
|
||||
|
||||
// loadHermesSession reads from JSON path, so we test the existing function directly
|
||||
// The JSON-first priority is inherent in the implementation
|
||||
const { readFile } = await import("node:fs/promises");
|
||||
const text = await readFile(jsonPath, "utf8");
|
||||
const parsed = JSON.parse(text);
|
||||
expect(parsed.model).toBe("json-model");
|
||||
|
||||
await rm(tmpDir, { recursive: true });
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user