import { mkdirSync, mkdtempSync, readFileSync, rmSync, utimesSync, writeFileSync, } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, describe, expect, test } from "vitest"; import { latestSessionFile, parseEditorCommand, renderLatestSessionLog, renderSessionLogMarkdown, } from "../log"; const tempDirs: string[] = []; afterEach(() => { for (const dir of tempDirs.splice(0)) rmSync(dir, { recursive: true, force: true }); }); describe("editor command parsing", () => { test("splits VISUAL/EDITOR without shell", () => { expect(parseEditorCommand("nvim -p --cmd 'set ft=markdown'")).toEqual({ command: "nvim", args: ["-p", "--cmd", "set ft=markdown"], }); expect(parseEditorCommand("code --wait")).toEqual({ command: "code", args: ["--wait"], }); expect(parseEditorCommand(" ")).toBeUndefined(); }); }); describe("llmiterate log renderer", () => { test("renders session JSONL to markdown", () => { const root = tempDir(); const session = join(root, "session.jsonl"); writeFileSync( session, [ JSON.stringify({ message: { role: "user", content: "do thing" } }), JSON.stringify({ message: { role: "assistant", content: [{ type: "text", text: "I edited it." }], }, }), JSON.stringify({ type: "tool_execution_end", toolName: "edit", isError: false, }), ].join("\n") + "\n", ); expect(renderSessionLogMarkdown(session)).toContain("## Assistant"); expect(renderSessionLogMarkdown(session)).toContain("I edited it."); expect(renderSessionLogMarkdown(session)).not.toContain('{"message"'); }); test("picks newest jsonl recursively and writes latest markdown", () => { const root = tempDir(); mkdirSync(join(root, "a")); const older = join(root, "old.jsonl"); const newer = join(root, "a", "new.jsonl"); writeFileSync( older, JSON.stringify({ message: { role: "assistant", content: "old" } }) + "\n", ); writeFileSync( newer, JSON.stringify({ message: { role: "assistant", content: "new" } }) + "\n", ); utimesSync(older, new Date(1000), new Date(1000)); utimesSync(newer, new Date(2000), new Date(2000)); expect(latestSessionFile(root)).toBe(newer); const target = renderLatestSessionLog(root, join(root, "logs")); expect(target?.sessionFile).toBe(newer); expect(readFileSync(target?.markdownFile, "utf-8")).toContain("new"); }); }); function tempDir(): string { const dir = mkdtempSync(join(tmpdir(), "llmiterate-log-")); tempDirs.push(dir); return dir; }