@@ -4,6 +4,14 @@ import { MockTerminal } from "./terminal.js";
44 import { Text } from "./primitives/text.js";
55 import { TextInput } from "./primitives/text-input.js";
66 import { VStack, HStack } from "./primitives/stacks.js";
7+import { kittyEncode } from "./test-helpers.js";
8+
9+// Kitty protocol byte sequences for commonly used keys
10+const TAB = kittyEncode("tab");
11+const ENTER = kittyEncode("enter");
12+const ESCAPE = kittyEncode("escape");
13+const SHIFT_TAB = kittyEncode("shift+tab");
14+const CTRL_S = kittyEncode("ctrl+s");
715
816 describe("cel end-to-end", () => {
917 let term: MockTerminal;
@@ -172,7 +180,7 @@ describe("cel end-to-end", () => {
172180 await waitForRender();
173181
174182 // Send Ctrl+S
175- term.sendInput("\x13");
183+ term.sendInput(CTRL_S);
176184 await waitForRender();
177185
178186 expect(receivedKey).toBe("ctrl+s");
@@ -314,12 +322,12 @@ describe("cel end-to-end", () => {
314322 await waitForRender();
315323
316324 // Tab to focus first element
317- term.sendInput("\t");
325+ term.sendInput(TAB);
318326 await waitForRender();
319327 expect(focused).toEqual(["btn1"]);
320328
321329 // Tab to focus second element
322- term.sendInput("\t");
330+ term.sendInput(TAB);
323331 await waitForRender();
324332 expect(focused).toEqual(["btn1", "btn2"]);
325333 });
@@ -367,7 +375,7 @@ describe("cel end-to-end", () => {
367375 await waitForRender();
368376
369377 // Shift+Tab from btn2 should focus btn1
370- term.sendInput("\x1b[Z");
378+ term.sendInput(SHIFT_TAB);
371379 await waitForRender();
372380 expect(focused).toEqual(["btn1"]);
373381 });
@@ -394,7 +402,7 @@ describe("cel end-to-end", () => {
394402 );
395403 await waitForRender();
396404
397- term.sendInput("\x1b");
405+ term.sendInput(ESCAPE);
398406 await waitForRender();
399407 expect(blurred).toBe(true);
400408 });
@@ -418,7 +426,7 @@ describe("cel end-to-end", () => {
418426 );
419427 await waitForRender();
420428
421- term.sendInput("\r");
429+ term.sendInput(ENTER);
422430 await waitForRender();
423431 expect(clicked).toBe(true);
424432 });
@@ -492,7 +500,7 @@ describe("cel end-to-end", () => {
492500 await waitForRender();
493501
494502 // Tab from btn2 (last) should wrap to btn1
495- term.sendInput("\t");
503+ term.sendInput(TAB);
496504 await waitForRender();
497505 expect(focused).toEqual(["btn1"]);
498506 });
@@ -735,7 +743,7 @@ describe("cel end-to-end", () => {
735743 await waitForRender();
736744
737745 // Ctrl+S is not an editing key — should bubble up
738- term.sendInput("\x13");
746+ term.sendInput(CTRL_S);
739747 await waitForRender();
740748 expect(parentKey).toBe("ctrl+s");
741749 });
@@ -782,7 +790,7 @@ describe("cel end-to-end", () => {
782790 await waitForRender();
783791
784792 // Tab should insert \t, not traverse focus
785- term.sendInput("\t");
793+ term.sendInput(TAB);
786794 await waitForRender();
787795 expect(value).toBe("hello\t");
788796 });
@@ -816,7 +824,7 @@ describe("cel end-to-end", () => {
816824 await waitForRender();
817825
818826 // Shift+Tab should not move focus away from TextInput
819- term.sendInput("\x1b[Z");
827+ term.sendInput(SHIFT_TAB);
820828 await waitForRender();
821829 expect(btnFocused).toBe(false);
822830 });
@@ -840,7 +848,7 @@ describe("cel end-to-end", () => {
840848 await waitForRender();
841849
842850 // Tab should traverse to the TextInput (it's not focused yet)
843- term.sendInput("\t");
851+ term.sendInput(TAB);
844852 await waitForRender();
845853 expect(tiFocused).toBe(true);
846854 });
@@ -876,12 +884,12 @@ describe("cel end-to-end", () => {
876884 await waitForRender();
877885
878886 // Escape to leave TextInput
879- term.sendInput("\x1b");
887+ term.sendInput(ESCAPE);
880888 await waitForRender();
881889 expect(tiFocused).toBe(false);
882890
883891 // Tab should now traverse to the button
884- term.sendInput("\t");
892+ term.sendInput(TAB);
885893 await waitForRender();
886894 expect(btnFocused).toBe(true);
887895 });
@@ -922,13 +930,13 @@ describe("cel end-to-end", () => {
922930 expect(focused as string | null).toBe("ti");
923931
924932 // Escape to unfocus TextInput
925- term.sendInput("\x1b");
933+ term.sendInput(ESCAPE);
926934 await waitForRender();
927935 expect(focused as string | null).toBe(null);
928936
929937 // Tab should go FORWARD to the button (next after TextInput),
930938 // not back to the TextInput
931- term.sendInput("\t");
939+ term.sendInput(TAB);
932940 await waitForRender();
933941 expect(focused as string | null).toBe("btn");
934942 });
@@ -983,18 +991,18 @@ describe("cel end-to-end", () => {
983991 await waitForRender();
984992
985993 // Tab to B
986- term.sendInput("\t"); // → A
994+ term.sendInput(TAB); // → A
987995 await waitForRender();
988- term.sendInput("\t"); // → B
996+ term.sendInput(TAB); // → B
989997 await waitForRender();
990998 expect(focused as string | null).toBe("b");
991999
9921000 // Escape, then Shift+Tab should go backward to A
993- term.sendInput("\x1b");
1001+ term.sendInput(ESCAPE);
9941002 await waitForRender();
9951003 expect(focused as string | null).toBe(null);
9961004
997- term.sendInput("\x1b[Z"); // Shift+Tab
1005+ term.sendInput(SHIFT_TAB); // Shift+Tab
9981006 await waitForRender();
9991007 expect(focused as string | null).toBe("a");
10001008 });
@@ -1130,11 +1138,11 @@ describe("cel end-to-end", () => {
11301138 await waitForRender();
11311139
11321140 // Tab to focus the button
1133- term.sendInput("\t");
1141+ term.sendInput(TAB);
11341142 await waitForRender();
11351143
11361144 // Enter to activate
1137- term.sendInput("\r");
1145+ term.sendInput(ENTER);
11381146 await waitForRender();
11391147
11401148 expect(clicked).toBe(true);
@@ -1153,16 +1161,16 @@ describe("cel end-to-end", () => {
11531161 await waitForRender();
11541162
11551163 // Tab to first, activate
1156- term.sendInput("\t");
1164+ term.sendInput(TAB);
11571165 await waitForRender();
1158- term.sendInput("\r");
1166+ term.sendInput(ENTER);
11591167 await waitForRender();
11601168 expect(clicks).toEqual(["btn1"]);
11611169
11621170 // Tab to second, activate
1163- term.sendInput("\t");
1171+ term.sendInput(TAB);
11641172 await waitForRender();
1165- term.sendInput("\r");
1173+ term.sendInput(ENTER);
11661174 await waitForRender();
11671175 expect(clicks).toEqual(["btn1", "btn2"]);
11681176 });
@@ -1180,16 +1188,16 @@ describe("cel end-to-end", () => {
11801188 await waitForRender();
11811189
11821190 // Shift+Tab to focus last element
1183- term.sendInput("\x1b[Z");
1191+ term.sendInput(SHIFT_TAB);
11841192 await waitForRender();
1185- term.sendInput("\r");
1193+ term.sendInput(ENTER);
11861194 await waitForRender();
11871195 expect(clicks).toEqual(["btn2"]);
11881196
11891197 // Shift+Tab to go backward to first
1190- term.sendInput("\x1b[Z");
1198+ term.sendInput(SHIFT_TAB);
11911199 await waitForRender();
1192- term.sendInput("\r");
1200+ term.sendInput(ENTER);
11931201 await waitForRender();
11941202 expect(clicks).toEqual(["btn2", "btn1"]);
11951203 });
@@ -1213,13 +1221,13 @@ describe("cel end-to-end", () => {
12131221 await waitForRender();
12141222
12151223 // Tab to focus, then Escape to unfocus
1216- term.sendInput("\t");
1224+ term.sendInput(TAB);
12171225 await waitForRender();
1218- term.sendInput("\x1b");
1226+ term.sendInput(ESCAPE);
12191227 await waitForRender();
12201228
12211229 // Enter should do nothing — no element is focused
1222- term.sendInput("\r");
1230+ term.sendInput(ENTER);
12231231 await waitForRender();
12241232 expect(clicked).toBe(false);
12251233 });
@@ -1248,7 +1256,7 @@ describe("cel end-to-end", () => {
12481256 await waitForRender();
12491257
12501258 // Enter should activate the clicked element
1251- term.sendInput("\r");
1259+ term.sendInput(ENTER);
12521260 await waitForRender();
12531261 expect(clicked).toBe(true);
12541262 });
@@ -1270,7 +1278,7 @@ describe("cel end-to-end", () => {
12701278 );
12711279 await waitForRender();
12721280
1273- term.sendInput("\t");
1281+ term.sendInput(TAB);
12741282 await waitForRender();
12751283 expect(events).toEqual(["focus"]);
12761284 });
@@ -1301,12 +1309,12 @@ describe("cel end-to-end", () => {
13011309 await waitForRender();
13021310
13031311 // Tab to first
1304- term.sendInput("\t");
1312+ term.sendInput(TAB);
13051313 await waitForRender();
13061314 expect(events).toEqual(["focus1"]);
13071315
13081316 // Tab to second — first should get onBlur
1309- term.sendInput("\t");
1317+ term.sendInput(TAB);
13101318 await waitForRender();
13111319 expect(events).toEqual(["focus1", "blur1", "focus2"]);
13121320 });
@@ -1328,7 +1336,7 @@ describe("cel end-to-end", () => {
13281336 await waitForRender();
13291337
13301338 // Tab to focus the TextInput
1331- term.sendInput("\t");
1339+ term.sendInput(TAB);
13321340 await waitForRender();
13331341
13341342 // Type a character
@@ -1350,13 +1358,13 @@ describe("cel end-to-end", () => {
13501358 await waitForRender();
13511359
13521360 // Tab to first, Tab to second, Tab wraps to first
1353- term.sendInput("\t");
1361+ term.sendInput(TAB);
13541362 await waitForRender();
1355- term.sendInput("\t");
1363+ term.sendInput(TAB);
13561364 await waitForRender();
1357- term.sendInput("\t");
1365+ term.sendInput(TAB);
13581366 await waitForRender();
1359- term.sendInput("\r");
1367+ term.sendInput(ENTER);
13601368 await waitForRender();
13611369 expect(clicks).toEqual(["btn1"]);
13621370 });
@@ -1389,15 +1397,15 @@ describe("cel end-to-end", () => {
13891397 await waitForRender();
13901398
13911399 // Tab to first (controlled)
1392- term.sendInput("\t");
1400+ term.sendInput(TAB);
13931401 await waitForRender();
13941402 expect(controlledFocused).toBe(true);
13951403
13961404 // Tab to second (uncontrolled), Enter to activate
1397- term.sendInput("\t");
1405+ term.sendInput(TAB);
13981406 await waitForRender();
13991407 expect(controlledFocused).toBe(false);
1400- term.sendInput("\r");
1408+ term.sendInput(ENTER);
14011409 await waitForRender();
14021410 expect(clicks).toEqual(["uncontrolled"]);
14031411 });
@@ -1426,7 +1434,7 @@ describe("cel end-to-end", () => {
14261434 expect(buf.get(0, 0).bgColor).toBe("black");
14271435
14281436 // Tab to focus
1429- term.sendInput("\t");
1437+ term.sendInput(TAB);
14301438 await waitForRender();
14311439
14321440 // After focus — focusStyle bgColor
@@ -1460,14 +1468,14 @@ describe("cel end-to-end", () => {
14601468 await waitForRender();
14611469
14621470 // Tab to first
1463- term.sendInput("\t");
1471+ term.sendInput(TAB);
14641472 await waitForRender();
14651473 let buf = cel._getBuffer()!;
14661474 expect(buf.get(0, 0).bgColor).toBe("cyan");
14671475 expect(buf.get(0, 1).bgColor).toBe("black");
14681476
14691477 // Tab to second — first loses focusStyle, second gains it
1470- term.sendInput("\t");
1478+ term.sendInput(TAB);
14711479 await waitForRender();
14721480 buf = cel._getBuffer()!;
14731481 expect(buf.get(0, 0).bgColor).toBe("black");
@@ -1496,7 +1504,7 @@ describe("cel end-to-end", () => {
14961504 expect(buf.get(0, 0).fgColor).toBe("white");
14971505
14981506 // Tab to focus
1499- term.sendInput("\t");
1507+ term.sendInput(TAB);
15001508 await waitForRender();
15011509
15021510 // After focus — Text inherits black from focusStyle
@@ -1531,7 +1539,7 @@ describe("cel end-to-end", () => {
15311539 expect(buf.get(0, 0).bgColor).toBe("cyan");
15321540
15331541 // Escape to blur
1534- term.sendInput("\x1b");
1542+ term.sendInput(ESCAPE);
15351543 await waitForRender();
15361544
15371545 // focused=false — normal style