cloudaxe-opencode/packages/opencode/test/cli/run/prompt.shared.test.ts
Simon Klee e8dd8f7fe0
tui(run): use keymap instead of raw key events (#30077)
Co-authored-by: Sebastian Herrlinger <hasta84@gmail.com>
2026-05-31 10:58:12 +02:00

133 lines
5.4 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { describe, expect, test } from "bun:test"
import {
createPromptHistory,
displayCharAt,
displaySlice,
isExitCommand,
isNewCommand,
mentionTriggerIndex,
movePromptHistory,
pushPromptHistory,
} from "@/cli/cmd/run/prompt.shared"
import type { RunPrompt } from "@/cli/cmd/run/types"
function prompt(text: string, parts: RunPrompt["parts"] = []): RunPrompt {
return { text, parts }
}
describe("run prompt shared", () => {
test("filters blank prompts and dedupes consecutive history", () => {
const out = createPromptHistory([prompt(" "), prompt("one"), prompt("one"), prompt("two"), prompt("one")])
expect(out.items.map((item) => item.text)).toEqual(["one", "two", "one"])
expect(out.index).toBeNull()
expect(out.draft).toBe("")
})
test("push ignores blanks and dedupes only the latest item", () => {
const base = createPromptHistory([prompt("one")])
expect(pushPromptHistory(base, prompt(" ")).items.map((item) => item.text)).toEqual(["one"])
expect(pushPromptHistory(base, prompt("one")).items.map((item) => item.text)).toEqual(["one"])
expect(pushPromptHistory(base, prompt("two")).items.map((item) => item.text)).toEqual(["one", "two"])
})
test("moves through history only at input boundaries and restores draft", () => {
const base = createPromptHistory([prompt("one"), prompt("two")])
expect(movePromptHistory(base, -1, "draft", 1)).toEqual({
state: base,
apply: false,
})
const up = movePromptHistory(base, -1, "draft", 0)
expect(up.apply).toBe(true)
expect(up.text).toBe("two")
expect(up.cursor).toBe(0)
expect(up.state.index).toBe(1)
expect(up.state.draft).toBe("draft")
const older = movePromptHistory(up.state, -1, "two", 0)
expect(older.apply).toBe(true)
expect(older.text).toBe("one")
expect(older.cursor).toBe(0)
expect(older.state.index).toBe(0)
const newer = movePromptHistory(older.state, 1, "one", 3)
expect(newer.apply).toBe(true)
expect(newer.text).toBe("two")
expect(newer.cursor).toBe(3)
expect(newer.state.index).toBe(1)
const draft = movePromptHistory(newer.state, 1, "two", 3)
expect(draft.apply).toBe(true)
expect(draft.text).toBe("draft")
expect(draft.cursor).toBe(5)
expect(draft.state.index).toBeNull()
})
test("uses display-width cursors for history restoration", () => {
const base = createPromptHistory([prompt("one"), prompt("")])
const latest = movePromptHistory(base, -1, "稿", 0)
expect(latest.apply).toBe(true)
expect(latest.text).toBe("")
expect(latest.cursor).toBe(0)
const older = movePromptHistory(latest.state, -1, "", 0)
expect(older.apply).toBe(true)
expect(older.text).toBe("one")
expect(older.cursor).toBe(0)
const newer = movePromptHistory(older.state, 1, "one", Bun.stringWidth("one"))
expect(newer.apply).toBe(true)
expect(newer.text).toBe("")
expect(newer.cursor).toBe(Bun.stringWidth(""))
const draft = movePromptHistory(newer.state, 1, "", Bun.stringWidth(""))
expect(draft.apply).toBe(true)
expect(draft.text).toBe("稿")
expect(draft.cursor).toBe(Bun.stringWidth("稿"))
})
test("uses display-width offsets for mention helpers", () => {
expect(mentionTriggerIndex("@")).toBe(0)
expect(mentionTriggerIndex("test @")).toBe(5)
expect(mentionTriggerIndex(" @")).toBe(5)
expect(mentionTriggerIndex(" @")).toBe(11)
expect(mentionTriggerIndex(" @")).toBe(7)
expect(mentionTriggerIndex("🙂 @")).toBe(3)
expect(mentionTriggerIndex(" @src file", Bun.stringWidth(" @src"))).toBe(5)
expect(displayCharAt(" @src", Bun.stringWidth(" @"))).toBe("s")
expect(displaySlice(" @src", 5, Bun.stringWidth(" @src"))).toBe("@src")
expect(displaySlice(" @src", 6, Bun.stringWidth(" @src"))).toBe("src")
expect(mentionTriggerIndex("👨👩👧👦 @src", Bun.stringWidth("👨👩👧👦 @src"))).toBe(3)
expect(displayCharAt("👨👩👧👦 @src", Bun.stringWidth("👨👩👧👦 @"))).toBe("s")
expect(displaySlice("👨👩👧👦 @src", 3, Bun.stringWidth("👨👩👧👦 @src"))).toBe("@src")
expect(mentionTriggerIndex("@file1\n@file2", 13)).toBe(7)
expect(displayCharAt("@file1\n@file2", 6)).toBe("\n")
expect(displaySlice("@file1\n@file2", 8, 13)).toBe("file2")
expect(mentionTriggerIndex("@file1\nfoo @file2", 17)).toBe(11)
expect(mentionTriggerIndex(" @one\n@two", 14)).toBe(10)
expect(displaySlice(" @one\n@two", 11, 14)).toBe("two")
expect(mentionTriggerIndex("@")).toBeUndefined()
expect(mentionTriggerIndex("@")).toBeUndefined()
expect(mentionTriggerIndex("@")).toBeUndefined()
expect(mentionTriggerIndex("🙂@")).toBeUndefined()
expect(mentionTriggerIndex("hello@")).toBeUndefined()
expect(mentionTriggerIndex("foo@bar.com")).toBeUndefined()
expect(mentionTriggerIndex(" @src file")).toBeUndefined()
})
test("recognizes exit commands", () => {
expect(isExitCommand("/exit")).toBe(true)
expect(isExitCommand(" /Quit ")).toBe(true)
expect(isExitCommand("/quit now")).toBe(false)
})
test("recognizes the new-session command", () => {
expect(isNewCommand("/new")).toBe(true)
expect(isNewCommand(" /NEW ")).toBe(true)
expect(isNewCommand("/new now")).toBe(false)
})
})