Skip to content

Commit 8fa12f7

Browse files
committed
test: keybind + git utilities — parsing, matching, and error handling
Add 27 tests for two untested utility modules that are used across multiple critical code paths (TUI keybindings, git operations in 8+ files). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> https://claude.ai/code/session_014mq5rKy5AhzTbSNH9rhxFF
1 parent abcaa1d commit 8fa12f7

2 files changed

Lines changed: 211 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { describe, test, expect } from "bun:test"
2+
import { tmpdir } from "../fixture/fixture"
3+
import { git } from "../../src/util/git"
4+
5+
describe("git() utility", () => {
6+
test("runs a simple git command and returns stdout", async () => {
7+
// Use tmpdir without git:true to avoid commit signing issues; just git init manually
8+
await using tmp = await tmpdir()
9+
await Bun.spawn(["git", "init"], { cwd: tmp.path, stdout: "ignore", stderr: "ignore" }).exited
10+
await Bun.spawn(["git", "config", "core.fsmonitor", "false"], { cwd: tmp.path, stdout: "ignore", stderr: "ignore" }).exited
11+
12+
const result = await git(["rev-parse", "--is-inside-work-tree"], { cwd: tmp.path })
13+
expect(result.exitCode).toBe(0)
14+
expect(result.text().trim()).toBe("true")
15+
})
16+
17+
test("returns non-zero exit code for unknown git subcommand", async () => {
18+
await using tmp = await tmpdir()
19+
await Bun.spawn(["git", "init"], { cwd: tmp.path, stdout: "ignore", stderr: "ignore" }).exited
20+
21+
const result = await git(["not-a-real-subcommand"], { cwd: tmp.path })
22+
expect(result.exitCode).not.toBe(0)
23+
})
24+
25+
test("stderr is populated on error", async () => {
26+
await using tmp = await tmpdir()
27+
await Bun.spawn(["git", "init"], { cwd: tmp.path, stdout: "ignore", stderr: "ignore" }).exited
28+
29+
const result = await git(["checkout", "nonexistent-branch-xyz"], { cwd: tmp.path })
30+
expect(result.exitCode).not.toBe(0)
31+
expect(result.stderr.length).toBeGreaterThan(0)
32+
})
33+
34+
test("passes custom env vars through to git process", async () => {
35+
await using tmp = await tmpdir()
36+
await Bun.spawn(["git", "init"], { cwd: tmp.path, stdout: "ignore", stderr: "ignore" }).exited
37+
38+
// Use GIT_CONFIG_COUNT to inject a config value that only exists via env
39+
const result = await git(["config", "--get", "test.injected"], {
40+
cwd: tmp.path,
41+
env: {
42+
...process.env,
43+
GIT_CONFIG_COUNT: "1",
44+
GIT_CONFIG_KEY_0: "test.injected",
45+
GIT_CONFIG_VALUE_0: "from-env",
46+
},
47+
})
48+
expect(result.exitCode).toBe(0)
49+
expect(result.text().trim()).toBe("from-env")
50+
})
51+
52+
test("returns exitCode 1 and empty stdout when cwd does not exist", async () => {
53+
const result = await git(["status"], { cwd: "/tmp/nonexistent-dir-" + Math.random().toString(36) })
54+
expect(result.exitCode).not.toBe(0)
55+
expect(result.stdout.length).toBe(0)
56+
})
57+
})
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import { describe, test, expect } from "bun:test"
2+
import { Keybind } from "../../src/util/keybind"
3+
4+
describe("Keybind.parse", () => {
5+
test("returns empty array for 'none'", () => {
6+
expect(Keybind.parse("none")).toEqual([])
7+
})
8+
9+
test("parses simple key", () => {
10+
const [info] = Keybind.parse("a")
11+
expect(info.name).toBe("a")
12+
expect(info.ctrl).toBe(false)
13+
expect(info.meta).toBe(false)
14+
expect(info.shift).toBe(false)
15+
expect(info.leader).toBe(false)
16+
})
17+
18+
test("parses ctrl+key combo", () => {
19+
const [info] = Keybind.parse("ctrl+s")
20+
expect(info.ctrl).toBe(true)
21+
expect(info.name).toBe("s")
22+
})
23+
24+
test("parses multi-modifier combo ctrl+shift+a", () => {
25+
const [info] = Keybind.parse("ctrl+shift+a")
26+
expect(info.ctrl).toBe(true)
27+
expect(info.shift).toBe(true)
28+
expect(info.name).toBe("a")
29+
})
30+
31+
test("recognizes 'alt', 'meta', and 'option' as meta modifier", () => {
32+
for (const alias of ["alt", "meta", "option"]) {
33+
const [info] = Keybind.parse(`${alias}+x`)
34+
expect(info.meta).toBe(true)
35+
expect(info.name).toBe("x")
36+
}
37+
})
38+
39+
test("parses super modifier", () => {
40+
const [info] = Keybind.parse("super+s")
41+
expect(info.super).toBe(true)
42+
expect(info.name).toBe("s")
43+
})
44+
45+
test("parses <leader> prefix", () => {
46+
const [info] = Keybind.parse("<leader>a")
47+
expect(info.leader).toBe(true)
48+
expect(info.name).toBe("a")
49+
})
50+
51+
test("normalizes 'esc' to 'escape'", () => {
52+
const [info] = Keybind.parse("esc")
53+
expect(info.name).toBe("escape")
54+
})
55+
56+
test("parses comma-separated multi-binding", () => {
57+
const bindings = Keybind.parse("ctrl+a,ctrl+b")
58+
expect(bindings).toHaveLength(2)
59+
expect(bindings[0].name).toBe("a")
60+
expect(bindings[0].ctrl).toBe(true)
61+
expect(bindings[1].name).toBe("b")
62+
expect(bindings[1].ctrl).toBe(true)
63+
})
64+
})
65+
66+
describe("Keybind.toString", () => {
67+
test("returns empty string for undefined", () => {
68+
expect(Keybind.toString(undefined)).toBe("")
69+
})
70+
71+
test("formats ctrl+key", () => {
72+
const result = Keybind.toString({
73+
ctrl: true, meta: false, shift: false, super: false, leader: false, name: "s",
74+
})
75+
expect(result).toBe("ctrl+s")
76+
})
77+
78+
test("formats meta as 'alt'", () => {
79+
const result = Keybind.toString({
80+
ctrl: false, meta: true, shift: false, super: false, leader: false, name: "x",
81+
})
82+
expect(result).toBe("alt+x")
83+
})
84+
85+
test("formats super modifier", () => {
86+
const result = Keybind.toString({
87+
ctrl: false, meta: false, shift: false, super: true, leader: false, name: "s",
88+
})
89+
expect(result).toBe("super+s")
90+
})
91+
92+
test("formats leader prefix with key", () => {
93+
const result = Keybind.toString({
94+
ctrl: false, meta: false, shift: false, super: false, leader: true, name: "a",
95+
})
96+
expect(result).toBe("<leader> a")
97+
})
98+
99+
test("formats leader-only (no key)", () => {
100+
const result = Keybind.toString({
101+
ctrl: false, meta: false, shift: false, super: false, leader: true, name: "",
102+
})
103+
expect(result).toBe("<leader>")
104+
})
105+
106+
test("maps 'delete' to 'del'", () => {
107+
const result = Keybind.toString({
108+
ctrl: false, meta: false, shift: false, super: false, leader: false, name: "delete",
109+
})
110+
expect(result).toBe("del")
111+
})
112+
})
113+
114+
describe("Keybind.match", () => {
115+
test("returns false for undefined first argument", () => {
116+
const b: Keybind.Info = { ctrl: true, meta: false, shift: false, super: false, leader: false, name: "s" }
117+
expect(Keybind.match(undefined, b)).toBe(false)
118+
})
119+
120+
test("matches identical bindings", () => {
121+
const a: Keybind.Info = { ctrl: true, meta: false, shift: false, super: false, leader: false, name: "s" }
122+
const b: Keybind.Info = { ctrl: true, meta: false, shift: false, super: false, leader: false, name: "s" }
123+
expect(Keybind.match(a, b)).toBe(true)
124+
})
125+
126+
test("treats missing super as false (normalization)", () => {
127+
// Simulate an Info object where super is undefined (e.g. from older code)
128+
const a = { ctrl: false, meta: false, shift: false, leader: false, name: "a" } as Keybind.Info
129+
const b: Keybind.Info = { ctrl: false, meta: false, shift: false, super: false, leader: false, name: "a" }
130+
expect(Keybind.match(a, b)).toBe(true)
131+
})
132+
133+
test("does not match different keys", () => {
134+
const a: Keybind.Info = { ctrl: true, meta: false, shift: false, super: false, leader: false, name: "s" }
135+
const b: Keybind.Info = { ctrl: true, meta: false, shift: false, super: false, leader: false, name: "x" }
136+
expect(Keybind.match(a, b)).toBe(false)
137+
})
138+
})
139+
140+
describe("Keybind.parse → Keybind.toString roundtrip", () => {
141+
test("roundtrips ctrl+shift+a", () => {
142+
const [parsed] = Keybind.parse("ctrl+shift+a")
143+
expect(Keybind.toString(parsed)).toBe("ctrl+shift+a")
144+
})
145+
146+
test("meta aliases normalize to 'alt' on roundtrip", () => {
147+
// parse("meta+x") sets meta:true, toString emits "alt" — lossy but correct
148+
const [parsed] = Keybind.parse("meta+x")
149+
expect(Keybind.toString(parsed)).toBe("alt+x")
150+
151+
const [parsed2] = Keybind.parse("option+x")
152+
expect(Keybind.toString(parsed2)).toBe("alt+x")
153+
})
154+
})

0 commit comments

Comments
 (0)