agentlens / tests /sessions.test.ts
hongcoding's picture
feat: full conversation migration (266 sessions / 70k messages) with robust parsing
11e2b39 verified
Raw History Blame Contribute Delete
2.4 kB
import { describe, it, expect } from "vitest";
import { parseSessionLines } from "../src/export/sessions.js";
function jsonl(events: unknown[]): string {
return events.map((e) => JSON.stringify(e)).join("\n");
}
describe("parseSessionLines", () => {
it("parses title, user, assistant and tool events", () => {
const raw = jsonl([
{ type: "session", id: "s1", cwd: "/p", createdAt: 1000 },
{ type: "session/title", data: { title: "测试会话" } },
{ type: "user/message", time: 2000, data: { source: { kind: "user" }, content: [{ type: "text", text: "你好" }] } },
{ type: "assistant/message", time: 3000, data: { message: { content: [{ type: "text", text: "你好,有什么可以帮你?" }] } } },
{ type: "tool/call", time: 4000, data: { name: "bash", arguments: '{"command":"ls"}' } },
{ type: "tool/result", time: 5000, data: { message: { content: [{ type: "text", text: "file.txt" }] } } },
]);
const t = parseSessionLines(raw)!;
expect(t).toBeDefined();
expect(t.title).toBe("测试会话");
expect(t.cwd).toBe("/p");
const who = t.entries.map((e) => e.who);
expect(who).toEqual(["用户", "助手", "工具", "工具"]);
expect(t.entries[0].text).toBe("你好");
});
it("skips system-reminder injections", () => {
const raw = jsonl([
{ type: "session", id: "s1" },
{ type: "user/message", data: { source: { kind: "user" }, content: [{ type: "text", text: "<system-reminder>workspace instructions</system-reminder>" }] } },
{ type: "user/message", data: { source: { kind: "harness" }, content: [{ type: "text", text: "injected" }] } },
{ type: "user/message", data: { source: { kind: "user" }, content: [{ type: "text", text: "真实消息" }] } },
]);
const t = parseSessionLines(raw)!;
expect(t.entries.length).toBe(1);
expect(t.entries[0].text).toBe("真实消息");
});
it("returns undefined for sessions with no renderable messages", () => {
const raw = jsonl([{ type: "session", id: "empty" }]);
expect(parseSessionLines(raw)).toBeUndefined();
});
it("ignores malformed lines without throwing", () => {
const raw = "{bad json}\n" + jsonl([{ type: "user/message", data: { source: { kind: "user" }, content: [{ type: "text", text: "ok" }] } }]);
const t = parseSessionLines(raw)!;
expect(t.entries.length).toBe(1);
});
});