212 lines
9.8 KiB
TypeScript
212 lines
9.8 KiB
TypeScript
import { expect, test } from "bun:test"
|
||
import {
|
||
buildLiveSnapshot,
|
||
cycleTab,
|
||
effectiveTab,
|
||
extractBash,
|
||
extractChanges,
|
||
extractFiles,
|
||
extractImages,
|
||
fitImage,
|
||
freshImage,
|
||
imageProtocol,
|
||
initialNav,
|
||
resetFollow,
|
||
resolveIndex,
|
||
stepEntry,
|
||
} from "../src/tui/displays.ts"
|
||
import { Bus } from "../src/bus/index.ts"
|
||
import { createView, type Item, type Pending, type ViewState } from "../src/tui/state.ts"
|
||
|
||
function baseState(overrides: Partial<ViewState> = {}): ViewState {
|
||
return {
|
||
items: [],
|
||
phase: "idle",
|
||
busySince: 0,
|
||
showReasoning: false,
|
||
expandTools: false,
|
||
queued: { texts: [], mode: "steer" },
|
||
jobs: [],
|
||
...overrides,
|
||
}
|
||
}
|
||
|
||
test("extractFiles, extractChanges, and extractBash derive entries cleanly from items and pending diffs", () => {
|
||
const diff = "--- a/src/a.ts\n+++ b/src/a.ts\n@@ -1,1 +1,2 @@\n-old\n+new\n+more\n"
|
||
const pendingDiff = "--- a/src/b.ts\n+++ b/src/b.ts\n@@ -1,1 +1,1 @@\n-x\n+y\n"
|
||
const items: Item[] = [
|
||
{ kind: "tool", id: "t1", name: "read", args: { path: "src/a.ts" }, status: "done", output: "1: old\n", facts: ["1 line"], ms: 12 },
|
||
{ kind: "tool", id: "t2", name: "edit", args: { path: "src/a.ts" }, status: "done", diff, ms: 40 },
|
||
{ kind: "tool", id: "t3", name: "bash", args: { command: "bun test\n" }, status: "running", title: "bun test" },
|
||
]
|
||
const pending: Pending = {
|
||
tool: "edit",
|
||
args: { path: "src/b.ts" },
|
||
request: { permission: "edit", class: "write", patterns: ["src/b.ts"] },
|
||
decision: { action: "ask", reason: "edit needs approval", always: [] },
|
||
preview: { diff: pendingDiff },
|
||
resolve: () => {},
|
||
}
|
||
|
||
const files = extractFiles(items)
|
||
expect(files.length).toBe(1)
|
||
expect(files[0]!.title).toBe("src/a.ts")
|
||
expect(files[0]!.body).toContain("1: old")
|
||
|
||
const changes = extractChanges(items, pending)
|
||
expect(changes.length).toBe(2)
|
||
expect(changes[0]!.status).toBe("done")
|
||
expect(changes[0]!.added).toBe(2)
|
||
expect(changes[0]!.removed).toBe(1)
|
||
expect(changes[1]!.status).toBe("proposed")
|
||
expect(changes[1]!.title).toBe("src/b.ts")
|
||
expect(changes[1]!.subtitle).toContain("WAITING FOR APPROVAL")
|
||
|
||
const bash = extractBash(items)
|
||
expect(bash.length).toBe(1)
|
||
expect(bash[0]!.status).toBe("running")
|
||
expect(bash[0]!.output).toContain("running")
|
||
})
|
||
|
||
test("buildLiveSnapshot surfaces active tools, live thinking, plan, jobs, and waiting state", () => {
|
||
const state = baseState({
|
||
phase: "tools",
|
||
items: [
|
||
{ kind: "reasoning", id: "r1", text: "Checking imports\nEditing parser.ts next", streaming: true, started: 1000 },
|
||
{ kind: "tool", id: "t1", name: "read", args: { path: "src/parser.ts" }, status: "done", output: "line1\nline2", ms: 20 },
|
||
{
|
||
kind: "tool",
|
||
id: "t2",
|
||
name: "task",
|
||
args: { description: "inspect tests" },
|
||
status: "running",
|
||
title: "inspect tests",
|
||
children: [{ name: "grep", title: "parseExpr" }],
|
||
},
|
||
],
|
||
jobs: [{ id: "job1", kind: "command", description: "run suite", state: "running", started: 1000, unread: 0 }],
|
||
})
|
||
|
||
const snap = buildLiveSnapshot(state, 4000)
|
||
expect(snap.thinkingStreaming).toBe(true)
|
||
expect(snap.thinkingTail).toContain("Editing parser.ts next")
|
||
expect(snap.activeTools.length).toBe(1)
|
||
expect(snap.activeTools[0]!.children?.[0]?.name).toBe("grep")
|
||
expect(snap.recentTools.length).toBe(1)
|
||
expect(snap.recentTools[0]!.name).toBe("read")
|
||
expect(snap.jobs.length).toBe(1)
|
||
expect(snap.jobs[0]!.detail).toContain("running")
|
||
})
|
||
|
||
test("DisplayNav auto-jumps to changes on proposed diff, respects pinning, and resets on new prompt", () => {
|
||
let nav = initialNav()
|
||
expect(effectiveTab(nav, false)).toBe("live")
|
||
expect(effectiveTab(nav, true)).toBe("changes")
|
||
|
||
// Alt+Down cycles live -> files and pins.
|
||
nav = cycleTab(nav, 1)
|
||
expect(nav.tab).toBe("files")
|
||
expect(nav.pinned).toBe(true)
|
||
// Pinned stays on files even if proposed diff appears.
|
||
expect(effectiveTab(nav, true)).toBe("files")
|
||
|
||
// Stepping entries back and forward.
|
||
expect(resolveIndex(nav, "files", 3)).toBe(2)
|
||
nav = stepEntry(nav, "files", -1, 3)
|
||
expect(resolveIndex(nav, "files", 3)).toBe(1)
|
||
nav = stepEntry(nav, "files", 1, 3)
|
||
expect(resolveIndex(nav, "files", 3)).toBe(2)
|
||
|
||
// New prompt resets follow back to live.
|
||
nav = resetFollow(nav)
|
||
expect(nav.tab).toBe("live")
|
||
expect(nav.pinned).toBe(false)
|
||
expect(effectiveTab(nav, true)).toBe("changes")
|
||
})
|
||
|
||
test("the session's pictures: finished view_image and show calls, and what was attached — in order, by path", () => {
|
||
const abs = (p: string) => (p.startsWith("/") ? p : `/work/${p}`)
|
||
const items: Item[] = [
|
||
{ kind: "user", id: "u1", text: "look", images: ["/work/in/a.png"] },
|
||
{ kind: "tool", id: "t1", name: "view_image", args: { path: "shots/one.PNG" }, status: "done" },
|
||
// Not pictures: a failed view, one refused, one still running, a shown text file, a read.
|
||
{ kind: "tool", id: "t2", name: "view_image", args: { path: "gone.png" }, status: "error" },
|
||
{ kind: "tool", id: "t3", name: "view_image", args: { path: "secret.png" }, status: "denied" },
|
||
{ kind: "tool", id: "t4", name: "view_image", args: { path: "later.png" }, status: "running" },
|
||
{ kind: "tool", id: "t5", name: "show", args: { path: "notes.md" }, status: "done", output: "Shown to the user: notes.md." },
|
||
{ kind: "tool", id: "t6", name: "read", args: { path: "pic.png" }, status: "done" },
|
||
{ kind: "tool", id: "t7", name: "show", args: { path: "chart.webp" }, status: "done" },
|
||
]
|
||
expect(extractImages(items, abs)).toEqual([
|
||
{ id: "u1.0", title: "/work/in/a.png", path: "/work/in/a.png", source: "attached" },
|
||
{ id: "t1", title: "shots/one.PNG", path: "/work/shots/one.PNG", source: "viewed" },
|
||
{ id: "t7", title: "chart.webp", path: "/work/chart.webp", source: "shown" },
|
||
])
|
||
// A shown picture is in images, not in files; a shown text file is in files, and says so.
|
||
const files = extractFiles(items)
|
||
expect(files.map((f) => f.title)).toEqual(["notes.md", "pic.png"])
|
||
expect(files[0]!.subtitle).toContain("shown")
|
||
})
|
||
|
||
test("the pane goes to a picture by itself only while it is the newest thing a tool did, and never over a pinned tab", () => {
|
||
const view = (id: string): Item => ({ kind: "tool", id, name: "view_image", args: { path: "a.png" }, status: "done" })
|
||
const bash: Item = { kind: "tool", id: "b", name: "bash", args: { command: "ls" }, status: "done" }
|
||
expect(freshImage([view("1")])).toBe(true)
|
||
expect(freshImage([view("1"), { kind: "assistant", id: "a", text: "A cat.", streaming: false }])).toBe(true)
|
||
// The agent went on to something else; a new prompt; a view still running.
|
||
expect(freshImage([view("1"), bash])).toBe(false)
|
||
expect(freshImage([view("1"), { kind: "user", id: "u", text: "next" }])).toBe(false)
|
||
expect(freshImage([{ ...view("1"), status: "running" } as Item])).toBe(false)
|
||
// What the person attached does not move the pane.
|
||
expect(freshImage([{ kind: "user", id: "u", text: "see", images: ["/a.png"] }])).toBe(false)
|
||
|
||
const nav = initialNav()
|
||
expect(effectiveTab(nav, false, true)).toBe("images")
|
||
// A change waiting for an answer comes first.
|
||
expect(effectiveTab(nav, true, true)).toBe("changes")
|
||
expect(effectiveTab({ ...nav, tab: "bash", pinned: true }, true, true)).toBe("bash")
|
||
expect(effectiveTab(nav, false, false)).toBe("live")
|
||
// The images tab is one of the tabs: alt+↑ from live reaches it, alt+←→ walks it.
|
||
expect(cycleTab(nav, -1).tab).toBe("images")
|
||
expect(resolveIndex(nav, "images", 3)).toBe(2)
|
||
expect(stepEntry(nav, "images", -1, 3).index.images).toBe(1)
|
||
expect(resetFollow(stepEntry(nav, "images", -1, 3)).index.images).toBe(-1)
|
||
})
|
||
|
||
test("images: auto leaves the protocol to what the terminal is found to do, on asks for kitty graphics, off draws nothing", () => {
|
||
expect(imageProtocol(undefined)).toBe("auto")
|
||
expect(imageProtocol("auto")).toBe("auto")
|
||
expect(imageProtocol("on")).toBe("kitty")
|
||
expect(imageProtocol("off")).toBeUndefined()
|
||
})
|
||
|
||
test("a picture's box keeps its aspect inside the cells it is given", () => {
|
||
// Cells twice as tall as wide: 64×32 px is four cells wide for each row.
|
||
expect(fitImage({ width: 64, height: 32 }, 48, 26)).toEqual({ width: 48, height: 12 })
|
||
expect(fitImage({ width: 100, height: 400 }, 48, 20)).toEqual({ width: 10, height: 20 })
|
||
// The terminal's own cell size, where it says one (10×22 px cells).
|
||
expect(fitImage({ width: 800, height: 600 }, 40, 30, 2.2)).toEqual({ width: 40, height: 14 })
|
||
for (const px of [{ width: 1, height: 5000 }, { width: 5000, height: 1 }, { width: 640, height: 480 }])
|
||
for (let w = 1; w <= 90; w += 7)
|
||
for (let h = 1; h <= 60; h += 5) {
|
||
const box = fitImage(px, w, h)
|
||
expect(box.width).toBeGreaterThanOrEqual(1)
|
||
expect(box.width).toBeLessThanOrEqual(w)
|
||
expect(box.height).toBeGreaterThanOrEqual(1)
|
||
expect(box.height).toBeLessThanOrEqual(h)
|
||
}
|
||
expect(fitImage({ width: 0, height: 10 }, 10, 10)).toEqual({ width: 0, height: 0 })
|
||
})
|
||
|
||
test("a stored session's attached pictures are found again by the path the attachment named", () => {
|
||
const view = createView(new Bus())
|
||
view.load([
|
||
{ role: "user", parts: [{ type: "text", text: "what is this @shots/a.png" }, { type: "text", text: '<image path="shots/a.png"/>' }, { type: "image", mime: "image/png", data: "" }, { type: "text", text: '<image path="b.png">(not attached: this model has no vision)</image>' }] },
|
||
{ role: "assistant", parts: [{ type: "text", text: "A cat." }] },
|
||
] as never)
|
||
expect(extractImages(view.state.items, (p) => `/work/${p}`)).toEqual([{ id: expect.any(String) as never, title: "shots/a.png", path: "/work/shots/a.png", source: "attached" }])
|
||
// What the person typed is shown as before: the attachment's text is not.
|
||
const user = view.state.items[0]!
|
||
expect(user.kind === "user" && user.text).toBe("what is this @shots/a.png")
|
||
})
|