Files
plugins/packages/agent-console/test/v1/gate.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

99 lines
4.1 KiB
TypeScript

// Plugin gate (lib/gate.mjs): driven by a fake ensureDaemon, no real daemon is started.
import { describe, expect, it } from "vitest";
import { join, resolve } from "node:path";
import { pathToFileURL } from "node:url";
import { freePort, request } from "./helpers-net.js";
const gateUrl = pathToFileURL(join(resolve(__dirname, "..", ".."), "lib", "gate.mjs")).href;
describe("gate (lib/gate.mjs)", () => {
it("checks Host, refuses any Origin, requires the gate key, and serializes opens", async () => {
const { createGate } = await import(/* @vite-ignore */ gateUrl);
const port = await freePort();
const daemonPort = await freePort();
const key = "c".repeat(64);
const token = "a".repeat(64);
let calls = 0;
const gate = createGate({
port,
key,
token,
ensureDaemon: async () => {
calls++;
await new Promise((r) => setTimeout(r, 50));
return { port: daemonPort };
},
});
await gate.listen();
try {
expect((await request(port, `/open?k=${key}`, { host: `evil.example:${port}` })).status).toBe(421);
expect((await request(port, `/open?k=${key}`, { host: `127.0.0.1:${port + 1}` })).status).toBe(421);
expect((await request(port, `/open?k=${key}`, { origin: "null" })).status).toBe(403);
expect((await request(port, `/open?k=${key}`, { origin: "https://evil.example" })).status).toBe(403);
expect((await request(port, "/")).status).toBe(404);
expect((await request(port, "/open")).status).toBe(403);
expect((await request(port, "/open?k=wrong")).status).toBe(403);
// the daemon token is not a gate key, in either parameter
expect((await request(port, `/open?k=${token}`)).status).toBe(403);
expect((await request(port, `/open?t=${token}`)).status).toBe(403);
expect((await request(port, `/open?t=${key}`)).status).toBe(403);
expect(calls).toBe(0);
const results = await Promise.all([
request(port, `/open?k=${key}&theme=dark`),
request(port, `/open?k=${key}&theme=dark`),
]);
expect(calls).toBe(1);
for (const result of results) {
expect(result.status).toBe(302);
// 1.3: to the gate's own wrapper page, the daemon port next to the token in the fragment
expect(result.headers.location).toBe(`http://127.0.0.1:${port}/app?theme=dark#t=${token}&p=${daemonPort}`);
expect(result.headers["cache-control"]).toBe("no-store");
expect(result.headers["x-content-type-options"]).toBe("nosniff");
expect(result.headers["referrer-policy"]).toBe("no-referrer");
expect(result.headers["access-control-allow-origin"]).toBeUndefined();
expect(result.headers.location).not.toContain(key);
}
} finally {
await gate.close();
}
});
it("answers 503 when the daemon cannot start, and recovers on the next open", async () => {
const { createGate } = await import(/* @vite-ignore */ gateUrl);
const port = await freePort();
const key = "d".repeat(64);
const token = "b".repeat(64);
let fail = true;
const gate = createGate({
port,
key,
token,
ensureDaemon: async () => {
if (fail) throw new Error("spawn ENOENT");
return { port: 45678 };
},
});
await gate.listen();
try {
const first = await request(port, `/open?k=${key}`);
expect(first.status).toBe(503);
expect(first.body).not.toContain(token);
fail = false;
const second = await request(port, `/open?k=${key}`);
expect(second.status).toBe(302);
} finally {
await gate.close();
}
});
it("refuses to start with a missing gate key or one equal to the daemon token", async () => {
const { createGate } = await import(/* @vite-ignore */ gateUrl);
const ensureDaemon = async () => ({ port: 1 });
const token = "e".repeat(64);
expect(() => createGate({ port: 1, token, ensureDaemon })).toThrow();
expect(() => createGate({ port: 1, key: token, token, ensureDaemon })).toThrow();
expect(() => createGate({ port: 1, key: "f".repeat(64), token, ensureDaemon })).not.toThrow();
});
});