|
| 1 | +/** @jsxImportSource @opentui/solid */ |
| 2 | +// altimate_change — dialog-level actions with in-dialog keybinds (#1328). |
| 3 | +// |
| 4 | +// The Skills browser's "Actions" picker was reachable only through a plugin-registered |
| 5 | +// global keymap layer, which the open dialog's own layer (and its focused filter input) |
| 6 | +// outranked, so ctrl+a never opened it. Declared as DialogSelect `actions` with `bindings` |
| 7 | +// the chord is handled inside the dialog. This mounts a real DialogSelect in the provider |
| 8 | +// stack and presses the key. |
| 9 | +import { createDefaultOpenTuiKeymap } from "@opentui/keymap/opentui" |
| 10 | +import { testRender, useRenderer } from "@opentui/solid" |
| 11 | +import { expect, test } from "bun:test" |
| 12 | +import { onCleanup } from "solid-js" |
| 13 | +import { createTuiResolvedConfig } from "../fixture/tui-runtime" |
| 14 | +import { TestTuiContexts } from "../fixture/tui-environment" |
| 15 | + |
| 16 | +async function wait(fn: () => boolean, timeout = 2000) { |
| 17 | + const start = Date.now() |
| 18 | + while (!fn()) { |
| 19 | + if (Date.now() - start > timeout) throw new Error("timed out waiting for condition") |
| 20 | + await Bun.sleep(10) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +async function mount(opts: { bindings?: { key: string; cmd: string }[]; globalLayer?: boolean } = {}) { |
| 25 | + const [ |
| 26 | + { DialogProvider, useDialog }, |
| 27 | + { DialogSelect }, |
| 28 | + { ThemeProvider }, |
| 29 | + { TuiConfigProvider }, |
| 30 | + { OpencodeKeymapProvider, registerOpencodeKeymap }, |
| 31 | + { KVProvider }, |
| 32 | + { ArgsProvider }, |
| 33 | + { ToastProvider }, |
| 34 | + ] = await Promise.all([ |
| 35 | + import("../../src/ui/dialog"), |
| 36 | + import("../../src/ui/dialog-select"), |
| 37 | + import("../../src/context/theme"), |
| 38 | + import("../../src/config"), |
| 39 | + import("../../src/keymap"), |
| 40 | + import("../../src/context/kv"), |
| 41 | + import("../../src/context/args"), |
| 42 | + import("../../src/ui/toast"), |
| 43 | + ]) |
| 44 | + const triggered: string[] = [] |
| 45 | + |
| 46 | + function Opener() { |
| 47 | + const dialog = useDialog() |
| 48 | + dialog.replace(() => ( |
| 49 | + <DialogSelect |
| 50 | + title="Skills" |
| 51 | + options={[ |
| 52 | + { title: "alpha", value: "alpha" }, |
| 53 | + { title: "beta", value: "beta" }, |
| 54 | + ]} |
| 55 | + actions={[ |
| 56 | + { |
| 57 | + command: "altimate.skill.actions", |
| 58 | + title: "Actions", |
| 59 | + // A function-valued `disabled`, as the Skills browser passes (the synthetic |
| 60 | + // Install row must not open the picker). |
| 61 | + disabled: (o) => o === undefined || o.value === "__install__", |
| 62 | + onTrigger: (o) => triggered.push(`actions:${o.value}`), |
| 63 | + }, |
| 64 | + { command: "altimate.skill.create", title: "New", onTrigger: () => triggered.push("create") }, |
| 65 | + ]} |
| 66 | + bindings={ |
| 67 | + opts.bindings ?? [ |
| 68 | + { key: "ctrl+a", cmd: "altimate.skill.actions" }, |
| 69 | + { key: "ctrl+e", cmd: "altimate.skill.create" }, |
| 70 | + ] |
| 71 | + } |
| 72 | + /> |
| 73 | + )) |
| 74 | + return <box /> |
| 75 | + } |
| 76 | + |
| 77 | + function Harness() { |
| 78 | + const renderer = useRenderer() |
| 79 | + const keymap = createDefaultOpenTuiKeymap(renderer) |
| 80 | + const resolvedConfig = createTuiResolvedConfig({ leader_timeout: 1000 }) |
| 81 | + const off = registerOpencodeKeymap(keymap, renderer, resolvedConfig) |
| 82 | + onCleanup(off) |
| 83 | + // The plugin's global layer, as skill-ops.tsx registers it at plugin init: the same |
| 84 | + // command name, bound to the same chord, active everywhere. |
| 85 | + if (opts.globalLayer) { |
| 86 | + const offGlobal = keymap.registerLayer({ |
| 87 | + commands: [ |
| 88 | + { |
| 89 | + name: "altimate.skill.actions", |
| 90 | + title: "Skill actions", |
| 91 | + run() { |
| 92 | + triggered.push("global") |
| 93 | + }, |
| 94 | + }, |
| 95 | + ], |
| 96 | + bindings: [{ key: "ctrl+a", cmd: "altimate.skill.actions" }], |
| 97 | + }) |
| 98 | + onCleanup(offGlobal) |
| 99 | + } |
| 100 | + return ( |
| 101 | + <TestTuiContexts> |
| 102 | + <OpencodeKeymapProvider keymap={keymap}> |
| 103 | + <TuiConfigProvider config={resolvedConfig}> |
| 104 | + <ArgsProvider> |
| 105 | + <KVProvider> |
| 106 | + <ToastProvider> |
| 107 | + <ThemeProvider mode="dark"> |
| 108 | + <DialogProvider> |
| 109 | + <Opener /> |
| 110 | + </DialogProvider> |
| 111 | + </ThemeProvider> |
| 112 | + </ToastProvider> |
| 113 | + </KVProvider> |
| 114 | + </ArgsProvider> |
| 115 | + </TuiConfigProvider> |
| 116 | + </OpencodeKeymapProvider> |
| 117 | + </TestTuiContexts> |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + const app = await testRender(() => <Harness />) |
| 122 | + await Bun.sleep(50) |
| 123 | + return { app, triggered } |
| 124 | +} |
| 125 | + |
| 126 | +test("ctrl+a inside an open DialogSelect triggers the declared action for the highlighted row", async () => { |
| 127 | + const { app, triggered } = await mount() |
| 128 | + try { |
| 129 | + app.mockInput.pressKey("a", { ctrl: true }) |
| 130 | + await wait(() => triggered.length > 0) |
| 131 | + expect(triggered).toEqual(["actions:alpha"]) |
| 132 | + } finally { |
| 133 | + app.renderer.destroy() |
| 134 | + } |
| 135 | +}) |
| 136 | + |
| 137 | +test("the action follows the highlight: Down then ctrl+a names the second row", async () => { |
| 138 | + const { app, triggered } = await mount() |
| 139 | + try { |
| 140 | + app.mockInput.pressKey("ARROW_DOWN") |
| 141 | + await Bun.sleep(20) |
| 142 | + app.mockInput.pressKey("a", { ctrl: true }) |
| 143 | + await wait(() => triggered.length > 0) |
| 144 | + expect(triggered).toEqual(["actions:beta"]) |
| 145 | + } finally { |
| 146 | + app.renderer.destroy() |
| 147 | + } |
| 148 | +}) |
| 149 | + |
| 150 | +test("a second action with its own chord fires independently", async () => { |
| 151 | + const { app, triggered } = await mount() |
| 152 | + try { |
| 153 | + app.mockInput.pressKey("e", { ctrl: true }) |
| 154 | + await wait(() => triggered.length > 0) |
| 155 | + expect(triggered).toEqual(["create"]) |
| 156 | + } finally { |
| 157 | + app.renderer.destroy() |
| 158 | + } |
| 159 | +}) |
| 160 | + |
| 161 | +test("without a binding the chord does nothing — the test proves the binding is what carries it", async () => { |
| 162 | + const { app, triggered } = await mount({ bindings: [] }) |
| 163 | + try { |
| 164 | + app.mockInput.pressKey("a", { ctrl: true }) |
| 165 | + await Bun.sleep(150) |
| 166 | + expect(triggered).toEqual([]) |
| 167 | + } finally { |
| 168 | + app.renderer.destroy() |
| 169 | + } |
| 170 | +}) |
| 171 | + |
| 172 | +test("REPRO: with the plugin's global layer registering the same command name, the dialog action is what fires", async () => { |
| 173 | + const { app, triggered } = await mount({ globalLayer: true }) |
| 174 | + try { |
| 175 | + app.mockInput.pressKey("a", { ctrl: true }) |
| 176 | + await Bun.sleep(200) |
| 177 | + expect(triggered).toEqual(["actions:alpha"]) |
| 178 | + } finally { |
| 179 | + app.renderer.destroy() |
| 180 | + } |
| 181 | +}) |
| 182 | + |
| 183 | +test("the actions render as footer buttons with their chords, so the picker is discoverable without one", async () => { |
| 184 | + const { app } = await mount({ globalLayer: true }) |
| 185 | + try { |
| 186 | + await app.renderOnce() |
| 187 | + const frame = app.captureCharFrame() |
| 188 | + expect(frame).toContain("Actions") |
| 189 | + expect(frame).toContain("New") |
| 190 | + expect(frame).toMatch(/ctrl\+a|\^a/i) |
| 191 | + } finally { |
| 192 | + app.renderer.destroy() |
| 193 | + } |
| 194 | +}) |
0 commit comments