import { writeFile } from "node:fs/promises"; import { join } from "node:path"; import { SessionStore } from "@keywork/engine"; import { scratchDirs } from "@keywork/shared/testing"; import { describe, expect, it } from "vitest"; import { sessionHtml } from "./export-html.ts"; const tempDir = scratchDirs("keywork-export-html-"); const exportedAt = new Date("2026-10-02T12:00:00.000Z"); describe("sessionHtml", () => { it("renders every entry type on the active path", async () => { const html = sessionHtml(await fixtureSession(), "path", exportedAt); expect(html).toContain("Export <fixture>"); expect(html).toContain("named Export <fixture>"); expect(html).toContain("model → openai/gpt-5"); expect(html).toContain("thinking → on"); expect(html).toContain("effort → high"); expect(html).toContain("arc → billing"); expect(html).toContain("context folded · 1,200 tokens summarized"); expect(html).toContain("branch summary · from e6"); expect(html).toContain("the other road"); expect(html).toContain('

probe

'); expect(html).toContain("visible <note>"); expect(html).toContain("label best "one" on e9"); }); it("escapes every piece of session text and runs nothing", async () => { const html = sessionHtml(await fixtureSession(), "path", exportedAt); expect(html).toContain("<script>alert(1)</script> fix it"); expect(html).toContain("<img src=x onerror=alert(1)>"); expect(html).toContain("secret plan <b>"); expect(html).not.toMatch(/ fix it" }, { type: "image", mediaType: "image/png", data: "iVBORw0KGgo=" }, { type: "image", mediaType: "text/html", data: "PGgxPg==" }, ], }, }, { type: "message", id: "e7", parentId: "e6", timestamp: at(7), usage: { inputTokens: 1000, outputTokens: 100 }, message: { role: "assistant", parts: [ { type: "thinking", thinking: "secret plan ", signature: "sig" }, { type: "text", text: "On it." }, { type: "tool-call", callId: "c1", name: "bash", arguments: { command: "echo ''" } }, { type: "tool-call", callId: "c2", name: "github__search", arguments: { q: "x" } }, ], }, }, { type: "message", id: "e8", parentId: "e7", timestamp: at(8), message: { role: "tool", parts: [ { type: "tool-result", callId: "c1", output: "", isError: false, }, ], }, }, { type: "message", id: "e9", parentId: "e8", timestamp: at(9), usage: { inputTokens: 10, outputTokens: 5, cacheReadInputTokens: 400 }, message: { role: "assistant", parts: [ { type: "redacted-thinking", data: "opaque" }, { type: "text", text: "done ‮ evil" }, ], }, }, { type: "message", id: "e15", parentId: "e9", timestamp: at(10), message: { role: "user", parts: [{ type: "text", text: "the road not taken" }] }, }, { type: "compaction", id: "e10", parentId: "e9", timestamp: at(11), summary: "folded ", firstKeptEntryId: "e9", tokensBefore: 1200, }, { type: "branch_summary", id: "e11", parentId: "e10", timestamp: at(12), fromId: "e6", summary: "the other road", }, { type: "custom", id: "e12", parentId: "e11", timestamp: at(13), customType: "probe" }, { type: "custom_message", id: "e13", parentId: "e12", timestamp: at(14), customType: "note", content: "visible ", display: true, }, { type: "label", id: "e14", parentId: "e13", timestamp: at(15), targetId: "e9", label: 'best "one"', }, ]; function sliceFrom(html: string, start: string, end: string): string { const from = html.indexOf(start); return from === -1 ? "" : html.slice(from, html.indexOf(end, from)); } const voidElements = new Set(["meta", "img", "br", "hr", "link", "input"]); function wellFormed(html: string): { balanced: boolean; strayAngles: string[] } { const open: string[] = []; let balanced = true; const withoutStyle = html.replace(/"); for (const [, closing, name] of withoutStyle.matchAll(/<(\/?)([a-z][a-z0-9]*)\b[^<>]*>/gi)) { const tag = (name ?? "").toLowerCase(); if (voidElements.has(tag)) continue; if (closing === "") open.push(tag); else if (open.pop() !== tag) balanced = false; } const text = withoutStyle .replace(/^/i, "") .replace(/<\/?[a-z][a-z0-9]*\b[^<>]*>/gi, ""); const strayAngles = text.match(/[<>]/g) ?? []; return { balanced: balanced && open.length === 0, strayAngles }; }