Files
opencode/packages/app/test-browser/settings-keybinds.test.ts
+31 302e9b45ab chore: merge dev into v2 (#39290)
Co-authored-by: Luke Parker <[email protected]>
Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>
Co-authored-by: Aarav Sareen <[email protected]>
Co-authored-by: Brendan Allan <[email protected]>
Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Co-authored-by: Aiden Cline <[email protected]>
Co-authored-by: opencode <[email protected]>
Co-authored-by: Jay <[email protected]>
Co-authored-by: David Hill <[email protected]>
Co-authored-by: BB84 <[email protected]>
Co-authored-by: Aiden Cline <[email protected]>
Co-authored-by: Dax <[email protected]>
Co-authored-by: Brendan Allan <[email protected]>
Co-authored-by: Jay V <[email protected]>
Co-authored-by: Adam <[email protected]>
Co-authored-by: Aiden Cline <[email protected]>
Co-authored-by: Dustin Deus <[email protected]>
Co-authored-by: Frank <[email protected]>
Co-authored-by: usrnk1 <[email protected]>
Co-authored-by: Jack <[email protected]>
Co-authored-by: Sebastian <[email protected]>
Co-authored-by: Jérôme Benoit <[email protected]>
Co-authored-by: Test User <[email protected]>
Co-authored-by: Simon Klee <[email protected]>
Co-authored-by: Rahul A Mistry <[email protected]>
Co-authored-by: Qiping Li <[email protected]>
Co-authored-by: liqiping <[email protected]>
Co-authored-by: OpeOginni <[email protected]>
Co-authored-by: Matthias Reso <[email protected]>
Co-authored-by: tobwen <[email protected]>
Co-authored-by: Daniel Polito <[email protected]>
Co-authored-by: opencode <[email protected]>
Co-authored-by: Devin R Leopold <[email protected]>
Co-authored-by: Zach Bruggeman <[email protected]>
Co-authored-by: Zach Bruggeman <[email protected]>
Co-authored-by: Kit Langton <[email protected]>
Co-authored-by: David Siewert <[email protected]>
Co-authored-by: Andrei Dziahel <[email protected]>
Co-authored-by: adityachaudhary99 <[email protected]>
Co-authored-by: Vladimir Glafirov <[email protected]>
Co-authored-by: Matt Carey <[email protected]>
2026-07-28 18:36:55 +10:00

108 lines
3.4 KiB
TypeScript

import { describe, expect, test } from "bun:test"
import { createRoot } from "solid-js"
import { createKeybindSettingsController } from "../src/components/settings-keybinds"
function setup(overrides: Record<string, string> = {}) {
const changes: [string, string][] = []
const suppression: boolean[] = []
const notifications: { title: string; description?: string }[] = []
let resets = 0
let controller: ReturnType<typeof createKeybindSettingsController>
const dispose = createRoot((dispose) => {
controller = createKeybindSettingsController(
{
command: {
catalog: [
{ id: "session.alpha", title: "Alpha", keybind: "mod+a" },
{ id: "session.beta", title: "Beta", keybind: "mod+b" },
],
options: [],
keybinds: (enabled) => suppression.push(enabled),
},
settings: {
current: { keybinds: overrides },
keybinds: {
get: (id) => overrides[id],
set: (id, value) => {
overrides[id] = value
changes.push([id, value])
},
resetAll: () => {
resets++
},
},
},
notify: (toast) => notifications.push(toast),
},
{
locale: () => "en",
t: (key, params) => {
if (params) return `${key}:${Object.values(params).join("|")}`
if (key === "common.key.alt") return "Alt"
return String(key)
},
},
)
return dispose
})
return {
controller: controller!,
changes,
suppression,
notifications,
resets: () => resets,
dispose,
}
}
function modKey(key: string) {
const mac = /(Mac|iPod|iPhone|iPad)/.test(navigator.platform)
return new KeyboardEvent("keydown", { key, ctrlKey: !mac, metaKey: mac, bubbles: true, cancelable: true })
}
describe("keybind settings controller", () => {
test("derives the catalog, effective bindings, and filtered groups", () => {
const state = setup({ "session.beta": "alt+k" })
expect(state.controller.catalog.title("session.alpha")).toBe("Alpha")
expect(state.controller.catalog.keybind("session.beta")).toBe("Alt+K")
expect(state.controller.catalog.filtered("alt k").get("Session")).toEqual(["session.beta"])
expect(state.controller.settings.hasOverrides()).toBe(true)
state.dispose()
})
test("captures bindings, rejects conflicts, and restores command handling", () => {
const state = setup()
state.controller.capture.toggle("session.beta")
document.dispatchEvent(modKey("a"))
expect(state.changes).toEqual([])
expect(state.notifications).toHaveLength(1)
expect(state.controller.capture.active()).toBe("session.beta")
document.dispatchEvent(modKey("x"))
expect(state.changes).toEqual([["session.beta", "mod+x"]])
expect(state.suppression).toEqual([false, true])
expect(state.controller.capture.active()).toBeNull()
state.controller.capture.toggle("session.alpha")
state.dispose()
expect(state.suppression).toEqual([false, true, false, true])
document.dispatchEvent(modKey("z"))
expect(state.changes).toEqual([["session.beta", "mod+x"]])
})
test("resets persisted overrides and reports success", () => {
const state = setup({ "session.alpha": "none" })
state.controller.settings.reset()
expect(state.resets()).toBe(1)
expect(state.notifications[0]?.title).toBe("settings.shortcuts.reset.toast.title")
state.dispose()
})
})