-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathpromptBuffer.test.ts
More file actions
131 lines (113 loc) · 4.1 KB
/
promptBuffer.test.ts
File metadata and controls
131 lines (113 loc) · 4.1 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
import { test } from "node:test";
import assert from "node:assert/strict";
import {
EMPTY_BUFFER,
backspace,
deleteForward,
deleteWordBefore,
deleteWordAfter,
getCurrentSlashToken,
insertText,
killLine,
moveDown,
moveLeft,
moveLineEnd,
moveLineStart,
moveRight,
moveWordLeft,
moveWordRight,
moveUp,
} from "../ui";
test("insertText appends text and advances the cursor", () => {
const next = insertText(EMPTY_BUFFER, "hello");
assert.equal(next.text, "hello");
assert.equal(next.cursor, 5);
});
test("backspace removes the character before the cursor", () => {
const a = insertText(EMPTY_BUFFER, "abc");
const b = backspace(a);
assert.equal(b.text, "ab");
assert.equal(b.cursor, 2);
});
test("backspace at start is a no-op", () => {
const result = backspace({ text: "hi", cursor: 0 });
assert.equal(result.text, "hi");
assert.equal(result.cursor, 0);
});
test("deleteForward removes the character after the cursor", () => {
const result = deleteForward({ text: "hello", cursor: 1 });
assert.equal(result.text, "hllo");
assert.equal(result.cursor, 1);
});
test("moveLeft and moveRight clamp at boundaries", () => {
const left = moveLeft({ text: "hi", cursor: 0 });
assert.equal(left.cursor, 0);
const right = moveRight({ text: "hi", cursor: 2 });
assert.equal(right.cursor, 2);
});
test("word movement skips whitespace and preserves buffer text", () => {
const buffer = { text: "hello brave world", cursor: 18 };
assert.deepEqual(moveWordLeft(buffer), { text: buffer.text, cursor: 13 });
assert.deepEqual(moveWordRight({ text: buffer.text, cursor: 5 }), { text: buffer.text, cursor: 12 });
});
test("moveUp navigates to the previous line preserving column", () => {
const buffer = { text: "hello\nworld", cursor: 9 };
const result = moveUp(buffer);
assert.equal(result.cursor, 3);
});
test("moveUp from first line moves to start of buffer", () => {
const buffer = { text: "hello", cursor: 3 };
const result = moveUp(buffer);
assert.equal(result.cursor, 0);
});
test("moveDown moves to next line preserving column", () => {
const buffer = { text: "hello\nworld", cursor: 3 };
const result = moveDown(buffer);
assert.equal(result.cursor, 9);
});
test("moveLineStart and moveLineEnd respect line boundaries", () => {
const buffer = { text: "first\nsecond line", cursor: 9 };
const start = moveLineStart(buffer);
assert.equal(start.cursor, 6);
const end = moveLineEnd(buffer);
assert.equal(end.cursor, "first\nsecond line".length);
});
test("killLine removes from the cursor to end of line only", () => {
const buffer = { text: "abc\nxyz", cursor: 1 };
const result = killLine(buffer);
assert.equal(result.text, "a\nxyz");
});
test("deleteWordBefore removes the previous word and any adjacent whitespace", () => {
const result = deleteWordBefore({ text: "ask the model", cursor: 8 });
assert.equal(result.text, "ask model");
assert.equal(result.cursor, 4);
});
test("deleteWordAfter removes the next word and leading whitespace", () => {
const result = deleteWordAfter({ text: "ask the model now", cursor: 3 });
assert.equal(result.text, "ask model now");
assert.equal(result.cursor, 3);
});
test("getCurrentSlashToken returns the slash word at the cursor", () => {
const buffer = { text: "/skill", cursor: 6 };
assert.equal(getCurrentSlashToken(buffer), "/skill");
});
test("getCurrentSlashToken returns null when token contains whitespace", () => {
const buffer = { text: "/skill foo", cursor: 10 };
assert.equal(getCurrentSlashToken(buffer), null);
});
test("getCurrentSlashToken supports slash on a new line", () => {
const buffer = { text: "do this\n/n", cursor: 10 };
assert.equal(getCurrentSlashToken(buffer), "/n");
});
test("getCurrentSlashToken returns null when no slash prefix", () => {
const buffer = { text: "hello", cursor: 5 };
assert.equal(getCurrentSlashToken(buffer), null);
});
test("inserting newlines builds a multi-line buffer", () => {
let buf = EMPTY_BUFFER;
buf = insertText(buf, "abc");
buf = insertText(buf, "\n");
buf = insertText(buf, "def");
assert.equal(buf.text, "abc\ndef");
assert.equal(buf.cursor, 7);
});