Files
plugins/packages/agent-console/test/v1/decode.test.ts
T
qingnian108 5703c9f18a agent-console 1.3: reviewed features from #727 and timeline polish (#733)
This carries #727's reviewed part forward, without auto-approve (that comes as its own PR). Permission behaviour is the same as 1.2.

- Windows Git Bash detection, never the WSL System32 bash. It can be overridden in Settings and is passed per spawn only.
- GBK / system code page decoding of raw process output.
- Folded tool steps that don't wrap at ~600px.
- Select-to-quote, and copy/edit under the user bubble.
- Instant send with the echo replacing the bubble in place, and retry when a send fails.
- `/app` heals itself after sleep/wake. `frame-ancestors` is limited to loopback and Magpie's window.
- Grayscale timeline polish: one-line tool rows, a task-list pill, a reply footer and code blocks.
- Test fixes:
  - The `initializing` push is awaited.
  - `afterEach` waits for the thread store's writes.
  - `runtimeInfo().starting` is exposed, and the steer test waits on it.
  - "idle recycling" waits for closed.

Run on mote (macOS) at 65ab7c94:
- `npm ci`, `npm run check` and `build` are ok; `verify:build` gives 5/5 OK.
- Full `vitest run` x5: 4 green (427 passed). One red, "send while running → queued", is a race that is on main too.
  - Cause: `setStatus(idle)` runs before `queue.shift()`.
  - A 300ms pause injected there makes it red 3/3 on main and on this head.
  - Filed on #585 with the cause and the repro.
- Focused send-latency + manager runs: 12/12. `e2e:v1`: 25/25, with no pageerror or console error.
- Broken by hand:
  - With the old steer wait and the mock send held 400ms, the test is red 3/3; the new wait passes 3/3.
  - With `recycleIdle` made a no-op, "idle recycling" goes red.
- Playwright render:
  - Chromium and WebKit × zh/en × light/dark × 1280/420.
  - No gradients, no coloured left borders, no `<select>`, no page errors.
  - At 420 the page scrolls sideways (scrollWidth 480), the same as main.

Not tried: Windows (Git Bash detection, GBK decoding) and a real agent, rather than the mock.
2026-11-05 11:08:38 +08:00

114 lines
5.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 1.3 item 3: UTF-8 vs GBK (system code page) decoding of raw process output
import { describe, it, expect, afterEach } from "vitest";
import { OutputDecoder, decodeOutput, parseCodePage, codePageLabel, labelForLocale, systemLegacyEncoding, _setSystemLegacyEncoding } from "../../daemon/src/util/decode.js";
const hex = (h: string) => Buffer.from(h.replace(/\s+/g, ""), "hex");
// GBK fixtures (encoded with Python's gbk codec)
const GBK_HELLO = hex("c4e3bac3a3accac0bde7"); // 你好,世界
const GBK_NOPATH = hex("cfb5cdb3d5d2b2bbb5bdd6b8b6a8b5c4c2b7beb6a1a3"); // 系统找不到指定的路径。
const GBK_ERR = hex("b4edcef33a20d5d2b2bbb5bdcec4bcfe20612e7478740d0a"); // 错误: 找不到文件 a.txt\r\n
const GBK_YI = hex("d2bb"); // 一 — also valid UTF-8 (U+04BB "һ")
/** Feed buf in pieces of the given sizes (the rest as one piece) */
function feed(d: OutputDecoder, buf: Buffer, sizes: number[]): string {
let out = "", i = 0;
for (const n of sizes) { out += d.write(buf.subarray(i, i + n)); i += n; }
return out + d.write(buf.subarray(i)) + d.end();
}
afterEach(() => _setSystemLegacyEncoding(undefined));
describe("OutputDecoder (Windows, code page 936)", () => {
it("TextDecoder has gbk/gb18030/big5/shift_jis (full ICU, also Node 20)", () => {
expect(new TextDecoder("gbk").decode(GBK_HELLO)).toBe("你好,世界");
for (const l of ["gbk", "gb18030", "big5", "shift_jis", "euc-kr"]) expect(codePageLabel({ gbk: 936, gb18030: 54936, big5: 950, shift_jis: 932, "euc-kr": 949 }[l]!)).toBe(l);
});
it("valid UTF-8 stays UTF-8, GBK chunks are decoded as GBK", () => {
const d = new OutputDecoder("gbk");
expect(d.write(Buffer.from("构建成功 ✓\n"))).toBe("构建成功 ✓\n");
expect(d.write(GBK_NOPATH)).toBe("系统找不到指定的路径。");
expect(d.write(Buffer.from("ok\n"))).toBe("ok\n");
expect(d.write(Buffer.from("又是 UTF-8"))).toBe("又是 UTF-8");
expect(d.end()).toBe("");
});
it("streaming-safe: GBK and UTF-8 characters split at every byte boundary", () => {
for (let cut = 1; cut < GBK_ERR.length; cut++) {
expect(feed(new OutputDecoder("gbk"), GBK_ERR, [cut]), `gbk cut ${cut}`).toBe("错误: 找不到文件 a.txt\r\n");
}
const u = Buffer.from("中文输出 → 完成😀");
for (let cut = 1; cut < u.length; cut++) expect(feed(new OutputDecoder("gbk"), u, [cut]), `utf8 cut ${cut}`).toBe("中文输出 → 完成😀");
// one byte at a time
expect(feed(new OutputDecoder("gbk"), GBK_HELLO, Array(GBK_HELLO.length).fill(1))).toBe("你好,世界");
});
it("bounded: never holds more than 3 bytes", () => {
const d = new OutputDecoder("gb18030");
expect(d.write(hex("9439fc"))).toBe(""); // 3 of the 4 bytes of 😀 in GB18030
expect(d.write(hex("36"))).toBe("😀");
const big = Buffer.concat(Array(2000).fill(GBK_HELLO));
expect(d.write(big).length).toBe(5 * 2000);
expect((d as unknown as { pending: Uint8Array }).pending.length).toBeLessThanOrEqual(3);
});
it("after GBK output, an ambiguous 2-byte chunk (一 = D2 BB) sticks with GBK; a fresh decoder reads it as UTF-8", () => {
const d = new OutputDecoder("gbk");
d.write(GBK_HELLO);
expect(d.write(GBK_YI)).toBe("一");
expect(new OutputDecoder("gbk").write(GBK_YI)).toBe("һ");
// 3-byte UTF-8 (CJK) is strong evidence and switches back
expect(d.write(Buffer.from("好"))).toBe("好");
expect(d.write(GBK_YI)).toBe("һ");
});
it("end() flushes a truncated character instead of dropping it", () => {
const d = new OutputDecoder("gbk");
expect(d.write(hex("c4e3ba"))).toBe("你");
expect(d.end()).toBe("\ufffd");
});
it("strings pass through (streams already set to utf8)", () => {
expect(new OutputDecoder("gbk").write("abc")).toBe("abc");
});
});
describe("OutputDecoder off Windows / no legacy decoder", () => {
it("lenient UTF-8 with split characters kept", () => {
const d = new OutputDecoder(null);
const u = Buffer.from("你好");
expect(d.write(u.subarray(0, 4)) + d.write(u.subarray(4)) + d.end()).toBe("你好");
expect(decodeOutput(GBK_HELLO, null)).toContain("\ufffd");
});
it("systemLegacyEncoding is null on macOS and Linux", () => {
expect(systemLegacyEncoding("darwin")).toBeNull();
expect(systemLegacyEncoding("linux")).toBeNull();
});
it("an unknown label falls back to UTF-8", () => {
expect(new OutputDecoder("no-such-enc").write(Buffer.from("ok"))).toBe("ok");
});
});
describe("code page detection", () => {
it("parses reg query and chcp output", () => {
expect(parseCodePage("\r\nHKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Nls\\CodePage\r\n ACP REG_SZ 936\r\n\r\n")).toBe(936);
expect(parseCodePage("Active code page: 65001\r\n")).toBe(65001);
expect(parseCodePage("活动代码页: 936")).toBe(936);
expect(parseCodePage("garbage")).toBeNull();
});
it("maps code pages and locales", () => {
expect(codePageLabel(936)).toBe("gbk");
expect(codePageLabel(437)).toBeNull();
expect(codePageLabel(1252)).toBe("windows-1252");
expect(labelForLocale("zh-CN")).toBe("gbk");
expect(labelForLocale("zh-TW")).toBe("big5");
expect(labelForLocale("zh-Hant-HK")).toBe("big5");
expect(labelForLocale("ja-JP")).toBe("shift_jis");
expect(labelForLocale("en-US")).toBeNull();
});
it("decodeOutput uses the cached system encoding by default", () => {
_setSystemLegacyEncoding("gbk");
expect(decodeOutput(GBK_NOPATH)).toBe("系统找不到指定的路径。");
});
});