@@ -0,0 +1,119 @@
1+import { describe, expect, test } from "bun:test";
2+import { parseInput } from "./input.ts";
3+
4+describe("parseInput", () => {
5+ test("single printable character", () => {
6+ expect(parseInput("a")).toEqual([{ type: "char", char: "a" }]);
7+ });
8+
9+ test("multiple printable characters", () => {
10+ expect(parseInput("abc")).toEqual([
11+ { type: "char", char: "a" },
12+ { type: "char", char: "b" },
13+ { type: "char", char: "c" },
14+ ]);
15+ });
16+
17+ test("space character", () => {
18+ expect(parseInput(" ")).toEqual([{ type: "char", char: " " }]);
19+ });
20+
21+ test("enter (CR)", () => {
22+ expect(parseInput("\r")).toEqual([{ type: "enter" }]);
23+ });
24+
25+ test("enter (LF)", () => {
26+ expect(parseInput("\n")).toEqual([{ type: "enter" }]);
27+ });
28+
29+ test("backspace (DEL 0x7f)", () => {
30+ expect(parseInput("\x7f")).toEqual([{ type: "backspace" }]);
31+ });
32+
33+ test("backspace (BS 0x08)", () => {
34+ expect(parseInput("\x08")).toEqual([{ type: "backspace" }]);
35+ });
36+
37+ test("bare ESC", () => {
38+ expect(parseInput("\x1b")).toEqual([{ type: "escape" }]);
39+ });
40+
41+ test("arrow up", () => {
42+ expect(parseInput("\x1b[A")).toEqual([{ type: "up" }]);
43+ });
44+
45+ test("arrow down", () => {
46+ expect(parseInput("\x1b[B")).toEqual([{ type: "down" }]);
47+ });
48+
49+ test("home key", () => {
50+ expect(parseInput("\x1b[H")).toEqual([{ type: "home" }]);
51+ });
52+
53+ test("home key (alternate)", () => {
54+ expect(parseInput("\x1b[1~")).toEqual([{ type: "home" }]);
55+ });
56+
57+ test("end key", () => {
58+ expect(parseInput("\x1b[F")).toEqual([{ type: "end" }]);
59+ });
60+
61+ test("end key (alternate)", () => {
62+ expect(parseInput("\x1b[4~")).toEqual([{ type: "end" }]);
63+ });
64+
65+ // THE BUG: mixed sequences in a single data event
66+ test("arrow down followed by printable char", () => {
67+ expect(parseInput("\x1b[Bv")).toEqual([
68+ { type: "down" },
69+ { type: "char", char: "v" },
70+ ]);
71+ });
72+
73+ test("arrow up followed by multiple chars", () => {
74+ expect(parseInput("\x1b[Aabc")).toEqual([
75+ { type: "up" },
76+ { type: "char", char: "a" },
77+ { type: "char", char: "b" },
78+ { type: "char", char: "c" },
79+ ]);
80+ });
81+
82+ test("two arrow keys in sequence", () => {
83+ expect(parseInput("\x1b[B\x1b[B")).toEqual([
84+ { type: "down" },
85+ { type: "down" },
86+ ]);
87+ });
88+
89+ test("char then arrow then char", () => {
90+ expect(parseInput("x\x1b[Ay")).toEqual([
91+ { type: "char", char: "x" },
92+ { type: "up" },
93+ { type: "char", char: "y" },
94+ ]);
95+ });
96+
97+ test("arrow followed by enter", () => {
98+ expect(parseInput("\x1b[B\r")).toEqual([
99+ { type: "down" },
100+ { type: "enter" },
101+ ]);
102+ });
103+
104+ test("ESC followed by non-bracket is ESC + char", () => {
105+ expect(parseInput("\x1ba")).toEqual([
106+ { type: "escape" },
107+ { type: "char", char: "a" },
108+ ]);
109+ });
110+
111+ test("unknown escape sequence is skipped", () => {
112+ // \x1b[C is arrow right — not handled, should be skipped
113+ expect(parseInput("\x1b[Ca")).toEqual([{ type: "char", char: "a" }]);
114+ });
115+
116+ test("empty string returns empty array", () => {
117+ expect(parseInput("")).toEqual([]);
118+ });
119+});