From 41b44cad24a3f8af5b43bb89b39f9d670f564143 Mon Sep 17 00:00:00 2001 From: myu003 Date: Wed, 15 Apr 2026 21:21:34 +0800 Subject: [PATCH] test(tui): isolate Windows Terminal backspace env coverage (#3220) --- packages/tui/test/keys.test.ts | 53 +++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/packages/tui/test/keys.test.ts b/packages/tui/test/keys.test.ts index d0ba9b16..e1c204fd 100644 --- a/packages/tui/test/keys.test.ts +++ b/packages/tui/test/keys.test.ts @@ -18,6 +18,19 @@ function withEnv(name: string, value: string | undefined, fn: () => void): void } } +function withEnvVars(vars: Record, fn: () => void): void { + const entries = Object.entries(vars); + const run = (index: number): void => { + if (index >= entries.length) { + fn(); + return; + } + const [name, value] = entries[index]!; + withEnv(name, value, () => run(index + 1)); + }; + run(0); +} + describe("matchesKey", () => { describe("Kitty protocol with alternate keys (non-Latin layouts)", () => { // Kitty protocol flag 4 (Report alternate keys) sends: @@ -351,14 +364,40 @@ describe("matchesKey", () => { }); }); - it("should treat raw 0x08 as ctrl+backspace in Windows Terminal", () => { + it("should treat raw 0x08 as ctrl+backspace in local Windows Terminal", () => { setKittyProtocolActive(false); - withEnv("WT_SESSION", "test-session", () => { - assert.strictEqual(matchesKey("\x08", "ctrl+backspace"), true); - assert.strictEqual(matchesKey("\x08", "backspace"), false); - assert.strictEqual(parseKey("\x08"), "ctrl+backspace"); - assert.strictEqual(matchesKey("\x08", "ctrl+h"), true); - }); + withEnvVars( + { + WT_SESSION: "test-session", + SSH_CONNECTION: undefined, + SSH_CLIENT: undefined, + SSH_TTY: undefined, + }, + () => { + assert.strictEqual(matchesKey("\x08", "ctrl+backspace"), true); + assert.strictEqual(matchesKey("\x08", "backspace"), false); + assert.strictEqual(parseKey("\x08"), "ctrl+backspace"); + assert.strictEqual(matchesKey("\x08", "ctrl+h"), true); + }, + ); + }); + + it("should treat raw 0x08 as plain backspace in Windows Terminal over SSH", () => { + setKittyProtocolActive(false); + withEnvVars( + { + WT_SESSION: "test-session", + SSH_CONNECTION: "1 2 3 4", + SSH_CLIENT: "1 2 3", + SSH_TTY: "/dev/pts/1", + }, + () => { + assert.strictEqual(matchesKey("\x08", "ctrl+backspace"), false); + assert.strictEqual(matchesKey("\x08", "backspace"), true); + assert.strictEqual(parseKey("\x08"), "backspace"); + assert.strictEqual(matchesKey("\x08", "ctrl+h"), true); + }, + ); }); it("should parse legacy alt-prefixed sequences when kitty inactive", () => {