Files
Netcatty/electron/bridges/terminalEncoding.test.cjs
T
陈大猫andClaude Opus 4.8 0b8206aecb fix(terminal): encode input with the session charset (#1216) (#1222)
The terminal output path decodes remote bytes with an iconv decoder built
from the user's configured charset (GB18030, etc.), but the input path
serialized keystrokes as UTF-8 unconditionally. On a non-UTF-8 device that
made input and output asymmetric: with GB18030 selected the device's command
completion decoded correctly while manually typed Chinese went out as UTF-8
bytes and showed up garbled (and the reverse with UTF-8 selected).

Make input symmetric with output:

- Add electron/bridges/terminalEncoding.cjs as the single source of truth for
  charset normalization plus encodeTerminalInput(), which encodes a keystroke
  string with the same iconv charset (returning the string untouched for UTF-8
  and for unset/unknown encodings so the transport's native serialization and
  the Mosh/local-PTY paths are unchanged). ASCII control bytes and CSI escape
  sequences pass through byte-for-byte under GB18030.
- terminalBridge.writeToSession() now encodes outgoing data via
  encodeTerminalInput(session.encoding) before writing to the SSH stream,
  telnet socket, or serial port. Telnet IAC 0xFF escaping still runs on the
  encoded bytes.
- session.encoding (the input charset) is now kept in lock-step with the
  output decoder everywhere the decoder is configured:
    * Telnet/serial already stored session.encoding; writeToSession now reads
      it for input too.
    * SSH mirrors session.encoding wherever it sets sessionEncodings: the
      GB-variant pre-seed at session start and the runtime setEncoding handler.
      The pre-seed stays gated to GB variants to match the renderer's two-value
      encoding state, so behavior for other/arbitrary charsets is unchanged —
      the renderer still pushes the effective encoding via setEncoding on
      attach, and that handler keeps both halves in sync.
- The SSH startup command is encoded with the same charset as interactive
  input.

Mosh stays UTF-8 (mosh-client is UTF-8-only and sets LANG accordingly), and
local PTY stays UTF-8 — neither sets session.encoding, so their input is
untouched.

Adds unit tests for the encoding helper (GB18030/UTF-8 round-trips, ASCII
control preservation, symmetry with the output decoder) and integration tests
driving writeToSession over a raw TCP device to assert GB18030 bytes on the
wire and a UTF-8 regression guard.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-03 19:08:21 +08:00

111 lines
5.0 KiB
JavaScript

const test = require("node:test");
const assert = require("node:assert/strict");
const iconv = require("iconv-lite");
const {
normalizeTerminalEncoding,
isUtf8Encoding,
encodeTerminalInput,
} = require("./terminalEncoding.cjs");
test("normalizeTerminalEncoding maps GB variants onto the gb18030 superset", () => {
assert.equal(normalizeTerminalEncoding("GB18030"), "gb18030");
assert.equal(normalizeTerminalEncoding("gbk"), "gb18030");
assert.equal(normalizeTerminalEncoding("GB2312"), "gb18030");
assert.equal(normalizeTerminalEncoding("gb-18030"), "gb18030");
});
test("normalizeTerminalEncoding normalizes utf-8 spellings and defaults", () => {
assert.equal(normalizeTerminalEncoding("UTF-8"), "utf-8");
assert.equal(normalizeTerminalEncoding("utf8"), "utf-8");
assert.equal(normalizeTerminalEncoding(""), "utf-8");
assert.equal(normalizeTerminalEncoding(undefined), "utf-8");
// Unknown / unsupported charset falls back to utf-8 rather than throwing.
assert.equal(normalizeTerminalEncoding("not-a-real-charset"), "utf-8");
});
test("isUtf8Encoding recognizes utf-8 spellings and treats unset as utf-8", () => {
assert.equal(isUtf8Encoding("utf-8"), true);
assert.equal(isUtf8Encoding("UTF8"), true);
assert.equal(isUtf8Encoding(undefined), true);
assert.equal(isUtf8Encoding(""), true);
assert.equal(isUtf8Encoding("gb18030"), false);
});
test("encodeTerminalInput leaves UTF-8 input as the original string", () => {
// For UTF-8 we hand the string straight to the transport's native
// serialization, so the value must be returned unchanged (no Buffer).
assert.equal(encodeTerminalInput("ls -la 你好\r", "utf-8"), "ls -la 你好\r");
// Unset encoding (mosh / local PTY) behaves like UTF-8.
assert.equal(encodeTerminalInput("你好", undefined), "你好");
});
test("encodeTerminalInput encodes non-UTF-8 input to matching bytes", () => {
const out = encodeTerminalInput("你好", "gb18030");
assert.ok(Buffer.isBuffer(out), "non-UTF-8 input should produce a Buffer");
assert.deepEqual([...out], [...iconv.encode("你好", "gb18030")]);
// Crucially, the GB18030 bytes differ from the UTF-8 bytes — this is the bug:
// the old path always sent UTF-8 bytes regardless of the configured charset.
assert.notDeepEqual([...out], [...Buffer.from("你好", "utf8")]);
});
test("encodeTerminalInput preserves ASCII control bytes under GB18030", () => {
// CSI cursor-up, CR, Ctrl-C — all ASCII, must pass through byte-for-byte so
// escape sequences and control keys keep working on non-UTF-8 sessions.
const input = "\x1b[A\r\x03";
const out = encodeTerminalInput(input, "gb18030");
assert.deepEqual([...out], [0x1b, 0x5b, 0x41, 0x0d, 0x03]);
});
test("encodeTerminalInput round-trips with the GB18030 output decoder", () => {
// Input/output symmetry guarantee (issue #1216): bytes we write for a
// keystroke decode back to the same text the output path would show.
const text = "查看版本 v1.2\tabc\r";
const wireBytes = encodeTerminalInput(text, "gb18030");
const decoder = iconv.getDecoder("gb18030");
assert.equal(decoder.write(wireBytes), text);
});
test("encodeTerminalInput round-trips with the UTF-8 output decoder", () => {
const text = "echo 你好世界\r";
// UTF-8 path returns a string; the transport serializes it natively, which is
// byte-identical to encoding it as UTF-8.
const wire = encodeTerminalInput(text, "utf-8");
const wireBytes = typeof wire === "string" ? Buffer.from(wire, "utf8") : wire;
const decoder = iconv.getDecoder("utf-8");
assert.equal(decoder.write(wireBytes), text);
});
test("encodeTerminalInput falls back to the string for unknown encodings", () => {
// A misconfigured charset must degrade to today's UTF-8 behavior instead of
// throwing on the keystroke hot path.
assert.equal(encodeTerminalInput("你好", "definitely-not-real"), "你好");
});
test("encodeTerminalInput refuses ASCII-incompatible encodings (UTF-16/UCS-2)", () => {
// iconv accepts these, but encoding control bytes would widen "\r" to 0d 00
// and "\x1b[A" to 1b 00 5b 00 ..., breaking Enter / arrows on the remote.
// Fall back to the original string (today's UTF-8 behavior) instead.
for (const enc of ["utf-16le", "utf-16be", "ucs2", "utf-16"]) {
assert.equal(
encodeTerminalInput("ls\r", enc),
"ls\r",
`${enc} should fall back to the original string`,
);
}
});
test("encodeTerminalInput still encodes other ASCII-superset CJK charsets", () => {
// Sanity check that the ASCII-compat guard does not over-reject: legacy
// multi-byte charsets that ARE ASCII supersets keep working for input.
for (const enc of ["big5", "shift_jis", "euc-jp"]) {
const out = encodeTerminalInput("\r", enc);
assert.ok(Buffer.isBuffer(out), `${enc} should encode to a Buffer`);
assert.deepEqual([...out], [0x0d], `${enc} must keep CR single-byte`);
}
});
test("encodeTerminalInput passes non-string payloads through untouched", () => {
const buf = Buffer.from([0x01, 0x02, 0x03]);
assert.equal(encodeTerminalInput(buf, "gb18030"), buf);
});