|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { |
| 3 | + decodeCapturedOutputBuffer, |
| 4 | + parseWindowsCodePage, |
| 5 | + resolveWindowsConsoleEncoding, |
| 6 | +} from "./windows-encoding.js"; |
| 7 | + |
| 8 | +describe("parseWindowsCodePage", () => { |
| 9 | + it("parses English chcp output", () => { |
| 10 | + expect(parseWindowsCodePage("Active code page: 936")).toBe(936); |
| 11 | + }); |
| 12 | + |
| 13 | + it("parses Chinese chcp output", () => { |
| 14 | + expect(parseWindowsCodePage("活动代码页: 65001")).toBe(65001); |
| 15 | + }); |
| 16 | + |
| 17 | + it("returns null for empty string", () => { |
| 18 | + expect(parseWindowsCodePage("")).toBeNull(); |
| 19 | + }); |
| 20 | + |
| 21 | + it("returns null when no code page number found", () => { |
| 22 | + expect(parseWindowsCodePage("no code page")).toBeNull(); |
| 23 | + }); |
| 24 | + |
| 25 | + it("parses Japanese chcp output", () => { |
| 26 | + expect(parseWindowsCodePage("アクティブ コード ページ: 932")).toBe(932); |
| 27 | + }); |
| 28 | + |
| 29 | + it("parses Korean chcp output", () => { |
| 30 | + expect(parseWindowsCodePage("활성 코드 페이지: 949")).toBe(949); |
| 31 | + }); |
| 32 | + |
| 33 | + it("returns null for non-numeric code page", () => { |
| 34 | + expect(parseWindowsCodePage("Active code page: abc")).toBeNull(); |
| 35 | + }); |
| 36 | + |
| 37 | + it("returns null for zero code page", () => { |
| 38 | + expect(parseWindowsCodePage("Active code page: 0")).toBeNull(); |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +describe("resolveWindowsConsoleEncoding", () => { |
| 43 | + it("returns null on non-Windows platforms", () => { |
| 44 | + const platformSpy = vi.spyOn(process, "platform", "get").mockReturnValue("darwin"); |
| 45 | + try { |
| 46 | + expect(resolveWindowsConsoleEncoding()).toBeNull(); |
| 47 | + } finally { |
| 48 | + platformSpy.mockRestore(); |
| 49 | + } |
| 50 | + }); |
| 51 | +}); |
| 52 | + |
| 53 | +describe("decodeCapturedOutputBuffer", () => { |
| 54 | + it("returns UTF-8 string on non-Windows platforms", () => { |
| 55 | + const raw = Buffer.from("hello world"); |
| 56 | + const decoded = decodeCapturedOutputBuffer({ buffer: raw, platform: "darwin" }); |
| 57 | + expect(decoded).toBe("hello world"); |
| 58 | + }); |
| 59 | + |
| 60 | + it("returns UTF-8 string when encoding is utf-8", () => { |
| 61 | + const raw = Buffer.from("hello world"); |
| 62 | + const decoded = decodeCapturedOutputBuffer({ |
| 63 | + buffer: raw, |
| 64 | + platform: "win32", |
| 65 | + windowsEncoding: "utf-8", |
| 66 | + }); |
| 67 | + expect(decoded).toBe("hello world"); |
| 68 | + }); |
| 69 | + |
| 70 | + it("returns UTF-8 string when encoding is null", () => { |
| 71 | + const raw = Buffer.from("hello world"); |
| 72 | + const decoded = decodeCapturedOutputBuffer({ |
| 73 | + buffer: raw, |
| 74 | + platform: "win32", |
| 75 | + windowsEncoding: null, |
| 76 | + }); |
| 77 | + expect(decoded).toBe("hello world"); |
| 78 | + }); |
| 79 | + |
| 80 | + it("decodes GBK output on Windows when code page is known", () => { |
| 81 | + let supportsGbk = true; |
| 82 | + try { |
| 83 | + void new TextDecoder("gbk"); |
| 84 | + } catch { |
| 85 | + supportsGbk = false; |
| 86 | + } |
| 87 | + |
| 88 | + const raw = Buffer.from([0xb2, 0xe2, 0xca, 0xd4, 0xa1, 0xab, 0xa3, 0xbb]); |
| 89 | + const decoded = decodeCapturedOutputBuffer({ |
| 90 | + buffer: raw, |
| 91 | + platform: "win32", |
| 92 | + windowsEncoding: "gbk", |
| 93 | + }); |
| 94 | + |
| 95 | + if (!supportsGbk) { |
| 96 | + expect(decoded).toContain("�"); |
| 97 | + return; |
| 98 | + } |
| 99 | + expect(decoded).toBe("测试~;"); |
| 100 | + }); |
| 101 | + |
| 102 | + it("decodes Shift_JIS output on Windows", () => { |
| 103 | + let supportsShiftJis = true; |
| 104 | + try { |
| 105 | + void new TextDecoder("shift_jis"); |
| 106 | + } catch { |
| 107 | + supportsShiftJis = false; |
| 108 | + } |
| 109 | + |
| 110 | + const raw = Buffer.from([0x82, 0xb1, 0x82, 0xf1, 0x82, 0xc9, 0x82, 0xbf, 0x82, 0xcd]); |
| 111 | + const decoded = decodeCapturedOutputBuffer({ |
| 112 | + buffer: raw, |
| 113 | + platform: "win32", |
| 114 | + windowsEncoding: "shift_jis", |
| 115 | + }); |
| 116 | + |
| 117 | + if (!supportsShiftJis) { |
| 118 | + expect(decoded).toContain("�"); |
| 119 | + return; |
| 120 | + } |
| 121 | + expect(decoded).toBe("こんにちは"); |
| 122 | + }); |
| 123 | + |
| 124 | + it("falls back to UTF-8 on unsupported encoding", () => { |
| 125 | + const raw = Buffer.from([0xb2, 0xe2, 0xca, 0xd4]); |
| 126 | + const decoded = decodeCapturedOutputBuffer({ |
| 127 | + buffer: raw, |
| 128 | + platform: "win32", |
| 129 | + windowsEncoding: "nonexistent-encoding", |
| 130 | + }); |
| 131 | + expect(decoded).toBe(raw.toString("utf8")); |
| 132 | + }); |
| 133 | + |
| 134 | + it("handles empty buffer", () => { |
| 135 | + const raw = Buffer.alloc(0); |
| 136 | + const decoded = decodeCapturedOutputBuffer({ buffer: raw, platform: "win32" }); |
| 137 | + expect(decoded).toBe(""); |
| 138 | + }); |
| 139 | + |
| 140 | + it("handles pure ASCII on Windows with GBK encoding", () => { |
| 141 | + const raw = Buffer.from("ASCII text"); |
| 142 | + const decoded = decodeCapturedOutputBuffer({ |
| 143 | + buffer: raw, |
| 144 | + platform: "win32", |
| 145 | + windowsEncoding: "gbk", |
| 146 | + }); |
| 147 | + expect(decoded).toBe("ASCII text"); |
| 148 | + }); |
| 149 | + |
| 150 | + it("handles mixed CJK and ASCII in GBK", () => { |
| 151 | + let supportsGbk = true; |
| 152 | + try { |
| 153 | + void new TextDecoder("gbk"); |
| 154 | + } catch { |
| 155 | + supportsGbk = false; |
| 156 | + } |
| 157 | + |
| 158 | + const gbkBytes = Buffer.from([0xc4, 0xe3, 0xba, 0xc3]); |
| 159 | + const asciiBytes = Buffer.from(" hello"); |
| 160 | + const raw = Buffer.concat([gbkBytes, asciiBytes]); |
| 161 | + const decoded = decodeCapturedOutputBuffer({ |
| 162 | + buffer: raw, |
| 163 | + platform: "win32", |
| 164 | + windowsEncoding: "gbk", |
| 165 | + }); |
| 166 | + |
| 167 | + if (!supportsGbk) { |
| 168 | + return; |
| 169 | + } |
| 170 | + expect(decoded).toBe("你好 hello"); |
| 171 | + }); |
| 172 | +}); |
0 commit comments