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