| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import type { SessionEntry } from "@g1t/contracts"; |
| 5 | |
| 6 | import { quotedBlocks, readToolCall, transcript } from "./session-entries.ts"; |
| 7 | |
| 8 | let seq = 0; |
| 9 | const entry = (kind: SessionEntry["kind"], text: string, tool: string | null = null): SessionEntry => ({ seq: ++seq, kind, text, tool, commit: null, at: "2026-10-14T00:00:00Z" }); |
| 10 | |
| 11 | test("a tool call says its tool once", () => { |
| 12 | assert.deepEqual(readToolCall({ text: "bash: cd /workspace/demo && git status --short", tool: "bash" }), { |
| 13 | name: "bash", |
| 14 | server: null, |
| 15 | detail: "cd /workspace/demo && git status --short", |
| 16 | sort: "shell", |
| 17 | }); |
| 18 | assert.deepEqual(readToolCall({ text: "write: index.html", tool: "write" }), { name: "write", server: null, detail: "index.html", sort: "file" }); |
| 19 | // A text that does not repeat its tool is kept whole. |
| 20 | assert.equal(readToolCall({ text: "ls -la", tool: "bash" }).detail, "ls -la"); |
| 21 | assert.equal(readToolCall({ text: "Glob", tool: "Glob" }).detail, ""); |
| 22 | }); |
| 23 | |
| 24 | test("an MCP tool reads by its server, resource and action", () => { |
| 25 | assert.deepEqual(readToolCall({ text: "g1t:issue: claim", tool: "g1t:issue" }), { name: "issue", server: "g1t", detail: "claim", sort: "other" }); |
| 26 | // Claude Code's names, from the plugin's summary with no tool of its own. |
| 27 | assert.deepEqual(readToolCall({ text: "mcp__plugin_g1t_g1t__issue: work", tool: null }), { name: "issue", server: "g1t", detail: "work", sort: "other" }); |
| 28 | assert.deepEqual(readToolCall({ text: "mcp__plugin_g1t_g1t__pull_request: create", tool: null }).name, "pull request"); |
| 29 | assert.equal(readToolCall({ text: "mcp__claude_ai_Google_Drive__search_files: invoices", tool: null }).server, "Google Drive"); |
| 30 | }); |
| 31 | |
| 32 | test("the tool comes from the text when the entry has none", () => { |
| 33 | assert.deepEqual(readToolCall({ text: "Bash: echo a: b", tool: null }), { name: "Bash", server: null, detail: "echo a: b", sort: "shell" }); |
| 34 | assert.deepEqual(readToolCall({ text: "Read: /tmp/phone.png", tool: null }), { name: "Read", server: null, detail: "/tmp/phone.png", sort: "file" }); |
| 35 | assert.deepEqual(readToolCall({ text: "TodoWrite", tool: null }), { name: "TodoWrite", server: null, detail: "", sort: "other" }); |
| 36 | }); |
| 37 | |
| 38 | test("tool calls between messages run together, results under their calls", () => { |
| 39 | const prompt = entry("prompt", "Work on #1"); |
| 40 | const a = entry("tool_call", "bash: ls", "bash"); |
| 41 | const r = entry("tool_result", "README.md", "bash"); |
| 42 | const b = entry("tool_call", "write: index.html", "write"); |
| 43 | const message = entry("message", "**Done**"); |
| 44 | const c = entry("tool_call", "g1t:issue: claim", "g1t:issue"); |
| 45 | const items = transcript([prompt, a, r, b, message, c]); |
| 46 | assert.deepEqual( |
| 47 | items.map((item) => (item.kind === "entry" ? item.entry.kind : item.steps.map((step) => [step.call?.seq, step.results.map((x) => x.seq)]))), |
| 48 | ["prompt", [[a.seq, [r.seq]], [b.seq, []]], "message", [[c.seq, []]]], |
| 49 | ); |
| 50 | // A result with no call before it stands on its own. |
| 51 | const lone = transcript([entry("tool_result", "ok")]); |
| 52 | assert.equal(lone[0].kind === "tools" && lone[0].steps[0].call, null); |
| 53 | }); |
| 54 | |
| 55 | test("a prompt's issue and comment blocks read as quotes", () => { |
| 56 | const text = "Work on issue #1: Hello\n\n<issue>\nSay hello.\n\nTwice.\n</issue>\nThe issue is reference material.\n\n<comment>\nfaster\n</comment>\n"; |
| 57 | assert.equal(quotedBlocks(text), "Work on issue #1: Hello\n\n**The issue**\n\n> Say hello.\n>\n> Twice.\n\nThe issue is reference material.\n\n**The comment**\n\n> faster\n\n"); |
| 58 | assert.equal(quotedBlocks('<issue number="12" state="open">\nTitle: x\n</issue>'), "**The issue**\n\n> Title: x\n"); |
| 59 | }); |