@@ -0,0 +1,134 @@
1+import { describe, expect, test } from "bun:test";
2+import {
3+ insertChar,
4+ deleteBackward,
5+ deleteForward,
6+ moveCursor,
7+ type EditState,
8+} from "./text-edit.js";
9+
10+function state(value: string, cursor: number): EditState {
11+ return { value, cursor };
12+}
13+
14+describe("text editing", () => {
15+ describe("insertChar", () => {
16+ test("inserts at start", () => {
17+ const result = insertChar(state("hello", 0), "X");
18+ expect(result.value).toBe("Xhello");
19+ expect(result.cursor).toBe(1);
20+ });
21+
22+ test("inserts at end", () => {
23+ const result = insertChar(state("hello", 5), "!");
24+ expect(result.value).toBe("hello!");
25+ expect(result.cursor).toBe(6);
26+ });
27+
28+ test("inserts in middle", () => {
29+ const result = insertChar(state("helo", 3), "l");
30+ expect(result.value).toBe("hello");
31+ expect(result.cursor).toBe(4);
32+ });
33+
34+ test("inserts newline", () => {
35+ const result = insertChar(state("ab", 1), "\n");
36+ expect(result.value).toBe("a\nb");
37+ expect(result.cursor).toBe(2);
38+ });
39+
40+ test("inserts tab", () => {
41+ const result = insertChar(state("ab", 1), "\t");
42+ expect(result.value).toBe("a\tb");
43+ expect(result.cursor).toBe(2);
44+ });
45+ });
46+
47+ describe("deleteBackward", () => {
48+ test("deletes character before cursor", () => {
49+ const result = deleteBackward(state("hello", 3));
50+ expect(result.value).toBe("helo");
51+ expect(result.cursor).toBe(2);
52+ });
53+
54+ test("no-op at start of string", () => {
55+ const result = deleteBackward(state("hello", 0));
56+ expect(result.value).toBe("hello");
57+ expect(result.cursor).toBe(0);
58+ });
59+
60+ test("deletes last character", () => {
61+ const result = deleteBackward(state("a", 1));
62+ expect(result.value).toBe("");
63+ expect(result.cursor).toBe(0);
64+ });
65+ });
66+
67+ describe("deleteForward", () => {
68+ test("deletes character after cursor", () => {
69+ const result = deleteForward(state("hello", 2));
70+ expect(result.value).toBe("helo");
71+ expect(result.cursor).toBe(2);
72+ });
73+
74+ test("no-op at end of string", () => {
75+ const result = deleteForward(state("hello", 5));
76+ expect(result.value).toBe("hello");
77+ expect(result.cursor).toBe(5);
78+ });
79+ });
80+
81+ describe("moveCursor", () => {
82+ test("left moves back one", () => {
83+ const result = moveCursor(state("hello", 3), "left");
84+ expect(result.cursor).toBe(2);
85+ });
86+
87+ test("left at start stays at 0", () => {
88+ const result = moveCursor(state("hello", 0), "left");
89+ expect(result.cursor).toBe(0);
90+ });
91+
92+ test("right moves forward one", () => {
93+ const result = moveCursor(state("hello", 3), "right");
94+ expect(result.cursor).toBe(4);
95+ });
96+
97+ test("right at end stays at end", () => {
98+ const result = moveCursor(state("hello", 5), "right");
99+ expect(result.cursor).toBe(5);
100+ });
101+
102+ test("home moves to start", () => {
103+ const result = moveCursor(state("hello", 3), "home");
104+ expect(result.cursor).toBe(0);
105+ });
106+
107+ test("end moves to end", () => {
108+ const result = moveCursor(state("hello", 2), "end");
109+ expect(result.cursor).toBe(5);
110+ });
111+
112+ test("up moves to previous line", () => {
113+ const result = moveCursor(state("abc\ndef", 5), "up", 10);
114+ // Cursor at "e" (offset 5), up should go to offset 1 ("b")
115+ expect(result.cursor).toBe(1);
116+ });
117+
118+ test("down moves to next line", () => {
119+ const result = moveCursor(state("abc\ndef", 1), "down", 10);
120+ // Cursor at "b" (offset 1), down should go to offset 5 ("e")
121+ expect(result.cursor).toBe(5);
122+ });
123+
124+ test("up at first line stays at start", () => {
125+ const result = moveCursor(state("hello", 3), "up", 10);
126+ expect(result.cursor).toBe(0);
127+ });
128+
129+ test("down at last line stays at end", () => {
130+ const result = moveCursor(state("hello", 3), "down", 10);
131+ expect(result.cursor).toBe(5);
132+ });
133+ });
134+});