fix(tui): prioritize slash command name matches
This commit is contained in:
1 parent
33aa70e4a5
commit
5c71b87a3f
3 files changed
+85
-2
No files matched your search
@@ -5,6 +5,8 @@ import { NanobotTui } from "./app"
|
||||
import type { MessageOptions, SkillCandidate, SlashCommand } from "../client"
|
||||
import type { ClipboardImageReader } from "../composer/clipboard-image"
|
||||
import { options, client, mount, waitUntil, occurrences } from "./test-support"
|
||||
import { LOCAL_COMMANDS } from "./commands"
|
||||
import type { CommandMenu } from "../menus/command-menu"
|
||||
|
||||
describe("NanobotTui composer", () => {
|
||||
let setup: TestRendererSetup | undefined
|
||||
@@ -680,6 +682,37 @@ describe("NanobotTui composer", () => {
|
||||
expect(sent).toEqual([])
|
||||
})
|
||||
|
||||
test.each(["enter", "tab"])("completes /se to /sessions with %s", async (key) => {
|
||||
setup = await createRenderer({ width: 80, height: 24, screenMode: "alternate-screen" })
|
||||
const sent: string[] = []
|
||||
const app = mount(setup, sent)
|
||||
app.accept({ event: "attached", chat_id: "chat" })
|
||||
const ui = app as unknown as {
|
||||
ready: boolean
|
||||
composer: TextareaRenderable
|
||||
commandMenu: CommandMenu
|
||||
}
|
||||
await waitUntil(() => ui.ready)
|
||||
ui.commandMenu.setCommands([{
|
||||
command: "/model",
|
||||
title: "Switch model preset",
|
||||
description: "Show or switch the active model preset.",
|
||||
argHint: "[preset]",
|
||||
lifecycle: "side_channel",
|
||||
acceptsArgs: true,
|
||||
}], LOCAL_COMMANDS)
|
||||
|
||||
await setup.mockInput.typeText("/se")
|
||||
await setup.renderOnce()
|
||||
expect(setup.captureCharFrame()).toContain("› /sessions")
|
||||
if (key === "enter") setup.mockInput.pressEnter()
|
||||
else setup.mockInput.pressTab()
|
||||
await waitUntil(() => ui.composer.plainText !== "/se")
|
||||
|
||||
expect(ui.composer.plainText).toBe("/sessions")
|
||||
expect(sent).toEqual([])
|
||||
})
|
||||
|
||||
test("completes available skills with arrows, Enter, Tab, and Escape", async () => {
|
||||
setup = await createRenderer({ width: 80, height: 24, screenMode: "alternate-screen" })
|
||||
const sent: string[] = []
|
||||
|
||||
@@ -3,6 +3,7 @@ import { createTestRenderer, type TestRendererSetup } from "@opentui/core/testin
|
||||
|
||||
import { CommandMenu, resolveSlashCommandLifecycle } from "./command-menu"
|
||||
import type { SlashCommand } from "../client"
|
||||
import { LOCAL_COMMANDS } from "../app/commands"
|
||||
|
||||
const commands: SlashCommand[] = [
|
||||
{
|
||||
@@ -21,6 +22,14 @@ const commands: SlashCommand[] = [
|
||||
lifecycle: "side_channel",
|
||||
acceptsArgs: true,
|
||||
},
|
||||
{
|
||||
command: "/model",
|
||||
title: "Switch model preset",
|
||||
description: "Show or switch the active model preset.",
|
||||
argHint: "[preset]",
|
||||
lifecycle: "side_channel",
|
||||
acceptsArgs: true,
|
||||
},
|
||||
]
|
||||
|
||||
describe("CommandMenu", () => {
|
||||
@@ -53,6 +62,34 @@ describe("CommandMenu", () => {
|
||||
expect(menu.visible).toBe(false)
|
||||
})
|
||||
|
||||
test("prioritizes command prefixes while retaining title discovery and navigation", async () => {
|
||||
setup = await createTestRenderer({ width: 80, height: 18, screenMode: "alternate-screen" })
|
||||
const menu = new CommandMenu(setup.renderer, {
|
||||
text: "#FFFFFF",
|
||||
muted: "#999999",
|
||||
border: "#555555",
|
||||
})
|
||||
setup.renderer.root.add(menu.root)
|
||||
menu.setCommands(commands, LOCAL_COMMANDS)
|
||||
menu.update("/se")
|
||||
await setup.renderOnce()
|
||||
|
||||
const frame = setup.captureCharFrame()
|
||||
expect(frame).toContain("› /sessions")
|
||||
expect(frame.indexOf("/sessions")).toBeLessThan(frame.indexOf("/model"))
|
||||
expect(menu.completion("/se")).toBe("/sessions")
|
||||
|
||||
expect(menu.move(1)).toBe(true)
|
||||
menu.update("/se", 1)
|
||||
expect(menu.complete()).toBe("/model ")
|
||||
|
||||
menu.update("/preset")
|
||||
expect(menu.complete()).toBe("/model ")
|
||||
|
||||
menu.update("/")
|
||||
expect(menu.completion("/")).toBe("/branch")
|
||||
})
|
||||
|
||||
test("hides outside the leading command token", async () => {
|
||||
setup = await createTestRenderer({ width: 60, height: 12, screenMode: "alternate-screen" })
|
||||
const menu = new CommandMenu(setup.renderer, {
|
||||
|
||||
@@ -32,6 +32,14 @@ function descriptor(choice: CommandChoice): SlashCommand | TuiCommand {
|
||||
return choice.command
|
||||
}
|
||||
|
||||
function commandMatchRank(choice: CommandChoice, query: string): number {
|
||||
const name = descriptor(choice).command.slice(1).toLocaleLowerCase()
|
||||
if (name === query) return 0
|
||||
if (name.startsWith(query)) return 1
|
||||
if (name.includes(query)) return 2
|
||||
return 3
|
||||
}
|
||||
|
||||
export function resolveSlashCommandLifecycle(
|
||||
input: string,
|
||||
command: SlashCommand,
|
||||
@@ -108,8 +116,13 @@ export class CommandMenu {
|
||||
this.hide()
|
||||
return
|
||||
}
|
||||
if (changed || !this.picker.visible) this.picker.show(this.commands, token.slice(1), limit)
|
||||
else this.picker.update(token.slice(1), limit)
|
||||
const query = token.slice(1)
|
||||
if (changed || !this.picker.visible) {
|
||||
const commands = [...this.commands].sort((left, right) => (
|
||||
commandMatchRank(left, query) - commandMatchRank(right, query)
|
||||
))
|
||||
this.picker.show(commands, query, limit)
|
||||
} else this.picker.update(query, limit)
|
||||
}
|
||||
|
||||
move(direction: -1 | 1): boolean {
|
||||
|
||||
Reference in new issue
Block a user