Files
moltbot/src/infra/windows-encoding.test.ts
Vincent Koc e6d2c9b080 fix(process): decode Windows command output with console codepage awareness (#72393)
* fix(process): decode Windows command output with console codepage awareness

* fix(clownfish): address review for ghcrawl-199248-agentic-merge (1)
2026-04-26 23:10:59 -07:00

75 lines
2.4 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
createWindowsOutputDecoder,
decodeWindowsOutputBuffer,
parseWindowsCodePage,
} from "./windows-encoding.js";
describe("windows output encoding", () => {
it("parses code pages from chcp output text", () => {
expect(parseWindowsCodePage("Active code page: 936")).toBe(936);
expect(parseWindowsCodePage("活动代码页: 65001")).toBe(65001);
expect(parseWindowsCodePage("no code page")).toBeNull();
});
it("decodes GBK output on Windows when UTF-8 is invalid and code page is known", () => {
const raw = Buffer.from([0xb2, 0xe2, 0xca, 0xd4, 0xa1, 0xab, 0xa3, 0xbb]);
expect(
decodeWindowsOutputBuffer({
buffer: raw,
platform: "win32",
windowsEncoding: "gbk",
}),
).toBe("测试~;");
});
it("prefers valid UTF-8 output on Windows even when the console code page is legacy", () => {
const raw = Buffer.from("测试", "utf8");
expect(
decodeWindowsOutputBuffer({
buffer: raw,
platform: "win32",
windowsEncoding: "gbk",
}),
).toBe("测试");
});
it("keeps multibyte Windows codepage characters intact across chunk boundaries", () => {
const decoder = createWindowsOutputDecoder({
platform: "win32",
windowsEncoding: "gbk",
});
expect(decoder.decode(Buffer.from([0xb2]))).toBe("");
expect(decoder.decode(Buffer.from([0xe2, 0xca]))).toBe("测");
expect(decoder.decode(Buffer.from([0xd4]))).toBe("试");
expect(decoder.flush()).toBe("");
});
it("replays buffered UTF-8 lead bytes when split GBK output falls back to the console code page", () => {
const decoder = createWindowsOutputDecoder({
platform: "win32",
windowsEncoding: "gbk",
});
expect(decoder.decode(Buffer.from([0xc4]))).toBe("");
expect(decoder.decode(Buffer.from([0xe3]))).toBe("你");
expect(decoder.flush()).toBe("");
});
it("keeps split valid UTF-8 output on the UTF-8 path for streaming decode", () => {
const decoder = createWindowsOutputDecoder({
platform: "win32",
windowsEncoding: "gbk",
});
const raw = Buffer.from("测试", "utf8");
expect(decoder.decode(raw.subarray(0, 1))).toBe("");
expect(decoder.decode(raw.subarray(1, 3))).toBe("测");
expect(decoder.decode(raw.subarray(3))).toBe("试");
expect(decoder.flush()).toBe("");
});
});