forked from openai/codex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext-buffer-word.test.ts
More file actions
137 lines (101 loc) · 4.16 KB
/
text-buffer-word.test.ts
File metadata and controls
137 lines (101 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import TextBuffer from "../src/text-buffer.js";
import { describe, test, expect } from "vitest";
describe("TextBuffer – word‑wise navigation & deletion", () => {
test("wordRight moves to end‑of‑line when no further boundary", () => {
const tb = new TextBuffer("hello");
// Move the caret inside the word (index 3)
tb.move("right");
tb.move("right");
tb.move("right");
tb.move("wordRight");
const [, col] = tb.getCursor();
expect(col).toBe(5); // end of the word / line
});
test("Ctrl+Backspace on raw byte deletes previous word", () => {
const tb = new TextBuffer("hello world");
const vp = { height: 10, width: 80 } as const;
// Place caret at end
tb.move("end");
// Simulate terminal sending DEL (0x7f) byte with ctrl modifier – Ink
// usually does *not* set `key.backspace` in this path.
tb.handleInput("\x7f", { ctrl: true }, vp);
expect(tb.getText()).toBe("hello ");
});
test("Option/Alt+Backspace deletes previous word", () => {
const tb = new TextBuffer("foo bar baz");
const vp = { height: 10, width: 80 } as const;
// caret at end
tb.move("end");
// Simulate Option+Backspace (alt): Ink sets key.backspace = true, key.alt = true (no raw byte)
tb.handleInput(undefined, { backspace: true, alt: true }, vp);
expect(tb.getText()).toBe("foo bar ");
});
test("Option/Alt+Delete deletes previous word (matches shells)", () => {
const tb = new TextBuffer("foo bar baz");
const vp = { height: 10, width: 80 } as const;
// Place caret at end so we can test backward deletion.
tb.move("end");
// Simulate Option+Delete (parsed as alt-modified Delete on some terminals)
tb.handleInput(undefined, { delete: true, alt: true }, vp);
expect(tb.getText()).toBe("foo bar ");
});
test("wordLeft eventually reaches column 0", () => {
const tb = new TextBuffer("hello world");
// Move to end of line first
tb.move("end");
// two wordLefts should land at start of line
tb.move("wordLeft");
tb.move("wordLeft");
const [, col] = tb.getCursor();
expect(col).toBe(0);
});
test("wordRight jumps over a delimiter into the next word", () => {
const tb = new TextBuffer("hello world");
tb.move("wordRight"); // from start – should land after "hello" (between space & w)
let [, col] = tb.getCursor();
expect(col).toBe(5);
// Next wordRight should move to end of line (after "world")
tb.move("wordRight");
[, col] = tb.getCursor();
expect(col).toBe(11);
});
test("deleteWordLeft after trailing space only deletes the last word, not the whole line", () => {
const tb = new TextBuffer("I want you to refactor my view ");
tb.move("end"); // Place caret after the space
tb.deleteWordLeft();
expect(tb.getText()).toBe("I want you to refactor my ");
const [, col] = tb.getCursor();
expect(col).toBe("I want you to refactor my ".length);
});
test("deleteWordLeft removes the previous word and positions the caret correctly", () => {
const tb = new TextBuffer("hello world");
// Place caret at end of line
tb.move("end");
// Act
tb.deleteWordLeft();
expect(tb.getText()).toBe("hello ");
const [, col] = tb.getCursor();
expect(col).toBe(6); // after the space
});
test("deleteWordRight removes the following word", () => {
const tb = new TextBuffer("hello world");
// Move caret to start of "world"
tb.move("wordRight"); // caret after "hello"
tb.move("right"); // skip the space, now at index 6 (start of world)
// Act
tb.deleteWordRight();
expect(tb.getText()).toBe("hello ");
const [, col] = tb.getCursor();
expect(col).toBe(6);
});
test("Shift+Option/Alt+Delete deletes next word", () => {
const tb = new TextBuffer("foo bar baz");
const vp = { height: 10, width: 80 } as const;
// Move caret between first and second word (after space)
tb.move("wordRight"); // after foo
tb.move("right"); // skip space -> start of bar
// Shift+Option+Delete should now remove "bar "
tb.handleInput(undefined, { delete: true, alt: true, shift: true }, vp);
expect(tb.getText()).toBe("foo baz");
});
});